Recently, I met a confusing phenomenon when I used Pytorch to do simple experiments on logistic regression.
The question is when I fixed the random seed like this:
def set_seed(seed, cuda=True):
np.random.seed(seed)
torch.manual_seed(seed)
if cuda:
torch.cuda.manual_seed(seed)
and defined the following model with 2 layers:
class net(nn.Module):
def __init__(self):
super(net, self).__init__()
self.hidden = nn.Linear(784, 100)
self.output = nn.Linear(100, 10)
def forward(self, x):
x = self.hidden(x)
x = self.output(x)
return x
trained the network with:
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(net.parameters(), lr=0.1)
The orignal loss value was 0.6422, which is reproducible.
However, when I added an additional layer that was not involoved in the forward process like this:
class net(nn.Module):
def __init__(self):
super(net, self).__init__()
self.hidden = nn.Linear(784, 100)
self.output = nn.Linear(100, 10)
self.add = nn.Linear(10,10)
def forward(self, x):
x = self.hidden(x)
x = self.output(x)
return x
The original loss value changed to 0.7431, which was not equal to the previous one and the model performance dropped simultaneously.
I really wonder the reason to this. Thank you!