torch.manual_seed() 和 torch.cuda.manual_seed() 功能及实例
功能设置固定生成随机数的种子,使得每次运行该 .py 文件时生成的随机数相同设置方法CPU:torch.manual_seed(整数)官方文档:https://pytorch.org/docs/stable/torch.html?highlight=manual_seed#torch.manual_seedGPU:torch.cuda.manual_seed(整数)官方文档:https://pyt
·
功能
- 设置固定生成随机数的种子,使得每次运行该 .py 文件时生成的随机数相同
设置方法
- CPU:torch.manual_seed(整数)
- 官方文档:https://pytorch.org/docs/stable/torch.html?highlight=manual_seed#torch.manual_seed
- GPU:torch.cuda.manual_seed(整数)
- 官方文档:https://pytorch.org/docs/stable/cuda.html?highlight=manual_seed#torch.cuda.manual_seed
实例
# 需要注意不要在终端中单行敲入运行如下代码,要将如下代码先拷贝到 *.py 文件中,再在终端命令中通过 python *.py 运行
import torch
if torch.cuda.is_available():
print("gpu cuda is available!")
torch.cuda.manual_seed(1000)
else:
print("cuda is not available! cpu is available!")
torch.manual_seed(1000)
print(torch.rand(1, 2))
欢迎来到FlagOS开发社区,这里是一个汇聚了AI开发者、数据科学家、机器学习爱好者以及业界专家的活力平台。我们致力于成为业内领先的Triton技术交流与应用分享的殿堂,为推动人工智能技术的普及与深化应用贡献力量。
更多推荐

所有评论(0)