如何在scatter_update中启用梯度流?

时间:2018-11-01 01:10:52

标签: python tensorflow gradient

我正在尝试通过从训练循环内的所有固定大小(例如5x5)的可能窗口中获取数据来计算图像的局部方差图。为了矢量化此操作,我正在考虑使用类似于this的操作在训练循环内使用Start of month one year ago = EOMONTH(TODAY(),-13)+1 扩展原始图像。此操作的本质作用是将原始张量中的每个元素映射到新张量中可能的许多位置,然后在训练循环内计算位置。

但是,scatter_update/scatter_nd_update不允许梯度传播,而我为scatter_update创建简单的自定义梯度的尝试没有用。

scatter_update

运行上面的代码显示@tf.RegisterGradient("CustomGrad") def _clip_grad(unused_op, grad): return tf.constant(5., dtype=tf.float32, shape=(1)) # tf.clip_by_value(grad, -0.1, 0.1) x = tf.Variable([3.0], dtype=tf.float32) y = tf.get_variable('y', shape=(1), dtype=tf.float32) g = tf.get_default_graph() with g.gradient_override_map({"ScatterNdUpdate1": "CustomGrad"}): output = tf.scatter_nd_update(y, [[0]], x, name="ScatterNdUpdate1") grad_custom = tf.gradients(output, y) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print(grad_custom) 包含grad_custom。是否有人对如何正确实现可在训练循环中使用的局部方差图有任何想法?解决梯度问题也可以帮助我解决另一个问题。

0 个答案:

没有答案
相关问题