Tensorflow TypeError:获取参数None是否具有无效的类型< type'NoneType'&gt ;? [英] Tensorflow TypeError: Fetch argument None has invalid type <type 'NoneType'>?

查看:318
本文介绍了Tensorflow TypeError:获取参数None是否具有无效的类型< type'NoneType'&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于我模型的相关部分如下:

The relevant parts of my model are as follows:

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

运行此命令时,出现以下错误:

When I run this, I get the following error:

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

也许最奇怪的部分是该错误在 second 迭代中引发,并且第一个迭代完全正常.我正在尝试修复头发,因此任何帮助将不胜感激.

Perhaps the weirdest part is that this error gets thrown the second iteration, and the first works completely fine. I'm ripping my hair trying to fix this, so any help would be greatly appreciated.

推荐答案

您正在将train_step变量重新分配给sess.run()结果的第二个元素(恰好是None).因此,在第二次迭代中,train_stepNone,这会导致错误.

You are re-assigning the train_step variable to the second element of the result of sess.run() (which happens to be None). Hence, on the second iteration, train_step is None, which leads to the error.

幸运的是,此修复很简单:

The fix is fortunately simple:

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

这篇关于Tensorflow TypeError:获取参数None是否具有无效的类型&lt; type'NoneType'&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