

# Task 1: Introduction

Welcome to Basic Image Classification with TensorFlow.

This graph describes the problem that we are trying to solve visually. We want to create and train a model that takes an image of a hand written digit as input and predicts the class of that digit, that is, it predicts the digit or it predicts the class of the input image.



### Import TensorFlow


```python
import tensorflow as tf

tf.logging.set_verbosity(tf.logging.ERROR)
print('Using TensorFlow version', tf.__version__)
```

    Using TensorFlow version 1.13.1


# Task 2: The Dataset
### Import MNIST


```python
from tensorflow.keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
```

### Shapes of Imported Arrays


```python
print('x_train shape:', x_train.shape)
print('y_train shape:', y_train.shape)
print('x_test shape:', x_test.shape)
print('y_test shape:', y_test.shape)
```

    x_train shape: (60000, 28, 28)
    y_train shape: (60000,)
    x_test shape: (10000, 28, 28)
    y_test shape: (10000,)


### Plot an Image Example


```python
from matplotlib import pyplot as plt
%matplotlib inline


plt.imshow(x_train[0], cmap='binary')
plt.show()
```

### Display Labels


```python
y_train[0]
```




    5




```python
print(set(y_train))
```

    {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}


# Task 3: One Hot Encoding
After this encoding, every label will be converted to a list with 10 elements and the element at index to the corresponding class will be set to 1, rest will be set to 0:

| original label | one-hot encoded label |
|------|------|
| 5 | [0, 0, 0, 0, 0, 1, 0, 0, 0, 0] |
| 7 | [0, 0, 0, 0, 0, 0, 0, 1, 0, 0] |
| 1 | [0, 1, 0, 0, 0, 0, 0, 0, 0, 0] |

### Encoding Labels


```python
from tensorflow.keras.utils import to_categorical

y_train_encoded = to_categorical(y_train)
y_test_encoded = to_categorical(y_test)
```

### Validated Shapes


```python
print('y_train_encoded shape:', y_train_encoded.shape)
print('y_test_encoded shape:', y_test_encoded.shape)
```

    y_train_encoded shape: (60000, 10)
    y_test_encoded shape: (10000, 10)


### Display Encoded Labels


```python
y_train_encoded[0]
```




    array([0., 0., 0., 0., 0., 1., 0., 0., 0., 0.], dtype=float32)



# Task 4: Neural Networks

### Linear Equations



The above graph simply represents the equation:

\begin{equation}
y = w1 * x1 + w2 * x2 + w3 * x3 + b
\end{equation}

Where the `w1, w2, w3` are called the weights and `b` is an intercept term called bias. The equation can also be *vectorised* like this:

\begin{equation}
y = W . X + b
\end{equation}

Where `X = [x1, x2, x3]` and `W = [w1, w2, w3].T`. The .T means *transpose*. This is because we want the dot product to give us the result we want i.e. `w1 * x1 + w2 * x2 + w3 * x3`. This gives us the vectorised version of our linear equation.

A simple, linear approach to solving hand-written image classification problem - could it work?



### Neural Networks



This model is much more likely to solve the problem as it can learn more complex function mapping for the inputs and outputs in our dataset.

# Task 5: Preprocessing the Examples

### Unrolling N-dimensional Arrays to Vectors


```python
import numpy as np
x_train_reshaped = np.reshape(x_train, (60000, 784))
x_test_reshaped = np.reshape(x_test, (10000, 784))

print('x_train_reshaped shape:', x_train_reshaped.shape)
print('x_test_reshaped shape:', x_test_reshaped.shape)

```

    x_train_reshaped shape: (60000, 784)
    x_test_reshaped shape: (10000, 784)


### Display Pixel Values


```python
print(set(x_train_reshaped[0]))
```

    {0, 1, 2, 3, 9, 11, 14, 16, 18, 23, 24, 25, 26, 27, 30, 35, 36, 39, 43, 45, 46, 49, 55, 56, 64, 66, 70, 78, 80, 81, 82, 90, 93, 94, 107, 108, 114, 119, 126, 127, 130, 132, 133, 135, 136, 139, 148, 150, 154, 156, 160, 166, 170, 171, 172, 175, 182, 183, 186, 187, 190, 195, 198, 201, 205, 207, 212, 213, 219, 221, 225, 226, 229, 238, 240, 241, 242, 244, 247, 249, 250, 251, 252, 253, 255}


### Data Normalization


```python
x_mean = np.mean(x_train_reshaped)
x_std = np.std(x_train_reshaped)

epsilon =1e-10
x_train_norm = (x_train_reshaped - x_mean) / (x_std + epsilon)
x_test_norm = (x_test_reshaped - x_mean) / (x_std + epsilon)
```

### Display Normalized Pixel Values


