Create multiple torch.normal tensors

I’m trying to create a tensor of shape (n, m, o) from a normal distribution, but using a mean in the shape (m, o). It appears torch.normal only allows setting size when mean and std are numbers. For example in the simple case, I’m trying to create shape 5, 1, 1.

mean = torch.tensor([[0.0]])
std = torch.tensor([[1.0]])

print(torch.normal(mean, std)) # works, only creates 1
print(torch.normal(mean, std, size=(5, 1, 1)) # doesn't work, no matching signature

What’s a good way to handle the above? (Should ideally be created right on the GPU)

Hi Alex!

You can either expand() mean and std to add your “batch size” of n = 5
or use torch.distributions.Normal to whose .sample() method you
can pass n (packaged as a tuple):

>>> import torch
>>> print (torch.__version__)
2.1.1
>>>
>>> device = 'cuda'
>>>
>>> mean = torch.tensor ([[0.0]]).to (device)
>>> std = torch.tensor ([[1.0]]).to (device)
>>>
>>> _ = torch.manual_seed (2023)
>>> torch.normal (mean.unsqueeze (0).expand (5, 1, 1), std.unsqueeze (0).expand (5, 1, 1))
tensor([[[-0.3201]],

        [[-0.0794]],

        [[ 0.2601]],

        [[ 0.7900]],

        [[-0.3996]]], device='cuda:0')
>>>
>>> dist = torch.distributions.Normal (mean, std)
>>> _ = torch.manual_seed (2023)
>>> dist.sample ((5,))
tensor([[[-0.3201]],

        [[-0.0794]],

        [[ 0.2601]],

        [[ 0.7900]],

        [[-0.3996]]], device='cuda:0')

Best.

K. Frank