-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathMNIST.py
33 lines (29 loc) · 1.18 KB
/
MNIST.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
import tensorflow as tf
import tensorflowjs as tfjs
from tensorflow import keras
# load the data
(train_img,train_label),(test_img,test_label) = keras.datasets.mnist.load_data()
train_img = train_img.reshape([-1, 28, 28, 1])
test_img = test_img.reshape([-1, 28, 28, 1])
train_img = train_img/255.0
test_img = test_img/255.0
train_label = keras.utils.to_categorical(train_label)
test_label = keras.utils.to_categorical(test_label)
# define the model architecture
model = keras.Sequential([
keras.layers.Conv2D(32, (5, 5), padding="same", input_shape=[28, 28, 1]),
keras.layers.MaxPool2D((2,2)),
keras.layers.Conv2D(64, (5, 5), padding="same"),
keras.layers.MaxPool2D((2,2)),
keras.layers.Flatten(),
keras.layers.Dense(1024, activation='relu'),
keras.layers.Dropout(0.2),
keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# train the model
model.fit(train_img,train_label, validation_data=(test_img,test_label), epochs=10)
test_loss,test_acc = model.evaluate(test_img, test_label)
print('Test accuracy:', test_acc)
# save model as tfjs format
tfjs.converters.save_keras_model(model, 'models')