-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEpicHandwrittenNumberReader.py
83 lines (40 loc) · 1.25 KB
/
EpicHandwrittenNumberReader.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
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
#!/usr/bin/env python
# coding: utf-8
# In[28]:
import tensorflow as tf
mnist = tf.keras.datasets.mnist #28x28 images of hand written images 0-9
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = tf.keras.utils.normalize(x_train, axis=1)
x_test = tf.keras.utils.normalize(x_test, axis=1)
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(10, activation=tf.nn.softmax))
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
model.fit(x_train, y_train, epochs = 5)
# In[29]:
val_loss, val_acc = model.evaluate(x_test, y_test)
print(val_loss, val_acc)
# In[23]:
import matplotlib.pyplot as plt
plt.imshow(x_train[0], cmap = plt.cm.binary)
plt.show()
#print(x_train[0])
# In[30]:
model.save('epicNumReader.model')
# In[39]:
new_model = tf.keras.models.load_model('epicNumReader.model')
# In[41]:
predictions = new_model.predict(x_test)
# In[42]:
print(predictions)
# In[45]:
plt.imshow(x_test[1])
plt.show()
# In[44]:
import numpy as np
print(np.argmax(predictions[1]))