<jupyter_start><jupyter_text>Importing the libraries and dataset<jupyter_code>import numpy as np
import tensorflow as tf
from tensorflow.keras.datasets import fashion_mnist
tf.__version__<jupyter_output><empty_output><jupyter_text>Data Preprocessing  Loading the dataset<jupyter_code>(X_train, y_train), (X_test, y_test) = fashion_mnist.load_data()<jupyter_output><empty_output><jupyter_text>Normalizing the imagesWe divide each pixel of the image in the training and test sets by the maximum number of pixels (255).In this way each pixel will be in the range [0, 1]. By normalizing images we make sure that our model (ANN) trains faster and reduce the chances of getting stuck in local optima<jupyter_code>X_train = X_train / 255.0
X_test = X_test / 255.0<jupyter_output><empty_output><jupyter_text>Reshaping the datasetSince we are building a fully connected network, we reshape the training set and the test set to be into the vector format.<jupyter_code># Since each image's dimension is 28x28, we reshape the full dataset to [-1 (all elements), height * width]
X_train = X_train.reshape(-1, 28*28)
X_train.shape
# We reshape the test set the same way
X_test = X_test.reshape(-1, 28*28)<jupyter_output><empty_output><jupyter_text>Step 4: Building an Artificial Neural Network  Defining the modelSimply define an object of the Sequential model.<jupyter_code>model = tf.keras.models.Sequential()<jupyter_output><empty_output><jupyter_text>Adding a first fully-connected hidden layerLayer hyper-parameters:- number of units/neurons: 128- activation function: ReLU- input_shape: (784, )<jupyter_code>model.add(tf.keras.layers.Dense(units=128, activation='relu', input_shape=(784, )))<jupyter_output><empty_output><jupyter_text>Adding a second layer with DropoutDropout is a Regularization technique where we randomly set neurons in a layer to zero. That way while training those neurons won't be updated. Because some percentage of neurons won't be updated the whole training process is long and we have less chance for overfitting.<jupyter_code>model.add(tf.keras.layers.Dropout(0.2))<jupyter_output><empty_output><jupyter_text>second hidden layer<jupyter_code>model.add(tf.keras.layers.Dense(units=128, activation='relu'))
model.add(tf.keras.layers.Dropout(0.2))<jupyter_output><empty_output><jupyter_text>Adding the output layer- units: number of classes (10 in the Fashion MNIST dataset)- activation: softmax<jupyter_code>model.add(tf.keras.layers.Dense(units=10, activation='softmax'))<jupyter_output><empty_output><jupyter_text>Compiling the model- Optimizer: Adam- Loss: Sparse softmax (categorical) crossentropy<jupyter_code>model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['sparse_categorical_accuracy'])
model.summary()<jupyter_output>Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 128)               100480    
_________________________________________________________________
dropout (Dropout)            (None, 128)               0         
_________________________________________________________________
dense_1 (Dense)              (None, 128)               16512     
_________________________________________________________________
dropout_1 (Dropout)          (None, 128)               0         
_________________________________________________________________
dense_2 (Dense)              (None, 10)                1290      
=================================================================
Total params: 118,282
Trainable params: 118,282
Non-trainable params: 0
__________________________________________________[...]<jupyter_text>Training the model<jupyter_code>model.fit(X_train, y_train, epochs=10,batch_size=60)<jupyter_output>Epoch 1/10
1000/1000 [==============================] - 2s 2ms/step - loss: 0.5785 - sparse_categorical_accuracy: 0.7941
Epoch 2/10
1000/1000 [==============================] - 2s 2ms/step - loss: 0.4208 - sparse_categorical_accuracy: 0.8470
Epoch 3/10
1000/1000 [==============================] - 2s 2ms/step - loss: 0.3834 - sparse_categorical_accuracy: 0.8601
Epoch 4/10
1000/1000 [==============================] - 2s 2ms/step - loss: 0.3601 - sparse_categorical_accuracy: 0.8685
Epoch 5/10
1000/1000 [==============================] - 2s 2ms/step - loss: 0.3432 - sparse_categorical_accuracy: 0.8726
Epoch 6/10
1000/1000 [==============================] - 2s 2ms/step - loss: 0.3293 - sparse_categorical_accuracy: 0.8785
Epoch 7/10
1000/1000 [==============================] - 2s 2ms/step - loss: 0.3219 - sparse_categorical_accuracy: 0.8813
Epoch 8/10
1000/1000 [==============================] - 2s 2ms/step - loss: 0.3110 - sparse_categorical_accuracy: 0.8837
Epoch 9/10
1000/1000 [==========[...]<jupyter_text>Model evaluation and prediction<jupyter_code>test_loss, test_accuracy = model.evaluate(X_test, y_test)
print("Test accuracy: {}".format(test_accuracy))<jupyter_output>Test accuracy: 0.8809000253677368
{'fraction_non_alphanumeric': 0.1603128054740958, 'fraction_numerical': 0.06158357771260997, 'mean_word_length': 5.61836998706339, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 46, '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': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '126700', 'license': 'Apache-2.0', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1492, 'n_words': 388, 'path': 'ANN.ipynb', 'repo_name': 'jyrj/deeplearning-masterclass-tensorflow', 'size': '5115'}