import torch
torch.manual_seed(1)
print(torch.rand(2))
运行这段代码会发现,每次得到的随机数是固定的。但是如果不加上torch.manual_seed这个函数调用的话,打印出来的随机数每次都不一样。当把torch.manual_seed(1)注释掉后,print(torch.rand(2))生成的随机数是每次都变化的
本文探讨了PyTorch中随机数生成的机制。通过使用torch.manual_seed()函数,可以确保每次运行代码时生成相同的随机数序列。若不使用该函数,每次运行将产生不同的随机数。
import torch
torch.manual_seed(1)
print(torch.rand(2))
运行这段代码会发现,每次得到的随机数是固定的。但是如果不加上torch.manual_seed这个函数调用的话,打印出来的随机数每次都不一样。当把torch.manual_seed(1)注释掉后,print(torch.rand(2))生成的随机数是每次都变化的
1万+
645
1924