How can I create a tensor of batch batch_size of uniformly distributed values between -1 and 1?

Viewed 276

The title pretty much sums it, I'm trying to implement a GAN: How can I create a tensor of batch batch_size of uniformly distributed values between -1 and 1 with pytorch?

def create_latent_batch_vectors(batch_size, latent_vector_size, device):
'''
The function creates a random batch of latent vectors with random values 
distributed uniformly between -1 and 1. 
Finally, it moves the tensor to the given ```device``` (cpu or gpu).
The output should have a shape of [batch_size, latent_vector_size].
'''
# maybe torch.distributions.uniform.Uniform() somehow?
return z.to(device)

Thanks!

1 Answers

Let us first define an uniform distribution with a low-range as -1 and high-range as +1

dist = torch.distributions.uniform.Uniform(-1,1)
sample_shape = torch.Size([2])
dist.sample(sample_shape)
>tensor([0.7628, 0.3497])

This is a tensor of shape 2 (sample_shape). It doesn't have batch_shape. Let's check:

dist.batch_shape
>torch.Size([])

Now let's use expand. It essentially creates a new distribution instance by expanding the batch_shape.

new_batch_shape = torch.Size([5])   # batch_shape of [5]
expanded_dist = dist.expand(new_batch_shape)

Check:

expanded_dist.batch_shape
>torch.Size([5])

Creating a tensor of shape [batch_size, sample_shape]

expanded_dist.sample(sample_shape)
>tensor([[0.1592, 0.3404, 0.3520, 0.3038, 0.0393],
        [0.9368, 0.0108, 0.5836, 0.6156, 0.6704]])

The three types of shapes are defined as follows:

  • Sample shape describes independent, identically distributed draws from the distribution.
  • Batch shape describes independent, not identically distributed draws. Namely, we may have a set of (different) parameterizations to the same distribution. This enables the common use case in machine learning of a batch of examples, each modeled by its own distribution.
  • Event shape describes the shape of a single draw (event space) from the distribution; it may be dependent across dimensions.
Related