快捷方式

torch.kron

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

計算 inputother 的克羅內克積(Kronecker product),記為 \otimes

如果 input 是一個 (a0×a1××an)(a_0 \times a_1 \times \dots \times a_n) 張量,而 other 是一個 (b0×b1××bn)(b_0 \times b_1 \times \dots \times b_n) 張量,則結果將是一個 (a0b0×a1b1××anbn)(a_0*b_0 \times a_1*b_1 \times \dots \times a_n*b_n) 張量,其元素如下所示:

(inputother)k0,k1,,kn=inputi0,i1,,inotherj0,j1,,jn,(\text{input} \otimes \text{other})_{k_0, k_1, \dots, k_n} = \text{input}_{i_0, i_1, \dots, i_n} * \text{other}_{j_0, j_1, \dots, j_n},

其中 kt=itbt+jtk_t = i_t * b_t + j_t,對於 0tn0 \leq t \leq n 成立。如果一個張量的維度少於另一個,則會對其進行 unsqueeze 操作,直到兩者維度相同。

支援實值和複數值輸入。

注意

如上所述,此函式將兩個矩陣的典型克羅內克積定義推廣到兩個張量。當 input 是一個 (m×n)(m \times n) 矩陣,而 other 是一個 (p×q)(p \times q) 矩陣時,結果將是一個 (pm×qn)(p*m \times q*n) 分塊矩陣。

AB=[a11Ba1nBam1BamnB]\mathbf{A} \otimes \mathbf{B}=\begin{bmatrix} a_{11} \mathbf{B} & \cdots & a_{1 n} \mathbf{B} \\ \vdots & \ddots & \vdots \\ a_{m 1} \mathbf{B} & \cdots & a_{m n} \mathbf{B} \end{bmatrix}

其中 inputA\mathbf{A},而 otherB\mathbf{B}

引數
關鍵詞引數

out (Tensor, 可選) – 輸出張量。如果為 None 則忽略。預設值: None

示例

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

檢視文件

教程

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

檢視教程

資源

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

檢視資源