The model definition looks alright and shouldn’t be the problem:
class Discriminator(nn.Module):
def __init__(self):
super(Discriminator, self).__init__()
self.dis_model = nn.Sequential(
nn.Linear(784, 32),
nn.LeakyReLU(negative_slope=0.2),
nn.Linear(32, 32),
nn.LeakyReLU(negative_slope=0.2)
)
self.discriminator = nn.Sequential(
nn.Linear(32, 1),
nn.Sigmoid()
)
def forward(self, x):
x = self.dis_model(x)
x = self.discriminator(x)
return x
model = Discriminator()
x = torch.randn(1, 784)
output = model(x)
output.mean().backward()
for name, param in model.named_parameters():
print(name, param.grad)
>dis_model.0.weight tensor([[-0.0013, 0.0058, -0.0014, ..., -0.0057, -0.0007, -0.0087],
[-0.0004, 0.0016, -0.0004, ..., -0.0016, -0.0002, -0.0024],
[ 0.0008, -0.0037, 0.0009, ..., 0.0037, 0.0004, 0.0056],
...,
[ 0.0002, -0.0011, 0.0003, ..., 0.0011, 0.0001, 0.0016],
[ 0.0048, -0.0215, 0.0052, ..., 0.0212, 0.0025, 0.0322],
[-0.0006, 0.0026, -0.0006, ..., -0.0026, -0.0003, -0.0039]])
dis_model.0.bias tensor([ 5.0931e-03, 1.3960e-03, -3.2967e-03, 9.4221e-03, -2.1729e-03,
-1.0545e-03, -2.0167e-04, -6.2622e-04, 3.3179e-02, 4.4160e-03,
-1.2255e-03, -2.1004e-03, 6.5288e-04, -9.8875e-04, -2.2054e-03,
...
No. That would only be necessary, if you want to update your input, which is probably not the case.
Could you post the definition of the complete model?