-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
48 lines (37 loc) · 1.37 KB
/
main.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
#import Statements
import numpy as np
import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense, Flatten
# Load Data
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
# Pre Process the images
X_train = X_train.astype(np.float32)/255
X_test = X_test.astype(np.float32)/255
# converting y to one hot vectors
y_train = tf.keras.utils.to_categorical(y_train)
y_test = tf.keras.utils.to_categorical(y_test)
# # Build sequential Model
model = Sequential(
[
# Flatten and specify input size
Flatten(input_shape=(28, 28)),
# Add layers
Dense(128, activation='relu', name='reluLayer1', input_shape=(28, 28)),
Dense(128, activation='relu', name='reluLayer2'),
# Output layer
Dense(10, activation='softmax', name='softmaxLayer')
], name="handwritten_digit_recoginition_model"
)
# Compile the model
model.compile(optimizer='adam',
loss=tf.keras.losses.categorical_crossentropy, metrics=['accuracy'])
# fit the training data
model.fit(X_train, y_train, epochs=52)
print("The model has successfully trained")
# Test the model on testing set
score = model.evaluate(X_test, y_test)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
model.save('handwritten.model')
print("Saving the model as handwritten.model")