@@ -18,7 +18,9 @@ def __init__(self, *args, **kwargs):
18
18
19
19
def init_from_data (self , x , y , x_test , y_test , sample_weights , names ):
20
20
super (LinearSVM , self ).init_from_data (x , y , x_test , y_test , sample_weights , names )
21
- self .model_param_settings .setdefault ("metric" , "binary_acc" )
21
+ metric = self .model_param_settings .setdefault ("metric" , "binary_acc" )
22
+ if metric == "acc" :
23
+ self .model_param_settings ["metric" ] = "binary_acc"
22
24
self .n_class = 1
23
25
24
26
def init_model_param_settings (self ):
@@ -37,18 +39,18 @@ def _build_model(self, net=None):
37
39
net , [current_dimension , 1 ], "_final_projection"
38
40
)
39
41
40
- def _get_feed_dict (self , x , y = None , weights = None , is_training = False ):
41
- if y is not None :
42
- y [y == 0 ] = - 1
43
- return super (LinearSVM , self )._get_feed_dict (x , y , weights , is_training )
44
-
45
42
def _define_loss_and_train_step (self ):
46
- self ._loss = tf .reduce_sum (
43
+ self ._loss = self . c * tf .reduce_sum (
47
44
tf .maximum (0. , 1 - self ._tfy * self ._output )
48
- ) + self . c * tf .nn .l2_loss (self ._ws [0 ])
45
+ ) + tf .nn .l2_loss (self ._ws [0 ])
49
46
with tf .control_dependencies (tf .get_collection (tf .GraphKeys .UPDATE_OPS )):
50
47
self ._train_step = self ._optimizer .minimize (self ._loss )
51
48
49
+ def _get_feed_dict (self , x , y = None , weights = None , is_training = False ):
50
+ if y is not None :
51
+ y [y == 0 ] = - 1
52
+ return super (LinearSVM , self )._get_feed_dict (x , y , weights , is_training )
53
+
52
54
def predict_classes (self , x ):
53
55
return (self ._calculate (x , tensor = self ._output , is_training = False ) >= 0 ).astype (np .int32 )
54
56
@@ -101,9 +103,9 @@ def _define_py_collections(self):
101
103
self .py_collections += ["_x" , "_gram" ]
102
104
103
105
def _define_loss_and_train_step (self ):
104
- self ._loss = tf .reduce_sum (tf .maximum (0. , 1 - self ._tfy * self ._output )) + 0.5 * tf .matmul (
106
+ self ._loss = self . c * tf .reduce_sum (tf .maximum (0. , 1 - self ._tfy * self ._output )) + 0.5 * tf .matmul (
105
107
self ._ws [0 ], tf .matmul (self ._gram , self ._ws [0 ]), transpose_a = True
106
- )[0 ] + self . c * tf . nn . l2_loss ( self . _ws [ 0 ])
108
+ )[0 ]
107
109
with tf .control_dependencies (tf .get_collection (tf .GraphKeys .UPDATE_OPS )):
108
110
self ._train_step = self ._optimizer .minimize (self ._loss )
109
111
0 commit comments