我使用tensorflow来实现CNN,但我遇到了tensorflow.python.framework.errors_impl.InvalidArgumentError:

时间:2017-04-23 05:54:59

标签: tensorflow

Traceback (most recent call last):
  File "C:/Users/ljz/PycharmProjects/TFConvLSTM/LSTMnetwork.py", line 136, in <module>
    sess.run(train_step, feed_dict={x: X_train[start:end], y_: y_train[start:end]})
  File "C:\Users\ljz\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 767, in run
    run_metadata_ptr)
  File "C:\Users\ljz\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 965, in _run
    feed_dict_string, options, run_metadata)
  File "C:\Users\ljz\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1015, in _do_run
    target_list, options, run_metadata)
  File "C:\Users\ljz\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1035, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [1500,18] vs. [22500,18]
     [[Node: mul = Mul[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_Placeholder_1_0, Log)]]

Caused by op 'mul', defined at:
  File "C:/Users/ljz/PycharmProjects/TFConvLSTM/LSTMnetwork.py", line 113, in <module>
    cross_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y_conv),reduction_indices=[1]))
  File "C:\Users\ljz\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\math_ops.py", line 884, in binary_op_wrapper
    return func(x, y, name=name)
  File "C:\Users\ljz\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\math_ops.py", line 1105, in _mul_dispatch
    return gen_math_ops._mul(x, y, name=name)
  File "C:\Users\ljz\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 1625, in _mul
    result = _op_def_lib.apply_op("Mul", x=x, y=y, name=name)
  File "C:\Users\ljz\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 763, in apply_op
    op_def=op_def)
  File "C:\Users\ljz\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2395, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "C:\Users\ljz\AppData\Local\Programs\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1264, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): Incompatible shapes: [1500,18] vs. [22500,18]
     [[Node: mul = Mul[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_Placeholder_1_0, Log)]]

2 个答案:

答案 0 :(得分:0)

Caused by op 'mul', defined at: File "C:/Users/ljz/PycharmProjects/TFConvLSTM/LSTMnetwork.py", line 113, in cross_entropy=tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y_conv),reduction_indices=[1]))

InvalidArgumentError (see above for traceback): Incompatible shapes: [1500,18] vs. [22500,18] [[Node: mul = Mul[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_Placeholder_1_0, Log)]]

=&GT;确保yy_conv具有相同或可广播的形状。

答案 1 :(得分:0)

有问题的代码:       cross_entropy = tf.reduce_mean(-tf.reduce_sum(Y_ * tf.log(y_conv),reduction_indices = [1]))

当您尝试将两个张量y_和y_conv相乘时,它们必须具有相同的形状,即[1500,18]或两者[22500,18]

在你的情况下,y_应该是输入标签,y_conv应该是计算logits,而18应该是输出神经元的数量。因此,我猜你的输入数据(x)和标签(y_)的批量大小是不一样的。检查它并使它们相同。