class ConvNet(torch.nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.conv1 = torch.nn.Conv2d(1, 6, 5)
self.pool = torch.nn.MaxPool2d(2, 2)
self.conv2 = torch.nn.Conv2d(6, 16, 5)
self.fc1 = torch.nn.Linear(16 * 4 * 4, 62)
def forward(self, x):
x = self.pool(F.relu(self.conv1(x)))
x = self.pool(F.relu(self.conv2(x)))
x = x.view(-1, 16 * 4 * 4)
x = self.fc1(x)
return x
for x, y in loader:
x, y = x.to(device), y.to(device)
optimizer.zero_grad()
loss = torch.nn.CrossEntropyLoss()(model(x), y)
loss.backward()
print(Convnet().conv1.weight.grad)
optimizer.step()
I tried Convnet().conv1.weight.grad, but it gives None output. What are the other options to print gradient in Pytorch?