Python 2.7,Tensorflow 1.2.1
我正在尝试编写一个函数来计算负对数似然的总和,并接受一个3x5张量流矩阵和3个元素的python列表。矩阵将是LSTM的输出,该LSTM已经通过密集层并重新整形为3x5,并且列表将在执行时定义。
我的代码:
def normal_log(probs, groundTruth):
sum=0
for j in range(3):
probRow = tf.slice(probs, [j,0], [1,5])
probNum = tf.slice(probRow, [0, groundTruth[j]], [1,1])
num = tf.reshape(probNum, [])
sum += -tf.log(num)
print sum
return sum
我的错误讯息:
2017-08-09 11:40:28.613541: W tensorflow/core/kernels/queue_base.cc:294] _0_input_producer: Skipping cancelled enqueue attempt with queue not closed
2017-08-09 11:40:28.613604: W tensorflow/core/kernels/queue_base.cc:294] _1_batch/fifo_queue: Skipping cancelled enqueue attempt with queue not closed
Traceback (most recent call last):
File "model.py", line 135, in <module>
sess.run(training_op, feed_dict={X: C_})
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 789, in run
run_metadata_ptr)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 997, in _run
feed_dict_string, options, run_metadata)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1132, in _do_run
target_list, options, run_metadata)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1152, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Expected size[1] in [0, 0], but got 1
[[Node: Slice_6 = Slice[Index=DT_INT32, T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](Slice_5, Slice_6/begin, Slice_6/size)]]
Caused by op u'Slice_6', defined at:
File "model.py", line 77, in <module>
nll = -tf.reduce_sum(normal_log(outputs, A))
File "model.py", line 54, in normal_log
probNum = tf.slice(probRow, [0, index], [1,1])
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 547, in slice
return gen_array_ops._slice(input_, begin, size, name=name)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 2896, in _slice
name=name)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 767, in apply_op
op_def=op_def)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2506, in create_op
original_op=self._default_original_op, op_def=op_def)
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1269, in __init__
self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): Expected size[1] in [0, 0], but got 1
[[Node: Slice_6 = Slice[Index=DT_INT32, T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](Slice_5, Slice_6/begin, Slice_6/size)]]