python - 施工后坚持 GradientTape
问题描述
是否可以在构建后设置持久性GradientTape
?
用例是当我有不受我控制的代码时,我将非持久磁带传递给我,我必须使用它们之间的依赖关系获取两个渐变。
# I don't control this
with tf.GradientTape() as tape:
y = f(x)
z = g(y)
# I control this
dzdx = tape.gradient(z, x)
result = tape.gradient(z, y, output_gradients=dzdx) # not persistent
我考虑过tape._persistent
在调用之前简单设置的可能性gradient
,但是持久性在构造时一直传递给 C++ 代码,这可能不喜欢与 Python 代码不一致
def push_new_tape(persistent=False, watch_accessed_variables=True):
"""Pushes a new tape onto the tape stack."""
tape = pywrap_tfe.TFE_Py_TapeSetNew(persistent, watch_accessed_variables)
return Tape(tape)
最终以
template <typename Gradient, typename BackwardFunction, typename TapeTensor>
class GradientTape {
public:
// If `persistent` is true, GradientTape will not eagerly delete backward
// functions (and hence the tensors they keep alive). Instead, everything
// is deleted in ~GradientTape. Persistent GradientTapes are useful when
// users want to compute multiple gradients over the same tape.
explicit GradientTape(bool persistent) : persistent_(persistent) {}
~GradientTape() {
for (const auto& pair : op_tape_) {
pair.second.backward_function_deleter(pair.second.backward_function);
}
}
// ...
bool IsPersistent() const { return persistent_; }
IsPersistent
的唯一公共接口在哪里persistent_
。我不知道具体persistent_
是如何使用的,这听起来像是等待中的内存泄漏。
解决方案
推荐阅读
- android - 自动恢复在android中播放的背景音乐
- c - 在 C/C++ 中在 Raspberry Pi 中播放 WAV,必须使用 dsp?
- java - SpringMVC 表单中的两个按钮
- python - 用整数替换字典中的键
- flutter - Flutter 以编程方式关闭应用程序但从 appState
- android - 我无法转到 Webview 上的上一页,并且 OnBackPressed() 在我的 WebView Activity 中不起作用
- c# - 会话在超时前过期
- multithreading - (MSDN)使线程不断地“警报”而不阻塞线程
- javascript - 如何在完整日历上设置事件点击,以便您进入该事件的编辑页面?
- powershell - PowerShell VMWare Connect-VIServer