-
Notifications
You must be signed in to change notification settings - Fork 42
/
transfer_learning.py
47 lines (32 loc) · 1.11 KB
/
transfer_learning.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
import numpy as np
from keras.models import Sequential
from keras import applications
from keras.layers import Dense, Flatten, Dropout, Activation
from keras.datasets import cifar10
from keras.utils import to_categorical
from hyperactive import Hyperactive
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
nn = applications.VGG19(weights="imagenet", include_top=False)
for layer in nn.layers[:5]:
layer.trainable = False
def cnn(opt):
nn = Sequential()
nn.add(Flatten())
nn.add(Dense(opt["Dense.0"]))
nn.add(Activation("relu"))
nn.add(Dropout(opt["Dropout.0"]))
nn.add(Dense(10))
nn.add(Activation("softmax"))
nn.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
nn.fit(X_train, y_train, epochs=5, batch_size=256)
_, score = nn.evaluate(x=X_test, y=y_test)
return score
search_space = {
"Dense.0": list(range(100, 1000, 100)),
"Dropout.0": list(np.arange(0.1, 0.9, 0.1)),
}
hyper = Hyperactive()
hyper.add_search(cnn, search_space, n_iter=5)
hyper.run()