天天看點

Can not squeeze dim[1], expected a dimension of 1

遇上如題所示問題,多半是因為使用了​

​sparse_categorical_crossentropy​

​​作為損失函數

這會出現在分類問題中,這個激活函數要求如下:

labels vector must provide a single specific index for the true class for each row of logits

也就是說,你給的真實輸出值,得是一個索引index而不是one hot編碼,比如你給定的真實輸出值應該是0而非[1, 0, 0, 0, 0]

  • ​​ValueError: Can not squeeze dim[1], expected a dimension of 1, got 3 for 'sparse_softmax_cross_entropy_loss​​
  • ​​tf.keras.losses.sparse_categorical_crossentropy​​