快捷方式

torch.diagflat

torch.diagflat(input, offset=0) Tensor
  • 如果 input 是向量(一維張量),則返回一個二維方陣張量,其對角線元素是 input 的元素。

  • 如果 input 是一個多維張量,則返回一個二維張量,其對角線元素等於展平後的 input

引數 offset 控制考慮哪個對角線

  • 如果 offset = 0,則為主對角線。

  • 如果 offset > 0,則在主對角線上方。

  • 如果 offset < 0,則在主對角線下方。

引數
  • input (Tensor) – 輸入張量。

  • offset (int, optional) – 考慮的對角線。預設值:0(主對角線)。

示例

>>> a = torch.randn(3)
>>> a
tensor([-0.2956, -0.9068,  0.1695])
>>> torch.diagflat(a)
tensor([[-0.2956,  0.0000,  0.0000],
        [ 0.0000, -0.9068,  0.0000],
        [ 0.0000,  0.0000,  0.1695]])
>>> torch.diagflat(a, 1)
tensor([[ 0.0000, -0.2956,  0.0000,  0.0000],
        [ 0.0000,  0.0000, -0.9068,  0.0000],
        [ 0.0000,  0.0000,  0.0000,  0.1695],
        [ 0.0000,  0.0000,  0.0000,  0.0000]])

>>> a = torch.randn(2, 2)
>>> a
tensor([[ 0.2094, -0.3018],
        [-0.1516,  1.9342]])
>>> torch.diagflat(a)
tensor([[ 0.2094,  0.0000,  0.0000,  0.0000],
        [ 0.0000, -0.3018,  0.0000,  0.0000],
        [ 0.0000,  0.0000, -0.1516,  0.0000],
        [ 0.0000,  0.0000,  0.0000,  1.9342]])

文件

訪問 PyTorch 全面開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源