PyTorch torch.cuda.manual_seed 関数


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

torch.cuda.manual_seedPyTorch で CUDA ランダムシードを設定するための関数です。現在の CUDA デバイスにランダムシードを設定し、CUDA 操作の再現性を確保します。

関数定義

torch.cuda.manual_seed(seed)

使用例

例

import torch

# CUDA ランダムシードを設定
if torch.cuda.is_available():
    torch.cuda.manual_seed(42)
    x = torch.randn(3, 3).cuda()
    print(f"Random tensor on CUDA: {x}")

    # 同じシードで同じ結果が得られる
    torch.cuda.manual_seed(42)
    y = torch.randn(3, 3).cuda()
    print(f"Random tensor with same seed: {y}")
else:
    print("CUDA not available")

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

その他の拡張