gpt4 book ai didi

python - TypeError 使用 GradientTape.gradient 计算梯度

转载 作者:行者123 更新时间:2023-12-05 07:23:35 26 4
gpt4 key购买 nike

你好,

我目前正尝试在 Tensorflow 1.13.1 中计算梯度并使用 GradientTape official documentation 中解释的类, 但我得到一个 TypeError: Fetch argument None has invalid type <class 'NoneType'> .
下面,我将包括两个我得到这个错误的简单案例,只使用开箱即用的 Tensorflow 函数,第一个是更简单的最小工作示例,第二个是我实际需要解决/得到一个工作-大约。为了完整起见,我使用的是 Python 3.6.8

比较简单

import tensorflow as tf

tf.reset_default_graph()
x = tf.constant([1., 2., 3.])
with tf.GradientTape(persistent=True) as gg:
gg.watch(x)
f1 = tf.map_fn(lambda a: a**2, x)
f2 = x*x

# Computes gradients
d_fx1 = gg.gradient(f1, x) #Line that causes the error
d_fx2 = gg.gradient(f2, x) #No error
del gg #delete persistent GradientTape

with tf.Session() as sess:
d1, d2 = sess.run((d_fx1, d_fx2))
print(d1, d2)

在此代码中,f1f2以不同的方式计算,但给出相同的数组。但是,当尝试计算与它们相关的梯度时,第一行给出以下错误,而第二行完美无缺。我在错误的堆栈跟踪下方报告

TypeError                                 Traceback (most recent call last)
<ipython-input-1-9c59a2cf2d9b> in <module>()
15
16 with tf.Session() as sess:
---> 17 d1, d2 = sess.run((d_fx1, d_fx2))
18 print(d1, d2)

C:\HOMEWARE\Miniconda3-Windows-x86_64\envs\rdwsenv\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
927 try:
928 result = self._run(None, fetches, feed_dict, options_ptr,
--> 929 run_metadata_ptr)
930 if run_metadata:
931 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

C:\HOMEWARE\Miniconda3-Windows-x86_64\envs\rdwsenv\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1135 # Create a fetch handler to take care of the structure of fetches.
1136 fetch_handler = _FetchHandler(
-> 1137 self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles)
1138
1139 # Run request and get response.

C:\HOMEWARE\Miniconda3-Windows-x86_64\envs\rdwsenv\lib\site-packages\tensorflow\python\client\session.py in __init__(self, graph, fetches, feeds, feed_handles)
469 """
470 with graph.as_default():
--> 471 self._fetch_mapper = _FetchMapper.for_fetch(fetches)
472 self._fetches = []
473 self._targets = []

C:\HOMEWARE\Miniconda3-Windows-x86_64\envs\rdwsenv\lib\site-packages\tensorflow\python\client\session.py in for_fetch(fetch)
259 elif isinstance(fetch, (list, tuple)):
260 # NOTE(touts): This is also the code path for namedtuples.
--> 261 return _ListFetchMapper(fetch)
262 elif isinstance(fetch, collections.Mapping):
263 return _DictFetchMapper(fetch)

C:\HOMEWARE\Miniconda3-Windows-x86_64\envs\rdwsenv\lib\site-packages\tensorflow\python\client\session.py in __init__(self, fetches)
368 """
369 self._fetch_type = type(fetches)
--> 370 self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
371 self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers)
372

C:\HOMEWARE\Miniconda3-Windows-x86_64\envs\rdwsenv\lib\site-packages\tensorflow\python\client\session.py in <listcomp>(.0)
368 """
369 self._fetch_type = type(fetches)
--> 370 self._mappers = [_FetchMapper.for_fetch(fetch) for fetch in fetches]
371 self._unique_fetches, self._value_indices = _uniquify_fetches(self._mappers)
372

C:\HOMEWARE\Miniconda3-Windows-x86_64\envs\rdwsenv\lib\site-packages\tensorflow\python\client\session.py in for_fetch(fetch)
256 if fetch is None:
257 raise TypeError('Fetch argument %r has invalid type %r' % (fetch,
--> 258 type(fetch)))
259 elif isinstance(fetch, (list, tuple)):
260 # NOTE(touts): This is also the code path for namedtuples.

TypeError: Fetch argument None has invalid type <class 'NoneType'>

请注意,我也尝试过一次只计算一个梯度,即 persistent=False , 得到相同的结果。

实际需要

下面,我还将包括最小的工作示例,以重现我遇到的相同错误,但试图解决我实际正在处理的问题。

在此代码中,我使用了 RNN计算输出 w.r.t 一些输入,我需要计算 jacobian这个输出 w.r.t 输入。

import tensorflow as tf
from tensorflow.keras.layers import RNN, GRUCell

# Define size of variable. TODO: adapt to data
inp_dim = 2
num_units = 50
batch_size = 100
timesteps = 10

# Reset the graph, so as to avoid errors
tf.reset_default_graph()

# Building the model
inputs = tf.ones(shape=(timesteps, batch_size, inp_dim))

# Follow gradient computations
with tf.GradientTape() as g:
g.watch(inputs)
cells = [GRUCell(num_units), GRUCell(num_units)]
rnn = RNN(cells, time_major=True, return_sequences=True)
f = rnn(inputs)
d_fx = g.batch_jacobian(f, inputs)

# Run graph
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
grads = sess.run(d_fx)
grads.shape

关于堆栈跟踪,我得到了相同的错误,但行数较少(此堆栈跟踪中有一个 for_fetch<listcomp>__init 较少)。为了完整起见,我仍然在下面包含它

TypeError                                 Traceback (most recent call last)
<ipython-input-5-bb2ce4eebe87> in <module>()
25 with tf.Session() as sess:
26 sess.run(tf.global_variables_initializer())
---> 27 grads = sess.run(d_fx)
28 grads.shape

C:\HOMEWARE\Miniconda3-Windows-x86_64\envs\rdwsenv\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
927 try:
928 result = self._run(None, fetches, feed_dict, options_ptr,
--> 929 run_metadata_ptr)
930 if run_metadata:
931 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

C:\HOMEWARE\Miniconda3-Windows-x86_64\envs\rdwsenv\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
1135 # Create a fetch handler to take care of the structure of fetches.
1136 fetch_handler = _FetchHandler(
-> 1137 self._graph, fetches, feed_dict_tensor, feed_handles=feed_handles)
1138
1139 # Run request and get response.

C:\HOMEWARE\Miniconda3-Windows-x86_64\envs\rdwsenv\lib\site-packages\tensorflow\python\client\session.py in __init__(self, graph, fetches, feeds, feed_handles)
469 """
470 with graph.as_default():
--> 471 self._fetch_mapper = _FetchMapper.for_fetch(fetches)
472 self._fetches = []
473 self._targets = []

