pytorch加载模型的softmax概率不同

时间:2019-02-05 23:17:49

标签: python-3.x pytorch softmax

使用pytorch加载模型后,我无法重现相同的结果。 我正在训练模型“ net”并在同一文件中进行训练(kfold),然后将其保存并在一个特定的测试文件中进行测试:

class model(nn.Module):
    def __init__(self,size_net):
        print('Initialize net with size: ',size_net)
        self.T = size_net

        # Layer 1
        self.conv1 = nn.Conv2d(1, 16, (1,16), padding = 0)
        self.batchnorm1 = nn.BatchNorm2d(16, False)

        # Layer 2
        self.padding1 = nn.ZeroPad2d((16, 17, 0, 1))
        self.conv2 = nn.Conv2d(1, 4, (2, 32))
        self.batchnorm2 = nn.BatchNorm2d(4, False)
        self.pooling2 = nn.MaxPool2d(2, 4)

        # Layer 3
        self.padding2 = nn.ZeroPad2d((2, 1, 4, 3))
        self.conv3 = nn.Conv2d(4, 4, (8, 4))
        self.batchnorm3 = nn.BatchNorm2d(4, False)
        self.pooling3 = nn.MaxPool2d((2, 4))

        # FC Layer
        # NOTE: This dimension will depend on the number of timestamps per sample in your data.
        # I have 120 timepoints.

        self.fc1 = nn.Linear(int(self.T/2), 2)



    def forward(self, x):
        # Layer 1
        x = F.elu(self.conv1(x))
        x = self.batchnorm1(x)
        x = F.dropout(x, 0.25)
        x = x.permute(0, 3, 1, 2)
        #print "layer 1"
        # Layer 2
        x = self.padding1(x)
        x = F.elu(self.conv2(x))
        x = self.batchnorm2(x)
        x = F.dropout(x, 0.25)
        x = self.pooling2(x)

        #print "layer 2"

        # Layer 3
        x = self.padding2(x)
        x = F.elu(self.conv3(x))
        x = self.batchnorm3(x)
        x = F.dropout(x, 0.25)
        x = self.pooling3(x)

        #print "layer 3"

        # FC Layer
        #print ('view:',x.shape)
        x = x.view(-1, int(self.T/2))
        #x = torch.sigmoid(self.fc1(x))
        x= torch.softmax(self.fc1(x),1)


        #print "layer 4"

        return x

#now call the model and train

net = model(SIZE_NET)

....

eval.train_Kfold_validation(n_epochs=25)

## save models state

"""
net = EEGNet(SIZE_NET)
save_path = './eeg_net_{}.pt'.format(date.today().strftime("%Y%m%d"))
torch.save(net.state_dict(), save_path)


'''
TEST
'''
testfile = '1_testonline_1_20190202-163051.csv'
kun_1 = np.genfromtxt( '../'+ testfile, delimiter=',').astype('float32')[:-1, :]
kun_1 = kun_1[:, :SIZE_NET]
X, y = prep.list_2darrays_to_3d([kun_1], -1)
print(X.shape)
array_dstack = np.array(X)
array_dstack_reshaped = np.reshape(array_dstack,(1, 1, SIZE_NET, 16))
inputs = Variable(torch.from_numpy(array_dstack_reshaped))
pred = net(inputs)
print('prob: '+str(pred)) #Converted to probabilities

例如,对于这个文件,我得到:pred = tensor([[0.5912,0.4088]],grad_fn =)

相反,当我将保存的模型加载到新脚本中并再次尝试在同一测试文件上进行推断时:

prep= Data_prep()
fileName = '1_testonline_1_20190202-163051.csv'

kun_1 = np.genfromtxt(file_dir+fileName, delimiter=',').astype('float32')[:-1,:]
kun_1 = kun_1[:,:SIZE_NET]

X , y = prep.list_2darrays_to_3d([kun_1],[-1])

# Load pre-trained model
net = model(SIZE_NET)
load_path = file_dir+'/model_colors/model_20190205.pt'

net.load_state_dict(torch.load(load_path))
net.eval()




array_dstack = np.array(X)
print(X.shape)


# (#samples, 1, #timepoints, #channels)
array_dstack_reshaped = np.reshape(array_dstack,(1, 1, SIZE_NET, 16))
inputs = Variable(torch.from_numpy(array_dstack_reshaped))
pred = net(inputs)

print(pred)

当我运行测试脚本时,概率值是不同的,甚至更不稳定:多次运行会给出不同的预测...感谢任何帮助

1 个答案:

答案 0 :(得分:0)

@Jatentaki指出,解决方案是始终在需要在pytorch中使用该模型的所有脚本中修复种子

torch.manual_seed(0)