PyTorch torch.kron 関数
torch.kronPyTorch で 2 つのテンソルの Kronecker 積を計算するための関数です。Kronecker 積は、最初のテンソルの各要素を 2 番目のテンソルと乗算し、再配置します。
関数定義
torch.kron(input, other)
パラメータの説明:
input1 つ目の入力テンソルother2 つ目の入力テンソル
使用例
例
import torch
# 2 つの 1 次元テンソルを作成
a = torch.tensor([1, 2])
b = torch.tensor([3, 4])
# Kronecker 積を計算
y = torch.kron(a, b)
print(y)
# 2 つの 1 次元テンソルを作成
a = torch.tensor([1, 2])
b = torch.tensor([3, 4])
# Kronecker 積を計算
y = torch.kron(a, b)
print(y)
出力結果は次のとおりです:
tensor([3, 4, 6, 8])
例
import torch
# 2 つの 2 次元テンソルを作成
a = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([[5, 6], [7, 8]])
# Kronecker 積を計算
y = torch.kron(a, b)
print(y)
# 2 つの 2 次元テンソルを作成
a = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([[5, 6], [7, 8]])
# Kronecker 積を計算
y = torch.kron(a, b)
print(y)
出力結果は次のとおりです:
tensor([[ 5, 6, 10, 12],
[ 7, 8, 14, 16],
[15, 18, 20, 24],
[21, 24, 28, 32]])
その他の拡張
Pytorch torch リファレンスマニュアル