What is the difference between tf.keras and tf.python.keras?

前端 未结 1 1222
情歌与酒
情歌与酒 2020-11-27 08:23

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
  • S
相关标签:
1条回答
  • 2020-11-27 09:06

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

    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.

    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. Importing from tensorflow.python or any other modules (including import tensorflow_core) is not supported, and can break unannounced.

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

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


    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.

    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.

    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
    

    Though both share the python/ folder, they're not both tf.python - can be verified from their respective __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). I continue to see former in user code - consider this a note of warning.

    0 讨论(0)
提交回复
热议问题