torch.manual_seed(args.seed) #為CPU設置種子用於生成隨機數,以使得結果是確定的
代碼:
torch.manual_seed(1) # reproducible print(torch.rand(2)) torch.manual_seed(2) # reproducible print(torch.rand(2))
輸出:結果不同
tensor([0.7576, 0.2793]) tensor([0.6147, 0.3810])
代碼:
torch.manual_seed(1) # reproducible print(torch.rand(2)) torch.manual_seed(1) # reproducible print(torch.rand(2))
輸出:結果相同
tensor([0.7576, 0.2793]) tensor([0.7576, 0.2793])
代碼:
torch.manual_seed(1) # reproducible a=torch.rand(2) b=torch.rand(2) print(a,b)
輸出:結果不同,但再次運行a,b不變
tensor([0.7576, 0.2793]) tensor([0.4031, 0.7347])
