1
|
|
|
import numpy as np |
2
|
|
|
from keras.models import Sequential |
3
|
|
|
from keras import applications |
4
|
|
|
from keras.layers import Dense, 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
|
|
|
nn = applications.VGG19(weights="imagenet", include_top=False) |
16
|
|
|
|
17
|
|
|
for layer in nn.layers[:5]: |
18
|
|
|
layer.trainable = False |
19
|
|
|
|
20
|
|
|
|
21
|
|
View Code Duplication |
def cnn(para, X_train, y_train): |
|
|
|
|
22
|
|
|
nn = Sequential() |
23
|
|
|
|
24
|
|
|
nn.add(Flatten()) |
25
|
|
|
nn.add(Dense(para["Dense.0"])) |
26
|
|
|
nn.add(Activation("relu")) |
27
|
|
|
nn.add(Dropout(para["Dropout.0"])) |
28
|
|
|
nn.add(Dense(10)) |
29
|
|
|
nn.add(Activation("softmax")) |
30
|
|
|
|
31
|
|
|
nn.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"]) |
32
|
|
|
nn.fit(X_train, y_train, epochs=25, batch_size=128) |
33
|
|
|
|
34
|
|
|
_, score = nn.evaluate(x=X_test, y=y_test) |
35
|
|
|
|
36
|
|
|
return score |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
search_config = { |
40
|
|
|
cnn: {"Dense.0": range(100, 1000, 100), "Dropout.0": np.arange(0.1, 0.9, 0.1)} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
opt = Hyperactive(X_train, y_train) |
44
|
|
|
opt.search(search_config, n_iter=5) |
45
|
|
|
|