Tensorflow 中的 Variable 和 ResourceVariable 有什么区别 [英] What's the difference between Variable and ResourceVariable in Tensorflow

查看:78
本文介绍了Tensorflow 中的 Variable 和 ResourceVariable 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Tensorflow 中,Variable 是一种资源,继承自 ResourceBase 并由 ResourceMgr 管理.但是为什么还有另一个名为 ResourceVariable 的?它们都可以用于像 gradient_descent 这样的优化器(见这个 示例).有什么不同?我知道前者有据可查,并且最常用.后者的目的是什么?

In Tensorflow, Variable is a resource, inherited from ResourceBase and managed by ResourceMgr. But why is there another named ResourceVariable? Both of them can be used for optimizers like gradient_descent (see this example). What's the difference? I know the former is well documented and most often used. What's the purpose of the latter?

推荐答案

ResourceVariableVariable 的替代品,旨在清理Variable 的语义.

ResourceVariable is the replacement for Variable, that aims to clean up some of the messier aspects of the semantics of Variable.

ResourceVariable 是 TF 2.0 中的默认值,除非您正在深入研究 Tensorflow 实现的细节,否则您很可能不会关心两者之间的差异.启用急切执行时 tf.Variable 也会创建资源变量.

ResourceVariable is the default in TF 2.0 and you very likely don't care about the differences between the two unless you are working on details deep inside the Tensorflow implementation. When eager execution is enabled tf.Variable also creates resource variables.

所以现在就使用tf.Variable,这几乎肯定是你想要的;如果您遇到看起来像竞争条件或代码中变量值不一致的错误的问题,您可以尝试启用资源变量(通过将 use_resource=True 传递给您的变量创建代码或调用 tf.enable_resource_variables() 在 TF 1.x 中).

So just use tf.Variable for now, it's almost certainly what you want; if you experience issues which look like race conditions or bugs from inconsistent values of variables in code you can try enabling resource variables (by either passing use_resource=True to your variable-creating code or calling tf.enable_resource_variables() in TF 1.x).

这篇关于Tensorflow 中的 Variable 和 ResourceVariable 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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