-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathmlp-mnist.py
157 lines (111 loc) · 4.62 KB
/
mlp-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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- coding: utf-8 -*-
import os
import numpy as np
from sklearn.datasets import fetch_mldata
import npdl
def get_data():
# data
print("loading data, please wait ...")
mnist = fetch_mldata('MNIST original', data_home=os.path.join(os.path.dirname(__file__), './data'))
print('data loading is done ...')
X_train = mnist.data / 255.0
y_train = mnist.target
n_classes = np.unique(y_train).size
return n_classes, X_train, y_train
def main(max_iter):
n_classes, X_train, y_train = get_data()
# model
print("building model ...")
model = npdl.Model()
model.add(npdl.layers.Dense(n_out=200, n_in=784, activation=npdl.activations.ReLU()))
model.add(npdl.layers.Dense(n_out=n_classes, activation=npdl.activations.Softmax()))
model.compile(loss=npdl.objectives.SCCE(), optimizer=npdl.optimizers.SGD())
# train
print("train model ... ")
model.fit(X_train, npdl.utils.data.one_hot(y_train), max_iter=max_iter, validation_split=0.1)
def main2(max_iter):
# test Momentum optimizer
n_classes, X_train, y_train = get_data()
# model
print("building model ...")
model = npdl.Model()
model.add(npdl.layers.Dense(n_out=200, n_in=784, activation=npdl.activations.ReLU()))
model.add(npdl.layers.Dense(n_out=n_classes, activation=npdl.activations.Softmax()))
model.compile(loss=npdl.objectives.SCCE(), optimizer=npdl.optimizers.Momentum())
# train
print("train model ... ")
model.fit(X_train, npdl.utils.data.one_hot(y_train), max_iter=max_iter, validation_split=0.1)
def main3(max_iter):
# test NesterovMomentum optimizer
n_classes, X_train, y_train = get_data()
# model
print("building model ...")
model = npdl.Model()
model.add(npdl.layers.Dense(n_out=200, n_in=784, activation='relu'))
model.add(npdl.layers.Softmax(n_out=n_classes))
model.compile(loss=npdl.objectives.SCCE(), optimizer=npdl.optimizers.NesterovMomentum())
# train
print("train model ... ")
model.fit(X_train, npdl.utils.data.one_hot(y_train), max_iter=max_iter, validation_split=0.1)
def main4(max_iter):
# test Adagrad optimizer
n_classes, X_train, y_train = get_data()
# model
print("building model ...")
model = npdl.Model()
model.add(npdl.layers.Dense(n_out=100, n_in=784, activation='relu'))
model.add(npdl.layers.Softmax(n_out=n_classes))
model.compile(loss='scce', optimizer='adagrad')
# train
print("train model ... ")
model.fit(X_train, npdl.utils.data.one_hot(y_train), max_iter=max_iter, validation_split=0.1)
def main5(max_iter):
# test RMSProp optimizer
n_classes, X_train, y_train = get_data()
# model
print("building model ...")
model = npdl.Model()
model.add(npdl.layers.Dense(n_out=100, n_in=784, activation='relu'))
model.add(npdl.layers.Softmax(n_out=n_classes))
model.compile(loss='scce', optimizer='rmsprop')
# train
print("train model ... ")
model.fit(X_train, npdl.utils.data.one_hot(y_train), max_iter=max_iter, validation_split=0.1)
def main6(max_iter):
# test Adadelta optimizer
n_classes, X_train, y_train = get_data()
# model
print("building model ...")
model = npdl.Model()
model.add(npdl.layers.Dense(n_out=100, n_in=784, activation='relu'))
model.add(npdl.layers.Softmax(n_out=n_classes))
model.compile(loss='scce', optimizer='adadelta')
# train
print("train model ... ")
model.fit(X_train, npdl.utils.data.one_hot(y_train), max_iter=max_iter, validation_split=0.1)
def main7(max_iter):
# test Adam optimizer
n_classes, X_train, y_train = get_data()
# model
print("building model ...")
model = npdl.Model()
model.add(npdl.layers.Dense(n_out=100, n_in=784, activation='relu'))
model.add(npdl.layers.Softmax(n_out=n_classes))
model.compile(loss='scce', optimizer='adam')
# train
print("train model ... ")
model.fit(X_train, npdl.utils.data.one_hot(y_train), max_iter=max_iter, validation_split=0.1)
def main8(max_iter):
# test Adamax optimizer
n_classes, X_train, y_train = get_data()
# model
print("building model ...")
model = npdl.Model()
model.add(npdl.layers.Dense(n_out=100, n_in=784, activation='relu'))
model.add(npdl.layers.Softmax(n_out=n_classes))
model.compile(loss='scce', optimizer='adamax')
# train
print("train model ... ")
model.fit(X_train, npdl.utils.data.one_hot(y_train), max_iter=max_iter, validation_split=0.1)
if __name__ == '__main__':
main8(50)