0

I'm using tensorflow/keras to build an image classification model. The labels are provided as integers, & so I'm using tf.keras.losses.SparseCategoricalCrossEntropy as documented at https://www.tensorflow.org/api_docs/python/tf/keras/losses/SparseCategoricalCrossentropy

The model has output shape (batch_size,1,1,n_classes).

What shape do the labels need to have?

It seems like the labels should have the shape (batch_size,1,1,n_classes). However, from a few tests, it seems like (batch_size,) works too.

Often models have output shape (batch_size,n_classes) and when using SparseCategoricalCrossEntropy the label shape is (batch_size,).

user3731622
  • 4,844
  • 8
  • 45
  • 84
  • Actually the model output shape should be `(n_batches,n_classes)` and labels shape `(n_batches,)`. If your model output is `(batch_size,1,1,n_classes)` which does not make sense why you have 2 one dimensions , then the labels should be `(n_batches,1,1,1)`. – Kaveh Aug 24 '21 at 16:44
  • The model output shape during training is `(batch_size,1,1,n_classes)` b/c I want the model to be able to handle variable size inputs. The model output shape in general is `(batch_size,None,None,n_classes)` b/c the model input shape is `(batch_size,None,None,3)` – user3731622 Aug 24 '21 at 17:47

0 Answers0