Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name 'keras'

When running this in Jupyter notebooks (python):

import tensorflow as tf
from tensorflow import keras

I get this error:

ImportError: cannot import name 'keras'

I've tried other commands in place of the second one, such as (but not limited to)

from tensorflow.keras import layers

But it always returns some error. I'm using the online version of Jupyter, and running print(tf.VERSION) returns 1.1.0. I'm not sure if the problem is just that I have the wrong version, or if it's something else. How do I fix this?

like image 600
Ronan Venkat Avatar asked Feb 23 '19 23:02

Ronan Venkat


People also ask

Why can’t I import name ‘Adam’ from ‘keras’?

importerror: cannot import name ‘adam’ from ‘keras.optimizers’ error occurs because of using keras import in the place of tensorflow.keras imports . See, Since Keras is one of the most popular High level deep learning libraries. Later on TensorFlow 2.0 version, Google Tensorflow Team has released an encapsulated keras library.

How to solve cannot import name ‘pad_sequences’ from ‘keras_preprocessing’ error?

To Solve cannot import name ‘pad_sequences’ from ‘keras.preprocessing.sequence’ Error You just need to import pad_sequences in this way: from keras_preprocessing.sequence import pad_sequences And now, your error Will be gone. cannot import name ‘pad_sequences’ from ‘keras.preprocessing.sequence’

What is adadam optimizer in keras?

Adam is a stochastic gradient descent optimizer that works on adaptive estimations. Gradient descent is useful in adjusting the weights in hidden layers. it is computationally more complex to Adam optimizer. Q-2 How do I import Keras optimizer? However, Here we can configure optimizer_name as per the scenario.

Should I install keras or TensorFlow?

In that case, you didn't recommend install keras, just install tensorflow 2.5.0. Sorry, something went wrong. I'm getting this now. Sorry, something went wrong. I think you resolve the setting problem for your own device. because, depend on device, you can install the possible version. Sorry, something went wrong.


2 Answers

I think you are using old version tensorflow Try to update it like

! pip install tensorflow --upgrade
like image 186
Omer Tekbiyik Avatar answered Oct 19 '22 16:10

Omer Tekbiyik


You have an old version of Tensorflow; to access Keras from Tensorflow 1.1, you should use

import tensorflow.contrib.keras as keras

For Sequential, use

from tensorflow.contrib.keras.python.keras.models import Sequential
model = Sequential()
like image 3
desertnaut Avatar answered Oct 19 '22 17:10

desertnaut