Pytorch的梯度为None!

  • Post author:
  • Post category:其他




Pytorch 梯度为None

尽管设置了某个Tensor的属性


requires_grad = True


,但是,用某个loss对该Tensor计算梯度时,作者也遇到了梯度为None的情况 !!!



实例



情况说明

作者在写ADP的网络时,定义了A_Net,Model_Net,V_Net,在更新A_Net时候,定义损失:





l

o

s

s

A

=

s

e

l

f

.

c

r

i

t

e

r

i

o

n

(

p

r

e

d

i

c

t

,

t

a

r

g

e

t

)

lossA = self.criterion(predict,target)






l


o


s


s


A




=








s


e


l


f


.


c


r


i


t


e


r


i


o


n


(


p


r


e


d


i


c


t


,




t


a


r


g


e


t


)





target没啥好说,就是一个Label,也是一个定值。计算predict的时候,需要将输入state连续经过A_Net,Model_Net,V_Net三个网络,并且经过A_Net后还需要处理数据再传入Model_Net,同样Model_Net的输出也需要处理再传入V_Net。



问题出现

一开始考虑到每个网络的输出都要处理,因此,利用一个中间变量来存储。代码如下:

model_input_with_u = torch.randn(state_num, state_dim + action_dim)
model_input_with_u[:,0:state_dim] = torch.tensor(state).type(torch.FloatTensor)
model_input_with_u[:,state_dim] = self.A_model(Variable(torch.Tensor(state))).view(state_num).type(torch.FloatTensor)
next_xk_1 = self.modelnet(Variable(torch.Tensor(model_input_with_u))).type(torch.FloatTensor)
predict = self.V_model(Variable(torch.Tensor(next_xk_1))) 

# Variable(torch.Tensor(state)):输入到A_Net的数据
# model_input_with_u:定义的中间变量 保存了A_Net的输出,同时还附加另外两维数据
# 将model_input_with_u数据传入Model_Net,得到结果再传入V_Net

问题就出现了:当用lossA对A_Net网络求梯度,发现一直是None。查了网上也没有解决方案。



个人解决方案


不利用中间变量存储

。代码直接一步到位!!!!

predict = self.V_model(\
               self.modelnet(\
                   torch.cat((Variable(torch.tensor(state)).type(torch.FloatTensor),\
                              self.A_model(Variable(torch.Tensor(state))).view(state_num,1).type(torch.FloatTensor)),1)
               )
           )

最终,查看A_Net的梯度,可以看到非None了!



版权声明:本文为weixin_44231148原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。