2017-07-20 63 views
0

我完全失去了對TF和Python的耐心,我無法得到這個工作, 「ValueError:設置一個序列的數組元素。在sess.run被調用時在testx上。簡單的RNN網絡 - 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})) 

回答

1

這可能是你想要的。你會在代碼中發現一些評論。

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

哇。謝謝。這樣可行。我將對數據運行一些測試並驗證它是否按照我期望的方式進行處理。因爲原始樣本(MNIST RNN)實際上沒有經過我認爲的批次。也就是說它只會在第一次進入時進行訓練。 (只是一個理論,也許數據只是錯誤的方式) –

+0

一個問題:testx沒有任何序列數據的空間.. –

+0

你可以例如創建一個(不同的)長度爲序列的佔位符列表長度並使用feed dict與'testx'列表(而不是使用單個佔位符)。 – npf

相關問題