Пример #1
0
    def test_contrastive_loss_value(self):
        count = 0
        for i in range(0, 100):
            x0_val = Variable(self.x0)
            x1_val = Variable(self.x1)
            t_val = Variable(self.t)
            tml = ContrastiveLoss(margin=self.margin)
            loss = tml.forward(x0_val, x1_val, t_val)
            self.assertEqual(loss.data.numpy().shape, (1, ))
            self.assertEqual(loss.data.numpy().dtype, np.float32)
            loss_value = float(loss.data.numpy())

            # Compute expected value
            loss_expect = 0
            for i in range(self.x0.size()[0]):
                x0d, x1d, td = self.x0[i], self.x1[i], self.t[i]
                d = torch.sum(torch.pow(x0d - x1d, 2))
                if td == 1:  # similar pair
                    loss_expect += d

                elif td == 0:  # dissimilar pair
                    loss_expect += max(1 - np.sqrt(d), 0)**2
            loss_expect /= 2.0 * self.t.size()[0]
            #print("expected %s got %s" % (loss_expect, loss_value))
            if (round(loss_expect, 6) == round(loss_value, 6)):
                count += 1
            #print (self.assertAlmostEqual(loss_expect, loss_value, places=5))
        print(count * 1. / 100)
Пример #2
0
 def test_contrastive_loss(self):
     input1 = Variable(torch.randn(4, 4), requires_grad=True)
     input2 = Variable(torch.randn(4, 4), requires_grad=True)
     target = Variable(torch.randn(4), requires_grad=True)
     tml = ContrastiveLoss(margin=self.margin)
     self.assertTrue(
         gradcheck(lambda x1, x2, t: tml.forward(x1, x2, t),
                   (input1, input2, target)))