惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - lmqljt

LangChain教程,langchain快速入门, Agent智能体rag项目实战 火山图 差异分析等 箱线图 拓展(缺口箱线图)等 多尺度时序间相关性:MSGNet 时序分析通用基础模型:TimesNet 预测/插补/分类/异常检测 PatchTST:通道独立的时序Transformer 扩散模型Difussion 随笔 大论文题目类参考 注意力机制创新思维分析 热力图 以分类为例 02 国际象棋入门快易精 初级下法 棋子杀王 SCI拒稿重投 make_classification函数 杂志审稿人打分表参考 python+matplotlib绘图线条类型和颜色选择 Sobol全局灵敏性分析 np.transpose(),torch.permute(),tensor.permute() DTW(动态时间规整)算法原理与应用 第2期 分布迁移下的深度学习时间序列异常检测方法探究 2021-09-22
torch.manual_seed(seed)用法及注意事项
lmqljt · 2024-05-07 · via 博客园 - lmqljt

torch.manual_seed(0) 是 PyTorch 中的函数调用,用于设置随机数生成器的种子。通过指定种子值,我们可以确保每次运行代码时生成的随机数序列是相同的,这样有助于保持实验的可复现性。

在深度学习中,训练过程中的随机化(例如权重初始化、数据采样等)可能会影响模型的性能和结果。因此,在进行实验时,通过设置种子可以确保每次运行代码时都使用相同的随机数序列,从而使结果可重现。

需要注意的是,torch.manual_seed(0) 只会对使用了 PyTorch 内置的随机数生成器的部分产生影响,有些库可能使用了自己的随机数生成器,不受该函数调用的影响。
但是同时应注意,每次生成随机数时,都应设置一样的随机种子,才能保证生成的随机数都相同。如:https://zhuanlan.zhihu.com/p/662224844。

“You can use torch.manual_seed() to seed the RNG for all devices (both CPU and CUDA):”

如官方文档所述,torch.manual_seed(seed)用来生成CPU或GPU的随机种子,方便下次复现实验结果。

1.如果未设置随机种子,在CPU中生成随机数:

# test.py
import torch
print(torch.rand(1))

则每次运行test.py返回的结果都是不同的处于(0, 1)之间的随机数:

输出结果:#1

输出结果:#2

输出结果:#3

2.设置随机种子后,每次运行生成的随机数都是相同的

# test.py
import torch

torch.manual_seed(0)
print(torch.rand(1))

输出结果:#1,#2,#3结果一致

3.若更改随机种子,则会生成不同的值

# test.py
import torch

torch.manual_seed(1)
print(torch.rand(1))

输出结果:#1,#2,#3结果一致

4.设置随机种子后,每次运行文件生成的随机值相同,但是每次生成的结果却不一样:

# test.py
import torch

torch.manual_seed(1)
print(torch.rand(1))
print(torch.rand(1))

输出结果:#1,#2,#3结果一致

tensor([0.4356])
tensor([0.5647])

5.若希望每次运行的随机数结果都一样,则需要在每次随机数前设置相同的种子

# test.py
import torch

torch.manual_seed(1)
print(torch.rand(1))
torch.manual_seed(1)
print(torch.rand(1))

输出结果:

tensor([0.4356])
tensor([0.4356])