我对 TF 和 Python 完全失去了耐心,我无法让它工作,“ValueError: setting an array element with a sequence。” 调用 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}))