-
Notifications
You must be signed in to change notification settings - Fork 8
/
evaluate_fashion.py
90 lines (60 loc) · 2.36 KB
/
evaluate_fashion.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
import pickle
from multiprocessing import Pool
from keras.optimizers import RMSprop
from keras.models import Sequential,Input,Model
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
from keras.layers.advanced_activations import LeakyReLU
from dataset import load_data
from config import Config
from utils import error
def createNetwork(X_train):
model = Sequential()
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='linear',input_shape=(28,28,1),padding='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D((2, 2),padding='same'))
model.add(Dropout(0.25))
model.add(Conv2D(64, (3, 3), activation='linear',padding='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
model.add(Dropout(0.25))
model.add(Conv2D(128, (3, 3), activation='linear',padding='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
model.add(Dropout(0.4))
model.add(Flatten())
model.add(Dense(128, activation='linear'))
model.add(LeakyReLU(alpha=0.1))
model.add(Dropout(0.3))
model.add(Dense(10, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer=RMSprop())
return model
def evaluate(x, X_train, y_train, X_test, y_test):
from numpy.random import seed
seed(x)
network = createNetwork(X_train)
E_train, E_test = 0, 0
network.fit(X_train, y_train,
batch_size=Config.batch_size, nb_epoch=20, verbose=0)
yy_train = network.predict(X_train)
E_train = error(yy_train, y_train)
yy_test = network.predict(X_test)
E_test = error(yy_test, y_test)
return E_train, E_test
if __name__ == "__main__":
# load the data sets
X_train, y_train = load_data("data/fashion_mnist.train")
X_test, y_test = load_data("data/fashion_mnist.test")
def myeval(x):
return evaluate(x, X_train, y_train, X_test, y_test)
N = 10
p = Pool(N)
errors = list(p.map(myeval, range(N)))
print(errors)
E_train = sum([x[0] for x in errors]) / N
E_test = sum([x[1] for x in errors]) / N
print("E_train: {}".format(E_train))
print("E_test: {}".format(E_test))