深度学习中需要固定的随机数种子

文章目录

前言

主要是3个:

  • random.seed()
  • numpy.random.seed()
  • torch.manual_seed()

三个的原理和作用都是相似的,所以接下来我只简单介绍random.seed()

random.seed()

作用

random()函数是用来产生伪随机数的,而random.seed()是用来确定随机数种子,使得每次产生的随机数是一样的。从而保证程序的可复现性。

例子

python 复制代码
import random

for i in range(5):
	# Any number can be used in place of '0'
	random.seed(0)

	# Generated random number will be between 1 to 1000
	print(random.randint(1, 1000))
"""
Output:
865
865
865
865
865
"""
  • 注意是需要在每次调用函数产生随机数之前都必须声明随机数种子,如下所示。
python 复制代码
import random

random.seed(3)
print(random.randint(1, 1000))

random.seed(3)
print(random.randint(1, 1000))

print(random.randint(1, 1000))

"""
Output:
244
244
607
"""

Reference

例子来源于:

https://www.geeksforgeeks.org/random-seed-in-python/

np.random.seed()

torch.manual_seed()

相关推荐
r i c k7 分钟前
办公小程序开发----提高工作效率
python·python程序开发
wha the fuck4047 分钟前
(渗透脚本)TCP创建连接脚本----解题----极客大挑战2019HTTP
python·网络协议·tcp/ip·网络安全·脚本书写
qq_356196958 分钟前
day39模型的可视化和推理@浙大疏锦行
python
深蓝电商API14 分钟前
从 “能爬” 到 “稳爬”:Python 爬虫中级核心技术实战
开发语言·爬虫·python
czlczl2002092530 分钟前
如何添加“默认给Sql查询语句加上租户条件”的功能
数据库·python·sql
破烂pan30 分钟前
Python 长连接实现方式全景解析
python·websocket·sse
高洁0132 分钟前
一文了解图神经网络
人工智能·python·深度学习·机器学习·transformer
咸鱼加辣35 分钟前
按“最近是否用过”删(LRU)
python
serve the people1 小时前
tensorflow 零基础吃透:创建 tf.sparse.SparseTensor 的核心方法
人工智能·python·tensorflow
测试老哥1 小时前
UI自动化测试—Jenkins配置优化
自动化测试·软件测试·python·测试工具·ui·jenkins·测试用例