PyTorch torch.kron function
PyTorch torch reference manual
torch.kron is a function in PyTorch used to compute the Kronecker product of two tensors. The Kronecker product multiplies each element of the first tensor by the second tensor and rearranges the result.
Function definition
torch.kron(input, other)
Parameter Description:
input: The first input tensorother: 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
)
(
y
)
The output is:
tensor([3, 4, 6, 8])
Example
import
torch
Create two 2D tensors
a
=
torch.
tensor
(
[
[
1
,
2
]
,
[
3
,
4
]
]
)
b
=
torch.
tensor
(
[
[
5
,
6
]
,
[
7
,
8
]
]
)
Compute the Kronecker product
y
=
torch.
kron
(
a
,
b
)
(
y
)
The output is:
tensor([[ 5, 6, 10, 12],
[ 7, 8, 14, 16],
[15, 18, 20, 24],
[21, 24, 28, 32]])