Learn practical skills, build real-world projects, and advance your career
Updated 4 years ago
Diving into Tensor functions
Here we investigate 5 tensor functions
An short introduction about PyTorch and about the chosen functions.
- index_fill_(dim, index, val)
- expand(*sizes)
- torch.numel(input)
- torch.as_tensor(data, dtype=None, device=None)
- torch.reshape(input, shape)
# Import torch and other required modules
import torch
import numpy
Function 1 - index_fill_(dim, index, val) -> Tensor
Parameters
-
dim (int) – dimension along which to index
-
index (LongTensor) – indices of self tensor to fill in
-
val (float) – the value to fill with
This function used to fill values in given index in PyTorch tensor
# Example 1 - Fill the values in vertically
x = torch.tensor([[2,3,4],[6,7,8]],dtype = torch.float)
index = torch.tensor([1])
x.index_fill_(1,index, 0)
x
tensor([[2., 0., 4.],
[6., 0., 8.]])
in here we add 0 for 1st column