Handson-ml: chapter 10 why have we converted MNIST data to float32 and not use directly ?

Created on 29 Jul 2018  路  2Comments  路  Source: ageron/handson-ml

In chap 10 jupyter notebook code for MNIST dataset for DNN classifier was :

(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
X_train = X_train.astype(np.float32).reshape(-1, 28*28) / 255.0
X_test = X_test.astype(np.float32).reshape(-1, 28*28) / 255.0`

firstly why we have converted it to float and targets to int ? and why have we divided it by 255.0 ?
and also while predicting the model code was :

test_input_fn = tf.estimator.inputs.numpy_input_fn(
    x={"X": X_test}, y=y_test, shuffle=False)
eval_results = dnn_clf.evaluate(input_fn=test_input_fn)

y_pred_iter = dnn_clf.predict(input_fn=test_input_fn)
y_pred = list(y_pred_iter)

should not the test_input_fn be without targets i.e:

test_input_fn = tf.estimator.inputs.numpy_input_fn( x={"X": X_test}, shuffle=False) 

Most helpful comment

Hi @nitml ,

It is most common to use 32-bit precision when training a neural network, so at one point the training data will have to be converted to 32 bit floats. Since the dataset fits easily in RAM, we might as well convert to float immediately.

Regarding the division by 255, this is the maximum value of a byte (the input feature's type before the conversion to float32), so this will ensure that the input features are scaled between 0.0 and 1.0. This is not compulsory, but I usually prefer to have input features about that scale so that the default learning rate (and other hyperparameters) work reasonably well, and so that the cost can take on reasonable values that I am used to. For example, suppose the scale is 100 times the scale you are used to, then the learning rate should be 100 times smaller than you are used to, and the loss will be larger than usual (if it is the mean squared error, the loss will likely be 100^2=10000 times larger than you are used to).

Regarding the test_input_fn, it is used not only for predictions but also for evaluation. For predictions, you are right that the targets are not needed, but they are needed for evaluation.

Hope this helps,
Aur茅lien

All 2 comments

Hi @nitml ,

It is most common to use 32-bit precision when training a neural network, so at one point the training data will have to be converted to 32 bit floats. Since the dataset fits easily in RAM, we might as well convert to float immediately.

Regarding the division by 255, this is the maximum value of a byte (the input feature's type before the conversion to float32), so this will ensure that the input features are scaled between 0.0 and 1.0. This is not compulsory, but I usually prefer to have input features about that scale so that the default learning rate (and other hyperparameters) work reasonably well, and so that the cost can take on reasonable values that I am used to. For example, suppose the scale is 100 times the scale you are used to, then the learning rate should be 100 times smaller than you are used to, and the loss will be larger than usual (if it is the mean squared error, the loss will likely be 100^2=10000 times larger than you are used to).

Regarding the test_input_fn, it is used not only for predictions but also for evaluation. For predictions, you are right that the targets are not needed, but they are needed for evaluation.

Hope this helps,
Aur茅lien

Nicely explained ....Thanks for your Time !!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nitml picture nitml  路  6Comments

akjain90 picture akjain90  路  4Comments

nitml picture nitml  路  4Comments

hamzza-K picture hamzza-K  路  4Comments

tetsuyasu picture tetsuyasu  路  3Comments