def test_eval_generator(self, reduction):
     model = self._get_compiled_multi_io_model(loss=losses.MeanSquaredError(
         reduction=reduction))
     eval_result = model.evaluate_generator(custom_generator_multi_io(
         sample_weights=[self.sample_weight_1, self.sample_weight_2]),
                                            steps=2)
     self.assertAllClose(eval_result, self.expected_batch_result[reduction])
Beispiel #2
0
    def __init__(self,
                 visible_units,
                 hidden_units,
                 momentum=0.95,
                 k=1,
                 name='rbm'):
        super(RBM, self).__init__(name=name)

        self.w = self.add_weight(shape=(visible_units, hidden_units),
                                 initializer='random_normal',
                                 trainable=True,
                                 name="w")

        self.visible_units = visible_units

        self.v = DenseRBM(visible_units)
        self.h = DenseRBM(hidden_units)
        self.k = k

        self.momentum = momentum

        self.dW = tf.Variable(tf.zeros((visible_units, hidden_units)),
                              dtype=tf.float32)
        self.dVB = tf.Variable(tf.zeros((visible_units, )), dtype=tf.float32)
        self.dHB = tf.Variable(tf.zeros((hidden_units, )), dtype=tf.float32)

        self.mse = losses.MeanSquaredError()
 def test_fit_generator(self, reduction):
     model = self._get_compiled_multi_io_model(loss=losses.MeanSquaredError(
         reduction=reduction))
     history = model.fit_generator(custom_generator_multi_io(),
                                   steps_per_epoch=2,
                                   epochs=2)
     for key, value in self.expected_fit_result[reduction].items():
         self.assertAllClose(history.history[key], value)
  def test_standalone_loss_without_loss_reduction(self, distribution):
    with distribution.scope():
      loss_object = losses.MeanSquaredError()

      with self.assertRaisesRegexp(
          ValueError, 'Please use `tf.keras.losses.Reduction.SUM` or '
          '`tf.keras.losses.Reduction.NONE`'):
        y = np.asarray([1, 0])
        loss_object(y, y)
Beispiel #5
0
def get_loss_function(loss):
    """Returns the loss function corresponding to the given loss input."""
    if loss is None or isinstance(loss, losses.Loss):
        return loss

    # TODO(psv): After we have added all V2 losses, update this function.
    if loss in ['mse', 'MSE', 'mean_squared_error']:
        return losses.MeanSquaredError()
    return losses.get(loss)
Beispiel #6
0
 def test_eval_generator(self, reduction):
     if testing_utils.should_run_distributed():
         self.skipTest('b/137397816')
     model = self._get_compiled_multi_io_model(loss=losses.MeanSquaredError(
         reduction=reduction))
     eval_result = model.evaluate_generator(custom_generator_multi_io(
         sample_weights=[self.sample_weight_1, self.sample_weight_2]),
                                            steps=2)
     self.assertAllClose(eval_result, self.expected_batch_result[reduction])
 def test_eval(self, reduction):
     model = self._get_compiled_multi_io_model(loss=losses.MeanSquaredError(
         reduction=reduction))
     eval_result = model.evaluate([self.x, self.x], [self.y1, self.y2],
                                  batch_size=2,
                                  sample_weight={
                                      'output_1': self.sample_weight_1,
                                      'output_2': self.sample_weight_2,
                                  })
     self.assertAllClose(eval_result, self.expected_batch_result[reduction])
Beispiel #8
0
 def test_fit_generator(self, reduction):
     if testing_utils.should_run_distributed():
         self.skipTest('b/137397816')
     model = self._get_compiled_multi_io_model(loss=losses.MeanSquaredError(
         reduction=reduction))
     history = model.fit_generator(custom_generator_multi_io(
         sample_weights=[self.sample_weight_1, self.sample_weight_2]),
                                   steps_per_epoch=2,
                                   epochs=2)
     for key, value in self.expected_fit_result[reduction].items():
         self.assertAllClose(history.history[key], value)
Beispiel #9
0
    def test_loss_class_as_metric_with_distribution(self):
        distribution = one_device_strategy.OneDeviceStrategy('/device:CPU:0')
        with distribution.scope():
            metric_container = compile_utils.MetricsContainer(
                losses_mod.MeanSquaredError())
            y_t, y_p = array_ops.ones((10, 5)), array_ops.zeros((10, 5))
            metric_container.update_state(y_t, y_p)

            self.assertLen(metric_container.metrics, 1)
            metric = metric_container.metrics[0]
            self.assertEqual(metric.name, 'mean_squared_error')
            self.assertEqual(metric.result().numpy(), 1.)
 def test_test_on_batch(self, reduction):
     model = self._get_compiled_multi_io_model(loss=losses.MeanSquaredError(
         reduction=reduction))
     result = model.test_on_batch([self.x, self.x], [self.y1, self.y2],
                                  sample_weight={
                                      'output_1': self.sample_weight_1,
                                      'output_2': self.sample_weight_2,
                                  })
     expected_values = self.expected_batch_result[reduction]
     if reduction == losses_utils.ReductionV2.SUM:
         expected_values = self.expected_single_batch_result
     self.assertAllClose(result, expected_values)
 def test_fit(self, reduction):
     model = self._get_compiled_multi_io_model(loss=losses.MeanSquaredError(
         reduction=reduction))
     history = model.fit([self.x, self.x], [self.y1, self.y2],
                         sample_weight={
                             'output_1': self.sample_weight_1,
                             'output_2': self.sample_weight_2,
                         },
                         batch_size=2,
                         epochs=2,
                         shuffle=False)
     for key, value in self.expected_fit_result[reduction].items():
         self.assertAllClose(history.history[key], value)
Beispiel #12
0
 def test_test_on_batch(self, reduction):
   model = self._get_compiled_multi_io_model(
       loss=losses.MeanSquaredError(reduction=reduction))
   result = model.test_on_batch([self.x, self.x], [self.y1, self.y2],
                                sample_weight={
                                    'output_1': self.sample_weight_1,
                                    'output_2': self.sample_weight_2,
                                })
   expected_values = self.expected_batch_result[reduction]
   if reduction == loss_reduction.ReductionV2.SUM:
     # We are taking all the data as one batch, so undo the averaging here.
     expected_values = [x * 2 for x in self.expected_batch_result[reduction]]
   self.assertAllClose(result, expected_values)
def conf_loss(y_true, y_pred):
    y_true = K.cast(y_true, dtype=tf.float32)
    y_pred = K.cast(y_pred, dtype=tf.float32)
    bboxes_true = y_true[..., :4]
    bboxes_pred = y_pred[..., :4]
    conf_true = y_true[..., 4]
    conf_pred = y_pred[..., 4]
    #print(bboxes_true)
    #print(bboxes_true * K.expand_dims(conf_true))

    mseFunc = losses.MeanSquaredError(reduction='sum_over_batch_size')
    mse = mseFunc(bboxes_true * K.expand_dims(conf_true),
                  bboxes_pred * K.expand_dims(conf_true))

    bceFunc = losses.BinaryCrossentropy(reduction='sum_over_batch_size')
    bce = bceFunc(conf_true, conf_pred)
    #print("MSE: ", mse)
    #print("BCE: ", bce)
    return 10 * mse + bce
 def __init__(self, units, input_dim, alpha):
     super(CenterLossLinear, self).__init__()
     self.alpha = alpha
     self.centers = tf.zeros(shape=[units, input_dim], dtype=tf.float32)
     self.mse = losses.MeanSquaredError()()
     self.fc = layers.Dense(units)