Function 4 – index_copy_(dim, index, tensor) ? Tensor

It enables us to copy a tensor into a self tensor in a given index that is defined in index tensor it contains three arguments

  • dim
  • index tensor to hold indices order
  • tensor that we want to copy in our self tensor

Example 1:

Python3




self_tensor = torch.zeros(6, 3)
 
a
 
t = torch.tensor([[7, 6, 9], [3, 9, 1], [0, 1, 9]], dtype = torch.float)
 
i = torch.tensor([3, 1, 0])
 
self_tensor.index_copy_(0, i, t)


Now, if you look at the output tensor…

tensor([[0., 1., 9.],

       [3., 9., 1.],

       [0., 0., 0.],

       [7., 6., 9.],

       [0., 0., 0.],

       [0., 0., 0.]])

In the above example

we copied t tensor into self_tensor with an order that is defined in i tensor

Example 2:

Python3




a = torch.tensor([[1, 8], [9, 5], [1, 5]])
 
b = torch.tensor([[3, 5], [9, 0], [2, 2]])
 
i = torch.tensor([2, 1, 0])
 
a.index_copy_(0, i, b)


Output tensor:

tensor([[2, 2],

       [9, 0],

       [3, 5]])

we can perform the same thing of example 1 with two non-zero tensors

Example 3(common mistake )

Python3




a = torch.tensor([[8, 3], [1, 0]])
 
b = torch.tensor([[3, 9]])
 
i = torch.tensor([1])
 
a.index_copy(1, i, b)


————————————————————————— RuntimeError 
Traceback (most recent call last) <ipython-input-6-fb983dc7560f> in <module> 
3 b = torch.tensor([[3, 9]])       4 i = torch.tensor([1]) —-> 5 a.index_copy(1, i, b)  RuntimeError: index_copy_(): Source/destination tensor must have same slice shapes. Destination slice shape: 2 at dimension 1 and source slice shape: 1 at dimension 0. 
 

The above example throws an error because we have tried to give dim = 1

but, dimth element of the index should contain the same no of element as much element our b tensor have.

Conclusion: so whenever we need to copy a tensor into a self tensor with a specified order we can use this function

Reference Links –

  • PyTorch official docs link
  • notebook for entire code


Pytorch Functions – tensor(), fill_diagnol(), append(), index_copy()

This article aims to share some PyTorch functions that will help you a lot in your deep learning and data science journey. Each function will be explained using two write examples and one example where you can’t use those functions. So let’s get started.

PyTorch is an open-source machine learning library, it contains a tensor library that enables to create a scalar, a vector, a matrix or in short we can create an n-dimensional matrix. It is used in computer vision and natural language processing, primarily developed by Facebook’s Research Lab. It is open-source software and released under the modified BSD (Barkley Software Distribution) license.

Our Four functions: 

  • torch.tensor()
  • fill_diagonal_()
  • append(*size)
  • index_copy()

Similar Reads

Function 1 – torch.tensor()

This function enables us to create PyTorch tensors. Tensor could be anything i.e. it could be a scalar, it could be a 1-dimensional matrix, it could be an n-dimensional matrix....

Function 2 – fill_diagonal_()

...

Function 3 – expand(*size)

...

Function 4 – index_copy_(dim, index, tensor) ? Tensor

...