Search code examples
tensorflow

In Tensorflow Classification, how are the labels ordered when using "predict"?


I'm using the MNIST handwritten numerals dataset to train a CNN.

After training the model, i use predict like this:

predictions = cnn_model.predict(test_images)
predictions[0]

and i get output as:

array([2.1273775e-06, 2.9292005e-05, 1.2424786e-06, 7.6307842e-05,
       7.4305902e-08, 7.2301691e-07, 2.5368356e-08, 9.9952960e-01,
       1.2401938e-06, 1.2787555e-06], dtype=float32)

In the output, there are 10 probabilities, one for each of numeral from 0 to 9. But how do i know which probability refers to which numeral ?

In this particular case, the probabilities are arranged sequentially for numerals 0 to 9. But why is that ? I didn't define that anywhere.

I tried going over documentation and example implementations found elsewhere on the internet, but no one seems to have addressed this particular behaviour.

Edit:

For context, I've defined my train/test data by:

mnist = tf.keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
train_images = (np.expand_dims(train_images, axis=-1)/255.).astype(np.float32)
train_labels = (train_labels).astype(np.int64)
test_images = (np.expand_dims(test_images, axis=-1)/255.).astype(np.float32)
test_labels = (test_labels).astype(np.int64)

And my model consists of a a few convulution and pooling layers, then a Flatten layer, then a Dense layer with 128 neurons and an output Dense layer with 10 neurons.

After that I simply fit my model and use predict like this:

model.fit(train_images, train_labels, batch_size=BATCH_SIZE, epochs=EPOCHS)
predictions = cnn_model.predict(test_images)

I don't see where I've instructed my code to output first neuron as digit 0, second neuron as digit 1 etc And if i wanted to change the the sequence in which the resulting digits are output, where do i do that ? This is really confusing me a lot.


Solution

  • Models work with numbers. Your classes/labels should be represented as numbers (e.g., 0, 1, ...., n). The prediction is always indexed to show probabilities for class 0 at index 0, class 1 at index 1. Now in the MNIST case, you are lucky the labels are integers 0 to 9. Suppose you had to classify images into three classes: cars, bicycles, trucks. You must represent those classes as numerical values. You can arrange it as you wish. If you choose this: {cars: 0, bicycles: 1, trucks: 2}, in other words, if you label your cars as 0, bicycles as 1, and trucks as 2, then your prediction would show probability for cars at index 0, bicycles at index 1 and trucks at index 2.

    You could have also decided to choose this setting: {cars: 2, bicycles: 0, trucks: 1}, then your prediction would show probability for cars at index 2, bicycles at index 0 and trucks at index 1, and so on.

    The point is, you have to show your classes (as many as you have) as integers indexed from 0 to n where n is the num_classes-1. Your probabilities at prediction would be indexed as such. You don't have to tell the model.

    Hope this is now clear.