栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Tensorflow:如何替换或修改渐变?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Tensorflow:如何替换或修改渐变?

对于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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/646896.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号