为什么使用PyTorch生成的相同模型示例会产生不同的输出,解决方案是什么?

gc0ot86w  于 2023-10-20  发布在  其他
关注(0)|答案(1)|浏览(99)
import os
import random
import numpy as np
import torch
from torch import nn
from torch.backends import cudnn

seed = 0
random.seed(seed)
np.random.seed(seed)

torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
cudnn.deterministic = True
cudnn.benchmark = False

class SimpleNet(nn.Module):
      def __init__(self, input_size, hidden_size, output_size):
          super(SimpleNet, self).__init__()
          self.fc1 = nn.Linear(input_size, hidden_size)
      def forward(self, x):
          out = self.fc1(x)
    
          return out

input_size = 10
hidden_size = 20
output_size = 5

# device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

device = 'cpu'
input_data = torch.randn(32, input_size)
input_data = input_data.to(device)

model = SimpleNet(input_size, hidden_size, output_size).to(device)

output1 = model(input_data)

print(output1)
model = SimpleNet(input_size, hidden_size, output_size).to(device)

# del output1

output2 = model(input_data)
print(output2)
model = SimpleNet(input_size, hidden_size, output_size).to(device)

output3 = model(input_data)
print(output3)

产出:
Tensor([[-1.5116,1. 0399、0.5818、0.4947、0.2639、-0.1309、0. 3110,0. 4311,-0. 6150,0. 4050,-0. 1481,-0. 3213,0. 6688、0.5720、-0.1721、0. 1600,0.0351,1.0659,0.2054,0.1556],[-0.1409,-0.4082,-0.7771,0. 1999年,美国的一个大城市发生了大规模的地震。3136,-0. 0378,-0. 7738,0. 2000年,美国的石油产量为1000万吨。3762,-0. 6075,-0. 9881],[1. 0371,-0. 3052,-0. 7192,-0. 4954,-0. 0619,-0. 6773,-0. 3437,-0. 1149,-0. 7098,-0. 0663,0. 2402,-0. 1380,-0. 0546,-0. 1480,-0. 3620,-1. 0165,-0. 4548,0. 6009,-0. 1641,-0. 7223],[-0. 6194,0. 2212、0.2156、0.5785、-0.1053、-0.2406、-0.0536、0. 2633,-0.0827,0. 2190、0.1406、-0.3082、0. 4105、-0.0564、-0.4030、-0.6807、-0.2153、1. 0450,0.2360,-0.2577],[1.5008,0.2273,0.2086,-1.4349,-0.8325,-0.0553,-1.9506,1. 0662,-0.9117,-0.7985,0. 2942,-0. 4245,-0. 6362,-0. 6082,0. 3804,-0. 4315,-1. 2882,-0. 4164,0. 9965,-0.7636]],grad_fn =)Tensor([[0.3480,0.3380,-0.4902,-0.1727,-1.4587,-1.7824,-0.5210,-1.4188,0. 8898,-1. 6112,-0. 6507,-0. 4776,-0. 6237,0. 2907,-0. 1367,-0. 8204,-0. 1833,0. 0711,1.3449,-0.9399],[-0.3995,-0.1740,0. 0406,0.5558,-1.1816,-0.0281,0. 1738,-0.0590,0. 1638、0.4439、0.6594、-0.0111、-0.1184、0. 2536,-1. 1477,-0. 6620,-0. 5626,0. 3007,-0. 2232,-0. 0159],[-0. 6477,0. 0387、0.0104、0.6004、-0.5215、0. 2377、0.3120、0.5024、-0.1435、1. 0267,-0.0850,0. 5509,-0.1720,0. 7758,-0.4568,0. 0568,-0.3941,0. 0733,0.0714,0.1224],[-0.1056,0. 4826,-0. 4121,-0. 2473,-0. 6680,-0. 6018,0. 1496,-0.6604,0. 7170,-0. 0660,-0. 2835,0. 0948、0.3696、-0.4176、-0.3242、-0.1028、-0.0509、0. 9921,-0. 8176],[0. 3800,-0. 1418,0. 6162,-0.6415,1. 1636,2. 1334,-0. 2372,-0. 4634,0. 0067、0.7677、0.2029、-0.5407、1. 2410、0.9184、0.6625、-0.6612、0. 2142,-0.7860,0. 0127]],grad_fn =)Tensor([[-0.0243,1. 0001、0.7106、0.4752、-0.0075、-1.1864、0. 2284、-0.1393、-0.9675、-1.0423、-0.2470、0. 7237,0.3109,0.0975,0.7873,-0.6733,-1.1279,-0.2281,-0.2457,-0.0767],[0.5381,0.3391,0.6314,0.5580,-0.0700,-0.4536,0. 8431、0.0564、0.7927、-0.3688、0. 1646,0.2029,0.2588,0.0048,0.1181,0.2797,0.1471,0.4008,-0.9551,0. 0732],[0.2266,-0.4448,-0.4133,0. 9179、0.3264、-0.0059、0. 4354,0.4920,1.1738,-0.7267,0. 9867,-0.7227,0. 6543,1. 0040,-0. 7897,0. 0184,0.4296,0.4448,0.1173,0.1869],[0.0907,0.1008,0.6054,0.2081,-0.4937,-0.9011,-0.1375,-0.2694,-0.0065,-1.1464,0. 2610、0.0738、0.9818、0.0983、0.1097、-0.4880、-0.6180、-0.0958、-0.0334、0. 2447],[-0.0593,0. 8875,-1.4164,0. 4210、2.2157、0.9704、0.0399、0.9641、1.0622、1.3101、0.5949、0.8925、-0.6213、1. 2130、0.2840、0.5771、0.7983、-0.5174、0. 6473,0.2891]],grad_fn =)
如何使它们的输出相同?

pgpifvop

pgpifvop1#

这是因为SimpleNet的每个示例化都使用随机数生成器进行加权。第二次示例化时,随机数生成器的状态与第一次不同。
为了解决这个问题,你可以重复

seed = 0
random.seed(seed)
np.random.seed(seed)

torch.manual_seed(seed)
torch.cuda.manual_seed(seed)

在第二个示例化之前,再次在第三个示例化之前。

相关问题