9
9
from hyperopt import Trials , STATUS_OK , tpe
10
10
from hyperas import optim
11
11
from hyperas .distributions import choice , uniform
12
- from sklearn .metrics import roc_auc_score
13
12
14
13
def euclidean_distance (vects ):
15
14
x , y = vects
16
15
return K .sqrt (K .maximum (K .sum (K .square (x - y ), axis = 1 , keepdims = True ), K .epsilon ()))
17
16
18
-
19
17
def eucl_dist_output_shape (shapes ):
20
18
shape1 , shape2 = shapes
21
19
return (shape1 [0 ], 1 )
@@ -36,9 +34,7 @@ def create_pairs(x, digit_indices):
36
34
labels += [1 , 0 ]
37
35
return numpy .array (pairs ), numpy .array (labels )
38
36
39
-
40
37
def create_base_network (input_shape ,dense_filter1 ,dense_filter2 ,dense_filter3 ,dropout1 ,dropout2 ):
41
-
42
38
input = Input (shape = input_shape )
43
39
x = Flatten ()(input )
44
40
x = Dense (dense_filter1 , activation = 'relu' )(x )
@@ -48,15 +44,11 @@ def create_base_network(input_shape,dense_filter1,dense_filter2,dense_filter3,dr
48
44
x = Dense (dense_filter3 , activation = 'relu' )(x )
49
45
return Model (input , x )
50
46
51
-
52
47
def compute_accuracy (y_true , y_pred ):
53
-
54
48
pred = y_pred .ravel () < 0.5
55
49
return numpy .mean (pred == y_true )
56
50
57
-
58
51
def accuracy (y_true , y_pred ):
59
-
60
52
return K .mean (K .equal (y_true , K .cast (y_pred < 0.5 , y_true .dtype )))
61
53
62
54
def process_data ():
@@ -81,13 +73,10 @@ def data():
81
73
return tr_pairs , tr_y , te_pairs , te_y ,input_shape
82
74
83
75
def contrastive_loss (y_true , y_pred ):
84
-
85
76
margin = 1
86
77
return K .mean (y_true * K .square (y_pred ) +
87
78
(1 - y_true ) * K .square (K .maximum (margin - y_pred , 0 )))
88
79
89
-
90
-
91
80
def create_model (tr_pairs , tr_y , te_pairs , te_y ,input_shape ):
92
81
epochs = 20
93
82
dropout1 = {{uniform (0 ,1 )}}
@@ -101,9 +90,6 @@ def create_model(tr_pairs, tr_y, te_pairs, te_y,input_shape):
101
90
input_a = Input (shape = input_shape )
102
91
input_b = Input (shape = input_shape )
103
92
104
- # because we re-use the same instance `base_network`,
105
- # the weights of the network
106
- # will be shared across the two branches
107
93
processed_a = base_network (input_a )
108
94
processed_b = base_network (input_b )
109
95
@@ -112,7 +98,6 @@ def create_model(tr_pairs, tr_y, te_pairs, te_y,input_shape):
112
98
113
99
model = Model ([input_a , input_b ], distance )
114
100
115
- # train
116
101
rms = RMSprop ()
117
102
model .compile (loss = contrastive_loss , optimizer = rms , metrics = [accuracy ])
118
103
model .fit ([tr_pairs [:, 0 ], tr_pairs [:, 1 ]], tr_y ,
@@ -121,7 +106,6 @@ def create_model(tr_pairs, tr_y, te_pairs, te_y,input_shape):
121
106
verbose = 1 ,
122
107
validation_data = ([te_pairs [:, 0 ], te_pairs [:, 1 ]], te_y ))
123
108
124
- # compute final accuracy on training and test sets
125
109
y_pred = model .predict ([tr_pairs [:, 0 ], tr_pairs [:, 1 ]])
126
110
tr_acc = compute_accuracy (tr_y , y_pred )
127
111
y_pred = model .predict ([te_pairs [:, 0 ], te_pairs [:, 1 ]])
@@ -144,4 +128,3 @@ def create_model(tr_pairs, tr_y, te_pairs, te_y,input_shape):
144
128
print ("Evalutation of best performing model:" )
145
129
loss ,te_acc = best_model .evaluate ([te_pairs [:, 0 ], te_pairs [:, 1 ]], te_y )
146
130
print ("best prediction accuracy on test data %0.2f%%" % (100 * te_acc ))
147
-
0 commit comments