PyTorch is an open-source machine learning library developed by Facebook. It is used for deep neural network and natural language processing purposes.
The function torch.eye() returns a returns a 2-D tensor of size n*m with ones on the diagonal and zeros elsewhere.
Syntax: torch.eye(n, m, out=None)
Parameters:
n: the number of rows
m: the number of columns. Default – n
out (Tensor, optional): the output tensorReturn type: A 2-D tensor
Code #1:
# Importing the PyTorch library import torch # Applying the eye function and # storing the resulting tensor in 'a' a = torch.eye(3, 4) print("a = ", a) b = torch.eye(3, 3) print("b = ", b) c = torch.eye(5, 1) print("c = ", c) |
Output:
a = tensor([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.]])
b = tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
c = tensor([[1.],
[0.],
[0.],
[0.],
[0.]])
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | sympy.eye() method
- Python - Eye blink detection project
- Python - tensorflow.eye()
- numpy.eye() in Python
- numpy matrix operations | eye() function
- Keeping the eye on Keras models with CodeMonitor
- Python | PyTorch sin() method
- Python | PyTorch sinh() method
- Python | PyTorch cosh() method
- Python | PyTorch tanh() method
- Python | PyTorch cos() method
- Python | PyTorch tan() method
- Python | PyTorch asin() method
- Python | PyTorch acos() method
- Python | PyTorch atan() method
- Python PyTorch zeros() method
- Python Pytorch ones() method
- Python Pytorch arange() method
- Python Pytorch linspace() method
- Python Pytorch range() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

