-1

I've been trying to import keras from tensorflow using the following statement:

import tensorflow as tf
from tensorflow import keras

Tensorflow has been updated, it should work as far as I know but I still get the following message:

from tensorflow import keras
ImportError: cannot import name 'keras' from 'tensorflow'(/Users/gabork/PycharmProjects/Tester/venv/lib/python3.7/site-packages/tensorflow/__init__.py)

SOLUTION: I was using Python 3.7 which seems like recent TensorFlow versions do not support and on the other hand, older versions of TensorFlow do not support Keras. For now I’ll either downgrade Python or just import keras.

5
  • 1
    I think it should be from tf import keras Commented Sep 4, 2018 at 3:38
  • stackoverflow.com/questions/47262955/… Commented Sep 4, 2018 at 4:51
  • Which version of TensorFlow do you have installed? Commented Sep 4, 2018 at 6:07
  • @MatiasValdenegro using print(tf.__version__) I get 0.12.0 Commented Sep 4, 2018 at 11:35
  • @UpasanaMittal Using it directly does not work, using tensorflow.python or tensorflow.contrib, does not work either. Commented Sep 4, 2018 at 11:41

2 Answers 2

1

I would try import keras and see if that works.

Sign up to request clarification or add additional context in comments.

5 Comments

This seems to work but I would still be interested why the other way doesn't.
This does neither answer the question nor resolve the issue, since by doing this you import of course the stand-alone Keras, not the Tensorflow-included one (surprised that it is accepted by the OP @C.Szasz )
@desertnaut Because it turns out it was a compatibility problem with different versions, importing keras or downgrading will resolve my issue.
@C.Szasz you mean you didn't know that the option import keras was available in the first place?
@desertnaut I apologize that I’m not familiar with the hierarchy of these libraries. I was following a tutorial on TensorFlow, got an error message, tried researching it, asked a question and now I learnt something. Do you have a better solution if I may ask?
0

You are using TensorFlow 0.12.0, that is too old, keras was included in a newer version to the one you have, that's why you get an import error.

So either you update TensorFlow (highly recommended), or install keras separately.

3 Comments

Interesting. When I update in the terminal, it is version 1.10.1 but inside PyCharm when I try to update it simply says: Requirement already up-to-date: tensorflow in ./venv/lib/python3.7/site-packages (0.12.0)
@C.Szasz You simply have two TF version installed, one inside a virtualenv and another in python (without virtualenvs).
@matias-valdenegre Alright! Now I tried updating virtualenv following Tensorflow instructions and I get Could not find a version that satisfies the requirement tensorflow (from versions: ) No matching distribution found for tensorflow

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.