且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

tf.keras 和 tf.python.keras 有什么区别?

更新时间:2023-12-02 14:22:10

来自官方 TensorFlow dev,缩短(强调我的):

From an official TensorFlow dev, shortened (emphasis mine):

API 导入位于包的根目录中.任何其他导入都只是 Python,允许您访问私有数据,而无需考虑良好的编码实践.

The API import is in the root of the package. Any other import is just Python allowing you to access privates with no consideration for good coding practices.

导入的唯一方法是

import tensorflow as tf
tf.keras

我们还提供对 from tensorflow.keras import 的支持,尽管这很脆弱,并且可能会随着我们不断重构而损坏.tensorflow.python 或任何其他模块(包括import tensorflow_core)导入不受支持,并且可能会突然中断.

We also provide support for from tensorflow.keras import, though this is brittle and can break as we keep refactoring. Importing from tensorflow.python or any other modules (including import tensorflow_core) is not supported, and can break unannounced.

我:确认一下,tf.python.keras私有,用于开发,而不是公共使用?

Me: To confirm, tf.python.keras is private, intended for development, rather than public use?

是的,确实如此.tf.python 下的任何东西都是私有的

Yes, that's exactly the case. Anything under tf.python is private

然而,这不是全貌.tf.python 仍然是访问某些函数/类的唯一方法——例如,tf.python.frameworktf.python.ops,两者都是在 tf.keras.optimizers 中使用.但如上所述,除非您正在开发"——即编写自定义功能或类,否则这不会成为一个问题.开箱即用"的用法应该没问题,无需接触 tf.python.


This, however, is not the full picture. tf.python remains the only way to access certain functions / classes - e.g., tf.python.framework and tf.python.ops, both used in tf.keras.optimizers. But as per above, this doesn't become a concern unless you're "developing" - i.e. writing custom functionality or classes. "Out of box" usage should be fine without ever touching tf.python.

请注意,这不仅是兼容性问题,而且两者不能互换,只要没有损坏";例如,tf.keras 使用 optimizer_v2,它与 tf.python.keras 优化器.

Note this isn't only a compatibility matter, and the two are not interchangeable "as long as nothing breaks"; for example, tf.keras uses optimizer_v2, which differs substantially from tf.python.keras Optimizer.

最后,请注意上面的两个链接都以 tf.python.keras 结尾——不确定,但看起来 tf.keras 实际上并不存在于TF Github(例如没有引用OptimizerV2),但是当本地安装时,它确实tensorflow_core/python/keras/api/_v2文件夹中的TF合并:

Lastly, note that both above links end up in tf.python.keras -- not certain, but it appears that tf.keras doesn't actually exist in TF Github (e.g. nothing references OptimizerV2), but it does merge with TF in tensorflow_core/python/keras/api/_v2 folder when installed locally:

from tensorflow import keras
print(keras.__file__)
from tensorflow.python import keras
print(keras.__file__)

D:Anacondalibsite-packages	ensorflow_corepythonkerasapi\_v2keras\__init__.py
D:Anacondalibsite-packages	ensorflow_corepythonkeras\__init__.py

虽然两者共享 python/ 文件夹,但它们不是都是 tf.python - 可以从各自的 进行验证__init__.py.

Though both share the python/ folder, they're not both tf.python - can be verified from their respective __init__.py.

UPDATE:tf.python.keras.optimizerstf.python.keras.layerstf.keras 一起使用.与 tf.keras.layers 一起使用的优化器 运行慢 11.5 倍,适用于中型模型 (代码).我继续在用户代码中看到前者 - 将此视为警告.

UPDATE: tf.python.keras.optimizers used with tf.python.keras.layers vs tf.keras.optimizers used with tf.keras.layers runs 11.5x slower for a mid-sized model (code). I continue to see former in user code - consider this a note of warning.