MLLab 4
MLLab 4
len(train_labels)
60000
train_images.dtype
dtype('uint8')
train_labels
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
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 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).
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
train_images.shape
(60000, 784)
train_images[4]
train_labels[1]
array([1., 0., 0., 0., 0., 0., 0., 0., 0., 0.], dtype=float32)
<keras.src.callbacks.History at 0x7aab28740d30>
network.summary()
Model: "sequential_8"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_6 (Dense) (None, 512) 401920
=================================================================
Total params: 535818 (2.04 MB)
Trainable params: 535818 (2.04 MB)
Non-trainable params: 0 (0.00 Byte)
_________________________________________________________________