InvalidArgumentError:logits和labels必须大小相同:logits_size = [128,1] labels_size = [1,128]

时间:2016-11-20 01:05:31

标签: python numpy tensorflow deep-learning

我试图使用张量流中的递归神经网络来预测股市数据。数据文件中有5个特征和> 5000行。标签是调整后的关闭。

为我的输入文件编辑https://www.tensorflow.org/versions/r0.11/api_docs/python/train.html#AdamOptimizer后:

Traceback (most recent call last):
  File "rnn.py", line 70, in <module>
    train_neural_network(x)
  File "rnn.py", line 60, in train_neural_network
    y: batch_y})
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 717, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 915, in _run
    feed_dict_string, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 965, in _do_run
    target_list, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 985, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.InvalidArgumentError: logits and labels must be same size: logits_size=[128,1] labels_size=[1,128]
     [[Node: SoftmaxCrossEntropyWithLogits = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](Reshape_1, Reshape_2)]]

Caused by op u'SoftmaxCrossEntropyWithLogits', defined at:
  File "rnn.py", line 70, in <module>
    train_neural_network(x)
  File "rnn.py", line 42, in train_neural_network
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction, y))
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/nn_ops.py", line 676, in softmax_cross_entropy_with_logits
    precise_logits, labels, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_nn_ops.py", line 1744, in _softmax_cross_entropy_with_logits
    features=features, labels=labels, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 749, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2380, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1298, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): logits and labels must be same size: logits_size=[128,1] labels_size=[1,128]
     [[Node: SoftmaxCrossEntropyWithLogits = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](Reshape_1, Reshape_2)]]

追溯显示了这一点:

fnval

我不知道logit大小或标签尺寸应该是什么,因此无法绕过这个错误。请帮忙!!

2 个答案:

答案 0 :(得分:0)

错误在这一行:

cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction, y))

这里有两个问题:

  1. 形状错误,可以通过将y重塑为[128]来修复:

    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
        logits=prediction, labels=tf.reshape(y, [batch_size])))
    
  2. 代码使用具有单个输出类的softmax交叉熵损失。单个值的softmax为1,因此所有预测都将为1.0,您的模型将无法学习。考虑更改模型以预测2个或更多类,或计算回归。

答案 1 :(得分:-1)

def recurrent_neural_network(x):的最后一行

output = tf.transpose(tf.add(tf.matmul(outputs[-1], layer['weights']), layer['biases'])))