Ejemplo n.º 1
0
    def testCallWithAngles(self, datatype, ncols, mode):
        rtol, atol = 1e-4, 1e-7

        # Expected values
        X = torch.randn(2, ncols, dtype=datatype)
        R = torch.tensor([[math.cos(math.pi / 4), -math.sin(math.pi / 4)],
                          [math.sin(math.pi / 4),
                           math.cos(math.pi / 4)]],
                         dtype=datatype)
        if mode != 'Synthesis':
            expctdZ = R @ X
        else:
            expctdZ = R.T @ X

        # Instantiation of target class
        target = OrthonormalTransform(mode=mode)
        #target.angles.data = torch.tensor([math.pi/4])
        target.angles = nn.init.constant_(target.angles, val=math.pi / 4)

        # Actual values
        with torch.no_grad():
            actualZ = target.forward(X)

        # Evaluation
        self.assertTrue(torch.allclose(actualZ, expctdZ, rtol=rtol, atol=atol))
Ejemplo n.º 2
0
    def testNxNred(self, datatype, ncols, npoints, mode):
        rtol, atol = 1e-5, 1e-8

        # Configuration
        nAngles = int(npoints * (npoints - 1) / 2)

        # Expected values
        expctdLeftTop = torch.tensor(1., dtype=datatype)

        # Instantiation of target class
        target = OrthonormalTransform(n=npoints, mode=mode)
        target.angles = nn.init.uniform_(target.angles, a=0.0, b=2 * math.pi)
        target.angles.data[:npoints - 1] = torch.zeros(npoints - 1)

        # Actual values
        with torch.no_grad():
            matrix = target.forward(torch.eye(npoints, dtype=datatype))
        actualLeftTop = matrix[0, 0]  #.numpy()

        # Evaluation
        message = "actualLeftTop=%s differs from %s" % (str(actualLeftTop),
                                                        str(expctdLeftTop))
        #self.assertTrue(np.isclose(actualLeftTop,expctdLeftTop,rtol=rtol,atol=atol),message)
        self.assertTrue(
            torch.isclose(actualLeftTop, expctdLeftTop, rtol=rtol, atol=atol),
            message)
Ejemplo n.º 3
0
    def testBackwardAngsAndMus(self, datatype, mode, ncols):
        rtol, atol = 1e-4, 1e-7

        # Configuration
        #mode = 'Analysis'
        nPoints = 2
        #ncols = 1
        mus = [1, -1]

        # Expected values
        X = torch.randn(nPoints, ncols, dtype=datatype, requires_grad=True)
        dLdZ = torch.randn(nPoints, ncols, dtype=datatype)
        # angle = 2.*math.pi*randn(1)
        angle = 2. * math.pi * gauss(mu=0., sigma=1.)  #randn(1)
        R = torch.tensor([[math.cos(angle), -math.sin(angle)],
                          [-math.sin(angle), -math.cos(angle)]],
                         dtype=datatype)  #.squeeze()
        dRdW = torch.tensor(
            [[-math.sin(angle), -math.cos(angle)],
             [-math.cos(angle), math.sin(angle)]],
            dtype=datatype)  #.squeeze()
        if mode != 'Synthesis':
            expctddLdX = R.T @ dLdZ  # = dZdX @ dLdZ
            expctddLdW = torch.sum(dLdZ * (dRdW @ X))
        else:
            expctddLdX = R @ dLdZ  # = dZdX @ dLdZ
            expctddLdW = torch.sum(dLdZ * (dRdW.T @ X))

        # Instantiation of target class
        target = OrthonormalTransform(n=nPoints, dtype=datatype, mode=mode)
        target.angles = nn.init.constant_(target.angles, val=angle)
        target.mus = torch.tensor(mus, dtype=datatype)

        # Actual values
        torch.autograd.set_detect_anomaly(True)
        Z = target.forward(X)
        target.zero_grad()
        Z.backward(dLdZ)
        actualdLdX = X.grad
        actualdLdW = target.angles.grad

        # Evaluation
        self.assertTrue(
            torch.allclose(actualdLdX, expctddLdX, rtol=rtol, atol=atol))
        self.assertTrue(
            torch.allclose(actualdLdW, expctddLdW, rtol=rtol, atol=atol))
Ejemplo n.º 4
0
    def test4x4(self, datatype, ncols, mode):
        rtol, atol = 1e-5, 1e-8

        # Expected values
        expctdNorm = torch.tensor(1., dtype=datatype)

        # Instantiation of target class
        target = OrthonormalTransform(n=4, mode=mode)
        #target.angles.data = torch.randn(6,dtype=datatype)
        target.angles = nn.init.normal_(target.angles)

        # Actual values
        unitvec = torch.randn(4, ncols, dtype=datatype)
        unitvec /= unitvec.norm()
        with torch.no_grad():
            actualNorm = target.forward(unitvec).norm()  #.numpy()

        # Evaluation
        message = "actualNorm=%s differs from %s" % (str(actualNorm),
                                                     str(expctdNorm))
        #self.assertTrue(np.isclose(actualNorm,expctdNorm,rtol=rtol,atol=atol),message)
        self.assertTrue(
            torch.isclose(actualNorm, expctdNorm, rtol=rtol, atol=atol),
            message)