构造 ys w.r.t 之和的符号导数。 x 在 xs 。
用法
tf.gradients(
ys, xs, grad_ys=None, name='gradients', gate_gradients=False,
aggregation_method=None, stop_gradients=None,
unconnected_gradients=tf.UnconnectedGradients.NONE
)参数
-
ysTensor或要区分的张量列表。 -
xsTensor或用于微分的张量列表。 -
grad_ys可选的。Tensor或与ys大小相同的张量列表,并保存为ys中的每个 y 计算的梯度。 -
name用于将所有渐变操作分组在一起的可选名称。默认为'gradients'。 -
gate_gradients如果为 True,则在为操作返回的梯度周围添加一个元组。这避免了一些竞争条件。 -
aggregation_method指定用于组合梯度项的方法。接受的值是在类AggregationMethod中定义的常量。 -
stop_gradients可选的。Tensor或不区分的张量列表。 -
unconnected_gradients可选的。指定给定输入张量未连接时返回的梯度值。接受的值是在类tf.UnconnectedGradients中定义的常量,默认值为none。
返回
-
长度为
len(xs)的Tensor列表,其中每个张量是sum(dy/dx):对于ys中的 y 和xs中的 x。
抛出
-
LookupError如果x和y之间的操作之一没有注册的梯度函数。 -
ValueError如果参数无效。 -
RuntimeError如果在 Eager 模式下调用。
tf.gradients 仅在图形上下文中有效。特别是,它在 tf.function 包装器的上下文中有效,其中代码作为图形执行。
ys 和 xs 每个都是 Tensor 或张量列表。 grad_ys 是 Tensor 的列表,包含 ys 接收到的梯度。该列表的长度必须与 ys 相同。
gradients() 向图中添加操作以输出 ys 相对于 xs 的导数。它返回长度为 len(xs) 的 Tensor 列表,其中每个张量是 sum(dy/dx) :对于 ys 中的 y 和 xs 中的 x。
grad_ys 是与 ys 长度相同的张量列表,其中包含 ys 中每个 y 的初始梯度。当 grad_ys 为 None 时,我们为 ys 中的每个 y 填充一个形状为 y 的 '1's 张量。用户可以提供他们自己的初始grad_ys,以使用每个 y 的不同初始梯度来计算导数(例如,如果想要对每个 y 中的每个值以不同的方式加权梯度)。
stop_gradients 是一个 Tensor 或相对于所有 xs 被视为常数的张量列表。这些张量不会被反向传播,就好像它们已使用 stop_gradient 显式断开连接一样。除其他外,这允许计算偏导数而不是全导数。例如:
@tf.function
def example():
a = tf.constant(0.)
b = 2 * a
return tf.gradients(a + b, [a, b], stop_gradients=[a, b])
example()
[<tf.Tensor:shape=(), dtype=float32, numpy=1.0>,
<tf.Tensor:shape=(), dtype=float32, numpy=1.0>]
这里的偏导数 g 计算为 [1.0, 1.0] ,而总导数 tf.gradients(a + b, [a, b]) 考虑到 a 对 b 的影响并计算为 [3.0, 1.0] 。请注意,以上等价于:
@tf.function
def example():
a = tf.stop_gradient(tf.constant(0.))
b = tf.stop_gradient(2 * a)
return tf.gradients(a + b, [a, b])
example()
[<tf.Tensor:shape=(), dtype=float32, numpy=1.0>,
<tf.Tensor:shape=(), dtype=float32, numpy=1.0>]
stop_gradients 提供了一种在图已经构建后停止梯度的方法,与在图构建期间使用的tf.stop_gradient 相比。当这两种方法结合使用时,反向传播会在 tf.stop_gradient 节点和 stop_gradients 中的节点处停止,以先遇到者为准。
对于所有 xs ,所有整数张量都被认为是常数,就好像它们包含在 stop_gradients 中一样。
unconnected_gradients 确定 xs 中每个 x 的返回值,如果它在图中未连接到 ys。默认情况下,这是 None 以防止错误。从数学上讲,这些梯度为零,可以使用 'zero' 选项请求。 tf.UnconnectedGradients 提供以下选项和行为:
@tf.function
def example(use_zero):
a = tf.ones([1, 2])
b = tf.ones([3, 1])
if use_zero:
return tf.gradients([b], [a], unconnected_gradients='zero')
else:
return tf.gradients([b], [a], unconnected_gradients='none')
example(False)
[None]
example(True)
[<tf.Tensor:shape=(1, 2), dtype=float32, numpy=array([[0., 0.]], ...)>]
让我们举一个在反向传播阶段出现的实际例子。此函数用于评估成本函数关于权重 Ws 和偏差 bs 的导数。下面的示例实现提供了它实际用途的说明:
@tf.function
def example():
Ws = tf.constant(0.)
bs = 2 * Ws
cost = Ws + bs # This is just an example. Please ignore the formulas.
g = tf.gradients(cost, [Ws, bs])
dCost_dW, dCost_db = g
return dCost_dW, dCost_db
example()
(<tf.Tensor:shape=(), dtype=float32, numpy=3.0>,
<tf.Tensor:shape=(), dtype=float32, numpy=1.0>)
相关用法
- Python tf.grad_pass_through用法及代码示例
- Python tf.group用法及代码示例
- Python tf.gather用法及代码示例
- Python tf.get_current_name_scope用法及代码示例
- Python tf.gather_nd用法及代码示例
- Python tf.get_static_value用法及代码示例
- Python tf.compat.v1.distributions.Multinomial.stddev用法及代码示例
- Python tf.compat.v1.distribute.MirroredStrategy.experimental_distribute_dataset用法及代码示例
- Python tf.compat.v1.data.TFRecordDataset.interleave用法及代码示例
- Python tf.summary.scalar用法及代码示例
- Python tf.linalg.LinearOperatorFullMatrix.matvec用法及代码示例
- Python tf.linalg.LinearOperatorToeplitz.solve用法及代码示例
- Python tf.raw_ops.TPUReplicatedInput用法及代码示例
- Python tf.raw_ops.Bitcast用法及代码示例
- Python tf.compat.v1.distributions.Bernoulli.cross_entropy用法及代码示例
- Python tf.compat.v1.Variable.eval用法及代码示例
- Python tf.compat.v1.train.FtrlOptimizer.compute_gradients用法及代码示例
- Python tf.distribute.OneDeviceStrategy.experimental_distribute_values_from_function用法及代码示例
- Python tf.math.special.fresnel_cos用法及代码示例
- Python tf.keras.applications.inception_resnet_v2.preprocess_input用法及代码示例
注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.gradients。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
