PyTorch torch.kron Function


Pytorch torch 参考手册Pytorch torch Reference Manual

torch.kronis a function in PyTorch used to compute the Kronecker product of two tensors. The Kronecker product multiplies each element of the first tensor with the second tensor and rearranges them.

Function Definition

torch.kron(input, other)

Parameter Description:

  • input: The first input tensor
  • other: The second input tensor

Usage Examples

Example

import torch

# Create two one-dimensional tensors
a = torch.tensor([1, 2])
b = torch.tensor([3, 4])

# Compute the Kronecker product
y = torch.kron(a, b)
print(y)

The output result is:

tensor([3, 4, 6, 8])

Example

import torch

# Create two two-dimensional tensors
a = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([[5, 6], [7, 8]])

# Compute the Kronecker product
y = torch.kron(a, b)
print(y)

The output result is:

tensor([[ 5,  6, 10, 12],
        [ 7,  8, 14, 16],
        [15, 18, 20, 24],
        [21, 24, 28, 32]])

Pytorch torch 参考手册Pytorch torch Reference Manual

Other Extensions