-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
53 lines (39 loc) · 1.65 KB
/
model.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
import tensorflow as tf
import tensorflowjs as tfjs
from tensorflow import keras
(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)
model = keras.Sequential([
keras.layers.Conv2D(32, 5,
strides=(1, 1),
activation='relu',
kernel_initializer='VarianceScaling',
input_shape=[28, 28, 1]),
keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
keras.layers.Conv2D(64, 3,
strides=(1, 1),
padding="same",
activation='relu',
kernel_initializer='VarianceScaling'),
keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2)),
keras.layers.Flatten(),
keras.layers.Dense(256, activation='relu',
kernel_initializer='VarianceScaling'),
keras.layers.Dense(10, activation='softmax',
kernel_initializer='VarianceScaling')
])
model.compile(optimizer=keras.optimizers.SGD(learning_rate=0.15),
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit(train_img,
train_label,
validation_data=(test_img, test_label),
epochs=1)
test_loss, test_acc = model.evaluate(test_img, test_label)
print('Test accuracy:', test_acc)
tfjs.converters.save_keras_model(model, 'model')