<jupyter_start><jupyter_text>Let's see what the computer sees *Neurons for Vision* Here we will be using the Fashion MNIST Dataset for experimentation.Each of our images is a set of 784 values (28 × 28) between 0 and 255. They can beour X. We know that we have 10 different types of images in our dataset, so let’s con‐sider them to be our Y. Now we want to learn what the function looks like where Y isa function of X. *Exploring the Data*<jupyter_code>import tensorflow as tf
import matplotlib.pyplot as plt
data = tf.keras.datasets.fashion_mnist
(train_images,train_labels),(test_images,test_labels) = data.load_data()
plt.imshow(train_images[20])
plt.show()<jupyter_output><empty_output><jupyter_text>*Normalizing the Data*<jupyter_code>train_images = train_images/255.0
test_images = test_images/255.0<jupyter_output><empty_output><jupyter_text>*Neuron Architecture*<jupyter_code>from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Flatten<jupyter_output><empty_output><jupyter_text>* Flatten - Turns a 2D array into 1D in this case, making 28*28 into 784.* Relu - Activation function that outputs values either 0 or greater than zero.* Softmax - Used in cases of multiclass classification since it outputs only the most likely probability* Metrics - Way to measure our progress<jupyter_code>#collapse_output
model = Sequential([
        Flatten(input_shape=(28,28)),
        Dense(128,activation='relu'),
        Dense(10,activation='softmax')
    ])

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

model.fit(train_images,train_labels,epochs=10)<jupyter_output>Train on 60000 samples
Epoch 1/10
60000/60000 [==============================] - 4s 59us/sample - loss: 0.5013 - accuracy: 0.8235
Epoch 2/10
60000/60000 [==============================] - 3s 56us/sample - loss: 0.3754 - accuracy: 0.8647
Epoch 3/10
60000/60000 [==============================] - 3s 56us/sample - loss: 0.3367 - accuracy: 0.8764
Epoch 4/10
60000/60000 [==============================] - 3s 55us/sample - loss: 0.3132 - accuracy: 0.8843
Epoch 5/10
60000/60000 [==============================] - 3s 56us/sample - loss: 0.2937 - accuracy: 0.8916
Epoch 6/10
60000/60000 [==============================] - 3s 57us/sample - loss: 0.2822 - accuracy: 0.8963
Epoch 7/10
60000/60000 [==============================] - 3s 56us/sample - loss: 0.2691 - accuracy: 0.9002
Epoch 8/10
60000/60000 [==============================] - 3s 56us/sample - loss: 0.2588 - accuracy: 0.9031
Epoch 9/10
60000/60000 [==============================] - 3s 58us/sample - loss: 0.2488 - accuracy: 0.9074s - l
Epoch 10/[...]<jupyter_text>*Evaluating*<jupyter_code>model.evaluate(test_images,test_labels)<jupyter_output>10000/10000 [==============================] - ETA: 0s - loss: 0.3362 - accuracy: 0.88 - 0s 39us/sample - loss: 0.3351 - accuracy: 0.8800<jupyter_text>* Training set accuracy: [90.98%]* Test set accuracy : [88%] **Exploring the Model Output**<jupyter_code>classifications = model.predict(test_images)
print(classifications[0])
print(test_labels[0])<jupyter_output>[1.4614162e-07 3.7705106e-09 2.0430624e-09 2.1551809e-09 9.3017638e-10
 4.5569852e-04 4.8079949e-07 7.8137349e-03 3.7059172e-07 9.9172956e-01]
9<jupyter_text>You’ll notice that the classification gives us back an array of values. These are the val‐ues of the 10 output neurons. The label is the actual label for the item of clothing, inthis case 9. Take a look through the array—you’ll see that some of the values are verysmall, and the last one (array index 9) is the largest by far. These are the probabilitiesthat the image matches the label at that particular index. So, what the neural networkis reporting is that there’s a 91.4% chance that the item of clothing at index 0 is label 9.We know that it’s label 9, so it got it right.  Training for longer - Discovering Overfitting Here we will increase the no. of epochs to 50 & see how it helps in the accuracies<jupyter_code>#collapse_output
model = Sequential([
        Flatten(input_shape=(28,28)),
        Dense(128,activation='relu'),
        Dense(10,activation='softmax')
    ])

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

model.fit(train_images,train_labels,epochs=100)
model.evaluate(test_images,test_labels)<jupyter_output>10000/10000 [==============================] - 0s 39us/sample - loss: 0.4944 - accuracy: 0.8847<jupyter_text>* Training set accuracy: [98.03%]* Test set accuracy : [88.47%]  Stopping Training In each of the cases so far, we’ve hardcoded the number of epochs we’re training for.While that works, we might want to train until we reach the desired accuracy insteadof constantly trying different numbers of epochs and training and retraining until weget to our desired value. So, for example, if we want to train until the model is at 95%accuracy on the training set, without knowing how many epochs that will take, howcould we do that?The easiest approach is to use a callback on the training. Let’s take a look at the upda‐ted code that uses callbacks:<jupyter_code>#collapse_output
class myCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self,epoch,logs={}):
        if(logs.get('accuracy')>0.95):
            print("\nReached 95% accuracy, stopping...")
            self.model.stop_training=True
callbacks = myCallback()

model = Sequential([
        Flatten(input_shape=(28,28)),
        Dense(128,activation='relu'),
        Dense(10,activation='softmax')
    ])

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

model.fit(train_images,train_labels,callbacks=[callbacks],epochs=50)
model.evaluate(test_images,test_labels)<jupyter_output>10000/10000 [==============================] - 0s 43us/sample - loss: 0.4173 - accuracy: 0.8918
{'fraction_non_alphanumeric': 0.1583459404942007, 'fraction_numerical': 0.08774583963691376, 'mean_word_length': 4.639810426540285, 'pattern_counts': {'":': 0, '<': 28, '<?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': '190346', 'license': 'Apache-2.0', 'n_tokens_mistral': 2255, 'n_tokens_neox': 1855, 'n_words': 565, 'path': '_notebooks/2021-09-29-CV.ipynb', 'repo_name': 'astronaut99/ml-blog', 'size': '5949'}