Python Tensorflow TypeError:获取参数None的类型无效<;类型';非类型'>;?
我正在构建一个松散基于的RNN 我的模型的相关部分如下:Python Tensorflow TypeError:获取参数None的类型无效<;类型';非类型'>;?,python,artificial-intelligence,tensorflow,typeerror,recurrent-neural-network,Python,Artificial Intelligence,Tensorflow,Typeerror,Recurrent Neural Network,我正在构建一个松散基于的RNN 我的模型的相关部分如下: input_sequence = tf.placeholder(tf.float32, [BATCH_SIZE, TIME_STEPS, PIXEL_COUNT + AUX_INPUTS]) output_actual = tf.placeholder(tf.float32, [BATCH_SIZE, OUTPUT_SIZE]) lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(CELL_SIZE, st
input_sequence = tf.placeholder(tf.float32, [BATCH_SIZE, TIME_STEPS, PIXEL_COUNT + AUX_INPUTS])
output_actual = tf.placeholder(tf.float32, [BATCH_SIZE, OUTPUT_SIZE])
lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(CELL_SIZE, state_is_tuple=False)
stacked_lstm = tf.nn.rnn_cell.MultiRNNCell([lstm_cell] * CELL_LAYERS, state_is_tuple=False)
initial_state = state = stacked_lstm.zero_state(BATCH_SIZE, tf.float32)
outputs = []
with tf.variable_scope("LSTM"):
for step in xrange(TIME_STEPS):
if step > 0:
tf.get_variable_scope().reuse_variables()
cell_output, state = stacked_lstm(input_sequence[:, step, :], state)
outputs.append(cell_output)
final_state = state
而喂食:
cross_entropy = tf.reduce_mean(-tf.reduce_sum(output_actual * tf.log(prediction), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(learning_rate=LEARNING_RATE).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(output_actual, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
with tf.Session() as sess:
sess.run(tf.initialize_all_variables())
numpy_state = initial_state.eval()
for i in xrange(1, ITERATIONS):
batch = DI.next_batch()
print i, type(batch[0]), np.array(batch[1]).shape, numpy_state.shape
if i % LOG_STEP == 0:
train_accuracy = accuracy.eval(feed_dict={
initial_state: numpy_state,
input_sequence: batch[0],
output_actual: batch[1]
})
print "Iteration " + str(i) + " Training Accuracy " + str(train_accuracy)
numpy_state, train_step = sess.run([final_state, train_step], feed_dict={
initial_state: numpy_state,
input_sequence: batch[0],
output_actual: batch[1]
})
当我运行此操作时,会出现以下错误:
Traceback (most recent call last):
File "/home/agupta/Documents/Projects/Image-Recognition-with-LSTM/RNN/feature_tracking/model.py", line 109, in <module>
output_actual: batch[1]
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 698, in run
run_metadata_ptr)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 838, in _run
fetch_handler = _FetchHandler(self._graph, fetches)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 355, in __init__
self._fetch_mapper = _FetchMapper.for_fetch(fetches)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 181, in for_fetch
return _ListFetchMapper(fetch)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 288, in __init__
self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 178, in for_fetch
(fetch, type(fetch)))
TypeError: Fetch argument None has invalid type <type 'NoneType'>
回溯(最近一次呼叫最后一次):
文件“/home/agupta/Documents/Projects/Image Recognition with LSTM/RNN/feature_tracking/model.py”,第109行,在
实际输出:批次[1]
文件“/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py”,第698行,正在运行
运行_元数据_ptr)
文件“/usr/local/lib/python2.7/dist packages/tensorflow/python/client/session.py”,第838行,正在运行
fetch\u handler=\u FetchHandler(self.\u图形,fetches)
文件“/usr/local/lib/python2.7/dist packages/tensorflow/python/client/session.py”,第355行,在__
self.\u fetch\u mapper=\u FetchMapper.for\u fetch(fetches)
文件“/usr/local/lib/python2.7/dist packages/tensorflow/python/client/session.py”,第181行,for_fetch
return\u ListFetchMapper(fetch)
文件“/usr/local/lib/python2.7/dist packages/tensorflow/python/client/session.py”,第288行,在__
self._mappers=[_FetchMapper.for_fetch(fetch)for fetch in fetches]
文件“/usr/local/lib/python2.7/dist packages/tensorflow/python/client/session.py”,第178行,for_fetch
(fetch,键入(fetch)))
TypeError:获取参数None的类型无效
也许最奇怪的是,这个错误会在第二次迭代中抛出,而第一次迭代完全正常。我正在努力解决这个问题,因此非常感谢您的帮助。您正在将
train\u step
变量重新分配给sess.run()
(恰好是None
)结果的第二个元素。因此,在第二次迭代中,train_step
是None
,这导致了错误
幸运的是,解决方法很简单:
for i in xrange(1, ITERATIONS):
# ...
# Discard the second element of the result.
numpy_state, _ = sess.run([final_state, train_step], feed_dict={
initial_state: numpy_state,
input_sequence: batch[0],
output_actual: batch[1]
})
出现此错误的另一个常见原因是,如果包含摘要获取操作,但尚未编写任何摘要 例如:
# tf.summary.scalar("loss", loss) # <- uncomment this line and it will work fine
summary_op = tf.summary.merge_all()
sess = tf.Session()
# ...
summary = sess.run([summary_op, ...], feed_dict={...}) # TypeError, summary_op is "None"!
#tf.summary.scalar(“失去”,失去)#先生,你是有史以来最伟大的人。非常感谢。李先生,你能用一般的语言解释一下什么时候会出现这种错误吗?我无法理解,因为我在不同的上下文中有相同的错误。。。