1

I found there are 3 types of keras when I want to import keras: "keras", "tensorflow.keras" and "tensorflow.python.keras".

I always use 'import keras' directly. But I want to know if I made some modifications on the tf.gradients, would my keras model be affected? For example, I want to use a package named 'gradient-checkpointing' which need to replace the default tf_gradients by customized gradients:

from tensorflow.python.ops import gradients as tf_gradients tf_gradients.gradients = gc.gradients_speed

But If I use 'import keras' to build model, would my keras-based model call the new gc.gradients_speed??

Update: I know 'tf.keras' is recommended for normal user. I also know that 'tf.python.keras' is private and is just used by developmenters. But here I want to use my own customerzed gradients strategy which requires 'tf.python.ops.gradients'. So my question is: if I use modified 'tf.python.ops.gradients', which 'keras' would be affected? 'tf.keras'? or 'keras' ? or 'tf.python.keras'? (I hope to choose one keras which calls 'tf.python.ops.gradients' so that I can verify if my customerzed gradient actually works.

Jingnan Jia
  • 1,108
  • 2
  • 12
  • 28
  • See [here](https://stackoverflow.com/questions/58279628/what-is-the-difference-between-tf-keras-and-tf-python-keras); to complete, `keras` uses the currently backend-neutral [keras](https://github.com/keras-team/keras/) implementation, which differs significantly from the pure-tensorflow `tf.keras` implementation. There are also important [performance differences](https://stackoverflow.com/questions/58441514/why-is-tensorflow-2-much-slower-than-tensorflow-1/58653632#58653632). For best results w/ `tf.` features, use `tf.keras`. – OverLordGoldDragon Dec 30 '19 at 03:14
  • @OverLordGoldDragon Thanks for your answer, I know tf.keras is recommended for normal user. But here I want to use my own customerzed gradients strategy which requires 'tf.python.ops.gradients'. So my question is: if I use modified 'tf.python.ops.gradients', which 'keras' would be affected? 'tf.keras'? or 'keras' ? or 'tf.python.keras'? (I hope to choose one keras which calls 'tf.python.ops.gradients' so that I can verify if my customerzed gradient works. – Jingnan Jia Dec 30 '19 at 10:51
  • I suggest reading the first link in full; basically, it's fine to use `tf.python` to develop custom functionality exactly as you're describing, but _not_ for "out-of-box" usage (e.g. `from tensorflow.python.keras.layers import Dense`). To be sure, check if whatever you're importing has an existing `tf.keras` or `keras` alternative - if it does, use that alt instead. The rest is a matter of compatibility - in general, `tf.python.keras` works best with `tf.keras` objects. Whichever you pick, however, ensure it's _consistent_ - i.e. don't do `from keras` and `from tensorflow.keras`. – OverLordGoldDragon Dec 30 '19 at 13:08
  • I just re-read your comment question: "if I use modified ..." - the answer is, only `tf.keras` will be affected; if you use a text editor to search the `keras` package, you'll find 0 matches for `tensorflow.python.keras` in any .py file. P.S. I recommend [Notepad++](https://notepad-plus-plus.org/), which can do this via a simple Ctrl + Shift + F. – OverLordGoldDragon Dec 30 '19 at 13:11

0 Answers0