Hi,
I’m trying to modify self.message1 and self.message2 using two different mlps. But I find that the gradients of these two mlps are None for each epoch all the time. But I use the same way to modify the variable F_ue_update using mlp3, the gradients of its weights and bias are not None.
Here is some parts of my code.
Thanks for any help.
def message(self, F_ue, E, agg_ue, P, Noise):
if self.device.type == "cuda":
P = P.to(self.device)
Noise = Noise.to(self.device)
for m in range(self.M):
for k in range(self.K):
message_input1 = torch.cat((E[m,k,:], agg_ue[m,:], P[m], Noise[k]), 0)
message_input_var1 = torch.autograd.Variable(message_input1, requires_grad=True)
self.message_bs[m, k, :] = self.mlp1(message_input_var1)
agg_bs = torch.mean(self.message_bs, dim=0)
agg_bs = agg_bs.to(self.device) # of size [K, 2MN]
for k in range(self.K):
for m in range(self.M):
message_input = torch.cat((F_ue[:, k], E[m, k, :], agg_bs[k, :], P[m], Noise[k]), 0)
message_input_var = torch.autograd.Variable(message_input, requires_grad=True)
self.message_ue[k, m, :] = self.mlp2(message_input_var)
def update(self, E, F_ue, agg_bs, P, Noise):
if self.device.type == "cuda":
P = P.to(self.device)
Noise = Noise.to(self.device)
F_ue_update = torch.zeros(F_ue.shape)
for k in range(self.K):
edge_cat_ue = torch.cat([E[m, k, :] for m in range(self.M)], dim=0)
message_input3 = torch.cat((edge_cat_ue, F_ue[:, k], agg_bs[k,:], P[0], Noise[k]), 0)
message_input_var3 = torch.autograd.Variable(message_input3, requires_grad=True)
F_ue_update[:, k] = self.mlp3(message_input_var3)
F_ue_update = F_ue_update.to(self.device)
F_ue = model(F_ue0, edge_feature, P, Noise)
W = getW(F_ue, P, K)
loss = loss + Loss(W, H_complex, Noise)
optimizer.zero_grad()
loss.backward()
for name, param in model.named_parameters():
if param.grad is not None:
print(f'Parameter: {name}') # Only mlp3 weight and bias be printed
optimizer.step()