简单RNN网络值错误:使用序列设置数组元素

2024-09-30 06:20:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我对TF和Python完全失去了耐心,我不能让它工作, ValueError:使用序列设置数组元素赛斯·润已调用。你知道吗

我试过很多不同的方法。。好像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}))

Tags: layersizetfasnpfloatdtypernn
1条回答
网友
1楼 · 发布于 2024-09-30 06:20:37

这可能就是你想要的。您将在代码的注释中找到一些注释。你知道吗

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))

相关问题 更多 >

    热门问题