C:\HOMEWARE\Miniconda3-Windows-x86_64\envs\rdwsenv\lib\site-packages\tensorflow\python\client\session.py in for_fetch(fetch)
256 if fetch is None:
257 raise TypeError('Fetch argument %r has invalid type %r' % (fetch,
--> 258 type(fetch)))
259 elif isinstance(fetch, (list, tuple)):
260 # NOTE(touts): This is also the code path for namedtuples.

TypeError: Fetch argument None has invalid type <class 'NoneType'>

我觉得某些 Tensorflow 函数存在错误导致我出错,但我不确定。最后,我感兴趣的是得到 tensor包含我的网络 w.r.t 到输入的输出的 jacobian。我如何使用其他工具或更正我的代码来实现这一点?

编辑:好的,所以我考虑了 danyfang 的评论,并试图调查他在 Github 上提出的关于 tf.gradients 的问题。返回 None而不是 0由于低级 Tensorflow 中的一些实现设计。

因此,我尝试创建一个简单的案例,我确信梯度不同于 0 , 通过计算 tf.matmul(tf.transpose(x), x) .我在 MWE 下方发布。

import tensorflow as tf

tf.reset_default_graph()
x = tf.constant([[1., 2., 3.]])
with tf.GradientTape(persistent=True) as gg:
gg.watch(x)
y = tf.matmul(x, tf.transpose(x))
f1 = tf.map_fn(lambda a: a, y)

# Computes gradients
d_fx1 = gg.gradient(f1, x)
d_yx = gg.gradient(y, x)
del gg #delete persistent GradientTape

with tf.Session() as sess:
#d1 = sess.run(d_fx1) # Same error None type
d2 = sess.run(d_yx) #Works flawlessly. returns array([[2., 4., 6.]], dtype=float32)
d2

这表明(至少在我看来)错误的出现不是因为此 issue 报告的行为,但另一件事是由于较低级别的实现。

最佳答案

编辑:下面,我报告了我如何计算输出的 tf.hessians w.r.t 输入。

我使用函数 tf.gradients 成功计算了梯度。但是,根据文档,此函数使用符号推导,而 GradientTape.gradient 使用自动微分。我正在阅读的论文中,他们谈论自动微分,所以我不知道以后是否会遇到一些问题,但至少我的代码可以运行。

下面,我发布了一个 MWE,其中包含我已经使用过的 RNN 代码。

import tensorflow as tf
from tensorflow.keras.layers import RNN, GRUCell, Dense

# Define size of variable. TODO: adapt to data
inp_dim = 2
num_units = 50
batch_size = 100
timesteps = 10

# Reset the graph, so as to avoid errors
tf.reset_default_graph()

inputs = tf.ones(shape=(timesteps, batch_size, inp_dim))

### Building the model
cells = [GRUCell(num_units), GRUCell(num_units)]
rnn = RNN(cells, time_major=True, return_sequences=True)
final_layer = Dense(1, input_shape=(num_units,))

# Apply to inputs
last_state = rnn(inputs)
f = final_layer(last_state)

[derivs] = tf.gradients(f, inputs)

with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
grads = sess.run(derivs)

只是警告任何想要计算二阶导数的感兴趣的旁观者,不支持使用tf.gradients(tf.gradients(func, vars))。还有一个叫做tf.hessian的函数,但是在上面的代码中用tf.hessian替换tf.gradients没有起作用,导致错误太长了,我不会在这里包括它。我很可能会在 Github 上做一个问题,我会在此处为任何感兴趣的人提供链接。目前,当我遇到一个不令人满意的解决方法时,我会将我自己的响应标记为解决我的问题。

计算二阶导数

查看此 issue在 Github 上。

关于python - TypeError 使用 GradientTape.gradient 计算梯度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55904359/

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