|
1
|
|
|
""" |
|
2
|
|
|
This script describes how to save time during the optimization by |
|
3
|
|
|
using a pretrained model. It is similar to the transer learning example, |
|
4
|
|
|
but here you do the training and model creation of the pretrained model |
|
5
|
|
|
yourself. |
|
6
|
|
|
|
|
7
|
|
|
The problem is that most of the optimization time is "waisted" by |
|
8
|
|
|
training the model. The time to find a new position to explore by |
|
9
|
|
|
Hyperactive is very small compared to the training time of |
|
10
|
|
|
neural networks. This means, that we can do more optimization |
|
11
|
|
|
if we keep the training time as little as possible. |
|
12
|
|
|
|
|
13
|
|
|
The idea of pretrained neural architecture search is to pretrain a complete model one time. |
|
14
|
|
|
In the next step we remove the layers that should be optimized |
|
15
|
|
|
and make the remaining layers not-trainable. |
|
16
|
|
|
|
|
17
|
|
|
This results in a partial, pretrained, not-trainable model that will be |
|
18
|
|
|
used during the Hyperactive optimization. |
|
19
|
|
|
|
|
20
|
|
|
You can now add layers to the partial model in the objective function |
|
21
|
|
|
and add the parameters or layers that will be optimized by Hyperactive. |
|
22
|
|
|
|
|
23
|
|
|
With each iteration of the optimization run we are only training |
|
24
|
|
|
the added layers of the model. This saves a lot of training time. |
|
25
|
|
|
|
|
26
|
|
|
""" |
|
27
|
|
|
|
|
28
|
|
|
import numpy as np |
|
29
|
|
|
import keras |
|
30
|
|
|
from keras.models import Sequential |
|
31
|
|
|
from keras.layers import ( |
|
32
|
|
|
Dense, |
|
33
|
|
|
Conv2D, |
|
34
|
|
|
MaxPooling2D, |
|
35
|
|
|
Flatten, |
|
36
|
|
|
Activation, |
|
37
|
|
|
Dropout, |
|
38
|
|
|
) |
|
39
|
|
|
from keras.datasets import cifar10 |
|
40
|
|
|
from keras.utils import to_categorical |
|
41
|
|
|
|
|
42
|
|
|
from hyperactive import Hyperactive |
|
43
|
|
|
|
|
44
|
|
|
(X_train, y_train), (X_test, y_test) = cifar10.load_data() |
|
45
|
|
|
|
|
46
|
|
|
y_train = to_categorical(y_train, 10) |
|
47
|
|
|
y_test = to_categorical(y_test, 10) |
|
48
|
|
|
|
|
49
|
|
|
# to make the example quick |
|
50
|
|
|
X_train = X_train[0:1000] |
|
51
|
|
|
y_train = y_train[0:1000] |
|
52
|
|
|
|
|
53
|
|
|
X_test = X_test[0:1000] |
|
54
|
|
|
y_test = y_test[0:1000] |
|
55
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
# create model and train it |
|
58
|
|
|
model = Sequential() |
|
59
|
|
|
model.add(Conv2D(64, (3, 3), padding="same", input_shape=X_train.shape[1:])) |
|
60
|
|
|
model.add(Activation("relu")) |
|
61
|
|
|
model.add(Conv2D(32, (3, 3))) |
|
62
|
|
|
model.add(Activation("relu")) |
|
63
|
|
|
model.add(MaxPooling2D(pool_size=(2, 2))) |
|
64
|
|
|
model.add(Dropout(0.25)) |
|
65
|
|
|
|
|
66
|
|
|
model.add(Conv2D(32, (3, 3), padding="same")) |
|
67
|
|
|
model.add(Activation("relu")) |
|
68
|
|
|
model.add(Dropout(0.25)) |
|
69
|
|
|
model.add(Flatten()) |
|
70
|
|
|
model.add(Dense(200)) |
|
71
|
|
|
model.add(Activation("relu")) |
|
72
|
|
|
model.add(Dropout(0.5)) |
|
73
|
|
|
model.add(Dense(10)) |
|
74
|
|
|
model.add(Activation("softmax")) |
|
75
|
|
|
|
|
76
|
|
|
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"]) |
|
77
|
|
|
model.fit(X_train, y_train, epochs=5, batch_size=500) |
|
78
|
|
|
|
|
79
|
|
|
model_pretrained = model |
|
80
|
|
|
n_layers = len(model_pretrained.layers) |
|
81
|
|
|
|
|
82
|
|
|
# delete the last 9 layers |
|
83
|
|
|
for i in range(n_layers - 9): |
|
84
|
|
|
model_pretrained.pop() |
|
85
|
|
|
|
|
86
|
|
|
# set remaining layers to not-trainable |
|
87
|
|
|
for layer in model_pretrained.layers: |
|
88
|
|
|
layer.trainable = False |
|
89
|
|
|
|
|
90
|
|
|
model_pretrained.summary() |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
def cnn(opt): |
|
94
|
|
|
model = keras.models.clone_model(model_pretrained) |
|
95
|
|
|
|
|
96
|
|
|
model.add(Flatten()) |
|
97
|
|
|
model.add(Dense(opt["neurons.0"])) |
|
98
|
|
|
model.add(Activation("relu")) |
|
99
|
|
|
model.add(Dropout(0.5)) |
|
100
|
|
|
model.add(Dense(10)) |
|
101
|
|
|
model.add(Activation("softmax")) |
|
102
|
|
|
|
|
103
|
|
|
model.compile( |
|
104
|
|
|
optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] |
|
105
|
|
|
) |
|
106
|
|
|
model.fit(X_train, y_train, epochs=5, batch_size=500) |
|
107
|
|
|
|
|
108
|
|
|
model.summary() |
|
109
|
|
|
|
|
110
|
|
|
_, score = model.evaluate(x=X_test, y=y_test) |
|
111
|
|
|
|
|
112
|
|
|
return score |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
# conv 1, 2, 3 are functions that adds layers. We want to know which function is the best |
|
116
|
|
|
def conv1(model): |
|
117
|
|
|
model.add(Conv2D(64, (3, 3))) |
|
118
|
|
|
model.add(Activation("relu")) |
|
119
|
|
|
model.add(MaxPooling2D(pool_size=(2, 2))) |
|
120
|
|
|
return model |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
def conv2(model): |
|
124
|
|
|
model.add(Conv2D(64, (3, 3))) |
|
125
|
|
|
model.add(Activation("relu")) |
|
126
|
|
|
return model |
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
def conv3(model): |
|
130
|
|
|
return model |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
search_space = { |
|
134
|
|
|
"conv_layer.0": [conv1, conv2, conv3], |
|
135
|
|
|
"neurons.0": list(range(100, 1000, 100)), |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
hyper = Hyperactive() |
|
140
|
|
|
hyper.add_search(cnn, search_space, n_iter=3) |
|
141
|
|
|
hyper.run() |
|
142
|
|
|
|