NN LAB 13 SEP - Jupyter Notebook
NN LAB 13 SEP - Jupyter Notebook
In [ ]: num_classes = 10
img_height = 28
img_width = 28
model = Sequential([
layers.Conv2D(64, (3, 3), activation= 'relu', input_shape=(28,28,1)),
layers.Conv2D(32, 3, padding='same', activation= 'relu'),
layers.MaxPooling2D(),
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers. Flatten(),
layers.Dense (128, activation='relu'),
layers.Dense (10, activation='sigmoid')
])
In [ ]: model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=
metrics=['accuracy'])
In [ ]: model.summary()
Model: "sequential"
_________________________________________________________________
=================================================================
2D)
2D)
=================================================================
Non-trainable params: 0
_________________________________________________________________
In [ ]: epochs=10
history=model.fit(
train_images,
train_labels,
epochs=epochs
)
Epoch 1/10
/usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:1
082: UserWarning: "`sparse_categorical_crossentropy` received `from_logits=
True`, but the `output` argument was produced by a sigmoid or softmax activ
ation and thus does not represent logits. Was this intended?"
Epoch 2/10
Epoch 3/10
Epoch 4/10
Epoch 5/10
Epoch 6/10
Epoch 7/10
Epoch 8/10
Epoch 9/10
Epoch 10/10
In [ ]: acc=history.history['accuracy']
loss=history.history['loss']
epochs_range=range(epochs)
plt.figure(figsize=(8,8))
plt.plot(epochs_range, acc, label='Training Accuracy')
plt.plot(epochs_range, loss, label='Loss')
plt.legend(loc='lower right')
plt.title('Training Accuracy and Loss')
[0]
In [ ]: image=(train_images[2]).reshape(1,28,28,1)
model_pred=model.predict(image)
classes_x=np.argmax(model_pred, axis=1)
plt.imshow(image.reshape(28,28))
print(classes_x)
[4]
In [ ]: model.save("tf-cnn-model.hs")
In [ ]: loaded_model=models.load_model("tf-cnn-model.hs")
In [ ]: image=(train_images[2]).reshape(1,28,28,1)
model_pred=model.predict(image)
classes_x=np.argmax(model_pred, axis=1)
plt.imshow(image.reshape(28,28))
print(classes_x)
[4]
In [ ]: