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

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/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yongqiang Cheng

梦想不是浮躁,而是沉淀和积累。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值