我正在尝试学习如何在TensorFlow中构建图形,但是陷入了看似微不足道的操作中。这就是我所拥有的,
import tensorflow as tf
def loss(x, y):
tf.reduce_mean(tf.square(x - y))
xx = tf.random_normal([])
noise = tf.random_normal([])
yy = 3 * xx + 2 + noise
W = tf.get_variable("W", [])
W.assign(5)
b = tf.get_variable("b", [])
b.assign(0)
with tf.GradientTape() as t:
current_loss = loss(W*xx+b, yy)
dW = t.gradient(current_loss, W)
这时我得到了一个AttributeError,如下所示
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-7-26d05a240ccc> in <module>()
1 with tf.GradientTape() as t:
2 current_loss = loss(W*xx+b, yy)
----> 3 dW = t.gradient(current_loss, W)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/backprop.py in gradient(self, target, sources, output_gradients, unconnected_gradients)
944 flat_sources,
945 output_gradients=output_gradients,
--> 946 unconnected_gradients=unconnected_gradients)
947
948 if not self._persistent:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/imperative_grad.py in imperative_grad(tape, target, sources, output_gradients, unconnected_gradients)
70 sources,
71 output_gradients,
---> 72 compat.as_str(unconnected_gradients.value))
AttributeError: 'NoneType' object has no attribute '_id'
我做错了什么?如何获得渐变?预先感谢。
答案 0 :(得分:2)
如果您不想使用急切执行,则需要对上面的代码进行一些修改。如果您不希望在急切的执行模式下使用TensorFlow,则需要两个主要部分:
因此,我按照以下逻辑重新编写了上面的示例:
import tensorflow as tf
import numpy as np
# Building the graph
xx = tf.constant(np.random.normal(loc=0.0, scale=1.0))
noise = tf.constant(np.random.normal(loc=0.0, scale=1.0))
yy = 3 * xx + 2 + noise
W = tf.Variable(5.0, name="W")
b = tf.Variable(0.0, name="b")
current_loss = tf.reduce_mean(tf.square(tf.scalar_mul(W,xx)+b - yy))
dW = tf.gradients(current_loss, W, name='dW')[0]
# Initialisation operation
init = tf.global_variables_initializer()
# Creating a session and running the graph
with tf.Session() as sess:
sess.run(init)
dW_np = sess.run(dW)
print(dW_np)
答案 1 :(得分:1)
您的loss()
函数不返回任何内容。这就是为什么您拥有AttributeError
(因为current_loss
是None
)的原因。您应该添加return
语句。
关于您对先前答案的评论。 GradientTape
用于急切执行,因此您应在程序的开头添加tf.enable_eager_execution()
。如果要以图形方式构建,则应使用tf.gradients()
的子类的compute_gradients()
或tf.train.Optimizer
方法(例如tf.train.GradientDescentOptimizer
)。