预测值和标签的形状不一致

3

错误:

Traceback (most recent call last):
  File "C:/Users/xx/abc/Final.py", line 167, in <module>
    tf.app.run()
  File "C:\Users\xx\tensorflow\python\platform\app.py", line 126, in run
    _sys.exit(main(argv))
  File "C:/Users/xx/abc/Final.py", line 148, in main
    hooks=[logging_hook])
  File "C:\Users\xx\tensorflow\python\estimator\estimator.py", line 363, in train
    loss = self._train_model(input_fn, hooks, saving_listeners)
  File "C:\Users\xx\tensorflow\python\estimator\estimator.py", line 843, in _train_model
    return self._train_model_default(input_fn, hooks, saving_listeners)
  File "C:\Users\xx\tensorflow\python\estimator\estimator.py", line 856, in _train_model_default
    features, labels, model_fn_lib.ModeKeys.TRAIN, self.config)
  File "C:\Users\xx\tensorflow\python\estimator\estimator.py", line 831, in _call_model_fn
    model_fn_results = self._model_fn(features=features, **kwargs)
  File "C:/Users/xx/abc/Final.py", line 61, in cnn_model_fn
    loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits)
  File "C:\Users\xx\tensorflow\python\ops\losses\losses_impl.py", line 853, in sparse_softmax_cross_entropy
    name="xentropy")
  File "C:\Users\xx\tensorflow\python\ops\nn_ops.py", line 2046, in sparse_softmax_cross_entropy_with_logits
    logits.get_shape()))


ValueError: Shape mismatch: The shape of labels (received (100,)) should equal the shape of logits except for the last dimension (received (300, 10)).

训练输入函数:

train_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={"x": train_data},
      y=train_labels,
      batch_size=100,
      num_epochs=None,
      shuffle=True)

所有数据集形状

  print(train_data.shape)
  //Output: (9490, 2352) 

  train_labels = np.asarray(label_MAX[0], dtype=np.int32)


  print(train_labels.shape)
  //Output: (9490,)
  eval_data = datasets[1]  # Returns np.array


  print(eval_data.shape)
  //Output: (3175, 2352)
  eval_labels = np.asarray(label_MAX[1], dtype=np.int32)


  print(eval_labels.shape)
  //Output: (3175,)

我阅读了其他StackOverflow的问题,大部分指向损失函数的计算是错误的关键点。代码发送100个标签的批次是否会导致问题?

我该如何解决这个问题?图像和标签数量不是100的倍数是这个问题的根源吗?

我的模型仅用于训练0和1,所以我想我必须对此进行更改。

logits = tf.layers.dense(inputs=dropout, units=10)

并将单位数量更改为2个?


你能展示一下你是如何建立模型的吗? - Sunreef
@Sunreef 这是我正在跟随的教程 https://www.tensorflow.org/tutorials/layers - xmacz
看起来你应该将标签分成300批,因为你的logits大小为(300, 10)。 - Sunreef
@Sunreef 我把训练输入函数的 batch_size 参数值改为 300,出现了以下错误: 标签的形状(收到 (300,))应该等于 logits 的形状,除了最后一个维度(收到 (900,2))。我该怎么做?同时,由于我只训练 0 和 1,所以我把 logits 层的单元数改为了 2。这样做是合适的方法吗? - xmacz
@Sunreef 这是链接到Gist的链接 https://gist.github.com/abhay-iy97/94011a3bc0e3a0ae3b0048199f658089我的基本想法是输入我自己的图像和标签数据,而不是mnist"能否请你检查一下主要代码中图像数据处理部分。 我正在使用28 * 28 * 3的图像。 - xmacz
我曾经遇到过类似的问题。你的问题很可能是因为使用了不同于教程中的特征大小。对我来说,我必须更改这一行代码:pool2_flat = tf.reshape(pool2, [-1, 7 * 7 * 64]) 改为 pool2_flat = tf.reshape(pool2, [-1, image_width_in_pixels / 4 * image_height_in_pixels / 4 * 64]) 我还需要在其他几行代码上进行一些额外的调整。 - uesports135
2个回答

3

我遇到了同样的错误。后来我意识到我没有将我的图像数据展平。一旦我加入了Flatten()层,我就能够正确地处理神经网络了。你可以试试在Dense层之前加入一个Flatten层吗?


3
问题出在你使用的是RGB图像。该模型旨在使用灰度图像,如CNN定义顶部显示的那行代码“input_layer = tf.reshape(features["x"], [-1, 28, 28, 1])”。由于有三个通道而不是一个,这意味着批量大小将增加三倍。
要解决这个问题,请将该行更改为“input_layer = tf.reshape(features["x"], [-1, 28, 28, 3])”。

我遇到了相同的错误 ValueError: Shape mismatch: The shape of labels (received (20,)) should equal the shape of logits except for the last dimension (received (80, 4)). 我的输入层已经是 tf.reshape(features["x"], [-1, 28, 28, 3]),还有什么问题吗? 这是我的模型:https://gist.github.com/jenyckee/c8090ad2d7639bd54f8ca4238aeb5985 - Jeremy Knees

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接