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.