Passed
Push — master ( d03f6d...d5da96 )
by Simon
01:24
created

NeuralArchitectureSearch   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 72
Duplicated Lines 40.28 %

Importance

Changes 0
Metric Value
wmc 4
eloc 50
dl 29
loc 72
rs 10
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A conv2() 0 4 1
A cnn() 29 29 1
A conv3() 0 2 1
A conv1() 0 5 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
from keras.nns import Sequential
2
from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Activation, Dropout
3
from keras.datasets import cifar10
4
from keras.utils import to_categorical
5
6
from hyperactive import Hyperactive
7
8
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
9
10
y_train = to_categorical(y_train, 10)
11
y_test = to_categorical(y_test, 10)
12
13
14
def conv1(nn):
15
    nn.add(Conv2D(32, (3, 3)))
16
    nn.add(Activation("relu"))
17
    nn.add(MaxPooling2D(pool_size=(2, 2)))
18
    return nn
19
20
21
def conv2(nn):
22
    nn.add(Conv2D(32, (3, 3)))
23
    nn.add(Activation("relu"))
24
    return nn
25
26
27
def conv3(nn):
28
    return nn
29
30
31 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...
32
    nn = Sequential()
33
    nn.add(
34
        Conv2D(para["filters.0"], (3, 3), padding="same", input_shape=X_train.shape[1:])
35
    )
36
    nn.add(Activation("relu"))
37
    nn.add(Conv2D(para["filters.0"], (3, 3)))
38
    nn.add(Activation("relu"))
39
    nn.add(MaxPooling2D(pool_size=(2, 2)))
40
    nn.add(Dropout(0.25))
41
42
    nn.add(Conv2D(para["filters.0"], (3, 3), padding="same"))
43
    nn.add(Activation("relu"))
44
    nn = para["conv_layer.0"](nn)
45
    nn.add(Dropout(0.25))
46
47
    nn.add(Flatten())
48
    nn.add(Dense(para["neurons.0"]))
49
    nn.add(Activation("relu"))
50
    nn.add(Dropout(0.5))
51
    nn.add(Dense(10))
52
    nn.add(Activation("softmax"))
53
54
    nn.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
55
    nn.fit(X_train, y_train, epochs=25, batch_size=128)
56
57
    _, score = nn.evaluate(x=X_test, y=y_test)
58
59
    return score
60
61
62
search_config = {
63
    cnn: {
64
        "conv_layer.0": [conv1, conv2, conv3],
65
        "filters.0": [16, 32, 64, 128],
66
        "neurons.0": range(100, 1000, 100),
67
    }
68
}
69
70
opt = Hyperactive(X_train, y_train)
71
opt.search(search_config, n_iter=5)
72