随机数种子

随机数种子

torch.manual_seed():为CPU设置随机数种子

torch.cuda.manual_seed():为GPU设置随机数种子

torch.cuda.manual_seed_all():为所有的GPU设置随机数种子

random.seed():为random模块的随机数种子

说明

  1. torch.manual_seed() 一般和 torch.rand()、torch.randn() 等函数搭配使用。
  2. 通过指定seed值,可以令每次生成的随机数相同,从而方便复现实验结果
  3. 设置随机种子后,是每次运行 py 文件的输出结果都一样,而不是每次随机函数生成的结果一样。
import torch
torch.manual_seed(0)
print(torch.rand(1, 2))	# 返回一个张量,包含了从区间[0, 1)的均匀分布中抽取的一组随机数
print(torch.randn(1, 2)) # 返回一个张量,包含了均值0,方差1的正态分布中抽取的一组随机数
# torch.manual_seed(0)
print(torch.rand(1, 2))
print(torch.randn(1, 2))

原文链接:

.detach() .data

找的新的资料

[Pytorch中.detach()与.data的用法](https://zhuanlan.zhihu.com/p/410199046#:~:text=Tensor.data和Tensor.detach ()一样,,都会返回一个新的Tensor, 这个Tensor和原来的Tensor共享内存空间,一个改变,另一个也会随着改变,且都会设置新的Tensor的requires_grad属性为False。)

随机数种子_第1张图片

你可能感兴趣的:(pytorch基础知识积累,pytorch,深度学习,python)