• 文档 >
  • torch >
  • torch.kron
快捷键

torch.kron

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

计算由 inputother 表示的克罗内克积,记作 \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},

如果一个张量维度少于另一个张量,它将被展开直到具有相同的维度数。

支持实值和复值输入。

注意

此函数将两个矩阵的 Kronecker 积的典型定义推广到两个张量,如上所述。当 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}

参数:
  • 输入(张量) –

  • 其他(张量) –

关键字参数:

输出(张量,可选) – 输出张量。如果 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 贡献者。

使用 Sphinx 构建,并使用 Read the Docs 提供的主题。

文档

PyTorch 的全面开发者文档

查看文档

教程

深入了解初学者和高级开发者的教程

查看教程

资源

查找开发资源并获得您的疑问解答

查看资源