Function 2 – fill_diagonal_()

Fill_diagonal() fill the main diagonal with fill_value but that matrix should be a 2D matrix, for a ndim > 2 matrix number of rows should be equal to number of columns and if it is not then we have to set wrap = True so that diagonal value can repeat itself.

Syntax and parameters: 
fill_diagonal_(fill_value, wrap=False) -> tensor 

fill_value: tensor matrix whose diagonals we want to fill.
wrap:       it takes boolean, it enables us to work with a non-square matrix.

Example 1:

In this example, firstly we will create a tensor of size(3, 3) using torch.zeros(3, 3).

Python3




a = torch.zeros(3, 3)


a is a tensor of size(3, 3) which have all of it’s element zero i.e. a is a zero matrix

now what we want is to replace all of it’s diagonal values with .4, so this we can do with the help of fill_diagonal_().

Python3




a.fill_diagonal_(fill_value = 4, wrap = False)
 
print(a)


If, we look at tensor a it will look like

tensor([[4., 0., 0.], [0., 4., 0.], [0., 0.,  4.]])

Example 2 :

As we learned from example 1, that we can replace diagonal elements using this function but what if we have a tensor in which no. of rows is not equal to no. of columns, then in this scenario we have to set the “wrap” parameter of fill_diagonal_().

You can look at this code,

Python3




b = torch.zeros([9, 4])
 
# without putting wrap = True
 
A = b.fill_diagonal_(5, wrap = False)


In the above code, firstly we created a zero tensor of size(9, 4), then we filled it’s diagonal with 5 but we didn’t set wrap = True. Now let’s look at tensor A,

tensor([[5., 0., 0., 0.], [0., 5., 0., 0.], [0., 0., 5., 0.], [0., 0., 0., 5.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0.,  0.]])

Here you can see that once diagonal reaches to the end of the length of a particular dimension then the rest of the diagonal was not changed

So to solve this problem we have to set wrap as True. Look at this code,

Python3




B = b.fill_diagonal_(6, True)
 
print(B)


Now take a look at tensor B

tensor([[6., 0., 0., 0.], [0., 6., 0., 0.], [0., 0., 6., 0.], [0., 0., 0., 6.], [0., 0., 0., 0.], [6., 0., 0., 0.], [0., 6., 0., 0.], [0., 0., 6., 0.], [0., 0., 0.,  6.]])

You will observe that even diagonal reached to the end of a particular dimension, it again starts changing the diagonals form next dimension.

Example 3(common mistake):

let’s assume that you have a zero tensor of size(4, 5) and you want to change its diagonal with .3 but if you want to change the data type of that tensor to int32, then probably you will think to set a dype = torch.int32 in fill_diagonal_() as, 

B = b.fill_diagonal_(6, True)

print(B) 

Python3




B = b.fill_diagonal_(6, True)
 
print(B)


But, here you have to remember a little thing that fill_diagonal_() only takes two arguments as parameter, one is data that you want to put in diagonal and another one is wrap for working with non-square tensor,

So, the above code will throw an error as,

TypeError

Traceback (most recent call last) <ipython-input-26–133c4c6a6759> in <module>

1 # Example 3?-?breaking (but if try to set it’s data type it throws an error)

2 a = torch.zeros(4, 5)

– ? 3 a.fill_diagonal_(.3, dtype = torch.float32) TypeError: fill_diagonal_() got an unexpected keyword argument ‘dtype’

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

...