对于TensorFlow 1.7和TensorFlow 2.0,请看编辑打击。
首先定义您的自定义渐变:
@tf.RegisterGradient("CustomGrad")def _const_mul_grad(unused_op, grad): return 5.0 * grad由于您不希望前进过程中发生任何事情,因此请使用新的梯度来替代身份操作的梯度:
g = tf.get_default_graph()with g.gradient_override_map({"Identity": "CustomGrad"}): output = tf.identity(input, name="Identity")这是一个工作示例,其中的图层使用相同的方法在向后传递中剪切渐变,而在向前传递中不执行任何操作:
import tensorflow as tf@tf.RegisterGradient("CustomClipGrad")def _clip_grad(unused_op, grad): return tf.clip_by_value(grad, -0.1, 0.1)input = tf.Variable([3.0], dtype=tf.float32)g = tf.get_default_graph()with g.gradient_override_map({"Identity": "CustomClipGrad"}): output_clip = tf.identity(input, name="Identity")grad_clip = tf.gradients(output_clip, input)# output without gradient clipping in the backwards pass for comparison:output = tf.identity(input)grad = tf.gradients(output, input)with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print("with clipping:", sess.run(grad_clip)[0]) print("without clipping:", sess.run(grad)[0])编辑TensorFlow 1.7和TensorFlow 2.0
从1.7开始,有一种新的方法可以使用较短的语法重新定义渐变,这也适用于Tensorflow
2.0。它还允许同时重新定义多个操作的梯度。以下是上面为TensorFlow 1.7和TensorFlow 2.0重写的示例:
在向后传递中缩放渐变的图层:
@tf.custom_gradientdef scale_grad_layer(x): def grad(dy): return 5.0 * dy return tf.identity(x), grad
带有在向后传递中剪切渐变的图层的示例:
@tf.custom_gradientdef clip_grad_layer(x): def grad(dy): return tf.clip_by_value(dy, -0.1, 0.1) return tf.identity(x), grad



