CSE488_Lab8_TensorFlow II
CSE488_Lab8_TensorFlow II
TensorFlow II
Out of the total 70,000 images, 60,000 are used for training and remaining 10,000 for testing. The labels
are integer arrays ranging from 0 to 9. The class names are not a part of the dataset and hence we need to
include the below mapping while training/prediction:
Label -> Description
0 -> T-shirt/top
1 -> Trouser
2 -> Pullover
1
CSE488: Computational Intelligence Lab08: TensorFlow II
3 -> Dress
4 -> Coat
5 -> Sandal
6 -> Shirt
7 -> Sneaker
8 -> Bag
9 -> Ankle boot
[6]: # Create class_names list object for mapping labels to names
[7]: # Use the below code to make sure that you select TensorFlow 2.0 in Colab
try:
%tensorflow_version 2.x
except Exception:
pass
2.1.0
Data Exploration
2
CSE488: Computational Intelligence Lab08: TensorFlow II
[11]: np.unique(training_images)
Data Visualization
[15]: index = 60
plt.figure()
plt.imshow(training_images[index], cmap='gray')
plt.colorbar()
plt.show()
print(training_labels[index])
print(class_names[training_labels[index]])
3
CSE488: Computational Intelligence Lab08: TensorFlow II
5
Sandal
Data Preprocessing
As the pixel values range from 0 to 255, we have to scale these values to a range of 0 to 1 before feeding
them to the model. We can scale these values (both for training and test datasets) by dividing the values
by 255:
Model Building
We will be using the keras implementation to build the different layers of a NN. We will keep it simple
by having only 1 hidden layer.
nn_model = ks.models.Sequential()
nn_model.add(ks.layers.Flatten(input_shape=input_data_shape, name='Input_layer'))
nn_model.add(ks.layers.Dense(64, activation=hidden_activation_function,
,→name='Hidden_layer'))
nn_model.add(ks.layers.Dense(10, activation=output_activation_function,
,→name='Output_layer'))
[22]: nn_model.summary()
Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
4
CSE488: Computational Intelligence Lab08: TensorFlow II
5
CSE488: Computational Intelligence Lab08: TensorFlow II
Epoch 9/10
60000/60000 [==============================] - 5s 88us/sample - loss: 0.2712 -
accuracy: 0.9006
Epoch 10/10
60000/60000 [==============================] - 6s 94us/sample - loss: 0.2643 -
accuracy: 0.9026
Model Evaluation
1. Training Evaluation
[46]: nn_model.predict(training_images[0].reshape((1,28,28)))
print(training_labels[0])
9
Deep Neural Network (DNN) Model
We will be using the keras implementation to build the different layers of a NN. We will keep it simple
by having only 1 hidden layer.
dnn_model = ks.models.Sequential()
dnn_model.add(ks.layers.Flatten(input_shape=input_data_shape, name='Input_layer'))
dnn_model.add(ks.layers.Dense(256, activation=hidden_activation_function,
,→name='Hidden_layer_1'))
6
CSE488: Computational Intelligence Lab08: TensorFlow II
dnn_model.add(ks.layers.Dense(192, activation=hidden_activation_function,
,→name='Hidden_layer_2'))
dnn_model.add(ks.layers.Dense(128, activation=hidden_activation_function,
,→name='Hidden_layer_3'))
dnn_model.add(ks.layers.Dense(10, activation=output_activation_function,
,→name='Output_layer'))
[48]: dnn_model.summary()
Model: "sequential_6"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
Input_layer (Flatten) (None, 784) 0
_________________________________________________________________
Hidden_layer_1 (Dense) (None, 256) 200960
_________________________________________________________________
Hidden_layer_2 (Dense) (None, 192) 49344
_________________________________________________________________
Hidden_layer_3 (Dense) (None, 128) 24704
_________________________________________________________________
Output_layer (Dense) (None, 10) 1290
=================================================================
Total params: 276,298
Trainable params: 276,298
Non-trainable params: 0
_________________________________________________________________
Now, we will use an optimization function with the help of compile method. An Adam optimizer with
objective function as sparse_categorical_crossentropy which optimzes for the accuracy metric can be built
as follows:
[49]: optimizer = 'adam'
loss_function = 'sparse_categorical_crossentropy'
metric = ['accuracy']
dnn_model.compile(optimizer=optimizer, loss=loss_function, metrics=metric)
7
CSE488: Computational Intelligence Lab08: TensorFlow II
8
CSE488: Computational Intelligence Lab08: TensorFlow II
accuracy: 0.9314
Epoch 19/20
60000/60000 [==============================] - 16s 273us/sample - loss: 0.1775 -
accuracy: 0.9325
Epoch 20/20
60000/60000 [==============================] - 15s 252us/sample - loss: 0.1677 -
accuracy: 0.9351
Model Evaluation
1. Training Evaluation