当前位置:网站首页>【Pytorch】torch.manual_seed()、torch.cuda.manual_seed() 解释
【Pytorch】torch.manual_seed()、torch.cuda.manual_seed() 解释
2022-07-30 05:38:00 【向大厂出发】
一、torch.manual_seed(seed) 介绍
1、torch.manual_seed(seed) 功能描述
设置 CPU 生成随机数的种子 ,方便下次复现实验结果。
为 CPU 设置种子用于生成随机数,以使得结果是确定的。
当你设置一个随机种子时,接下来的随机算法生成数根据当前的随机种子按照一定规律生成。
随机种子作用域是在设置时到下一次设置时。要想重复实验结果,设置同样随机种子即可。
2、语法
torch.manual_seed(seed) → torch._C.Generator3、参数
seed,int类型,是种子 – CPU生成随机数的种子。取值范围为 [-0x8000000000000000, 0xffffffffffffffff] ,十进制是 [-9223372036854775808, 18446744073709551615] ,超出该范围将触发 RuntimeError 报错。
4、返回
返回一个torch.Generator对象。
二、类似函数的功能
1、为CPU中设置种子,生成随机数:
torch.manual_seed(number)2、为特定GPU设置种子,生成随机数:
torch.cuda.manual_seed(number)3、为所有GPU设置种子,生成随机数:
# 如果使用多个GPU,应该使用torch.cuda.manual_seed_all()为所有的GPU设置种子。
torch.cuda.manual_seed_all(number)使用原因 :
在需要生成随机数据的实验中,每次实验都需要生成数据。设置随机种子是为了确保每次生成固定的随机数,这就使得每次实验结果显示一致了,有利于实验的比较和改进。使得每次运行该 .py 文件时生成的随机数相同。
三、实例
实例 1 :不设随机种子,生成随机数
# test.py
import torch
print(torch.rand(1)) # 返回一个张量,包含了从区间[0, 1)的均匀分布中抽取的一组随机数每次运行test.py的输出结果都不相同:
tensor([0.4351])
tensor([0.3651])
tensor([0.7465])实例 2 :设置随机种子,使得每次运行代码生成的随机数都一样
# test.py
import torch
# 设置随机种子
torch.manual_seed(0)
# 生成随机数
print(torch.rand(1)) # 返回一个张量,包含了从区间[0, 1)的均匀分布中抽取的一组随机数每次运行 test.py 的输出结果都是一样:
tensor([0.4963])实例 3 :不同的随机种子生成不同的值
改变随机种子的值,设为 1 :
# test.py
import torch
torch.manual_seed(1)
print(torch.rand(1)) # 返回一个张量,包含了从区间[0, 1)的均匀分布中抽取的一组随机数每次运行 test.py,输出结果都是:
tensor([0.7576])
改变随机种子的值,设为 5 :
# test.py
import torch
torch.manual_seed(5)
print(torch.rand(1)) # 返回一个张量,包含了从区间[0, 1)的均匀分布中抽取的一组随机数每次运行 test.py,输出结果都是:
tensor([0.8303])可见不同的随机种子能够生成不同的随机数。
但只要随机种子一样,每次运行代码都会生成该种子下的随机数。
实例 4 :设置随机种子后,是每次运行test.py文件的输出结果都一样,而不是每次随机函数生成的结果一样
# test.py
import torch
torch.manual_seed(0)
print(torch.rand(1))
print(torch.rand(1))输出结果:
tensor([0.4963])
tensor([0.7682])可以看到两次打印 torch.rand(1) 函数生成的结果是不一样的,但如果你再运行test.py,还是会打印:
tensor([0.4963])
tensor([0.7682])
实例 5 :如果你就是想要每次运行随机函数生成的结果都一样,那你可以在每个随机函数前都设置一模一样的随机种子
# test.py
import torch
torch.manual_seed(0)
print(torch.rand(1))
torch.manual_seed(0)
print(torch.rand(1))输出结果:
tensor([0.4963])
tensor([0.4963]二、torch.cuda.manual_seed() 解释
作用
torch.manual_seed() 为CPU设置种子,保证每次的随机初始化都是相同的,从而保证结果可以复现。
torch.cuda.manual_seed()为GPU设置种子,作用同上
torch.cuda.manual_seed_all()为所有GPU设置种子(适用于多GPU时),作用同上
参考:【PyTorch】torch.manual_seed() 详解_想变厉害的大白菜的博客-CSDN博客_torch.manual_seed
边栏推荐
- 破纪录者(Google Kickstart2020 Round D Problem A)
- MYSQL-InnoDB的线程模型
- 分布式事务之 LCN框架的原理和使用(二)
- Ranking of grades (Huazhong University of Science and Technology postgraduate examination questions) (DAY 87)
- idea 编译protobuf 文件的设置使用
- cmd(命令行)操作或连接mysql数据库,以及创建数据库与表
- 爬虫数据是如何收集和整理的?
- MySQL的 DDL和DML和DQL的基本语法
- 如何使用FirewallD限制网络访问
- 839. Simulated heap
猜你喜欢
随机推荐
cmd (command line) to operate or connect to the mysql database, and to create databases and tables
MySql fuzzy query Daquan
curl (7) Failed connect to localhost8080; Connection refused
Falling ants (Peking University entrance exam questions)
cookie和session区别
MySQL user authorization
2022 Pengcheng Cup web
mysql 中 in 的用法
75. 颜色分类
分布式事务之 Atomikos 原理和使用(一)
号称年薪30万占比最多的专业,你知道是啥嘛?
[GO语言基础] 一.为什么我要学习Golang以及GO语言入门普及
navicat无法连接mysql超详细处理方法
[Image processing] Image skeleton extraction based on central axis transformation with matlab code
idea设置自动带参数的方法注释(有效)
MySQL fuzzy query performance optimization
4、nerf(pytorch)
Within the SQL connection table (link connections, left or right, cross connection, full outer join)
面试题 17.13. 恢复空格(字典树)
Machine Learning - Gradient Descent Optimization - C language implementation

![[Other] DS5](/img/20/6863bb7b58d2e60b35469ba32e5830.png)







