图中似乎未使用 PyTorch Autograd 微分张量

问题描述

我正在尝试通过实施 this paper 中描述的加权损失方法来改进我制作的 CNN。为此,我查看了 this notebook,它实现了论文中描述的方法的伪代码

在将他们的代码转换为我的模型时,我在使用 RuntimeError: One of the differentiated Tensors appears to not have been used in the graph. Set allow_unused=True if this is the desired behavior 时遇到了错误 torch.autograd.grad()

我的代码错误在倒数第二行:

for epoch in range(1): #tqdm(range(params['epochs'])):
  model.train()
  text_t,labels_t = next(iter(train_iterator))
  text_t = to_var(text_t,requires_grad=False)
  labels_t = to_var(labels_t,requires_grad=False)
  dummy = L2RWCNN(INPUT_DIM,EMbedDING_DIM,N_FILTERS,FILTER_SIZES,OUTPUT_DIM,DROPOUT,PAD_IDX)
  dummy.state_dict(model.state_dict())
  dummy.cuda()
  y_f_hat = dummy(text_t)
  cost = F.binary_cross_entropy_with_logits(y_f_hat.squeeze(),labels_t,reduce = False)
  eps = to_var(torch.zeros(cost.size()))
  l_f_Meta = torch.sum(cost * eps)
  dummy.zero_grad()
  num_params = 0
  grads = torch.autograd.grad(l_f_Meta,(dummy.params()),create_graph = True)
  with torch.no_grad():
    for p,grad in zip(dummy.parameters(),grads):
      tmp = p - params['lr'] * grad
      p.copy_(tmp)
  text_v,labels_v = next(iter(valid_iterator))
  y_g_hat = dummy(text_v)
  l_g_Meta = F.binary_cross_entropy_with_logits(y_g_hat.squeeze(),labels_v,reduce = False)
  l_g_Meta = torch.sum(l_g_Meta)
  grad_eps = torch.autograd.grad(l_g_Meta,eps,only_inputs=True)[0]
  print(grad_eps)

我认为错误是因为 eps 没有出现在之前的任何 torch.autograd.grad() 调用中。我尝试了设置 allow_unused=True 的建议解决方案,但结果是 None 值。我查看了 this post 以找到解决方案,但是在这里解决问题的方法(不要对张量进行切片)对我不起作用,因为我没有传入任何部分变量。我还尝试在我的第一个 create_graph = False 调用中设置 autograd.grad(),但这并没有解决问题。有人有解决办法吗?

My full code if that's needed

编辑: 创建了一个新帖子,对问题 here

使用了不同的措辞

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)