Ejemplo n.º 1
0
def graph_distance_similarity(netC,
                              netG,
                              inpobj,
                              inplatent,
                              numsamples,
                              gpu=0):
    simCriterion = lnn.MSECriterion()
    dists = np.zeros(numsamples)
    sims = np.zeros(numsamples)
    if gpu > 0:
        simCriterion = simCriterion.cuda()
        latent_np = inplatent.cpu().numpy()
    else:
        latent_np = inplatent.numpy()
    latent_np = latent_np.reshape(inplatent.size(0))
    for i in range(0, numsamples):
        noise = torch.rand(inplatent.size(0), 1, 1, 1)
        noise = noise.mul((i + 1) * 0.05)
        if gpu > 0:
            noise = noise.cuda()
        olatent = inplatent.add(noise)
        olatent.unsqueeze_(0)
        if gpu > 0:
            olatent_np = olatent.cpu().numpy().reshape(inplatent.size(0))
        else:
            olatent_np = olatent.numpy().reshape(inplatent.size(0))
        oresult = netG.forward(olatent)
        difflatent_np = olatent_np - latent_np
        dist = np.linalg.norm(difflatent_np)
        feat1 = netC.forward(inpobj)
        feat1 = feat1.clone()
        feat2 = netC.forward(oresult)
        similarity = simCriterion.forward(feat2, feat1)
        dists[i] = dist
        sims[i] = similarity
        print(str(dist) + " " + str(similarity))
    return dists, sims
Ejemplo n.º 2
0
    def test_ParallelCriterion(self):
        input = [torch.rand(2, 10), torch.randn(2, 10)]
        target = [torch.LongTensor((1, 8)), torch.randn(2, 10)]
        nll = nn.ClassNLLCriterion()
        mse = nn.MSECriterion()
        pc = nn.ParallelCriterion().add(nll, 0.5).add(mse)
        output = pc.forward(input, target)
        output2 = nll.forward(input[0], target[0]) / 2 + mse.forward(
            input[1], target[1])
        self.assertEqual(output, output2)
        gradInput2 = [
            nll.backward(input[0], target[0]).clone().div(2),
            mse.backward(input[1], target[1])
        ]
        gradInput = pc.backward(input, target)
        self.assertEqual(gradInput[0], gradInput2[0])
        self.assertEqual(gradInput[1], gradInput2[1])

        # test type
        pc.float()
        gradInput[0], gradInput[1] = gradInput[0].clone(), gradInput[1].clone()
        input3 = [input[0].float(), input[1].float()]
        target3 = [target[0], target[1].float()]
        output3 = pc.forward(input3, target3)
        gradInput3 = pc.backward(input3, target3)
        self.assertEqual(output, output3)
        self.assertEqual(gradInput[0].float(), gradInput3[0])
        self.assertEqual(gradInput[1].float(), gradInput3[1])

        # test repeatTarget
        input = [torch.rand(2, 10), torch.randn(2, 10)]
        target = torch.randn(2, 10)
        mse = nn.MSECriterion()
        pc = nn.ParallelCriterion(True).add(mse, 0.5).add(nn.MSECriterion())
        output = pc.forward(input, target)
        output2 = mse.forward(input[0], target) / 2 + mse.forward(
            input[1], target)
        self.assertEqual(output, output2)
        gradInput = pc.backward(input, target)
        gradInput2 = [
            mse.backward(input[0], target).clone().div(2),
            mse.backward(input[1], target)
        ]
        self.assertEqual(gradInput[0], gradInput2[0])
        self.assertEqual(gradInput[1], gradInput2[1])

        # table input
        input = [torch.randn(2, 10), [torch.rand(2, 10), torch.randn(2, 10)]]
        target = [
            torch.LongTensor((2, 5)),
            [torch.LongTensor((1, 8)),
             torch.randn(2, 10)]
        ]
        nll2 = nn.ClassNLLCriterion()
        nll = nn.ClassNLLCriterion()
        mse = nn.MSECriterion()
        pc = nn.ParallelCriterion().add(nll, 0.5).add(mse)
        pc2 = nn.ParallelCriterion().add(nll2, 0.4).add(pc)
        output = pc2.forward(input, target)
        output2 = nll2.forward(input[0], target[0]) * 0.4 + nll.forward(
            input[1][0], target[1][0]) / 2 + mse.forward(
                input[1][1], target[1][1])
        self.assertEqual(output, output2)
        gradInput2 = [
            nll2.backward(input[0], target[0]).clone().mul(0.4),
            [
                nll.backward(input[1][1], target[1][0]).clone().div(2),
                mse.backward(input[1][1], target[1][1])
            ]
        ]
        gradInput = pc2.backward(input, target)
        self.assertEqual(gradInput[0], gradInput2[0])
        self.assertEqual(gradInput[1][0], gradInput2[1][0])
        self.assertEqual(gradInput[1][1], gradInput2[1][1])

        # Check that these don't raise errors
        pc.__repr__()
        str(pc)