1
|
|
|
import numpy as np |
2
|
|
|
from keras.models import Sequential |
3
|
|
|
from keras import applications |
4
|
|
|
from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Dropout, Activation |
5
|
|
|
from keras.datasets import cifar10 |
6
|
|
|
from keras.utils import to_categorical |
7
|
|
|
|
8
|
|
|
from hyperactive import Hyperactive |
9
|
|
|
|
10
|
|
|
(X_train, y_train), (X_test, y_test) = cifar10.load_data() |
11
|
|
|
|
12
|
|
|
y_train = to_categorical(y_train, 10) |
13
|
|
|
y_test = to_categorical(y_test, 10) |
14
|
|
|
|
15
|
|
|
model = applications.VGG19(weights = "imagenet", include_top=False) |
16
|
|
|
|
17
|
|
|
for layer in model.layers[:5]: |
18
|
|
|
layer.trainable = False |
19
|
|
|
|
20
|
|
|
def cnn(para, X_train, y_train): |
21
|
|
|
model = Sequential() |
22
|
|
|
|
23
|
|
|
model.add(Flatten()) |
24
|
|
|
model.add(Dense(para["Dense.0"])) |
25
|
|
|
model.add(Activation("relu")) |
26
|
|
|
model.add(Dropout(para["Dropout.0"])) |
27
|
|
|
model.add(Dense(10)) |
28
|
|
|
model.add(Activation("softmax")) |
29
|
|
|
|
30
|
|
|
model.compile( |
31
|
|
|
optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"] |
32
|
|
|
) |
33
|
|
|
model.fit(X_train, y_train, epochs=25, batch_size=128) |
34
|
|
|
|
35
|
|
|
loss, score = model.evaluate(x=X_test, y=y_test) |
36
|
|
|
|
37
|
|
|
return score |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
search_config = {cnn: {"Dense.0": range(100, 1000, 100), "Dropout.0": np.arange(0.1, 0.9, 0.1)}} |
41
|
|
|
|
42
|
|
|
opt = Hyperactive(search_config, n_iter=5) |
43
|
|
|
opt.search(X_train, y_train) |
44
|
|
|
|