Hello,guys~~I can not get the grad of the input of my net. I have tried a lot of solutions provided in the forum, but it can not work. Following is part of my code, and anybody can help? Thanks a lot!
X_emb = Variable(X_emb.data,requires_grad=True)
X_emb = X_emb.to(device)
print(X_emb.dtype)
#X_emb.requires_grad_()
print(X_emb.requires_grad)
X_emb.retain_grad()
T = opt.T
epsilon = opt.epsilon
net.zero_grad()
#y_hat = net(X,seq_lengths)
y_hat = net_rest1(net,X_emb,seq_lengths)
y_hat.requires_grad_()
print(y_hat.requires_grad)
, pred_idx = torch.max(y_hat.data,1)
labels = Variable(pred_idx)
y_hat = y_hat / T
y_hat.requires_grad()
print(y_hat.requires_grad)
loss = xent(y_hat,labels)
loss.requires_grad_()
X_emb.retain_grad()
loss.backward()
print(type(X_emb))
print(X_emb.grad)
X_emb = X_emb - epsilon * torch.sign(X_emb.grad) # here occures the error because X_emb.grad is None

loss.backwarc() can pass but I can’t get the grad. I am very confused with the result…