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

文章目录

前言

主要是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()

相关推荐
@一辈子爱你10 分钟前
归来九十余日:在时代的夹缝中,与你共筑一道光
python
HsuHeinrich32 分钟前
利用面积图探索历史温度的变化趋势
python·数据可视化
winfredzhang40 分钟前
Python实战:手把手教你写一个带界面的“照片按日期归档与清理”工具
python·复制·日期·回收站·媒体文件备份
程序员三藏4 小时前
Jmeter自动化测试
自动化测试·软件测试·python·测试工具·jmeter·测试用例·接口测试
吴佳浩6 小时前
Langchain 浅出
python·langchain·llm
smj2302_796826526 小时前
解决leetcode第3753题范围内总波动值II
python·算法·leetcode
mortimer6 小时前
破局视频翻译【最后一公里】––从语音克隆到口型对齐的完整工程思路
python·github·aigc
门框研究员8 小时前
解锁Python的强大能力:深入理解描述符
python
子不语1809 小时前
Python——函数
开发语言·python
daidaidaiyu10 小时前
一文入门 LangChain 开发
python·ai