Best way to assign initial value to tensor?

In case:

t=torch.Tensor(64,3,28,28)

I would like to have all values 32, not 0 by default.

torch.Tensor won’t initialize all values with 0s, but will use uninitialized memory, so you should manually initialize it.
To set all values to a constant value, you could use several approaches:

t = torch.empty(64, 3, 28, 28).fill_(32.)
t = torch.ones(64, 3, 28, 28) * 32.

torch.full((64,3,28,28),32)

1 Like