PyTorch torch.gradient 関数


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

torch.gradientPyTorch でテンソルの勾配を計算するための関数です。

関数定義

torch.gradient(input, *, dim=None, spacing=1, edge_order=1)

使用例

実例

import torch

# 1次元テンソルを作成
x = torch.tensor([1.0, 2.0, 4.0, 7.0, 11.0])

# 勾配を計算
grad = torch.gradient(x)
print("勾配:", grad[0])

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

梯度: tensor([1., 2., 3., 4.])

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

その他の拡張