def fit(self, x, y, c=None, lr=None, batch_size=None, epoch=None, tol=None, optimizer=None, animation_params=None): if c is None: c = self._params["c"] if lr is None: lr = self._params["lr"] if batch_size is None: batch_size = self._params["batch_size"] if epoch is None: epoch = self._params["epoch"] if tol is None: tol = self._params["tol"] if optimizer is None: optimizer = self._params["optimizer"] *animation_properties, animation_params = self._get_animation_params( animation_params) x, y = np.atleast_2d(x), np.asarray(y) y_2d = y[..., None] self._w = tf.Variable(np.zeros([x.shape[1], 1]), dtype=tf.float32, name="w") self._b = tf.Variable(0., dtype=tf.float32, name="b") self._tfx = tf.placeholder(tf.float32, [None, x.shape[1]]) self._tfy = tf.placeholder(tf.float32, [None, 1]) self._y_pred_raw = tf.matmul(self._tfx, self._w) + self._b self._y_pred = tf.sign(self._y_pred_raw) loss = tf.reduce_sum(tf.nn.relu( 1 - self._tfy * self._y_pred_raw)) + c * tf.nn.l2_loss(self._w) train_step = TFOptFac().get_optimizer_by_name(optimizer, lr).minimize(loss) self._sess.run(tf.global_variables_initializer()) bar = ProgressBar(max_value=epoch, name="TFLinearSVM") ims = [] train_repeat = self._get_train_repeat(x, batch_size) for i in range(epoch): l = self._batch_training(x, y_2d, batch_size, train_repeat, loss, train_step) if l < tol: bar.terminate() break self._handle_animation(i, x, y, ims, animation_params, *animation_properties) bar.update() self._handle_mp4(ims, animation_properties)
def _prepare(self, sample_weight, **kwargs): lr = kwargs.get("lr", self._params["lr"]) self._w = tf.Variable(np.zeros([len(self._x), 1]), dtype=tf.float32, name="w") self._b = tf.Variable(.0, dtype=tf.float32, name="b") self._tfx = tf.placeholder(tf.float32, [None, None]) self._tfy = tf.placeholder(tf.float32, [None]) self._y_pred_raw = tf.transpose(tf.matmul(self._tfx, self._w) + self._b) self._y_pred = tf.sign(self._y_pred_raw) self._loss = tf.reduce_sum( tf.maximum(1 - self._tfy * self._y_pred_raw, 0) * sample_weight ) + 0.5 * tf.matmul( # self._w, tf.matmul(self._tfx, self._w, transpose_b=True) (self._y_pred_raw - self._b), self._w )[0][0] self._train_step = TFOptFac().get_optimizer_by_name( self._optimizer, lr ).minimize(self._loss) self._sess.run(tf.global_variables_initializer())