def test_mean_squared_loss_with_invalid_labels(self): scores = [[1., 3., 2.]] labels = [[0., -1., 1.]] with self.cached_session(): self.assertAlmostEqual(ranking_losses._mean_squared_loss( labels, scores).eval(), (1. + 1.) / 2, places=5)
def test_mean_squared_loss(self): scores = [[0.2, 0.5, 0.3], [0.2, 0.3, 0.5], [0.2, 0.3, 0.5]] labels = [[0., 0., 1.], [0., 0., 2.], [0., 0., 0.]] weights = [[2.], [1.], [1.]] with self.cached_session(): self.assertAlmostEqual( ranking_losses._mean_squared_loss(labels, scores).eval(), (_mean_squared_error(labels[0], scores[0]) + _mean_squared_error(labels[1], scores[1]) + _mean_squared_error(labels[2], scores[2])) / 9., places=5) self.assertAlmostEqual( ranking_losses._mean_squared_loss(labels, scores, weights).eval(), (_mean_squared_error(labels[0], scores[0]) * 2.0 + _mean_squared_error(labels[1], scores[1]) + _mean_squared_error(labels[2], scores[2])) / 9., places=5)
def test_make_loss_fn(self): scores = [[0.2, 0.5, 0.3], [0.2, 0.3, 0.5]] labels = [[0., 0., 1.], [0., 0., 1.]] weights = [[2.], [1.]] weights_1d = [2., 1.] weights_3d = [[[2.], [1.], [0.]], [[0.], [1.], [2.]]] weights_feature_name = 'weights' weights_1d_feature_name = 'weights_1d' weights_3d_feature_name = 'weights_3d' features = { weights_feature_name: weights, weights_1d_feature_name: weights_1d, weights_3d_feature_name: weights_3d } with self.cached_session(): pairwise_hinge_loss = ranking_losses._pairwise_hinge_loss(labels, scores).eval() pairwise_hinge_loss_weighted = ranking_losses._pairwise_hinge_loss( labels, scores, weights=weights).eval() pairwise_hinge_loss_itemwise_weighted = ( ranking_losses._pairwise_hinge_loss( labels, scores, weights=tf.squeeze(weights_3d)).eval()) mean_squared_loss = ranking_losses._mean_squared_loss(labels, scores).eval() mean_squared_loss_weighted = ranking_losses._mean_squared_loss( labels, scores, weights=weights).eval() mean_squared_loss_itemwise_weighted = ranking_losses._mean_squared_loss( labels, scores, weights=tf.squeeze(weights_3d)).eval() loss_keys = [ ranking_losses.RankingLossKey.PAIRWISE_HINGE_LOSS, ranking_losses.RankingLossKey.MEAN_SQUARED_LOSS ] loss_fn_simple = ranking_losses.make_loss_fn(loss_keys) self.assertAlmostEqual( loss_fn_simple(labels, scores, features).eval(), pairwise_hinge_loss + mean_squared_loss, places=5) # With 2-d list-wise weighted examples. loss_fn_weighted_example = ranking_losses.make_loss_fn( loss_keys, weights_feature_name=weights_feature_name) self.assertAlmostEqual( loss_fn_weighted_example(labels, scores, features).eval(), pairwise_hinge_loss_weighted + mean_squared_loss_weighted, places=5) # With 1-d list-wise weighted examples. loss_fn_weighted_example = ranking_losses.make_loss_fn( loss_keys, weights_feature_name=weights_1d_feature_name) self.assertAlmostEqual( loss_fn_weighted_example(labels, scores, features).eval(), pairwise_hinge_loss_weighted + mean_squared_loss_weighted, places=5) # With 3-d item-wise weighted examples. loss_fn_weighted_example = ranking_losses.make_loss_fn( loss_keys, weights_feature_name=weights_3d_feature_name) self.assertAlmostEqual( loss_fn_weighted_example(labels, scores, features).eval(), pairwise_hinge_loss_itemwise_weighted + mean_squared_loss_itemwise_weighted, places=5) # With both weighted loss and weighted examples. loss_weights = [3., 2.] weighted_loss_fn_weighted_example = ranking_losses.make_loss_fn( loss_keys, loss_weights, weights_feature_name=weights_feature_name) self.assertAlmostEqual( weighted_loss_fn_weighted_example(labels, scores, features).eval(), pairwise_hinge_loss_weighted * loss_weights[0] + mean_squared_loss_weighted * loss_weights[1], places=5) # Test loss reduction method. # Two reduction methods should return different loss values. loss_fn_1 = ranking_losses.make_loss_fn( loss_keys, reduction=tf.compat.v1.losses.Reduction.SUM) loss_fn_2 = ranking_losses.make_loss_fn( loss_keys, reduction=tf.compat.v1.losses.Reduction.MEAN) self.assertNotAlmostEqual( loss_fn_1(labels, scores, features).eval(), loss_fn_2(labels, scores, features).eval()) # Test invalid inputs. with self.assertRaisesRegexp(ValueError, r'loss_keys cannot be None or empty.'): ranking_losses.make_loss_fn([]) with self.assertRaisesRegexp(ValueError, r'loss_keys cannot be None or empty.'): ranking_losses.make_loss_fn('') with self.assertRaisesRegexp( ValueError, r'loss_keys and loss_weights must have the same size.'): ranking_losses.make_loss_fn(loss_keys, [2.0]) invalid_loss_fn = ranking_losses.make_loss_fn(['invalid_key']) with self.assertRaisesRegexp(ValueError, r'Invalid loss_key: invalid_key.'): invalid_loss_fn(labels, scores, features).eval()