PyTorch torch.clamp 関数


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

torch.clampは、PyTorch においてテンソル値を指定範囲内に制限するための関数です。

関数定義

torch.clamp(input, min, max, out)

使用例

実例

import torch

x = torch.tensor([-2, -1, 0, 1, 2, 3])

# -1 から 2 の間に制限
result = torch.clamp(x, min=-1, max=2)

print(result)

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

tensor([-1, -1,  0,  1,  2,  2])

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

その他の拡張