捷徑

torch.Tensor.fill_diagonal_

Tensor.fill_diagonal_(fill_value, wrap=False) Tensor

填滿至少有 2 維張量的主要對角線。 當 dims>2 時,輸入的所有維度長度必須相等。 此函數會就地修改輸入張量,並傳回輸入張量。

參數
  • fill_value (Scalar) – 填充值

  • wrap (bool) – 對於高矩陣,在 N 欄之後,對角線是否「環繞」(wrapped)。

範例

>>> a = torch.zeros(3, 3)
>>> a.fill_diagonal_(5)
tensor([[5., 0., 0.],
        [0., 5., 0.],
        [0., 0., 5.]])
>>> b = torch.zeros(7, 3)
>>> b.fill_diagonal_(5)
tensor([[5., 0., 0.],
        [0., 5., 0.],
        [0., 0., 5.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.],
        [0., 0., 0.]])
>>> c = torch.zeros(7, 3)
>>> c.fill_diagonal_(5, wrap=True)
tensor([[5., 0., 0.],
        [0., 5., 0.],
        [0., 0., 5.],
        [0., 0., 0.],
        [5., 0., 0.],
        [0., 5., 0.],
        [0., 0., 5.]])

文件

存取 PyTorch 的完整開發者文件

檢視文件

教學

獲取針對初學者和進階開發者的深入教學

檢視教學

資源

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

檢視資源