The parameters of a CNN model is returning None

Hello, i create a CNN model like this global_model = CNNMnist(args=args). Then i send it to device, set it to train. Then i train my local models, collect the local_weights and the average them to get updated global_model.
Now i am trying to get the items from the .parameters() function but all i get is None as item.grad. When i do the same thing for the local_models, i get the desired output. What am i doing wrong?

global_model.to(device)
global_model.train()
...................
global_weights = average_weights(local_weights)
global_model.load_state_dict(global_weights)
last_update = []
for item in global_model.parameters():
    last_update.append(copy.deepcopy(item.grad))
    print(item.grad)

Output: None None None None None None None None

Any help would be appreciated.

item.grad stores the gradients of the parameters wrt the loss function and not the weights.

What you want is -

weights = [c.detach().numpy() for c in global_model.parameters()]

Hi, thanks for your reply. could you please elaborate a little? where exactly should i insert the line and how do i get the desired outcome?
Thanks

Hi. You can try the following.

for param in global_model.parameters():
    last_update.append(param.clone().data.cpu().numpy())