簡體   English   中英

Logit的形狀和標簽不匹配

[英]Shape of logits and labels mismatch

錯誤:

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?

問題來自您正在使用RGB圖像。 該模型設計用於灰度圖像,如CNN定義頂部附近的input_layer = tf.reshape(features["x"], [-1, 28, 28, 1])行所示。 具有3個通道而不是1個通道意味着此處的批處理大小將太大三倍。

要解決此問題,請將該行更改為input_layer = tf.reshape(features["x"], [-1, 28, 28, 3])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM