import tensorflow as tf import numpy as np import matplotlib.pyplot as plt (x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data() # Normalize the pixel values to be between 0 and 1 x_train = x_train / 255 x_test = x_test / 255 # Reshape the images into 4D arrays for use with a CNN x_train = x_train.reshape(x_train.shape[0], 28, 28, 1) x_test = x_test.reshape(x_test.shape[0], 28, 28, 1) # Convert the labels into one-hot encoded arrays y_train = tf.keras.utils.to_categorical(y_train, num_classes=10) y_test = tf.keras.utils.to_categorical(y_test, num_classes=10) # Define the model model = tf.keras.models.Sequential() model.add(tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1))) model.add(tf.keras.layers.MaxPooling2D((2, 2))) model.add(tf.keras.layers.Conv2D(64, (3, 3), activation='relu')) model.add(tf.keras.layers.MaxPooling2D((2, 2))) model.add(tf.keras.layers.Flatten()) model.add(tf.keras.layers.Dense(64, activation='relu')) model.add(tf.keras.layers.Dense(10, activation='softmax')) # Compile the model model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) # Train the model and record the history history = model.fit(x_train, y_train, epochs=3, batch_size=64, validation_data=(x_test, y_test)) # Get the weights of the Dense layer # plot the weights as a heatmap or image, where the weights are represented # as pixel values. # model.layers[2].get_weights()[0] returns only the weights of the third # layer. If you wanted to get the biases, you would use # model.layers[2].get_weights()[1]. #dense_weights = model.layers[2].get_weights()[0] last_layer_weights = model.layers[-1].get_weights()[0] # Plot the weights as a heatmap plt.imshow(last_layer_weights, cmap='coolwarm') plt.colorbar() plt.title('weights in the output layer') plt.show() # Plot loss and accuracy plt.figure(figsize=(12, 4)) # Plot the loss during training plt.subplot(1, 2, 1) plt.plot(history.history['loss'], label='training loss') plt.plot(history.history['val_loss'], label='validation loss') plt.xlabel('Epoch') plt.ylabel('Loss') plt.legend() plt.subplot(1, 2, 2) plt.plot(history.history['accuracy']) plt.plot(history.history['val_accuracy']) plt.xlabel('Epoch') plt.ylabel('Accuracy') plt.legend() plt.show() # Plot a confusion matrix of the test set predictions test_preds = np.argmax(model.predict(x_test), axis=1) conf_mat = tf.math.confusion_matrix(y_test.argmax(axis=1), test_preds) print ('Mist') plt.imshow(conf_mat, cmap="Blues") plt.xlabel("Predicted labels") plt.ylabel("True labels") plt.xticks(np.arange(10)) plt.yticks(np.arange(10)) plt.colorbar() plt.show() # Evaluate the model on the test set test_loss, test_acc = model.evaluate(x_test, y_test) print('Test accuracy:', test_acc) # Make predictions on the test set y_pred = model.predict(x_test) y_pred = np.argmax(y_pred, axis=1) # Plot some examples from the test set and their predictions fig, axes = plt.subplots(4, 4, figsize=(10, 10)) for i, ax in enumerate(axes.ravel()): ax.imshow(x_test[i].reshape(28, 28), cmap='gray') ax.set_title("True: %d\nPred: %d" % (np.argmax(y_test[i]), y_pred[i])) ax.axis('off') plt.suptitle("Examples of test set images and their predictions") plt.show()