函数英文注释
stop_gradient(input, name=None)
Stops gradient computation.
When executed in a graph, this op outputs its input tensor as-is.
When building ops to compute gradients, this op prevents the contribution of
its inputs to be taken into account. Normally, the gradient generator adds ops
to a graph to compute the derivatives of a specified 'loss' by recursively
finding out inputs that contributed to its computation. If you insert this op
in the graph it inputs are masked from the gradient generator. They are not
taken into account for computing gradients.
This is useful any time you want to compute a value with TensorFlow but need
to pretend that the value was a constant. Some examples include:
* The *EM* algorithm where the *M-step* should not involve backpropagation
through the output of the *E-step*.
* Contrastive divergence training of Boltzmann machines where, when
differentiating the energy function, the training must not backpropagate
through the graph that generated the samples from the model.
* Adversarial training, where no backprop should happen through the adversarial
example generation process.
Args:
input: A `Tensor`.
name: A name for the operation (optional).
Returns:
A `Tensor`. Has the same type as `input`.
函数中文注释
掺杂理解性注释,用中文符号“()”表示。
stop_gradient(input, name=None)
功能:停止对给定变量的梯度计算。
当在图中执行时,该op(op即operation,意为运算,加's'代表复数)原样输出其输入张量。(该
功能虽然对输入张量的值没直接影响,但在网络训练的梯度更新阶段,可选择性避免更新该张量。)
在构建运算来计算梯度时,此操作会阻止其输入的贡献被考虑在内。通常,梯度生成器将ops添加到
图形中,通过递归地找出有助于其计算的输入来计算指定“损失”的导数。如果在图形中插入此op,则输
入将从梯度生成器中屏蔽。计算梯度时不考虑它们。
当您想要使用TensorFlow计算值但需要假定该值是常量时,这非常有用。其中一些例子包括:
* EM算法,其中M-step不应涉及通过E-step的输出进行反向传播。
* 玻尔兹曼机器的对比散度训练,在区分能量函数时,训练不得反向传播通过模型生成样本的图形。
* 对抗性训练,通过对抗性示例生成过程不会发生反向训练。
相关变量:
input:一个'Tensor`。
name:操作的名称(可选项,即可以不指定)。
返回:
一个`Tensor`。与`input`具有相同的类型。
函数使用方法
from tensorflow.python.ops import array_ops # 引入函数
x = array_ops.stop_gradient(x) # 执行功能
小结
注释中其中最核心的一句话就是:“当您想要使用TensorFlow计算值但需要假定该值是常量时,这非常有用”。所以说,上述代码的功能就是告诉TensorFlow,在梯度计算时,将变量x作为常量计算,不考虑其变化量 Δ x \Delta x Δx对梯度的贡献。