快捷鍵

torch.diagflat

torch.diagflat(input, offset=0) Tensor
  • 如果 input 是一個向量(1 維 Tensor),則傳回一個 2 維正方形 Tensor,其中 input 的元素作為對角線。

  • 如果 input 是一個具有多個維度的 Tensor,則傳回一個 2 維 Tensor,其對角線元素等於一個扁平化的 input

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

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

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

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

參數 (Parameters)
  • input (Tensor) – 輸入張量 (the input tensor)。請參閱 Tensor

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

範例 (Examples)

>>> 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 的完整開發者文件

檢視文件 (View Docs)

教學課程 (Tutorials)

取得為初學者和進階開發者提供的深入教學課程

檢視教學課程 (View Tutorials)

資源 (Resources)

尋找開發資源並獲得問題解答

檢視資源 (View Resources)