# TensorFlow 2 one-hot encoding of labels

I was following this basic TensorFlow Image Classification problem, where images of flowers have to be classified into one of 5 possible classes. The labels in the training set are not one-hot encoded, and are individual numbers: 1,2,3,4 or 5 (corresponding to 5 classes). The final layer of the ConvNet however has num_class number of units.

Wouldn't there be a dimension mismatch while computing the loss, since you are finding the difference between a [num_class, 1] (predicted label) dimensioned vector and a [1, 1] (true label) dimensioned vector?

Does the Keras backend automatically convert the labels into one-hot vectors?

The loss function handles the conversion. TensorFlow has a SparseCategoricalCrossentropy and a CategoricalCrossentropy loss function. The first expect your labels to be provided as integers, where the latter expects one-hot encodings. In the given example, they use the SparseCategoricalCrossentropy loss function, therefore it is ok to supply your labels as integers.