-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHand_Digit_Prediction.py
More file actions
155 lines (62 loc) · 1.56 KB
/
Copy pathHand_Digit_Prediction.py
File metadata and controls
155 lines (62 loc) · 1.56 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python
# coding: utf-8
# In[2]:
#Importing Required Libraries
import tensorflow as tf
# In[3]:
#Importing Required Libraries
import tensorflow as tf
#Importing the Data Set
import pandas as pd
# In[4]:
mnist=tf.keras.datasets.mnist
# In[5]:
#dividing the data set in to Traning set and Test set
(x_train,y_train),(x_test,y_test)=mnist.load_data()
# In[6]:
#How Data Looks
print(x_train[0])
# In[7]:
#Visualization
import matplotlib.pyplot as plt
# In[8]:
plt.imshow(x_train[0],cmap=plt.cm.binary)
plt.show()
# In[9]:
print(y_train[0])
# In[10]:
#Normalize
x_train=tf.keras.utils.normalize(x_train,axis=1)
x_test=tf.keras.utils.normalize(x_test,axis=1)
# In[11]:
print(x_train[0])
# In[12]:
#Creating a Model
model=tf.keras.models.Sequential()
# In[13]:
model.add(tf.keras.layers.Flatten())
# In[14]:
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))
# In[15]:
#Output Layer
# In[16]:
model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))
# In[17]:
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
# In[18]:
model.fit(x_train,y_train,epochs=3)
# In[19]:
val_loss,val_acc=model.evaluate(x_test,y_test)
# In[20]:
predictions=model.predict(x_test)
# In[21]:
import numpy as np
print(np.argmax(predictions[0]))
# In[22]:
plt.imshow(x_test[0],cmap=plt.cm.binary)
plt.show()
# In[23]:
print(y_test[5])
# In[ ]: