Skip to content

Commit 2683b97

Browse files
committed
Update _Dist/NeuralNetworks/b_TraditionalML/SVM.py
1 parent bec7b6f commit 2683b97

File tree

1 file changed

+12
-10
lines changed
  • _Dist/NeuralNetworks/b_TraditionalML

1 file changed

+12
-10
lines changed

_Dist/NeuralNetworks/b_TraditionalML/SVM.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ def __init__(self, *args, **kwargs):
1818

1919
def init_from_data(self, x, y, x_test, y_test, sample_weights, names):
2020
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"
2224
self.n_class = 1
2325

2426
def init_model_param_settings(self):
@@ -37,18 +39,18 @@ def _build_model(self, net=None):
3739
net, [current_dimension, 1], "_final_projection"
3840
)
3941

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-
4542
def _define_loss_and_train_step(self):
46-
self._loss = tf.reduce_sum(
43+
self._loss = self.c * tf.reduce_sum(
4744
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])
4946
with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
5047
self._train_step = self._optimizer.minimize(self._loss)
5148

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+
5254
def predict_classes(self, x):
5355
return (self._calculate(x, tensor=self._output, is_training=False) >= 0).astype(np.int32)
5456

@@ -101,9 +103,9 @@ def _define_py_collections(self):
101103
self.py_collections += ["_x", "_gram"]
102104

103105
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(
105107
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]
107109
with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)):
108110
self._train_step = self._optimizer.minimize(self._loss)
109111

0 commit comments

Comments
 (0)