pytorch每日一学21(torch.eye())创建对角矩阵

第21个方法

torch.eye(n, m=None, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor
  • 创建一个二维矩阵 m × n m\times n m×n,对角全为1,其它都为0,其实就是对角矩阵。

接下来介绍参数:

  • n:行的数量。
  • m:列的数量,默认为n。
  • out:输出的tensor。
  • dtype:创建tensor的数据类型,如果为None,使用默认类型,可以使用torch.set_default_tensor_type()修改。
  • layout:返回tensor的布局,默认为稠密型torch.strided
  • device:返回tensor所处的位置,默认为cpu。可以使用torch.set_default_tensor_type()来更改。
  • requires_grad:指定tensor是否需要梯度,默认为False。

使用方法:

>>> torch.eye(3)
tensor([[ 1.,  0.,  0.],
        [ 0.,  1.,  0.],
        [ 0.,  0.,  1.]])

你可能感兴趣的:(pytorch每日一学,深度学习,pytorch,机器学习,数据挖掘,python)