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

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

问题描述

我遇到了严重的不兼容问题,因为同一代码相互冲突;例如:

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

  • Getting value of tensor
  • Compiling model
  • Saving optimizer

查看 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

推荐答案

来自官方

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 Github中实际上并不存在tf.keras(例如,没有引用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:\Anaconda\lib\site-packages\tensorflow_core\python\keras\api\_v2\keras\__init__.py
D:\Anaconda\lib\site-packages\tensorflow_core\python\keras\__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.

更新:与tf.python.keras.layers一起使用的tf.python.keras.optimizers与与tf.keras.layers一起使用的tf.keras.optimizers用于中型模型的运行速度慢了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天全站免登陆