當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python PyTorch kron用法及代碼示例

本文簡要介紹python語言中 torch.kron 的用法。

用法:

torch.kron(input, other, *, out=None) → Tensor

參數

關鍵字參數

out(Tensor,可選的) -輸出張量。如果 None 則忽略。默認值:None

計算 inputother 的克羅內克積,用 表示。

如果 input 張量且 other 張量,則結果將是具有以下條目的 張量:

其中 。如果一個張量的維數比另一個少,則它不會被壓縮,直到它具有相同的維數。

支持實值和complex-valued 輸入。

注意

如上所述,此函數將兩個矩陣的 Kronecker 積的典型定義推廣到兩個張量。當input 矩陣並且other 矩陣時,結果將是 塊矩陣:

其中 input other

例子:

>>> mat1 = torch.eye(2)
>>> mat2 = torch.ones(2, 2)
>>> torch.kron(mat1, mat2)
tensor([[1., 1., 0., 0.],
        [1., 1., 0., 0.],
        [0., 0., 1., 1.],
        [0., 0., 1., 1.]])

>>> mat1 = torch.eye(2)
>>> mat2 = torch.arange(1, 5).reshape(2, 2)
>>> torch.kron(mat1, mat2)
tensor([[1., 2., 0., 0.],
        [3., 4., 0., 0.],
        [0., 0., 1., 2.],
        [0., 0., 3., 4.]])

相關用法


注:本文由純淨天空篩選整理自pytorch.org大神的英文原創作品 torch.kron。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。