Passed
Pull Request — master (#101)
by Simon
02:06
created

keras.KerasMultiLayerPerceptron.__init__()   A

Complexity

Conditions 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nop 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
from tensorflow import keras
2
from sklearn.datasets import make_classification
3
from sklearn.model_selection import train_test_split
4
5
from hyperactive import BaseExperiment
6
7
8
X, y = make_classification(n_samples=1000, n_features=20, random_state=42)
9
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2)
10
11
12
class KerasMultiLayerPerceptron(BaseExperiment):
13
    def __init__(self, X_train, X_val, y_train, y_val):
14
        super().__init__()
15
16
        self.X_train = X_train
17
        self.X_val = X_val
18
        self.y_train = y_train
19
        self.y_val = y_val
20
21
    def _score(self, **params):
22
        dense_layer_0 = params["dense_layer_0"]
23
        activation_layer_0 = params["activation_layer_0"]
24
25
        model = keras.Sequential(
26
            [
27
                keras.layers.Dense(
28
                    dense_layer_0,
29
                    activation=activation_layer_0,
30
                    input_shape=(20,),
31
                ),
32
                keras.layers.Dense(1, activation="sigmoid"),
33
            ]
34
        )
35
        model.compile(
36
            optimizer=keras.optimizers.Adam(learning_rate=0.01),
37
            loss="binary_crossentropy",
38
            metrics=["accuracy"],
39
        )
40
        model.fit(
41
            self.X_train,
42
            self.y_train,
43
            batch_size=32,
44
            epochs=10,
45
            validation_data=(self.X_val, self.y_val),
46
        )
47
        return model.evaluate(X_val, y_val)[1]
48