import numpy as np
frommatplotlib import pyplot as plt
fromkeras.datasets import mnist
(ti,tl),(tei,tel)=mnist.load_data()
for i in range(20):
plt.subplot(5,4,i+1)
plt.imshow(ti[i],cmap='gray_r')
plt.title("label:{}".format(tl[i]))
plt.subplots_adjust(hspace=0.5)
plt.axis('off')
ti=ti/255.0
tei=tei/255.0
fromkeras.models import Sequential
fromkeras.layers import Flatten,Dense
nn=Sequential()
Flatten(input_shape=(28,28))
nn.add(Flatten(input_shape=(28,28)))
nn.add(Dense(784,activation='relu'))
nn.add(Dense(512,activation='relu'))
nn.add(Dense(10,activation='softmax'))
nn.compile(optimizer='adam',loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
nn.fit(ti,tl,epochs=3)
plt.imshow(tei[3],cmap='gray_r')
plt.title("label:{}".format(tel[3]))
p=nn.predict(tei)
plt.axis('off')
iftel[3]==np.argmax(p[3]):
print("Correct Prediction..................... ")
else:
print("Incorrect Prediction……"
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11490434/11490434 [==============================] - 0s 0us/step
1875/1875 [==============================] - 27s 14ms/step - loss: 0.0785 - accuracy:
0.9759
Epoch 3/
1875/1875 [==============================] - 26s 14ms/step - loss: 0.0539 - accuracy:
0.9834
313/313 [==============================] - 1s 3ms/step