问题描述
tf.compat 模块的目的是什么?看起来这个模块中只是复制了整个 Tensorflow API.文档说明
What's the purpose of tf.compat module? It looks like just the entire Tensorflow API is replicated inside this module.The documentation states
Python 2 与 3 兼容性的函数.
那么为什么会有一个v1"和一个v2"子模块?tf.compat具体解决了哪些兼容性问题?
So why there is a "v1" and a "v2" submodule? What are the compatibility problems address by tf.compat specifically?
推荐答案
tf.compat
允许您编写适用于 TensorFlow 1.x 和 2.x 的代码.比如下面这段代码:
tf.compat
allows you to write code that works both in TensorFlow 1.x and 2.x. For example, the following piece of code:
import tensorflow as tf
tf.compat.v1.disable_v2_behavior()
with tf.compat.v1.Session() as sess:
x = tf.compat.v1.placeholder(tf.float32, [2])
x2 = tf.square(x)
print(sess.run(x2, feed_dict={x: [2, 3]}))
# [4. 9.]
在 TensorFlow 1.15.0 和 2.0.0 上运行相同,即使会话和占位符在 2.x 中已弃用.同样,tf.compat.v2
允许您可以使用 1.x 中 2.x 中引入的东西.此外,这些 API 也为未来提供了向后兼容性,因此如果在某个时候发布了 3.x 版本,那么自 2.x 的第一个版本以来,编写与版本无关的代码的机制就已经存在了.
Runs the same on TensorFlow 1.15.0 and 2.0.0, even though session and placeholders were deprecated in 2.x. Likewise, tf.compat.v2
allows you to use things introduced in 2.x from 1.x. Also, these APIs provide also backwards compatibility for the future too, so if at some point a 3.x version is released, the mechanism to write version-independent code will already be there since the first version of 2.x.
实际上应该更改有关 Python 模块的文档.最初,tf.compat
只包含用于此目的的函数(直到 1.13 都是这样,查看所有模块文档).然而,它后来被重新用于 TensorFlow 版本兼容性.
The documentation for the module about Python should actually be changed. Originally, tf.compat
only held functions for that purpose (and it was like that until 1.13, see all module documentation). However, it was later repurposed for TensorFlow version compatibility.
这篇关于tf.compat 的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!