Is anyone using the TensorFlow and Keras libraries?

  • Thread starter Thread starter fog37
  • Start date Start date
  • Tags Tags
    Libraries
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
8 replies · 3K views
fog37
Messages
1,566
Reaction score
108
TL;DR
understand the difference and how to use Keras and TensorFlow
Hello,
Anyone using Keras and TensorFlow? I know there is TensorFlow 1 and TensorFlow 2. I am getting familiar with these two important deep learning libraries. So far, my understanding is the Keras library must be always imported along with the TensorFlow library but I have seen some code examples where only Keras is imported and the code works just fine...
If Keras is a high level API for TensorFlow, how can we use Keras alone without importing also Tensorflow? My understanding is that Keras is the front-end while TensorFlow is the back-end which means that Keras essentially allows us to use TensorFlow methods and functionalities without directly making calls to Tensorflow (which is running under the hood). Everything is done just through Keras...

The following code snippet has only Keras imported:
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizer import SGD


On the other hand, the code below shows both keras an tensorflow being imported in the dependencies:
import tensorflow as tf
import keras

from keras.models import Sequential
from keras.layers import Dense, Flatten, Activation, Dropout


Then I also saw the following code examples:
from tensorflow import keras as ksfrom tensorflow.keras import backend as K
 
Physics news on Phys.org
Briefly, Keras exists for historical reasons as a separate package from TensorFlow in PIP, however for current usage (about 12 months now?) you should be using the version of Keras bundled with TensorFlow e.g.
Python:
from tensorflow import keras
 
  • Like
Likes   Reactions: fog37
I see. Thanks.

I have seen the commands below which I think are the same as what you suggested, i.e.
from tensorflow import keras:

import tensorflow as tf
model = tf.keras.Sequential()


We import tensorflow and Keras is a module already part of it so we don't need to write import Keras.

Is this new usage for the newest version of TensorFlow, i.e. Tensorflow 2, and the newest Keras?

My code works just fine with only the command import Keras (I thnk I installed TensorFlow at one point) without import tensorflow as tf. Why?
 
fog37 said:
I have seen the commands below which I think are the same as what you suggested, i.e.
from tensorflow import keras:

import tensorflow as tf
model = tf.keras.Sequential()
Yes, this achieves the same result as
Python:
from tensorflow import keras
model = keras.Sequential()

fog37 said:
Is this new usage for the newest version of TensorFlow, i.e. Tensorflow 2, and the newest Keras?
Yes this is for TensorFlow 2.

fog37 said:
My code works just fine with only the command import Keras (I thnk I installed TensorFlow at one point) without import tensorflow as tf. Why?
Do not do this if you are using TensorFlow 2. It works because, as I said above, Keras still exists as an independent package in PIP (the Python package manager). At the moment, this independent package and the one bundled with TensorFlow 2 work interchangeably (with TensorFlow) but this is not guaranteed to be the case in future. To ensure your code continues to work therefore, use the version of Keras bundled with TensorFlow 2 as described here and in the TensorFlow (and Keras) documentation.

There is some more background in this blog post: https://www.pyimagesearch.com/2019/10/21/keras-vs-tf-keras-whats-the-difference-in-tensorflow-2-0/
 
  • Like
Likes   Reactions: fog37 and lomidrevo
pbuk said:
...It works because, as I said above, Keras still exists as an independent package in PIP (the Python package manager)...

Thank you! I see how Keras is an indepedent package and an API for TensorFlow. But, in one of the code snippets examples, the old Keras package is imported alone without importing TensorFlow...How can that work if Keras must rely on TensorFlow?
 
pbuk said:
Keras imports TensorFlow itself.

That I didn't know at all and would have not assumed.
In python, are there other libraries that, when imported, automatically import other libraries? Do you have any example? I know Keras can support Theanos and there is a Keras for programming languages other than Python...

pbuk, it looks like you are experienced with deep learning and CNN. Do you work with them?
 
fog37 said:
In python, are there other libraries that, when imported, automatically import other libraries? Do you have any example?
Yes lots of packages have dependencies, for example here they are for requests and flask. Feel free to search for more information about dependencies in Python.

fog37 said:
I know Keras can support Theanos
I don't think this is supported in the current version.

fog37 said:
and there is a Keras for programming languages other than Python...
No, Keras is specific to Python. TensorFlow exists outside Python though.

fog37 said:
pbuk, it looks like you are experienced with deep learning and CNN. Do you work with them?
No, I just know enough to know that I don't need to know any more, if you know what I mean :smile:
 
pbuk said:
No, I just know enough to know that I don't need to know any more, if you know what I mean :smile:
I should have known better to qualify that statement: I just know enough to know that I don't need to know any more at the moment.

Perhaps Fate has decided to punish me for my omission as it seems that that moment has already passed!
 
  • Like
Likes   Reactions: fog37