tf.keras 和 tf.python.keras 有什么区别? [英] What is the difference between tf.keras and tf.python.keras?

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

问题描述

我遇到了严重的不兼容问题,因为在运行相同的代码时,我遇到了严重的不兼容问题;例如:

I've ran into serious incompatibility problems for the same code ran with one vs. the other; e.g.:

查看 Github 源,模块及其导入看起来相当相同,tf.keras 甚至从 tf.python.keras 导入.在教程中,我看到两者都被不时使用.例如,下面的代码将因 tf.python.keras 而失败.

Looking into the Github source, the modules and their imports look fairly identical, and tf.keras even imports from tf.python.keras. In tutorials, I see both being used time to time. As an example, code below will fail with tf.python.keras.

怎么了?有什么区别,我什么时候应该使用其中一种?

What's the deal? What is the difference, and when should I use one or the other?

from tensorflow.keras.layers import Input, Dense
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Nadam
import numpy as np

ipt   = Input(shape=(4,))
out   = Dense(1, activation='sigmoid')(ipt)
model = Model(ipt, out)
model.compile(optimizer=Nadam(lr=1e-4), loss='binary_crossentropy')

X = np.random.randn(32,4)
Y = np.random.randint(0,2,(32,1))
model.train_on_batch(X,Y)

<小时>

附加信息:

  • CUDA 10.0.130、cuDNN 7.4.2、Python 3.7.4、Windows 10
  • tensorflowtensorflow-gpu v2.0.0 和 Keras 2.3.0 通过 pip,其他全部通过 Anaconda 3
  • CUDA 10.0.130, cuDNN 7.4.2, Python 3.7.4, Windows 10
  • tensorflow, tensorflow-gpu v2.0.0, and Keras 2.3.0 via pip, all else via Anaconda 3

推荐答案

来自官方 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.

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

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