gpt4 book ai didi

python - Tensorflow 2.0 Autograph 间接修改(隐藏状态)可以工作,但它不应该工作

转载 作者:行者123 更新时间:2023-12-01 06:46:51 25 4
gpt4 key购买 nike

所以,here它说间接修改不应该起作用,这意味着更改将是不可见的(隐形更改是什么意思?)

但是这段代码正确地计算了梯度:

import tensorflow as tf


class C:
def __init__(self):
self.x = tf.Variable(2.0)

@tf.function
def change(self):
self.x.assign_add(2.0)

@tf.function
def func(self):
self.change()
return self.x * self.x


c = C()
with tf.GradientTape() as tape:
y = c.func()
print(tape.gradient(y, c.x)) # --> tf.Tensor(8.0, shape=(), dtype=float32)

我在这里遗漏了什么吗?

谢谢

最佳答案

文档缺少细节,应予以澄清 - “不可见”意味着 AutoGraph 的分析器未检测到更改。由于 AutoGraph 一次分析一个函数,因此分析器看不到在另一函数中所做的修改。

但是,此警告不适用于具有副作用的操作,例如对 TF 变量的修改 - 这些操作仍将在图中正确连接。所以你的代码应该可以正常工作。

该限制仅适用于对纯 Python 对象(列表、字典等)进行的某些更改,并且仅在使用控制流时才会出现问题。

例如,下面是对代码的修改,但该修改不起作用:

class C:
def __init__(self):
self.x = None

def reset(self):
self.x = tf.constant(10)

def change(self):
self.x += 1

@tf.function
def func(self):
self.reset()
for i in tf.range(3):
self.change()
return self.x * self.x


c = C()
print(c.func())

错误消息相当晦涩,但如果您尝试访问在 tf.while_loop 主体内创建的操作的结果而不使用 loop_vars,则会引发相同的错误:

    <ipython-input-18-23f1641cfa01>:20 func  *
return self.x * self.x

... more internal frames ...

InaccessibleTensorError: The tensor 'Tensor("add:0", shape=(),
dtype=int32)' cannot be accessed here: it is defined in another function or
code block. Use return values, explicit Python locals or TensorFlow
collections to access it. Defined in: FuncGraph(name=while_body_685,
id=5029696157776); accessed from: FuncGraph(name=func, id=5029690557264).

关于python - Tensorflow 2.0 Autograph 间接修改(隐藏状态)可以工作,但它不应该工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59186417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com