torch.kron
- torch.kron(input, other, *, out=None) → Tensor
-
计算
input
和other
的克罗内克积,记作$\otimes$。如果
input
是一个$(a_0 \times a_1 \times \dots \times a_n)$ 张量,而other
是一个$(b_0 \times b_1 \times \dots \times b_n)$ 张量,则结果将是一个$(a_0*b_0 \times a_1*b_1 \times \dots \times a_n*b_n)$ 张量,其条目如下:$(\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},$其中 $k_t = i_t * b_t + j_t$,对于 $0 \leq t \leq n$。如果一个张量的维度少于另一个张量,则会对它进行 unsqueeze 操作,直到两个张量具有相同的维度。
支持实数和复数输入。
注意
此函数将两个矩阵的克罗内克积的典型定义推广到两个张量,如上所述。当
input
是一个$(m \times n)$矩阵且other
是一个$(p \times q)$矩阵时,结果将是一个$(p*m \times q*n)$的分块矩阵:$\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}$其中
input
为 $\mathbf{A}$,而other
为 $\mathbf{B}$。示例:
>>> 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.]])