Пример #1
0
def test_translationalinvariance_1d():
    Nlayers = 2
    Hs = 10
    Ht = 10
    sList = [MLP(2, Hs, activation=ScalableTanh([2])) for _ in range(Nlayers)]
    tList = [MLP(2, Ht, activation=ScalableTanh([2])) for _ in range(Nlayers)]
    masktypelist = ['evenodd', 'evenodd'] * (Nlayers // 2)
    #assamble RNVP blocks into a TEBD layer
    prior = Gaussian([8])
    layers = [
        RealNVP([2], sList, tList, Gaussian([2]), masktypelist)
        for _ in range(6)
    ]
    model = MERA(1, 2, 8, layers, prior)

    x = model.sample(10)
    xright = Roll(4, 1).forward(x)
    xleft = Roll(-4, 1).forward(x)

    logp = model.logProbability(x)
    assert_array_almost_equal(logp.data.numpy(),
                              model.logProbability(xleft).data.numpy(),
                              decimal=6)
    assert_array_almost_equal(logp.data.numpy(),
                              model.logProbability(xright).data.numpy(),
                              decimal=6)
Пример #2
0
def test_translationalinvariance():

    #RNVP block
    Depth = 8
    Nlayers = 2
    Hs = 10
    Ht = 10
    sList = [MLP(2, Hs) for _ in range(Nlayers)]
    tList = [MLP(2, Ht) for _ in range(Nlayers)]
    masktypelist = ['channel', 'channel'] * (Nlayers // 2)

    #assamble RNVP blocks into a TEBD layer
    prior = Gaussian([8])
    layers = [
        RealNVP([2], sList, tList, Gaussian([2]), masktypelist)
        for _ in range(Depth)
    ]

    model = TEBD(1, 2, Depth, layers, prior)

    x = model.sample(10)
    xright = Roll(2, 1).forward(x)
    xleft = Roll(-2, 1).forward(x)

    logp = model.logProbability(x)
    assert_array_almost_equal(logp.data.numpy(),
                              model.logProbability(xleft).data.numpy(),
                              decimal=6)
    assert_array_almost_equal(logp.data.numpy(),
                              model.logProbability(xright).data.numpy(),
                              decimal=6)
Пример #3
0
def test_mera_1d():
    masks = [
        Variable(torch.ByteTensor([1, 0, 1, 0, 1, 0, 1, 0])),
        Variable(torch.ByteTensor([1, 0, 0, 0, 1, 0, 0, 0]))
    ]
    masks_ = [
        Variable(torch.ByteTensor([0, 1, 0, 1, 0, 1, 0, 1])),
        Variable(torch.ByteTensor([0, 1, 1, 1, 0, 1, 1, 1]))
    ]

    rollList = [
        Placeholder(),
        Roll(1, 1),
        Placeholder(),
        Roll(1, 1),
        Placeholder(),
        Roll(1, 1)
    ]
    maskList = [
        Placeholder(2),
        Placeholder(2),
        Mask(masks[0], masks_[0]),
        Mask(masks[0], masks_[0]),
        Mask(masks[1], masks_[1]),
        Mask(masks[1], masks_[1])
    ]
    Nlayers = 4
    Hs = 10
    Ht = 10
    sList = [MLP(2, Hs) for _ in range(Nlayers)]
    tList = [MLP(2, Ht) for _ in range(Nlayers)]
    masktypelist = ['channel', 'channel'] * (Nlayers // 2)
    #assamble RNVP blocks into a TEBD layer
    prior = Gaussian([8])
    layers = [
        RealNVP([2], sList, tList, Gaussian([2]), masktypelist)
        for _ in range(6)
    ]
    model = HierarchyBijector(1, [2 for _ in range(6)], rollList, layers,
                              maskList, Gaussian([8]))
    z = prior(4)
    print(z)
    x = model.inference(z, True)
    print(x)
    fLog = model._inferenceLogjac
    print(model._inferenceLogjac)
    zz = model.generate(x, True)
    print(zz)
    bLog = model._generateLogjac
    print(model._generateLogjac)

    print(model.sample(10))

    saveDict = model.saveModel({})
    torch.save(saveDict, './savetest.testSave')

    masksp = [
        Variable(torch.ByteTensor([1, 0, 1, 0, 1, 0, 1, 0])),
        Variable(torch.ByteTensor([1, 0, 0, 0, 1, 0, 0, 0]))
    ]
    masks_p = [
        Variable(torch.ByteTensor([0, 1, 0, 1, 0, 1, 0, 1])),
        Variable(torch.ByteTensor([0, 1, 1, 1, 0, 1, 1, 1]))
    ]

    rollListp = [
        Placeholder(),
        Roll(1, 1),
        Placeholder(),
        Roll(1, 1),
        Placeholder(),
        Roll(1, 1)
    ]
    maskListp = [
        Placeholder(2),
        Placeholder(2),
        Mask(masksp[0], masks_p[0]),
        Mask(masksp[0], masks_p[0]),
        Mask(masksp[1], masks_p[1]),
        Mask(masksp[1], masks_p[1])
    ]
    Nlayersp = 4
    Hsp = 10
    Htp = 10
    sListp = [MLP(2, Hsp) for _ in range(Nlayersp)]
    tListp = [MLP(2, Htp) for _ in range(Nlayersp)]
    masktypelistp = ['channel', 'channel'] * (Nlayersp // 2)
    #assamble RNVP blocks into a TEBD layer
    priorp = Gaussian([8])
    layersp = [
        RealNVP([2], sListp, tListp, Gaussian([2]), masktypelistp)
        for _ in range(6)
    ]
    modelp = HierarchyBijector(1, [2 for _ in range(6)], rollListp, layersp,
                               maskListp, Gaussian([8]))

    saveDictp = torch.load('./savetest.testSave')
    modelp.loadModel(saveDictp)

    xp = modelp.inference(z)

    print(xp)

    assert_array_almost_equal(z.data.numpy(), zz.data.numpy())
    assert_array_almost_equal(fLog.data.numpy(), -bLog.data.numpy())
    assert_array_almost_equal(xp.data.numpy(), x.data.numpy())
Пример #4
0
def test_Roll():
    #t = np.random.randn(2,4,4)
    t = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
    r1 = np.roll(t,1,0)
    r2 = np.roll(t,1,1)
    r3 = np.roll(t,-1,0)
    r4 = np.roll(t,-1,1)

    tt = torch.from_numpy(t)
    tt = tt.view(-1,4,4)
    ll1 = Roll(1,1)
    l1 = ll1.forward(tt)
    l1p = ll1.reverse(l1)
    ll2 = Roll(1,2)
    l2 = ll2.forward(tt)
    l2p = ll2.reverse(l2)
    ll3 = Roll(-1,1)
    l3 = ll3.forward(tt)
    l3p = ll3.reverse(l3)
    ll4 = Roll(-1,2)
    l4 = ll4.forward(tt)
    l4p = ll4.reverse(l4)

    #print(tt)
    #print(l4)
    #print(l4p)

    assert_array_almost_equal(r1,l3.numpy()[0])
    assert_array_almost_equal(r2,l4.numpy()[0])
    assert_array_almost_equal(r3,l1.numpy()[0])
    assert_array_almost_equal(r4,l2.numpy()[0])
    assert_array_equal(l1p.numpy(),l2p.numpy())
    assert_array_equal(l3p.numpy(),l2p.numpy())
    assert_array_equal(l4p.numpy(),l2p.numpy())