Learn practical skills, build real-world projects, and advance your career

Function 1 - ones


This function returns a tensor filled with scalar value with the shape defined by the variable argument using a parameter like size.

# Example 1 - 
t1 = torch.ones(5,7)
t1
tensor([[1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1., 1., 1.]])

Here we have successfully gotten filled up columns and rows as specified.

# Example 2 - 
t2 = torch.ones ([4,3,6])
t2
tensor([[[1., 1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1., 1.]],

        [[1., 1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1., 1.]],

        [[1., 1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1., 1.]],

        [[1., 1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1., 1.],
         [1., 1., 1., 1., 1., 1.]]])

Above we have tried to increase the dimension and the values have also been returned as expected.