使用一个具有全局上下文的 GradientTape [英] using one GradientTape with global context

查看:28
本文介绍了使用一个具有全局上下文的 GradientTape的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 GradientTape 在急切执行模式下观察梯度.是否可以创建一个 GradientTape 一次,然后记录所有内容,就好像它具有全局上下文一样?

I would like to use GradientTape to observe gradients during eager execution mode. Is it possible to create a GradientTape once, which then records everything, as if it had global context?

这是我想做的一个例子:

Here is an example of what I would like to do:

import numpy as np
import tensorflow as tf

x = tf.Variable(np.ones((2,)))
y=2*x
z=2*y
tf.gradients(z, x) # RuntimeError, not supported in eager execution

现在,这可以轻松解决:

Now, this can be fixed easily:

with tf.GradientTape() as g:
    y = 2*x
    z = 2*y
    
g.gradient(y, x) # this works

但问题是我经常没有 y 和 z 的定义紧随其后.例如,如果代码在 Jupyter notebook 中执行并且它们在不同的单元格中怎么办?

But the problem is that I often don't have the definitions of y and z immediately after each other. For example, what if the code is executed in a Jupyter notebook and they are in different cells?

我可以定义一个全局监控一切的 GradientTape 吗?

Can I define a GradientTape that watches everything, globally?

推荐答案

我找到了这个解决方法:

I found this workaround:

import numpy as np
import tensorflow as tf

# persistent is not necessary for g to work globally
# it only means that gradients can be computed more than once,
# which is important for the interactive jupyter notebook use-case
g = tf.GradientTape(persistent=True)

# this is the workaround
g.__enter__()

# you can execute this anywhere, also splitted into separate cells
x = tf.Variable(np.ones((2,)))
y = 2*x
z = 2*y

g.gradient(z, x)

这篇关于使用一个具有全局上下文的 GradientTape的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