[英]What is the difference between tf.keras and tf.python.keras?
I've ran into serious incompatibility problems for the same code ran with one vs. the other;对于一个与另一个运行的相同代码,我遇到了严重的不兼容问题; eg:例如:
Looking into the Github source , the modules and their imports look fairly identical, and tf.keras
even imports from tf.python.keras
.查看Github 源代码,模块及其导入看起来相当相同,并且tf.keras
甚至从tf.python.keras
In tutorials, I see both being used time to time.在教程中,我看到两者都被不时使用。 As an example, code below will fail with tf.python.keras
.例如,下面的代码将因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)
ADDITIONAL INFO :附加信息:
tensorflow
, tensorflow-gpu
v2.0.0, and Keras 2.3.0 via pip, all else via Anaconda 3 tensorflow
, tensorflow-gpu
v2.0.0, and Keras 2.3.0 via pip, all else via Anaconda 3From an official TensorFlow dev , shortened (emphasis mine):来自官方TensorFlow dev ,缩短(强调我的):
The API import is in the root of the package. API 导入位于 package 的根目录中。 Any other import is just Python allowing you to access privates with no consideration for good coding practices.任何其他导入只是 Python 允许您访问私人而不考虑良好的编码实践。
The only way that imports should be are进口的唯一方法应该是
import tensorflow as tf tf.keras
We also provide support for
from tensorflow.keras import
, though this is brittle and can break as we keep refactoring.我们还提供了对from tensorflow.keras import
的支持,尽管这很脆弱,并且在我们不断重构时可能会损坏。 Importing fromtensorflow.python
or any other modules (includingimport tensorflow_core
) is not supported, and can break unannounced.不支持从tensorflow.python
或任何其他模块(包括import tensorflow_core
)导入,并且可能会突然中断。
Me: To confirm, tf.python.keras
is private , intended for development, rather than public use?我:确认一下, tf.python.keras
是私有的,用于开发,而不是公共使用?
Yes, that's exactly the case.是的,情况正是如此。 Anything under
tf.python
is privatetf.python
下的任何内容都是私有的
This, however, is not the full picture.然而,这还不是全貌。 tf.python
remains the only way to access certain functions / classes - eg, tf.python.framework
and tf.python.ops
, both used in tf.keras.optimizers
. tf.python
remains the only way to access certain functions / classes - eg, 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" - ie writing custom functionality or classes.但是如上所述,除非您正在“开发”,否则这不会成为问题 - 即编写自定义功能或类。 "Out of box" usage should be fine without ever touching tf.python
. “开箱即用”的用法应该没问题,而无需接触tf.python
。
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.keras
使用optimizer_v2 ,它与tf.python.keras
Optimizer有很大不同。
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 (eg nothing references OptimizerV2
), but it does merge with TF in tensorflow_core/python/keras/api/_v2
folder when installed locally: 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 (eg nothing references OptimizerV2
), but it does merge with TF in tensorflow_core/python/keras/api/_v2
本地安装时的tensorflow_core/python/keras/api/_v2
文件夹:
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
Though both share the python/
folder, they're not both tf.python
- can be verified from their respective __init__.py
.尽管两者都共享python/
文件夹,但它们并不都是tf.python
- 可以从它们各自的__init__.py
进行验证。
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 ). 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.我继续在用户代码中看到前者 - 将此视为警告。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.