Python Pytorch eye() 方法
PyTorch 是 Facebook 开发的开源机器学习库。它用于深度神经网络和自然语言处理目的。
函数torch.eye()
返回 a 返回一个大小为n*m的二维张量,其中对角线为 1,其他位置为 0。
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
代码#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)
输出:
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.]])