0% found this document useful (0 votes)
22 views

MLLab 4

The document discusses implementing a neural network in Python for digit recognition using the MNIST dataset. It provides 3 main ways to implement neural networks in Python: TensorFlow, PyTorch, and Keras. It then loads the MNIST dataset, preprocesses the images into matrices, creates a neural network model with an input, hidden and output layer, compiles and trains the model on the dataset. The model is trained for 10 epochs with a batch size of 128 to classify the 10 digit classes in the MNIST images.

Uploaded by

tremortech15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

MLLab 4

The document discusses implementing a neural network in Python for digit recognition using the MNIST dataset. It provides 3 main ways to implement neural networks in Python: TensorFlow, PyTorch, and Keras. It then loads the MNIST dataset, preprocesses the images into matrices, creates a neural network model with an input, hidden and output layer, compiles and trains the model on the dataset. The model is trained for 10 epochs with a batch size of 128 to classify the 10 digit classes in the MNIST images.

Uploaded by

tremortech15
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

#3 ways to implement the neural network in python

1. tensorflow - basic, complex syntax


2. pytorch- built on tensorflow,
3. keras- built on tensorflow, for educational purpose
#mnistload gives 4 data labels
#if don't want to import labels insert double _ test__labels in
labels name

from keras.datasets import mnist

(train_images, train_labels),(test_images, test_labels) =


mnist.load_data()

print(train_images.ndim) # how three dimension it should be 2 as


grayscale

train_images.shape # counted rows as 3rd dimensions

(60000, 28, 28)

len(train_labels)

60000

train_images.dtype

dtype('uint8')

train_labels

array([5, 0, 4, ..., 5, 6, 8], dtype=uint8)

#image each pixel values in form of matrix - as matrix helps in


parallel processing
train_images[4]

array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55,
148, 210, 253, 253, 113, 87, 148, 55, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 87,
232,
252, 253, 189, 210, 252, 252, 253, 168, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 57, 242,
252,
190, 65, 5, 12, 182, 252, 253, 116, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 96, 252, 252,
183,
14, 0, 0, 92, 252, 252, 225, 21, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 132, 253, 252, 146,
14,
0, 0, 0, 215, 252, 252, 79, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 126, 253, 247, 176, 9,
0,
0, 8, 78, 245, 253, 129, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 16, 232, 252, 176, 0, 0,
0,
36, 201, 252, 252, 169, 11, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 22, 252, 252, 30, 22, 119,
197,
241, 253, 252, 251, 77, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 16, 231, 252, 253, 252, 252,
252,
226, 227, 252, 231, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 55, 235, 253, 217, 138,
42,
24, 192, 252, 143, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
62, 255, 253, 109, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
71, 253, 252, 21, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 253, 252, 21, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
71, 253, 252, 21, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
106, 253, 252, 21, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
45, 255, 253, 21, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 218, 252, 56, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 96, 252, 189, 42, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 14, 184, 252, 170, 11, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0, 14, 147, 252, 42, 0, 0, 0, 0, 0, 0,
0,
0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
0, 0]], dtype=uint8)

train_images[4][7][12]

55

train_images[4,7,12]

55

tensorflow- matrix as tensor

digit = train_images[4]
import matplotlib.pyplot as plt
plt.imshow(digit)
plt.show()
digit = train_images[4]
import matplotlib.pyplot as plt
plt.imshow(digit, cmap =plt.cm.binary)# to see in two colors cmap
=plt.cm.binary
plt.show()
digit.dtype

dtype('uint8')

#The Network Architecture

#The number of hidden neurons should be 2/3 the size of the input
layer, plus the size of the output layer.
#Relu actvivation fn
#f(x)=max(0,x).

from keras import layers


from keras import models

network = models.Sequential()
network.add(layers.Dense(512,activation = 'relu', kernel_initializer =
'glorot_uniform', bias_initializer = 'zeros' , input_shape =
(28*28,)))
# dense means all the neurons is connected to next layer
#512 tells the no. of neurons as taken as example

network.add(layers.Dense(256,activation = 'relu' ))
network.add(layers.Dense(10,activation = 'softmax' ))
#last layer neurons is decided by the ground values like here only 10
digits applied softmax function
Compilation step

#rmsprop - under adaptive linear algo


network.compile(optimizer = 'rmsprop',loss=
'categorical_crossentropy', metrics= ['accuracy'])
# loss function here meansquared error can not be used as the data is
categorical in nature
# metrics as confusioin metrics having various parameter one of that
is accuracy

#Preprocessing the image using reshape method

train_images = train_images.reshape((60000, 28*28))


train_images = train_images.astype('float32')/255 # range will be
[0,1]
test_images = test_images.reshape((10000, 28*28))
test_images = test_images.astype('float32')/255
# as a ressult creating 1 d vector of the images of size 784

train_images.shape

(60000, 784)

train_images[4]

#preparing the labels

train_labels[1]

from tensorflow.keras.utils import to_categorical


train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

# effect of to_categorical converts the class vector (int) to binary


class matrix
train_labels[1]

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

Training the model


epochs, batch_size are hyper parameter so it can be tuned for example taken this value.

1. batch size - computational easier


network.fit(train_images, train_labels, epochs = 10 , batch_size =
128)
Epoch 1/10
469/469 [==============================] - 9s 16ms/step - loss: 0.2358
- accuracy: 0.9287
Epoch 2/10
469/469 [==============================] - 6s 13ms/step - loss: 0.0862
- accuracy: 0.9732
Epoch 3/10
469/469 [==============================] - 7s 15ms/step - loss: 0.0553
- accuracy: 0.9824
Epoch 4/10
469/469 [==============================] - 6s 13ms/step - loss: 0.0390
- accuracy: 0.9877
Epoch 5/10
469/469 [==============================] - 7s 15ms/step - loss: 0.0284
- accuracy: 0.9908
Epoch 6/10
469/469 [==============================] - 6s 12ms/step - loss: 0.0214
- accuracy: 0.9933
Epoch 7/10
469/469 [==============================] - 7s 15ms/step - loss: 0.0171
- accuracy: 0.9943
Epoch 8/10
469/469 [==============================] - 6s 13ms/step - loss: 0.0115
- accuracy: 0.9966
Epoch 9/10
469/469 [==============================] - 7s 15ms/step - loss: 0.0094
- accuracy: 0.9972
Epoch 10/10
469/469 [==============================] - 6s 12ms/step - loss: 0.0072
- accuracy: 0.9974

<keras.src.callbacks.History at 0x7aab28740d30>

the accuracy of the network over training data = 99.74%

test_loss, test_acc = network.evaluate(test_images , test_labels)

313/313 [==============================] - 2s 6ms/step - loss: 0.0892


- accuracy: 0.9813

print('test acc: ', test_acc)

test acc: 0.9812999963760376

network.summary()

Model: "sequential_8"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_6 (Dense) (None, 512) 401920

dense_7 (Dense) (None, 256) 131328

dense_8 (Dense) (None, 10) 2570

=================================================================
Total params: 535818 (2.04 MB)
Trainable params: 535818 (2.04 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________

##dense_6 (Dense) (None, 512) 401920 =


784*512 +512
## dense_7 (Dense) (None, 256) 131328 =
512*256+256
##ense_8 (Dense) (None, 10) 2570
=256*10+10

You might also like