【问题标题】:Simple RNN network - ValueError: setting an array element with a sequence简单的 RNN 网络 - ValueError:使用序列设置数组元素
【发布时间】:2017-07-20 09:17:01
【问题描述】:

我对 TF 和 Python 完全失去了耐心,我无法让它工作, “ValueError:使用序列设置数组元素。”调用 sess.run 时在 testx 上。

我尝试了很多不同的东西.. 几乎就像 TF 坏了一样,有人可以帮忙吗?

import tensorflow as tf
import numpy as np

nColsIn = 1
nSequenceLen = 4
nBatches = 8
nColsOut = 1
rnn_size = 228

modelx = tf.placeholder("float",[None,nSequenceLen,1])
modely = tf.placeholder("float",[None,nColsOut])

testx = [tf.convert_to_tensor(np.zeros([nColsIn,nBatches])) for b in range(nSequenceLen)]
testy = np.zeros([nBatches, nColsOut])

layer = {
    'weights': tf.Variable(tf.random_normal([rnn_size, nColsOut],dtype=tf.float64),),
    'biases': tf.Variable(tf.random_normal([nColsOut],dtype=tf.float64))}

lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(rnn_size, forget_bias=1.0)
outputs, states = tf.nn.static_rnn(lstm_cell,modelx ,dtype=tf.float64)
prediction = tf.matmul(outputs[-1], layer['weights']) + layer['biases']

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=modely))
optimizer = tf.train.AdamOptimizer().minimize(cost)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(modely, 1));
    accuracy = tf.reduce_mean(tf.cast(correct, 'float'))

    _, epoch_loss = sess.run([optimizer, cost], feed_dict={modelx: testx, modely: testy})
    print('Epoch Loss: ',epoch_loss,' Accuracy: ', accuracy.eval({modelx: testx, modely: testy}))

【问题讨论】:

    标签: python tensorflow deep-learning rnn


    【解决方案1】:

    这可能就是你想要的。您会在代码中的 cmets 中找到一些注释。

    import tensorflow as tf
    import numpy as np
    
    nColsIn = 1
    nSequenceLen = 4
    nBatches = 8
    nColsOut = 1
    rnn_size = 228
    
    # As you use static_rnn it has to be a list of inputs
    modelx = [tf.placeholder(tf.float64,[nBatches, nColsIn]) for _ in range(nSequenceLen)]
    modely = tf.placeholder(tf.float64,[None,nColsOut])
    
    # testx should be a numpy array and is not part of the graph
    testx = [np.zeros([nBatches,nColsIn]) for _ in range(nSequenceLen)]
    testy = np.zeros([nBatches, nColsOut])
    
    layer = {
        'weights': tf.Variable(tf.random_normal([rnn_size, nColsOut],dtype=tf.float64),),
        'biases': tf.Variable(tf.random_normal([nColsOut],dtype=tf.float64))}
    
    lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(rnn_size, forget_bias=1.0)
    # Replaced testx by modelx
    outputs, states = tf.nn.static_rnn(lstm_cell,modelx, dtype=tf.float64)
    # output is of shape (8, 4, 128), you probably want the last element in the sequence direction
    prediction = tf.matmul(outputs[-1], layer['weights']) + layer['biases']
    
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=modely))
    optimizer = tf.train.AdamOptimizer().minimize(cost)
    
    if __name__ == '__main__':
        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
    
            correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(modely, 1));
            accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
            feed_dict = {k: v for k,v in zip(modelx, testx)}
            feed_dict[modely] = testy
            _, epoch_loss = sess.run([optimizer, cost], feed_dict=feed_dict)
            print('Epoch Loss: ',epoch_loss,' Accuracy: ', accuracy.eval(feed_dict))
    

    【讨论】:

    • 哇。谢谢你。这样可行。我将对数据进行一些测试并验证它是否按照我期望的方式处理它。因为原始样本(MNIST RNN)实际上并没有经过我相信的批次。即它只会在第一次进入时训练。 (只是一个理论,也许数据只是被错误地破坏了。)
    • 您可以例如创建一个长度为序列长度的(不同的)placeholders 列表,并使用带有 testx 列表的 feed dict(而不是使用单个占位符)。跨度>
    • 你能相应地修改你的例子吗?因为我收到诸如 unhashable type: 'list' for testx 或“使用序列设置数组元素”之类的错误。
    • 完成。您的错误原因可能是因为您尝试将占位符列表作为feed_dict 的键传递。
    • 它会吃掉整个批次并对其进行训练。这对我来说真的是一大步,谢谢。在任何地方都没有遇到过这种创建 feed_dict 的方法。很高兴找到它。
    猜你喜欢
    • 1970-01-01
    • 2020-10-01
    • 2011-06-08
    • 2018-08-04
    • 2019-08-04
    相关资源
    最近更新 更多