GradientTape 在循环中运行时返回 None

xiaoxingxing tensorflow 375

原文标题GradientTape returning None when run in a loop

以下梯度下降失败,因为当循环第二次运行时,tape.gradient()返回的梯度没有。

w = tf.Variable(tf.random.normal((3, 2)), name='w')
b = tf.Variable(tf.zeros(2, dtype=tf.float32), name='b')
x = tf.constant([[1., 2., 3.]])


for i in range(10):
  print("iter {}".format(i))
  with tf.GradientTape() as tape:
    #forward prop
    y = x @ w + b  
    loss = tf.reduce_mean(y**2)
    print("loss is \n{}".format(loss))
    print("output- y is \n{}".format(y))
    #vars getting dropped after couple of iterations
    print(tape.watched_variables()) 
  
  #get the gradients to minimize the loss
  dl_dw, dl_db = tape.gradient(loss,[w,b]) 

  #descend the gradients
  w = w.assign_sub(0.001*dl_dw)
  b = b.assign_sub(0.001*dl_db)
iter 0
loss is 
23.328645706176758
output- y is 
[[ 6.8125362  -0.49663293]]
(<tf.Variable 'w:0' shape=(3, 2) dtype=float32, numpy=
array([[-1.3461215 ,  0.43708783],
       [ 1.5931423 ,  0.31951016],
       [ 1.6574576 , -0.52424705]], dtype=float32)>, <tf.Variable 'b:0' shape=(2,) dtype=float32, numpy=array([0., 0.], dtype=float32)>)
iter 1
loss is 
22.634033203125
output- y is 
[[ 6.7103477  -0.48918355]]
()

TypeError                                 Traceback (most recent call last)
c:\projects\pyspace\mltest\test.ipynb Cell 7' in <cell line: 1>()
     11 dl_dw, dl_db = tape.gradient(loss,[w,b]) 
     13 #descend the gradients
---> 14 w = w.assign_sub(0.001*dl_dw)
     15 b = b.assign_sub(0.001*dl_db)

TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'

我检查了解释渐变变为None的可能性的文档,但它们都没有帮助。

原文链接:https://stackoverflow.com//questions/71590479/gradienttape-returning-none-when-run-in-a-loop

回复

我来回复
  • xdurch0的头像
    xdurch0 评论

    这是因为assign_sub返回aTensor。因此,在行w = w.assign_sub(0.001*dl_dw)中,您正在用具有新值的张量覆盖w。因此,在下一步中,它不再是aVariable,并且默认情况下不会被渐变磁带跟踪。这导致梯度变为None(张量也没有assign_sub方法,因此也会崩溃)。

    相反,只需写w.assign_sub(0.001*dl_dw)b相同。分配功能就地工作,因此不需要分配。

    2年前 0条评论