使用SoftMaxCrossentRopyWithLogits,逻辑和标签必须是相同的尺寸误差



我是TensorFlow的新手。我一直在尝试重塑深度MNIST教程,以预测Movielens数据集的电影评分。我已经稍微简化了模型,因此不是使用5点刻度,而是简单的二进制Y/N等级(类似于Netflix上的最新评级系统(。我试图仅使用零件等级来预测新项目的偏好。训练模型时,我会在堆栈跟踪中获得以下错误:

Traceback (most recent call last):
  File "/Users/Eric/dev/Coding Academy >Tutorials/tf_impl/deep_tf_group_rec_SO.py", line 223, in <module>
    train_step.run(feed_dict={x: batch_xs, y_: batch_ys, keep_prob: 0.5})
  File "/Library/Python/2.7/site->packages/tensorflow/python/framework/ops.py", line 1550, in run
    _run_using_default_session(self, feed_dict, self.graph, session)
  File "/Library/Python/2.7/site->packages/tensorflow/python/framework/ops.py", line 3764, in >_run_using_default_session
    session.run(operation, feed_dict)
  File "/Library/Python/2.7/site->packages/tensorflow/python/client/session.py", line 767, in run
    run_metadata_ptr)
  File "/Library/Python/2.7/site->packages/tensorflow/python/client/session.py", line 965, in _run
    feed_dict_string, options, run_metadata)
  File "/Library/Python/2.7/site->packages/tensorflow/python/client/session.py", line 1015, in _do_run
    target_list, options, run_metadata)
  File "/Library/Python/2.7/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: logits and >labels must be same size: logits_size=[1,2] labels_size=[50,2]
     [[Node: SoftmaxCrossEntropyWithLogits = >SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, >_device="/job:localhost/replica:0/task:0/cpu:0"](Reshape_2, Reshape_3)]]
Caused by op u'SoftmaxCrossEntropyWithLogits', defined at:
  File "/Users/Eric/dev/Coding Academy >Tutorials/tf_impl/deep_tf_group_rec_SO.py", line 209, in <module>
    cross_entropy = >tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, >logits=y_conv))
  File "/Library/Python/2.7/site->packages/tensorflow/python/ops/nn_ops.py", line 1617, in >softmax_cross_entropy_with_logits
    precise_logits, labels, name=name)
  File "/Library/Python/2.7/site->packages/tensorflow/python/ops/gen_nn_ops.py", line 2265, in >_softmax_cross_entropy_with_logits
    features=features, labels=labels, name=name)
  File "/Library/Python/2.7/site->packages/tensorflow/python/framework/op_def_library.py", line 763, in >apply_op
    op_def=op_def)
  File "/Library/Python/2.7/site->packages/tensorflow/python/framework/ops.py", line 2327, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/Library/Python/2.7/site->packages/tensorflow/python/framework/ops.py", line 1226, in __init__
    self._traceback = _extract_stack()
InvalidArgumentError (see above for traceback): logits and labels must >be same size: logits_size=[1,2] labels_size=[50,2]
     [[Node: SoftmaxCrossEntropyWithLogits = >SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, >_device="/job:localhost/replica:0/task:0/cpu:0"](Reshape_2, Reshape_3)]]

可以在此处查看引起错误的代码

模型中使用的变量尺寸:

  • x(?,1682(

  • y_(?,2(

  • x_history(?,290,290,1(
  • h_pool1(?,145,145,32(
  • h_pool2(?,73,73,64(
  • h_pool3(?,37,37,128(
  • h_pool4(?,19,19,256(
  • h_pool5(?,10,10,512(
  • H_FC1(?,1024(
  • H_FC1_DROP(?,1024(
  • y_conv(?,2(

问题是您在此行中重塑输入批次(形状50培训实例x 1682功能(至[-1,290,290,1]:

x_history = tf.reshape(x, [-1, 290, 290, 1])

您可以通过运行以下形状x_历史结束:

x_history.eval(feed_dict={x:batch[0], y_:batch[1], keep_prob:1.0}).shape
=> (1, 290, 290, 1)

这有效地将50个实例的所有功能都拿到一个实例中(第一个维度为1,需要为50个(,然后通过网络的其余部分运行该功能。因此,您的cross_entropy评估失败了,因为它不能将50个目标标签与网络的单个输出排列在一起。

您需要选择图层形状,以便通过网络保留批处理尺寸(您的形状打印输出(。一种方法是将您的功能添加到1764,然后将您的功能重新设计为[-1,42,42,1],因为42*42 = 1764。

值得注意的是,2D卷积最常用于自然2维的图像数据。鉴于您的功能不是2维,您可能会从更简单的完全连接的图层网络开始?

开始

要克服这个问题,我最终将批次大小从50降低到一个

batch = create_batches(train_data_pairs, 1)

并大大增加了训练迭代的数量。另外,要准确我针对大量的小片测试数据测试模型,然后采取此评估的平均值。

这比解决问题的解决方案更像是一个黑客,它让我继续探索TensorFlow的其他方面并以不同的方式播放数据。

最新更新