-
Notifications
You must be signed in to change notification settings - Fork 460
/
digit_recognization.py
46 lines (27 loc) · 1.19 KB
/
digit_recognization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# -*- coding: utf-8 -*-
"""Digit_Recognization.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/github/Shashwat-k/Digit_Recognization/blob/main/Digit_Recognization.ipynb
"""
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train , y_train) , (x_test , y_test) = mnist.load_data()
import matplotlib.pyplot as plt
x_train = tf.keras.utils.normalize(x_train,axis = 1 )
x_test = tf.keras.utils.normalize(x_test,axis = 1 )
x_train[0] , y_train[0]
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(128, activation = tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation = tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation = tf.nn.relu))
model.add(tf.keras.layers.Dense(128, activation = tf.nn.relu))
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))
model.compile(optimizer='Adam', loss='sparse_categorical_crossentropy' , metrics=['accuracy'])
model.fit(x_train,y_train,epochs=10)
val_loss , val_accu = model.evaluate(x_test,y_test)
predictions = model.predict(x_test)
import numpy as np
print(np.argmax(predictions[1]))
plt.imshow(x_test[0])