【问题标题】:Shape of logits and labels mismatchlogits 的形状和标签不匹配
【发布时间】:2018-05-28 05:29:33
【问题描述】:

错误:

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 这是我正在关注的教程tensorflow.org/tutorials/layers
  • 看起来您应该为标签提供 300 个批次,因为您的 logits 大小为 (300, 10)。
  • @Sunreef 我将 train 输入函数的 batch_size 参数的值更改为 300,我得到了这个错误:标签的形状(收到的(300,))应该等于 logits 的形状,除了最后一个维度 (received (900, 2)) 我该怎么办?此外,我将 logits 层中的单元数更改为 2,因为我只针对 0 和 1 进行训练。这将是一种恰当的方法,对吧?
  • @Sunreef 这是指向gist的链接gist.github.com/abhay-iy97/94011a3bc0e3a0ae3b0048199f658089我的基本想法是输入我自己的图像和标签数据而不是mnist“你能检查我的图像数据处理部分的代码吗主要的 fn。我使用的是 28*28*3 图像

标签: python tensorflow deep-learning conv-neural-network


【解决方案1】:

问题在于您使用的是 RGB 图像。该模型旨在用于灰度图像,如 CNN 定义顶部附近的行 input_layer = tf.reshape(features["x"], [-1, 28, 28, 1]) 所示。有 3 个通道而不是 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])还有什么问题?这是我的模型:gist.github.com/jenyckee/c8090ad2d7639bd54f8ca4238aeb5985
【解决方案2】:

我遇到了同样的错误。我意识到我没有展平我的图像数据。一旦我包含了 Flatten() 层,我就能够正确处理神经网络。你可以尝试在密集层之前添加一个扁平层吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-26
    • 2021-08-27
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多