def test_sample_weighted(self): pin_obj = quantiles.PinballLoss() y_true = tf.constant([1, 9, 2, -5, -2, 6], shape=(2, 3)) y_pred = tf.constant([4, 8, 12, 8, 1, 3], shape=(2, 3), dtype=tf.dtypes.float32) sample_weight = tf.constant([1.2, 3.4], shape=(2, 1)) loss = pin_obj(y_true, y_pred, sample_weight=sample_weight) self.assertAlmostEqual(self.evaluate(loss), 40.7 / 6, 3)
def test_timestep_weighted(): pin_obj = quantiles.PinballLoss() y_true = tf.constant([1, 9, 2, -5, -2, 6], shape=(2, 3, 1)) y_pred = tf.constant([4, 8, 12, 8, 1, 3], shape=(2, 3, 1), dtype=tf.dtypes.float32) sample_weight = tf.constant([3, 6, 5, 0, 4, 2], shape=(2, 3)) loss = pin_obj(y_true, y_pred, sample_weight=sample_weight) np.testing.assert_almost_equal(loss, 41.5 / 6, 3)
def test_invalid_sample_weight(): pin_obj = quantiles.PinballLoss() y_true = tf.constant([1, 9, 2, -5, -2, 6], shape=(2, 3, 1)) y_pred = tf.constant([4, 8, 12, 8, 1, 3], shape=(2, 3, 1)) sample_weight = tf.constant([3, 6, 5, 0], shape=(2, 2)) with pytest.raises(tf.errors.InvalidArgumentError, match="Incompatible shapes"): pin_obj(y_true, y_pred, sample_weight=sample_weight)
def test_invalid_sample_weight(self): pin_obj = quantiles.PinballLoss() y_true = tf.constant([1, 9, 2, -5, -2, 6], shape=(2, 3, 1)) y_pred = tf.constant([4, 8, 12, 8, 1, 3], shape=(2, 3, 1)) sample_weight = tf.constant([3, 6, 5, 0], shape=(2, 2)) with self.assertRaisesRegexp(ValueError, "weights can not be broadcast to values"): pin_obj(y_true, y_pred, sample_weight=sample_weight)
def test_sum_reduction(): pin_obj = quantiles.PinballLoss(reduction=tf.keras.losses.Reduction.SUM) y_true = tf.constant([1, 9, 2, -5, -2, 6], shape=(2, 3)) y_pred = tf.constant([4, 8, 12, 8, 1, 3], shape=(2, 3), dtype=tf.dtypes.float32) loss = pin_obj(y_true, y_pred, sample_weight=2.3) np.testing.assert_almost_equal(loss, 12.65, 3)
def test_zero_weighted(): pin_obj = quantiles.PinballLoss() y_true = tf.constant([1, 9, 2, -5, -2, 6], shape=(2, 3)) y_pred = tf.constant([4, 8, 12, 8, 1, 3], shape=(2, 3), dtype=tf.dtypes.float32) loss = pin_obj(y_true, y_pred, sample_weight=0) np.testing.assert_almost_equal(loss, 0.0, 3)
def test_unweighted_quantile_100pc(self): pin_obj = quantiles.PinballLoss(tau=1.0) y_true = tf.constant([1, 9, 2, -5, -2, 6], shape=(2, 3)) y_pred = tf.constant([4, 8, 12, 8, 1, 3], shape=(2, 3), dtype=tf.dtypes.float32) loss = pin_obj(y_true, y_pred) self.assertAlmostEqual(self.evaluate(loss), 0.6666, 3)
def test_unweighted_quantile_100pc(): pin_obj = quantiles.PinballLoss(tau=1.0) y_true = tf.constant([1, 9, 2, -5, -2, 6], shape=(2, 3)) y_pred = tf.constant([4, 8, 12, 8, 1, 3], shape=(2, 3), dtype=tf.dtypes.float32) loss = pin_obj(y_true, y_pred) np.testing.assert_almost_equal(loss, 0.6666, 3)
def test_sum_reduction(self): pin_obj = quantiles.PinballLoss( reduction=tf.keras.losses.Reduction.SUM) y_true = tf.constant([1, 9, 2, -5, -2, 6], shape=(2, 3)) y_pred = tf.constant([4, 8, 12, 8, 1, 3], shape=(2, 3), dtype=tf.dtypes.float32) loss = pin_obj(y_true, y_pred, sample_weight=2.3) self.assertAlmostEqual(self.evaluate(loss), 12.65, 3)
def test_invalid_sample_weight(self): self.skipTest("Failing. See https://github.com/tensorflow/addons/issues/1202") pin_obj = quantiles.PinballLoss() y_true = tf.constant([1, 9, 2, -5, -2, 6], shape=(2, 3, 1)) y_pred = tf.constant([4, 8, 12, 8, 1, 3], shape=(2, 3, 1)) sample_weight = tf.constant([3, 6, 5, 0], shape=(2, 2)) with self.assertRaisesRegexp( ValueError, "weights can not be broadcast to values" ): pin_obj(y_true, y_pred, sample_weight=sample_weight)
def test_no_reduction(self): pin_obj = quantiles.PinballLoss( reduction=tf.keras.losses.Reduction.NONE) y_true = tf.constant([1, 9, 2, -5, -2, 6], shape=(2, 3)) y_pred = tf.constant([4, 8, 12, 8, 1, 3], shape=(2, 3), dtype=tf.dtypes.float32) loss = pin_obj(y_true, y_pred, sample_weight=2.3) loss = self.evaluate(loss) self.assertArrayNear(loss, [5.3666, 7.28333], 1e-3)
def test_timestep_weighted(self): self.skipTest("Failing. See https://github.com/tensorflow/addons/issues/1202") pin_obj = quantiles.PinballLoss() y_true = tf.constant([1, 9, 2, -5, -2, 6], shape=(2, 3, 1)) y_pred = tf.constant( [4, 8, 12, 8, 1, 3], shape=(2, 3, 1), dtype=tf.dtypes.float32 ) sample_weight = tf.constant([3, 6, 5, 0, 4, 2], shape=(2, 3)) loss = pin_obj(y_true, y_pred, sample_weight=sample_weight) self.assertAlmostEqual(self.evaluate(loss), 41.5 / 6, 3)
def test_timestep_weighted(): pytest.skip( "Failing. See https://github.com/tensorflow/addons/issues/1202") pin_obj = quantiles.PinballLoss() y_true = tf.constant([1, 9, 2, -5, -2, 6], shape=(2, 3, 1)) y_pred = tf.constant([4, 8, 12, 8, 1, 3], shape=(2, 3, 1), dtype=tf.dtypes.float32) sample_weight = tf.constant([3, 6, 5, 0, 4, 2], shape=(2, 3)) loss = pin_obj(y_true, y_pred, sample_weight=sample_weight) np.testing.assert_almost_equal(loss, 41.5 / 6, 3)
def test_invalid_sample_weight(): pin_obj = quantiles.PinballLoss() y_true = tf.constant([1, 9, 2, -5, -2, 6], shape=(2, 3, 1)) y_pred = tf.constant([4, 8, 12, 8, 1, 3], shape=(2, 3, 1)) sample_weight = tf.constant([3, 6, 5, 0], shape=(2, 2)) if LooseVersion(tf.__version__) >= "2.2": with pytest.raises(tf.errors.InvalidArgumentError, match="Incompatible shapes"): pin_obj(y_true, y_pred, sample_weight=sample_weight) else: with pytest.raises(ValueError, match="weights can not be broadcast to values"): pin_obj(y_true, y_pred, sample_weight=sample_weight)
def test_all_correct_unweighted(): pin_obj = quantiles.PinballLoss() y_true = tf.constant([4, 8, 12, 8, 1, 3], shape=(2, 3)) loss = pin_obj(y_true, y_true) assert loss == 0
def test_config(): pin_obj = quantiles.PinballLoss( reduction=tf.keras.losses.Reduction.SUM, name="pin_1" ) assert pin_obj.name == "pin_1" assert pin_obj.reduction == tf.keras.losses.Reduction.SUM
def test_config(self): pin_obj = quantiles.PinballLoss( reduction=tf.keras.losses.Reduction.SUM, name="pin_1") self.assertEqual(pin_obj.name, "pin_1") self.assertEqual(pin_obj.reduction, tf.keras.losses.Reduction.SUM)
def test_all_correct_unweighted(self): pin_obj = quantiles.PinballLoss() y_true = tf.constant([4, 8, 12, 8, 1, 3], shape=(2, 3)) loss = pin_obj(y_true, y_true) self.assertAlmostEqual(self.evaluate(loss), 0.0, 3)