pytorch加载模型不一样SOFTMAX概率

问题描述 投票:0回答:1

使用pytorch加载模型后,我无法重现相同的结果。我正在训练模型“网”,并在同一个文件,培训(kfold),那么该模型被保存,也是在1个特定的测试文件进行测试后:

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

例如,对于这个文件我得到:预解码值=张量([[0.5912,0.4088]],grad_fn =)

当我反而加载保存的模型在一个新的剧本,我尝试在同一testfile的再次推论:

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)

当我运行测试脚本的概率值是不同的,甚至更糟不能稳定下来:运行多次给不同的预测...任何帮助表示赞赏

python-3.x pytorch softmax
1个回答
0
投票

作为@Jatentaki指出,解决的办法是请固定种子需要使用pytorch模型中的所有脚本

torch.manual_seed(0)
© www.soinside.com 2019 - 2024. All rights reserved.