PyTorch How to create a tensor with one values

Below is the code which can be used to create a tensor with one values:

torch.zeros(j, k)

Example:

torch.ones(4, 2)

With the above example, we created a tensor of 4×2 size with one values.

Complete sample code:

import torch

my_tensor = torch.ones(4, 2)

print(my_tensor)

Output:

tensor([[1., 1.],
        [1., 1.],
        [1., 1.],
        [1., 1.]])