PyTorch torch.manual_seed, torch.cuda.manual_seed and torch.cuda.manual_seed_all

PyTorch documentation
https://pytorch.org/docs/master/index.html

PyTorch is an optimized tensor library for deep learning using GPUs and CPUs.

import torch

torch.manual_seed(123456)
torch.cuda.manual_seed_all(123456)
  • torch.manual_seed (Python function, in torch.manual_seed)
  • torch.cuda.manual_seed (Python function, in torch.cuda.manual_seed)
  • torch.cuda.manual_seed_all (Python function, in torch.cuda.manual_seed_all)

1. torch.manual_seed

https://pytorch.org/docs/master/generated/torch.manual_seed.html

torch.manual_seed(seed)

Sets the seed for generating random numbers on all devices. Returns a torch.Generator object.
设置 seed 用于生成随机数,以使得结果是确定的。

  • Parameters

seed (int): The desired seed.

Value must be within the inclusive range [-0x8000_0000_0000_0000, 0xffff_ffff_ffff_ffff]. Otherwise, a RuntimeError is raised. Negative inputs are remapped to positive values with the formula 0xffff_ffff_ffff_ffff + seed.

2. torch.cuda.manual_seed

https://pytorch.org/docs/stable/generated/torch.cuda.manual_seed.html

torch.cuda.manual_seed(seed)

Set the seed for generating random numbers for the current GPU. It’s safe to call this function if CUDA is not available; in that case, it is silently ignored.
为当前 GPU 设置种子用于生成随机数,以使得结果是确定的。

  • Parameters

seed (int): The desired seed.

If you are working with a multi-GPU model, this function is insufficient to get determinism. To seed all GPUs, use manual_seed_all().

insufficient /ˌɪnsəˈfɪʃnt/:adj. 不足的,不能胜任的,缺乏能力的

3. torch.cuda.manual_seed_all

https://pytorch.org/docs/stable/generated/torch.cuda.manual_seed_all.html

torch.cuda.manual_seed_all(seed)

Set the seed for generating random numbers on all GPUs. It’s safe to call this function if CUDA is not available; in that case, it is silently ignored.
为所有的 GPU 设置种子用于生成随机数,以使得结果是确定的。

  • Parameters

seed (int): The desired seed.

4. Example

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# yongqiang cheng

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import torch

torch.manual_seed(123456)
torch.cuda.manual_seed_all(123456)

print(torch.rand([1, 5]))
print(torch.rand([1, 5]))
print(torch.rand([1, 5]))

print("9" * 16)

torch.manual_seed(123456)
torch.cuda.manual_seed_all(123456)

print(torch.rand([1, 5]))
print(torch.rand([1, 5]))
print(torch.rand([1, 5]))
/home/yongqiang/miniconda3/envs/pt-1.4_py-3.6/bin/python /home/yongqiang/pycharm_work/yongqiang.py
tensor([[0.5043, 0.8178, 0.4798, 0.9201, 0.6819]])
tensor([[0.6900, 0.6925, 0.3804, 0.4479, 0.4954]])
tensor([[0.0728, 0.9644, 0.5524, 0.0060, 0.1053]])
9999999999999999
tensor([[0.5043, 0.8178, 0.4798, 0.9201, 0.6819]])
tensor([[0.6900, 0.6925, 0.3804, 0.4479, 0.4954]])
tensor([[0.0728, 0.9644, 0.5524, 0.0060, 0.1053]])

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

Logo

欢迎来到由智源人工智能研究院发起的Triton中文社区,这里是一个汇聚了AI开发者、数据科学家、机器学习爱好者以及业界专家的活力平台。我们致力于成为业内领先的Triton技术交流与应用分享的殿堂,为推动人工智能技术的普及与深化应用贡献力量。

更多推荐