예제 #1
0
 def testTrivialCompatibilityWithHinge(self):
   # Tests compatibility with unweighted hinge loss.
   x_shape = [55, 10]
   logits = tf.constant(np.random.randn(*x_shape).astype(np.float32))
   targets = tf.to_float(tf.constant(np.random.random_sample(x_shape) > 0.3))
   weighted_loss = util.weighted_hinge_loss(targets, logits)
   expected_loss = hinge_loss(targets, logits)
   with self.test_session():
     self.assertAllClose(expected_loss.eval(), weighted_loss.eval())
예제 #2
0
 def testTrivialCompatibilityWithHinge(self):
   # Tests compatibility with unweighted hinge loss.
   x_shape = [55, 10]
   logits = tf.constant(np.random.randn(*x_shape).astype(np.float32))
   targets = tf.to_float(tf.constant(np.random.random_sample(x_shape) > 0.3))
   weighted_loss = util.weighted_hinge_loss(targets, logits)
   expected_loss = hinge_loss(targets, logits)
   with self.test_session():
     self.assertAllClose(expected_loss.eval(), weighted_loss.eval())
예제 #3
0
 def testLessTrivialCompatibilityWithHinge(self):
   # Tests compatibility with a constant weight for positives and negatives.
   x_shape = [56, 11]
   logits = tf.constant(np.random.randn(*x_shape).astype(np.float32))
   targets = tf.to_float(tf.constant(np.random.random_sample(x_shape) > 0.7))
   weight = 1.0 + 1.0/2 + 1.0/3 + 1.0/4 + 1.0/5 + 1.0/6 + 1.0/7
   weighted_loss = util.weighted_hinge_loss(targets, logits, weight, weight)
   expected_loss = hinge_loss(targets, logits) * weight
   with self.test_session():
     self.assertAllClose(expected_loss.eval(), weighted_loss.eval())
예제 #4
0
 def testLessTrivialCompatibilityWithHinge(self):
   # Tests compatibility with a constant weight for positives and negatives.
   x_shape = [56, 11]
   logits = tf.constant(np.random.randn(*x_shape).astype(np.float32))
   targets = tf.to_float(tf.constant(np.random.random_sample(x_shape) > 0.7))
   weight = 1.0 + 1.0/2 + 1.0/3 + 1.0/4 + 1.0/5 + 1.0/6 + 1.0/7
   weighted_loss = util.weighted_hinge_loss(targets, logits, weight, weight)
   expected_loss = hinge_loss(targets, logits) * weight
   with self.test_session():
     self.assertAllClose(expected_loss.eval(), weighted_loss.eval())
예제 #5
0
  def test3DLogitsAndTargets(self):
    # Tests correctness when logits is 3D and targets is 2D.
    targets_shape = [30, 4]
    logits_shape = [targets_shape[0], targets_shape[1], 3]
    targets = tf.to_float(
        tf.constant(np.random.random_sample(targets_shape) > 0.7))
    logits = tf.constant(np.random.randn(*logits_shape).astype(np.float32))
    weight_vector = [1.0, 1.0, 1.0]
    loss = util.weighted_hinge_loss(targets, logits, weight_vector)

    with self.test_session():
      loss_value = loss.eval()
      for i in range(len(weight_vector)):
        expected = hinge_loss(targets, logits[:, :, i]).eval()
        self.assertAllClose(loss_value[:, :, i], expected)
예제 #6
0
  def test3DLogitsAndTargets(self):
    # Tests correctness when logits is 3D and targets is 2D.
    targets_shape = [30, 4]
    logits_shape = [targets_shape[0], targets_shape[1], 3]
    targets = tf.to_float(
        tf.constant(np.random.random_sample(targets_shape) > 0.7))
    logits = tf.constant(np.random.randn(*logits_shape).astype(np.float32))
    weight_vector = [1.0, 1.0, 1.0]
    loss = util.weighted_hinge_loss(targets, logits, weight_vector)

    with self.test_session():
      loss_value = loss.eval()
      for i in range(len(weight_vector)):
        expected = hinge_loss(targets, logits[:, :, i]).eval()
        self.assertAllClose(loss_value[:, :, i], expected)
예제 #7
0
  def testNontrivialCompatibilityWithHinge(self):
    # Tests compatibility with different positive and negative weights.
    x_shape = [23, 8]
    logits_positives = tf.constant(np.random.randn(*x_shape).astype(np.float32))
    logits_negatives = tf.constant(np.random.randn(*x_shape).astype(np.float32))
    targets_positives = tf.ones(x_shape)
    targets_negatives = tf.zeros(x_shape)
    logits = tf.concat([logits_positives, logits_negatives], 0)
    targets = tf.concat([targets_positives, targets_negatives], 0)

    raw_loss = util.weighted_hinge_loss(targets,
                                        logits,
                                        positive_weights=3.4,
                                        negative_weights=1.2)
    loss = tf.reduce_sum(raw_loss, 0)
    positives_hinge = hinge_loss(targets_positives, logits_positives)
    negatives_hinge = hinge_loss(targets_negatives, logits_negatives)
    expected = tf.add(tf.reduce_sum(3.4 * positives_hinge, 0),
                      tf.reduce_sum(1.2 * negatives_hinge, 0))

    with self.test_session():
      self.assertAllClose(loss.eval(), expected.eval())
예제 #8
0
  def testNontrivialCompatibilityWithHinge(self):
    # Tests compatibility with different positive and negative weights.
    x_shape = [23, 8]
    logits_positives = tf.constant(np.random.randn(*x_shape).astype(np.float32))
    logits_negatives = tf.constant(np.random.randn(*x_shape).astype(np.float32))
    targets_positives = tf.ones(x_shape)
    targets_negatives = tf.zeros(x_shape)
    logits = tf.concat([logits_positives, logits_negatives], 0)
    targets = tf.concat([targets_positives, targets_negatives], 0)

    raw_loss = util.weighted_hinge_loss(targets,
                                        logits,
                                        positive_weights=3.4,
                                        negative_weights=1.2)
    loss = tf.reduce_sum(raw_loss, 0)
    positives_hinge = hinge_loss(targets_positives, logits_positives)
    negatives_hinge = hinge_loss(targets_negatives, logits_negatives)
    expected = tf.add(tf.reduce_sum(3.4 * positives_hinge, 0),
                      tf.reduce_sum(1.2 * negatives_hinge, 0))

    with self.test_session():
      self.assertAllClose(loss.eval(), expected.eval())