-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathtest_ensemble.py
51 lines (40 loc) · 1.59 KB
/
test_ensemble.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
48
49
50
51
import tensorflow as tf
import numpy as np
from tensorcircuit.templates.ensemble import bagging
data_amount = 100 # Amount of data to be used
linear_demension = 4 # linear demension of the data
epochs = 10
batch_size = 32
lr = 1e-3
x_train, y_train = (np.ones([data_amount, linear_demension]), np.ones([data_amount, 1]))
obj_bagging = bagging()
def model():
DROP = 0.1
activation = 'selu'
inputs = tf.keras.Input(shape=(linear_demension,), name="digits")
x0 = tf.keras.layers.Dense(1,
kernel_regularizer = tf.keras.regularizers.l2(9.613e-06),
activation = activation,
)(inputs)
x0 = tf.keras.layers.Dropout(DROP)(x0)
x = tf.keras.layers.Dense(1,
kernel_regularizer = tf.keras.regularizers.l2(1e-07),
activation='sigmoid',
)(x0)
model = tf.keras.Model(inputs, x)
return model
obj_bagging.append(model(), False)
obj_bagging.append(model(), False)
obj_bagging.append(model(), False)
obj_bagging.compile(
loss=tf.keras.losses.BinaryCrossentropy(),
optimizer=tf.keras.optimizers.Adam(lr),
metrics=[tf.keras.metrics.AUC(),'acc']
)
obj_bagging.train(x = x_train, y = y_train, epochs = epochs, batch_size = batch_size, verbose = 0)
v_weight = obj_bagging.predict(x_train, "weight")
v_most = obj_bagging.predict(x_train, "most")
v_average = obj_bagging.predict(x_train, "average")
validation_data = []
validation_data.append(obj_bagging.eval([y_train,v_weight],"acc"))
validation_data.append(obj_bagging.eval([y_train,v_weight],"auc"))