import tensorflow as tf
from tensorflow.keras 
import datasets, layers, models 
import matplotlib.pyplot as plt


(train_images, train_labels), (test_images, test_labels) = datasets.mnist.load_data() 
train_images = train_images.reshape((60000, 28, 28, 1)).astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1)).astype('float32') / 255


model = models.Sequential([
  layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
  layers.MaxPooling2D((2, 2)),
  layers.Conv2D(64, (3, 3), activation='relu'),
  layers.MaxPooling2D((2, 2)),
  layers.Conv2D(64, (3, 3), activation='relu'),
  layers.Flatten(),
  layers.Dense(64, activation='relu'),
  layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
	loss='sparse_categorical_crossentropy',
	metrics=['accuracy'])

history = model.fit(train_images, train_labels, epochs=5,
	validation_data=(test_images, test_labels))


test_loss, test_acc = model.evaluate(test_images, test_labels) 
print(f'Test accuracy: {test_acc}')

plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy']) 
plt.title('Model accuracy') 
plt.ylabel('Accuracy') 
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left') 
plt.show()

import numpy as np

def display_test_image(index):
  plt.imshow(test_images[index].reshape(28, 28), cmap=plt.cm.binary)
  plt.title(f"True Label: {test_labels[index]}")
  prediction = model.predict(np.expand_dims(test_images[index], axis=0))
  predicted_label = np.argmax(prediction)
  plt.xlabel(f"Predicted Label: {predicted_label}")
  plt.show()

display_test_image(0)