示例#1
0
def test_convolution(dist, n_dims):
    assert dist.sample(1).shape == (1, n_dims)
    assert dist.sample(64).shape == (64, n_dims)

    try:
        dist.log_prob(dist.sample(64))
    except NotImplementedError:
        pass

    dist.get_parameters()
示例#2
0
def test_gumbel_softmax(dist, n_dims):
    assert dist.sample(1).shape == (1, n_dims)
    assert dist.log_prob(dist.sample(1)).shape == (1, )

    samples = dist.sample(64)
    assert samples.shape == (64, n_dims)

    log_probs = dist.log_prob(samples)
    assert log_probs.shape == (64, )

    dist.get_parameters()
    try:
        dist.entropy()
    except NotImplementedError:
        pass
示例#3
0
def test_normal_dist(dist, n_dims):
    assert dist.sample(1).shape == (1, n_dims)
    assert dist.log_prob(dist.sample(1)).shape == (1, )

    samples = dist.sample(64)
    assert samples.shape == (64, n_dims)

    log_probs = dist.log_prob(samples)
    assert log_probs.shape == (64, )

    dist.get_parameters()
    dist.num_parameters
    try:
        dist.entropy()
    except NotImplementedError:
        pass
    try:
        dist.perplexity()
    except NotImplementedError:
        pass
示例#4
0
def test_dirac_delta(loc, n_dims):
    dist = DiracDelta(loc)
    assert dist.sample(1).shape == (1, n_dims), "{} {}".format(
        dist.sample(1).shape, n_dims)

    try:
        dist.log_prob(dist.sample(64))
    except NotImplementedError:
        pass

    samples = dist.sample(64)
    assert samples.shape == (64, n_dims)

    if n_dims == 1:
        assert dist.get_parameters()['loc'] == 1.0
    elif n_dims == 2:
        assert (dist.get_parameters()['loc'] == np.array([2.0, 3.0])).all()
    elif n_dims == 3:
        assert (dist.get_parameters()['loc'] == np.array([1.0, 2.0,
                                                          3.0])).all()
    else:
        raise ValueError()
    dist.get_parameters()
示例#5
0
def test_data_dist(n_dims):

    data = torch.randn(1000, n_dims)
    dist = Data(data)

    assert dist.sample(1).shape == (1, n_dims)
    assert dist.sample(64).shape == (64, n_dims)

    try:
        dist.log_prob(dist.sample(64))
    except NotImplementedError:
        pass

    assert dist.get_parameters()['n_dims'] == n_dims
    assert dist.get_parameters()['n_samples'] == 1000

    data = np.random.randn(100, n_dims)
    dist = Data(data)

    assert dist.sample(1).shape == (1, n_dims)
    assert dist.sample(64).shape == (64, n_dims)

    assert dist.get_parameters()['n_dims'] == n_dims
    assert dist.get_parameters()['n_samples'] == 100
示例#6
0
def sampleS(dist):
    print('sample* is called')
    print('input to sample is: ', str(dist))
    # takes in distribution type variable
    return dist.sample()
示例#7
0
def test_dirac_delta_2d():
    dist = DiracDelta([[1, 0], [0, 1]])
    assert dist.sample(1).shape == (1, 2, 2), "{} {}".format(
        dist.sample(1).shape, n_dims)
    dist.get_parameters()
 def reparameterize(self, dist, reparam=True):
     if reparam:
         latent = dist.rsample()
     else:
         latent = dist.sample()
     return latent