Is anyone using the TensorFlow and Keras libraries?

  • Thread starter fog37
  • Start date
  • Tags
    Libraries
In summary, if only Keras is imported, the code still works just fine. However, if both Keras and TensorFlow are imported, the code will work better.
  • #1
fog37
1,568
108
TL;DR Summary
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
 
Technology news on Phys.org
  • #2
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 fog37
  • #3
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?
 
  • #4
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 fog37 and lomidrevo
  • #5
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?
 
  • #6
Keras imports TensorFlow itself.
 
  • Like
Likes fog37
  • #7
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?
 
  • #8
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:
 
  • #9
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 fog37

1. What is TensorFlow and Keras?

TensorFlow and Keras are popular open-source libraries used for building and training machine learning models. TensorFlow was developed by Google and is used for high-performance numerical computation, while Keras is a high-level API that simplifies the process of building and training deep learning models.

2. How are TensorFlow and Keras different?

TensorFlow is a low-level library that provides a wide range of functionalities for building and training machine learning models. Keras, on the other hand, is a high-level API that simplifies the process of building and training deep learning models. Keras uses TensorFlow as its backend, so it is possible to use both libraries together.

3. What can I use TensorFlow and Keras for?

TensorFlow and Keras can be used for a variety of tasks, including image classification, natural language processing, and generative modeling. They are commonly used in research and production environments for building and training machine learning and deep learning models.

4. Are TensorFlow and Keras beginner-friendly?

While TensorFlow and Keras can be intimidating for beginners, there are plenty of resources and tutorials available to help users get started. The official documentation for both libraries is well-maintained and includes step-by-step guides and examples to help beginners learn how to use them.

5. Are there any alternatives to TensorFlow and Keras?

Yes, there are other libraries and frameworks available for building and training machine learning models, such as PyTorch, scikit-learn, and Caffe. However, TensorFlow and Keras are widely used and have a large community of users, making them a popular choice for many data scientists and machine learning practitioners.

Similar threads

  • Programming and Computer Science
Replies
9
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
792
  • Programming and Computer Science
Replies
7
Views
6K
Replies
4
Views
2K
  • Programming and Computer Science
Replies
15
Views
5K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
315
Back
Top