Passed
Push — master ( b87fc6...1c02b4 )
by Simon
01:32
created

pretrained_nas   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 76
dl 0
loc 141
rs 10
c 0
b 0
f 0
wmc 6

4 Functions

Rating   Name   Duplication   Size   Complexity  
A conv3() 0 2 1
A conv1() 0 5 1
A cnn() 0 30 3
A conv2() 0 4 1
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
29
import numpy as np
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_pretrained = Sequential()
59
model_pretrained.add(Conv2D(64, (3, 3), padding="same", input_shape=X_train.shape[1:]))
60
model_pretrained.add(Activation("relu"))
61
model_pretrained.add(Conv2D(32, (3, 3)))
62
model_pretrained.add(Activation("relu"))
63
model_pretrained.add(MaxPooling2D(pool_size=(2, 2)))
64
model_pretrained.add(Dropout(0.25))
65
66
model_pretrained.add(Conv2D(32, (3, 3), padding="same"))
67
model_pretrained.add(Activation("relu"))
68
model_pretrained.add(Dropout(0.25))
69
model_pretrained.add(Flatten())
70
model_pretrained.add(Dense(200))
71
model_pretrained.add(Activation("relu"))
72
model_pretrained.add(Dropout(0.5))
73
model_pretrained.add(Dense(10))
74
model_pretrained.add(Activation("softmax"))
75
76
model_pretrained.compile(
77
    optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"]
78
)
79
model_pretrained.fit(X_train, y_train, epochs=5, batch_size=256)
80
81
82
def cnn(opt):
83
    model = model_pretrained
84
    n_layers = len(model.layers)
85
86
    # delete the last 9 layers
87
    for i in range(n_layers - 9):
88
        model.pop()
89
90
    # set remaining layers to not-trainable
91
    for layer in model.layers:
92
        layer.trainable = False
93
94
    model = opt["conv_layer.0"](model)
95
    model.add(Dropout(0.25))
96
97
    model.add(Flatten())
98
    model.add(Dense(opt["neurons.0"]))
99
    model.add(Activation("relu"))
100
    model.add(Dropout(0.5))
101
    model.add(Dense(10))
102
    model.add(Activation("softmax"))
103
104
    model.compile(
105
        optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"]
106
    )
107
    model.fit(X_train, y_train, epochs=5, batch_size=256)
108
109
    _, score = model.evaluate(x=X_test, y=y_test)
110
111
    return score
112
113
114
# conv 1, 2, 3 are functions that adds layers. We want to know which function is the best
115
def conv1(model):
116
    model.add(Conv2D(64, (3, 3)))
117
    model.add(Activation("relu"))
118
    model.add(MaxPooling2D(pool_size=(2, 2)))
119
    return model
120
121
122
def conv2(model):
123
    model.add(Conv2D(64, (3, 3)))
124
    model.add(Activation("relu"))
125
    return model
126
127
128
def conv3(model):
129
    return model
130
131
132
search_space = {
133
    "conv_layer.0": [conv1, conv2, conv3],
134
    "neurons.0": list(range(100, 1000, 100)),
135
}
136
137
138
hyper = Hyperactive()
139
hyper.add_search(cnn, search_space, n_iter=5)
140
hyper.run()
141