PyTorch | torch.manual_seed(1)是什么意思?torch随机数manual_seed(1)有什么用?如何理解torch.manual_seed(1)

2022-06-17 13:15:37

torch.manual_seed(1)是为了设置CPU的的随机数固定,使得紧跟着的rand()函数生成的值是固定的随机

# 王博Kings
import torch
torch.manual_seed(1)
print(torch.rand(5))
tensor([0.7576, 0.2793, 0.4031, 0.7347, 0.0293])
# 王博Kings
torch.manual_seed(2)
print(torch.rand(5))
tensor([0.6147, 0.3810, 0.6371, 0.4745, 0.7136])
# 王博Kings
torch.manual_seed(1)
print(torch.rand(5))
tensor([0.7576, 0.2793, 0.4031, 0.7347, 0.0293])
# 王博Kings
torch.manual_seed(1)
print(torch.rand(5))
print(torch.rand(5))
tensor([0.7576, 0.2793, 0.4031, 0.7347, 0.0293])
tensor([0.7999, 0.3971, 0.7544, 0.5695, 0.4388])
# 王博Kings
torch.manual_seed(1)
print(torch.rand(5))
torch.manual_seed(1)
print(torch.rand(5))
tensor([0.7576, 0.2793, 0.4031, 0.7347, 0.0293])
tensor([0.6147, 0.3810, 0.6371, 0.4745, 0.7136])
# 王博Kings
torch.manual_seed(1)
print(torch.rand(5))
torch.manual_seed(2)
print(torch.rand(5))
tensor([0.7576, 0.2793, 0.4031, 0.7347, 0.0293])
tensor([0.6147, 0.3810, 0.6371, 0.4745, 0.7136])
  • 作者:人工智能博士
  • 原文链接:https://kings.blog.csdn.net/article/details/109473472
    更新时间:2022-06-17 13:15:37