我为收到的渐变定义了2个变量(D,ht)。然而,在损失函数和向后()计算之后,只有D具有梯度,ht没有计算任何梯度。能帮我理解为什么会这样吗?谢谢。
import torch.nn as nn
import torch
from torch.autograd import Variable, Function
x = Variable(torch.from_numpy(np.random.normal(0,1,(10,10))), requires_grad=False) # original
# depends on size of the dictionary, number of atoms.
D = Variable(torch.from_numpy(np.random.normal(0,1,(500,10,10))), requires_grad=True)
# hx sparse representation
ht = Variable(torch.from_numpy(np.random.normal(0,1,(500,1,1))), requires_grad=True)
ld = 0.02
lr = 0.001 # learning rate
# optimizer
optimizer = torch.optim.SGD([ht,D], lr=lr, momentum=0.9)
optimizer.zero_grad()
loss_ht = 0.5*torch.norm((x-(D*ht).sum(dim=0)),p=2)**2
loss_ht.backward() # back propogation
optimizer.step() # update parameters
D有种姓,见下文
D.grad.data
(0,。,。)= 2.0307e + 01 9.9208e + 00 4.9194e + 00 ... -2.3104e + 01 -2.6616e + 01 -9.8070e-01 3.7742e + 01 2.5255e + 01 4.5286e + 00 ... -3.5697e + 01 -1.1306e + 01 1.9366e + 01
ht没有任何渐变。但为什么?请帮忙。见下文。
ht.grad.data
AttributeError Traceback(最近一次调用最后一次) in() ----> 1 ht.grad.data
AttributeError:'NoneType'对象没有属性'data'