Passed
Push — master ( ef01f2...fd3757 )
by Simon
01:46 queued 10s
created

Transfer Learning   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 45
Duplicated Lines 35.56 %

Importance

Changes 0
Metric Value
wmc 1
eloc 30
dl 16
loc 45
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A cnn() 16 16 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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