PyTorch torch.mul 関数


Pytorch torch 参考手册PyTorch torch 参考マニュアル

torch.mulPyTorch で要素ごとの乗算を実行するための関数です。2つのテンソル、またはテンソルとスカラーを乗算します。

これは深層学習における最も基本的な数学演算の1つです。

関数定義

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

パラメータ:

  • input(Tensor): 最初の入力テンソル。
  • other(Tensor または float): 2番目の入力テンソルまたはスカラー。
  • out(Tensor, オプション): 出力テンソル。

戻り値:

  • torch.Tensor: 乗算後のテンソルを返します。

使用例

例1: テンソルの要素ごとの乗算

実例

import torch

a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])

c = torch.mul(a, b)
print(c)

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

tensor([ 4, 10, 18])

例2: スカラーとの乗算

実例

import torch

a = torch.tensor([1, 2, 3])

# 2を掛ける
b = torch.mul(a, 2)
print(b)

# * 演算子を使用することもできます
c = a * 2
print(c)

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

tensor([2, 4, 6])
tensor([2, 4, 6])

Pytorch torch 参考手册PyTorch torch 参考マニュアル

その他の拡張