```python
print(set(x_train_norm[0]))
```

    {-0.38589016215482896, 1.306921966983251, 1.17964285952926, 1.803310486053816, 1.6887592893452241, 2.8215433456857437, 2.719720059722551, 1.1923707702746593, 1.7396709323268205, 2.057868700961798, 2.3633385588513764, 2.096052433197995, 1.7651267538176187, 2.7960875241949457, 2.7451758812133495, 2.45243393406917, 0.02140298169794222, -0.22042732246464067, 1.2305545025108566, 0.2759611966059242, 2.210603629906587, 2.6560805059955555, 2.6051688630139593, -0.4240738943910262, 0.4668798577869107, 0.1486820891519332, 0.3905123933145161, 1.0905474843114664, -0.09314821501064967, 1.4851127174188385, 2.7579037919587486, 1.5360243604004349, 0.07231462467953861, -0.13133194724684696, 1.294194056237852, 0.03413089244334132, 1.3451056992194483, 2.274243183633583, -0.24588314395543887, 0.772349715676489, 0.75962180493109, 0.7214380726948927, 0.1995937321335296, -0.41134598364562713, 0.5687031437501034, 0.5941589652409017, 0.9378125553666773, 0.9505404661120763, 0.6068868759863008, 0.4159682148053143, -0.042236572029053274, 2.7706317027041476, 2.1342361654341926, 0.12322626766113501, -0.08042030426525057, 0.16140999989733232, 1.8924058612716097, 1.2560103240016547, 2.185147808415789, 0.6196147867316999, 1.943317504253206, -0.11860403650144787, -0.30952269768243434, 1.9942291472348024, -0.2840668761916362, 2.6306246845047574, 2.286971094378982, -0.19497150097384247, -0.39861807290022805, 0.2886891073513233, 1.7523988430722195, 2.3887943803421745, 2.681536327486354, 1.4596568959280403, 2.439706023323771, 2.7833596134495466, 2.490617666305367, -0.10587612575604877, 1.5614801818912332, 1.9051337720170087, 1.6123918248728295, 1.268738234747054, 1.9560454149986053, 2.6433525952501564, 1.026907930584471}


# Task 6: Creating a Model
### Creating the Model


```python
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

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

    WARNING:tensorflow:From /home/rhyme/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/resource_variable_ops.py:435: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
    Instructions for updating:
    Colocations handled automatically by placer.


### Activation Functions

The first step in the node is the linear sum of the inputs:
\begin{equation}
Z = W . X + b
\end{equation}

The second step in the node is the activation function output:

\begin{equation}
A = f(Z)
\end{equation}

Graphical representation of a node where the two operations are performed:



### Compiling the Model


```python
model.compile(
    optimizer='sgd',
    loss='categorical_crossentropy',
    metrics=['accuracy']
)

model.summary()
```

    _________________________________________________________________
    Layer (type)                 Output Shape              Param #   
    =================================================================
    dense (Dense)                (None, 128)               100480    
    _________________________________________________________________
    dense_1 (Dense)              (None, 128)               16512     
    _________________________________________________________________
    dense_2 (Dense)              (None, 10)                1290      
    =================================================================
    Total params: 118,282
    Trainable params: 118,282
    Non-trainable params: 0
    _________________________________________________________________


# Task 7: Training the Model

### Training the Model


```python
model.fit(x_train_norm, y_train_encoded, epochs=3)
```

    WARNING:tensorflow:From /home/rhyme/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
    Instructions for updating:
    Use tf.cast instead.
    Epoch 1/3
    60000/60000 [==============================] - 9s 144us/sample - loss: 0.3732 - acc: 0.8910
    Epoch 2/3
    60000/60000 [==============================] - 7s 110us/sample - loss: 0.1843 - acc: 0.9462
    Epoch 3/3
    60000/60000 [==============================] - 7s 109us/sample - loss: 0.1395 - acc: 0.9591





    <tensorflow.python.keras.callbacks.History at 0x7fa463117b38>



### Evaluating the Model


```python
__, accuracy = model.evaluate(x_test_norm, y_test_encoded)
print('Test set accuracy:', accuracy * 100)
```

    10000/10000 [==============================] - 1s 60us/sample - loss: 0.1368 - acc: 0.9607
    Test set accuracy: 96.069997549057


# Task 8: Predictions

### Predictions on Test Set


```python
preds = model.predict(x_test_norm)
print('Shape of preds:', preds.shape)
```

    Shape of preds: (10000, 10)


### Plotting the Results


```python
plt.figure(figsize=(12, 12))

start_index = 0

for i in range (25):
    plt.subplot(5, 5, i+1)
    plt.grid(False)
    plt.xticks([])
    plt.yticks([])
    
    pred = np.argmax(preds[start_index+i])
    gt= y_test[start_index+i]
    
    
    col = 'g'
    if pred != gt:
        col = 'r'
        
    plt.xlabel('i={}, pred{}, gt{}'.format(start_index+i, pred, gt), color=col)
    plt.imshow(x_test[start_index+i], cmap='binary')
plt.show()
```


```python
plt.plot(preds[8])
plt.show()
```


```python

```

59e2ade0597ba168bf86bf457656d76592155cc4
ipynb
Jupyter Notebook
hamza.ipynb
thehamzza/Neural-Network-Model-to-Predict-Handwritten-Digits
ec0613b3973bd1ece1c6e4ad9bb00bf27b27c73c
MIT
2021-08-23T21:43:35.000Z
2021-08-23T21:43:35.000Z
hamza.ipynb
thehamzza/Neural-Network-Model-to-Predict-Handwritten-Digits
ec0613b3973bd1ece1c6e4ad9bb00bf27b27c73c
MIT
hamza.ipynb
thehamzza/Neural-Network-Model-to-Predict-Handwritten-Digits
ec0613b3973bd1ece1c6e4ad9bb00bf27b27c73c
MIT
Qwen/Qwen-72B
1. YES
2. YES