dragen1860 / Deep-Learning-with-TensorFlow-book

深度学习入门开源书,基于TensorFlow 2.0案例实战。Open source Deep Learning book, based on TensorFlow 2.0 framework.
http://www.ai101edu.com
13.21k stars 4.08k forks source link

p.assign_sub(lr * g)报错 #156

Closed Co1lin closed 4 years ago

Co1lin commented 4 years ago

第五章代码里更新权重的语句,不知道为啥例程不会错但是我自己写就报错了: 代码: grads = tape.gradient(loss, [w1, b1, w2, b2, w3, b3]) # derivative for p, g in zip([w1, b1, w2, b2, w3, b3], grads): p.assign_sub(lr * g) # update the parameters

Traceback (most recent call last): File "train.py", line 66, in <module> main() File "train.py", line 58, in main p.assign_sub(lr * g) # update the parameters AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'assign_sub'

Co1lin commented 4 years ago

破案了。 原因是bias,我给b赋的值是tf.zeros([512])而不是tf.Variable(tf.zeros([512])),而只有tf.Variable类型才有assign_sub