<jupyter_start><jupyter_text>MNIST Classification with Simple Neural Network<jupyter_code>from __future__ import absolute_import, division, print_function, unicode_literals
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf

%matplotlib inline<jupyter_output><empty_output><jupyter_text>1. Load and prepare the MNIST datasetReminder that the popular MNIST dataset consists of 60,000 samples of digits. Each sample is a 28x28 grayscale image with values between 0 and 255. We also have access to 10,000 supplementary samples for the final testing of the trained model.The preprocessing here consists of normalizing the samples in order to get intensities between 0 and 1.<jupyter_code>mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
print("Training data shape: ",x_train.shape)
print("Test data shape: ",x_test.shape)
print("Training labels shape: ",y_train.shape)
print("Test labels shape: ",y_test.shape)
plt.imshow(x_test[15,:,:],cmap='gray')
plt.title('Test sample #15')
print("Digit label for test sample #15: ",y_test[15])<jupyter_output>Digit label for test sample #15:  5<jupyter_text>2. Define the neural network model in sequential modeWe are using a standard fully connected neural network here, so here are the blocks of our architecture:* Flatten the 28x28 image inputs to a new input tensor of 784 units* Create a hidden layer of 32 units with a standard ReLU activation function* Add an output layer of 10 units because we are doing multiclass classification (digits between 0 and 9)<jupyter_code>model1 = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(input_shape=(28,28)),
    tf.keras.layers.Dense(32, activation='relu'),
    tf.keras.layers.Dense(10)])
print(model1.summary())
tf.keras.utils.plot_model(model1,show_shapes=True)<jupyter_output><empty_output><jupyter_text>3. Define the optimizer, loss function and metric for the training procedure* Optimizer: how will the algorithm update the weights during the training procedure?* Loss: which function will be optimized?* Metric: which metric(s) will be monitored during training to make sure the algorithm is doing the right thing?<jupyter_code>optimizer = tf.keras.optimizers.Adam() # examples: 'Adam', 'RMSprop', 'Adagrad'
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True) # examples: 'BinaryCrossentropy', 'CategoricalCrossentropy'
metrics = ['accuracy']<jupyter_output><empty_output><jupyter_text>4. Compile and train the modelFor the training, we will be using a validation split of 70/30% (meaning that 70% of the training samples will be used for training and the other 30% will be used for monitoring the performance during training).<jupyter_code>model1.compile(optimizer, loss, metrics)
model1_history = model1.fit(x=x_train, y=y_train, batch_size=32,epochs=15,validation_split=0.3)<jupyter_output>Train on 42000 samples, validate on 18000 samples
Epoch 1/15
42000/42000 [==============================] - 4s 88us/sample - loss: 0.4069 - accuracy: 0.8880 - val_loss: 0.2469 - val_accuracy: 0.9274
Epoch 2/15
42000/42000 [==============================] - 3s 82us/sample - loss: 0.2015 - accuracy: 0.9418 - val_loss: 0.1910 - val_accuracy: 0.9429
Epoch 3/15
42000/42000 [==============================] - 4s 89us/sample - loss: 0.1556 - accuracy: 0.9549 - val_loss: 0.1695 - val_accuracy: 0.9511
Epoch 4/15
42000/42000 [==============================] - 3s 81us/sample - loss: 0.1315 - accuracy: 0.9612 - val_loss: 0.1567 - val_accuracy: 0.9561
Epoch 5/15
42000/42000 [==============================] - 4s 84us/sample - loss: 0.1131 - accuracy: 0.9669 - val_loss: 0.1526 - val_accuracy: 0.9571
Epoch 6/15
42000/42000 [==============================] - 3s 81us/sample - loss: 0.1005 - accuracy: 0.9698 - val_loss: 0.1449 - val_accuracy: 0.9606
Epoch 7/15
42000/42000 [==============================] [...]<jupyter_text>5. Plot the training loss and accuracy<jupyter_code># Plot training & validation accuracy values
plt.plot(model1_history.history['accuracy'])
plt.plot(model1_history.history['val_accuracy'])
plt.title('Model accuracy')
plt.ylabel('Accuracy')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'], loc='upper left')
plt.show()

# Plot training & validation loss values
plt.plot(model1_history.history['loss'])
plt.plot(model1_history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Validation'], loc='upper left')
plt.show()<jupyter_output><empty_output><jupyter_text>6. Evaluate the performance of the trained model on test (unseen) data<jupyter_code>model1.evaluate(x_test, y_test, verbose=2)<jupyter_output>10000/10000 - 0s - loss: 0.1297 - accuracy: 0.9631<jupyter_text>7. Make a prediction using the model<jupyter_code>test_predicted = model1.predict_classes(x_test[10:15,:,:])
print("Classes predicted by the model: ",test_predicted)
print("Targets: ",y_test[10:15])<jupyter_output>Classes predicted by the model:  [0 6 9 0 1]
Targets:  [0 6 9 0 1]
{'fraction_non_alphanumeric': 0.1498616053776196, 'fraction_numerical': 0.07354685646500593, 'mean_word_length': 6.026388888888889, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '598827', 'license': 'MIT', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1636, 'n_words': 490, 'path': 'TF-01_MNIST_with_simple_neural_network.ipynb', 'repo_name': 'alzaia/tensorflow_tutorials', 'size': '5058'}