I am writing a MLP in pytorch using sequential model, but I am not understanding if the model is actually updating weights when I call :
optimizer.zero_grad()
scores = model(data)
loss = criterion(scores, targets)
# backward
loss.backward()
# gradient descent or adam step
optimizer.step()
My model is as below:
def init(self, input_size, out_size):
super(Feedforward, self).init()
self.layer1 = nn.Sequential()
self.layer1.add_module(“fc1”, torch.nn.Linear(input_size, 65))
self.layer1.add_module(“bn1”, nn.BatchNorm1d(num_features=65, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))
self.layer1.add_module(“Relu1”, torch.nn.ReLU())
self.layer1.add_module(“dropout”,nn.Dropout(p=0.2))
self.layer1.add_module(“fc2”, torch.nn.Linear(65, 60))
self.layer1.add_module(“bn2”, nn.BatchNorm1d(num_features=60, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True))
self.layer1.add_module(“Relu2”, torch.nn.ReLU())
self.layer1.add_module(“dropout2”,nn.Dropout(p=0.2))
self.layer1.add_module(“fc4”, torch.nn.Linear(60, out_size))
self.layer1.add_module(“Softmax”,torch.nn.Softmax(dim=1))
def forward(self, x):
x = self.layer1(x)
return self.fc.forward(x)
def initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
elif isinstance(m, nn.Linear):
nn.init. xavier_normal_(m.weight)