-
Notifications
You must be signed in to change notification settings - Fork 1
/
train-minst.py
executable file
·87 lines (45 loc) · 1.49 KB
/
train-minst.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
83
84
85
86
# coding: utf-8
# In[1]:
from keras import layers, models
from keras.models import load_model
from keras.datasets import mnist
from keras.utils import to_categorical
from keras import backend as K
import tensorflow as tf
# In[2]:
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# In[3]:
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255
x_train = x_train.reshape(-1, 28, 28, 1)
x_test = x_test.reshape(-1, 28, 28, 1)
# In[4]:
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
# In[5]:
model = models.Sequential()
model.add(layers.Conv2D(16, 3, activation='relu', input_shape=(28, 28, 1)))
model.add(layers.MaxPool2D())
model.add(layers.Conv2D(32, 3, activation='relu'))
model.add(layers.MaxPool2D())
model.add(layers.Conv2D(64, 3, activation='relu'))
model.add(layers.MaxPool2D())
model.add(layers.Flatten())
model.add(layers.Dense(256, activation='relu'))
model.add(layers.Dropout(0.5))
model.add(layers.Dense(10, activation='softmax'))
model.summary()
# In[6]:
model.compile(optimizer='adam', metrics=['accuracy'], loss='categorical_crossentropy')
# In[7]:
history = model.fit(x_train, y_train, epochs=20, batch_size=128)
# In[8]:
print(model.evaluate(x_test, y_test))
# In[9]:
# model 與 weights 分別儲存
with open("metadata.json", "w") as file:
file.write(model.to_json())
model.save_weights("weights.h5")
# In[10]:
# model 與 weights存在同一檔案中,for convert-mnist-only-h5.ipynb
model.save('model.h5')