Skip to content
Advertisement

what’s up with tensorflow.js MNIST example nextbatch implementation?

While taking inspiration from the tensorflow.js Handwritten digit recognition with CNNs tutorial, I stumbled upon the following implementation of the nextBatch function in mnist_data.js:

JavaScript

I understood the point of this function was selecting the images and the corresponding label.
The problem with the provided implementation is that is correctly selecting the corresponding label but also other NUM_CLASSES-1 (10 elements in total) random labels which just happens to be after the selected one.

why is not implemented like the following?

JavaScript

I obviously tried to run it with the above implementation, but the model throws the following:

JavaScript

Being the dense step implemented as

JavaScript

Should I be correct, how should I fix the dense layer and the rest of the implementation?
If instead the provided implementation is correct, why does it work?

Advertisement

Answer

The issue is related to the shape of the label.

JavaScript

The labels are created with the most right axis having the shape 1. It should rather be equal to the number of classes there are (ie: 0, 1 …, 9) which should therefore be 10.

The error is straightforward indicating that the shape should be [, 10].

  • create tensor with the shape [batchSize, 10]

Obviously if the tensor is created with the shape [batchSize, 10] whereas batchLabelsArray has the length batchSize, it will throw a shape error. It should rather have the length batchSize * NUMBER_OF_CLASSES.

The codelab uses

JavaScript

An then to set the class of a certain batchSize it uses the following:

JavaScript
  • The other option is to use tf.oneHot:
JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement