logits和标签不匹配的形状

时间:2018-05-28 05:29:34

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

错误:

facebook sdk error(error code 6)

训练输入功能:

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)

我读了其他StackOverflow问题,其中大多数都指出将损失函数计算为错误点。代码发送一批100个标签的事实导致问题?

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

我的模型正在训练只有0和1 所以我想我必须改变这个

  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,)

并将单位数更改为2?

2 个答案:

答案 0 :(得分:2)

问题来自于您正在使用RGB图像。该模型设计用于灰度图像,如CNN定义顶部附近的行input_layer = tf.reshape(features["x"], [-1, 28, 28, 1])所示。有3个通道而不是1通道意味着这里的批量大小将是三倍。

要解决此问题,请将该行更改为input_layer = tf.reshape(features["x"], [-1, 28, 28, 3])

答案 1 :(得分:1)

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