element 0 of tensors does not require grad and does not have a grad_fn
这个是因为requires_grad=False,应该为true
import numpy as np import torch from torch.autograd import Variable x = Variable(torch.ones(2,2),requires_grad=False) y = x + 2 # print(x.creator) # None,用户直接创建没有creater属性 # print(y.creator) # <torch.autograd._functions.basic_ops.AddConstant object at 0x7fb9b4d4b208> z = y*y*3 out = z.mean() out.backward() print(x,y,z) print(x.grad) # 输出对out对x求倒结果 print(y.grad) # y不是自动求导变量
博客详细讨论了在深度学习中遇到的错误提示'element 0 of tensors does not require grad and does not have a grad_fn'的原因。核心原因是相关张量的requires_grad属性设置为了False,需要改为True以跟踪梯度。解决方案在于确保在计算过程中的变量正确设置了求导需求。
790

被折叠的 条评论
为什么被折叠?



