PyTorch torch.inner 関数


Pytorch torch 参考手册Pytorch torch リファレンスマニュアル

torch.innerPyTorch において2つのテンソルの内積を計算する関数です。ベクトルに対してはドット積と等価であり、高次元テンソルに対しては指定された次元で内積を計算します。

関数定義

torch.inner(input, other, out=None)

パラメータ:

  • input(Tensor): 1つ目の入力テンソル。
  • other(Tensor): 2つ目の入力テンソル。
  • out(Tensor, オプション): 出力テンソル。

戻り値:

  • torch.Tensor: 内積の結果を返します。

使用例

例 - ベクトルの内積

import torch

# 2つのベクトルを作成
a = torch.tensor([1.0, 2.0, 3.0])
b = torch.tensor([4.0, 5.0, 6.0])

# 内積を計算
result = torch.inner(a, b)

print("ベクトル a:", a)
print("ベクトル b:", b)
print("内積結果:", result)

出力結果は次のとおりです。

向量 a: tensor([1., 2., 3.])
向量 b: tensor([4., 5., 6.])
内积结果: tensor(32.)

例 - 行列の内積

import torch

# 2つの行列を作成
A = torch.randn(2, 3)
B = torch.randn(2, 3)

# 最後の次元で内積を計算
result = torch.inner(A, B)
print("A の形状:", A.shape)
print("B の形状:", B.shape)
print("結果の形状:", result.shape)

Pytorch torch 参考手册Pytorch torch リファレンスマニュアル

その他の拡張