Wednesday, October 15, 2025
HomeLanguagesPython Pytorch eye() method

Python Pytorch eye() method

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 tensor

Return 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.]])


 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11891 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11952 POSTS0 COMMENTS
Shaida Kate Naidoo
6851 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS