Is anyone using the TensorFlow and Keras libraries?

  • Thread starter Thread starter fog37
  • Start date Start date
  • Tags Tags
    Libraries
Click For Summary

Discussion Overview

The discussion revolves around the usage of Keras and TensorFlow libraries in deep learning, specifically addressing the relationship between the two, their import mechanisms, and historical context. Participants explore how Keras can function independently of TensorFlow and the implications of using different versions of these libraries.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • Some participants note that Keras is a high-level API for TensorFlow, suggesting that it typically requires TensorFlow to function properly.
  • Others point out that Keras exists as a separate package in PIP, which can work independently of TensorFlow, leading to questions about its usage.
  • A participant mentions that their code works with only Keras imported, raising questions about the necessity of importing TensorFlow as well.
  • There is a suggestion that Keras imports TensorFlow internally, which some participants were unaware of.
  • Discussion includes the historical context of Keras and its current integration with TensorFlow, particularly in TensorFlow 2.
  • Some participants express uncertainty about the future compatibility of the standalone Keras package with TensorFlow.
  • There is a mention of Keras's support for Theano, but a participant questions its current relevance.
  • One participant inquires about other Python libraries that automatically import dependencies, leading to a broader discussion about package dependencies.

Areas of Agreement / Disagreement

Participants generally agree on the historical context of Keras and its current integration with TensorFlow, but there are differing views on the necessity of importing TensorFlow when using Keras. The discussion remains unresolved regarding the implications of using Keras independently and the future of its compatibility with TensorFlow.

Contextual Notes

Limitations include the potential for confusion regarding the independent functionality of Keras and its reliance on TensorFlow, as well as the evolving nature of library dependencies in Python.

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
 
Technology 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?
 
Keras imports TensorFlow itself.
 
  • Like
Likes   Reactions: fog37
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

Similar threads

  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
8K
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
1
Views
2K