Exemplo n.º 1
0
def main():
    flow = build_mera()
    last_epoch = utils.get_last_checkpoint_step()
    utils.load_checkpoint(last_epoch, flow)
    flow.train(False)

    shape = (16, args.nchannels, args.L, args.L)
    prior_low = Laplace(torch.tensor(0.), torch.tensor(T_low / sqrt(2)))
    z = prior_low.sample(shape)
    prior_high = Laplace(torch.tensor(0.), torch.tensor(T_high / sqrt(2)))
    z_high = prior_high.sample(shape)
    k = 2**level_cutoff
    z[:, :, ::k, ::k] = z_high[:, :, ::k, ::k]
    z = z.to(args.device)

    with torch.no_grad():
        x, _ = flow.inverse(z)

    samples = x.permute(0, 2, 3, 1).detach().cpu().numpy()
    samples = 1 / (1 + np.exp(-samples))

    fig, axes = plt.subplots(4, 4, figsize=(4, 4), sharex=True, sharey=True)
    for i in range(4):
        for j in range(4):
            ax = axes[i, j]
            ax.imshow(samples[j * 4 + i])
            ax.axis('off')
    plt.tight_layout()
    plt.savefig('./mix_T.pdf', bbox_inches='tight')
Exemplo n.º 2
0
def test5():
    """
    Laplacian distribution is a continuous probability distribution
    parameterized by loc and scale
    :return:
    """
    from torch.distributions.laplace import Laplace
    dist = Laplace(torch.tensor([10.0]), torch.tensor([0.99]))
    dist.sample()  # >>> tensor([10.2167])
Exemplo n.º 3
0
    def addnoise(self, x):
        """Adds Laplacian noise to histogram of counts
           Args:
                counts[torch tensor]: Histogram counts
                epsilon[integer]:Amount of Noise
           Returns:
                counts[torch tensor]: Noisy histogram of counts
        """

        m = Laplace(torch.tensor([0.0]), torch.tensor([self.epsilon]))
        count = x + m.sample()

        return count
 def sample_laplace(size, gpu=None):
     m = Laplace(torch.tensor([0.0]), torch.tensor([1.0]))
     y = m.sample(sample_shape=torch.Size([size[0], size[1], size[2]
                                           ])).float().squeeze(3)
     y = y if gpu is None else y.cuda(gpu)
     return y
Exemplo n.º 5
0
def addnoise(x):

    m = Laplace(torch.tensor([0.0]), torch.tensor([0.5]))
    count = x + m.sample().cuda()

    return count