keras module not found (No module named 'keras') - python

I tried to import keras to my ANN model but I found this Module not found the type of error. I tried tp import from tensorflow.keras but still it's the same problem.
No module named 'keras'

Try importing keras first. Try:
from numpy import loadtxt
import keras
from keras.models import Sequential
from keras.layers import Dense
If this doesn't work, try using TensorFlow: pip install tensorflow and:
from tensorflow.keras import Sequential

from tensorflow.keras import Sequential

!pip install keras on jupyter notebook

I think this problem is related to the environment. Your TensorFlow module may be installed in a different env. and Keras is in a different env. So, try to uninstall the Keras module and reinstall it.
$ pip uninstall keras
$ pip install keras
If this doesn't work, try using TensorFlow: pip install tensorflow and:
from tensorflow.keras import Sequential

Related

Google Colab error: Import "tensorflow.keras.models" could not be resolved(reportMissingImports)

import tensorflow as tf
tf.__version__
!sudo pip3 install keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
Error message:
Import "tensorflow.keras.models" could not be resolved(reportMissingImports)
>Import "tensorflow.keras.layers" could not be resolved(reportMissingImports)
>>Import "tensorflow.keras.preprocessing.image" could not be resolved(reportMissingImports)
this worked for me.
from tensorflow import keras
from keras.layers import Dense
from keras.models import Sequential, load_model
import tensorflow as tf
tf.__version__
!sudo pip3 install keras
from tensorflow.python.keras.engine.sequential import Sequential
from tensorflow.python.keras.layers import Dense, Conv2D, Flatten, Dropout, MaxPooling2D
image_data_generator = tf.keras.preprocessing.image.ImageDataGenerator()
i had the same error , l did those steps
step 01 : uninstall the existing tensorflow with this instruction
!pip uninstall tensorflow
step 02 : install this version i hope you find what you need in this version with this instruction
!pip install tensorflow==2.7.0
you can change the version of tensorflow if 2.7.0 not good for your code
Issue solved.
Though the error: Import "tensorflow.keras.models" could not be resolved(reportMissingImports) prompts, it doesn't affect the entire code. My Tensorflow version is 2.8.0. I just found out that the problem is on the config of my cnn model.
I solved it.
The problem was my version was too new. You just need to lower your TensorFlow version so that its suitable for your requirement.
I've lowered my version from 2.8.0 to 2.7.0 using
pip install tensorflow==2.7.0.
Check these images, the warning lines disappeared for me.
Before I downgraded my TensorFlow version
After I downgraded my TensorFlow version

ModuleNotFoundError: No module named 'tensorflow.keras.datasets' when importing tensorflow.keras.datasets

I'm receving a ModuleNotFoundError: No module named 'tensorflow.keras.datasets' error when running this code:
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
I have tensorflow version 2.2.0 installed and keras 2.2.4 installed so I'm not sure why I'm receiving this particular error.
Shoudn't the tensorflow module encapsulate datasets?
the TensorFlow documentation has this method listed as the way to load the dataset tf.keras.datasets.cifar10.load_data()

Error importing BERT: module 'tensorflow._api.v2.train' has no attribute 'Optimizer'

I tried to use bert-tensorflow in Google Colab, but I got the following error:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call
last) in ()
1 import bert
----> 2 from bert import run_classifier_with_tfhub # run_classifier
3 from bert import optimization
4 from bert import tokenization
1 frames /usr/local/lib/python3.6/dist-packages/bert/optimization.py
in ()
85
86
---> 87 class AdamWeightDecayOptimizer(tf.train.Optimizer):
88 """A basic Adam optimizer that includes "correct" L2 weight decay."""
89
AttributeError: module 'tensorflow._api.v2.train' has no attribute
'Optimizer'
Here is the code I tried:
Install the libraries:
!pip install --upgrade --force-reinstall tensorflow
!pip install --upgrade --force-reinstall tensorflow-gpu
!pip install tensorflow_hub
!pip install sentencepiece
!pip install bert-tensorflow
Run this code:
from sklearn.model_selection import train_test_split
import pandas as pd
from datetime import datetime
from tensorflow.keras import optimizers
import bert
from bert import run_classifier
from bert import optimization
from bert import tokenization
I've also tried
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
But got the same error.
I did some experimentation in my own colab notebook (please provide a link next time) and I found that in the error message, there was
class AdamWeightDecayOptimizer(tf.train.Optimizer):
this being the header of the class. But there is nothing like tf.train.optimizer instead it should be :
class AdamWeightDecayOptimizer(tf.compat.v1.train.Optimizer):
The link where there is exact issue with (lol) exact same line is here
import tensorflow as tf
print(tf.__version__)
!pip uninstall tensorflow==2.2.0
!pip install tensorflow==1.15.0
!pip install bert-tensorflow
try this. it worked for me for the same issue
I fixed the issue in google colab by installing tensorflow 1.15 instead of 2.0. I get a warning only.
!pip install tensorflow-gpu==1.15.0
This issue has been reported and discussed on Github as well,
Try to change the code of line 87 (see your error message: /usr/local/lib/python3.6/dist-packages/bert/optimization.py), from
tf.train.Optimizer
# change to
tf.keras.optimizers.Optimizer
Are you in TF 1.x or TF 2.0? In general, tf.train.Optimizer has been deprecated in TF 2.0, and you need to use tf.compat.v1.Optimizer (then the deprecation message shows up but it's a warning only). In TF 2.0, the Keras optimziers tf.keras.optimizers.* are recommended to use.

AttributeError: module 'tensorflow' has no attribute 'name_scope' with Keras

I am trying to run a script, but I struggle already at the imports.
This import
from keras.preprocessing.image import save_img
raises the following error:
AttributeError: module 'tensorflow' has no attribute 'name_scope'.
I am using the following packages.
Keras 2.2.2,
Keras-Applications 1.0.4,
Keras-Preprocessing 1.0.2,
tensorflow 1.9.0,
tensorflow-gpu 1.9.0
I was unable to reproduce with the same versions of the keras and tensorflow, reinstalling keras and tensorflow, may solve the issue, please use commands below:
pip install --upgrade pip setuptools wheel
pip install -I tensorflow
pip install -I keras
NOTE: The -I parameter stands for ignore installed package.
For everyone who use Tensorflow 2.0 and stumble upon this question with the same error, like I did: I solved it by changing the imports from keras.xxx to tensorflow.keras.xxx
I also encountered this same problem when I stopped my IDE while executing. Restarting my IDE works for me. Just save your program and restart IDE. Hope it will work for you as well.
As Andriy Ivaneyko mentioned above, reinstalling tensorflow helps. I'm not sure why, but installing tensorflow-serving-api breaks something somewhere along the way. We solved this by running:
pip install --force-reinstall tensorflow
Note that this applies to both tensorflow and tensorflow-gpu installations. Specifically, the above command will fix this problem in situations where you're specifically using tensorlfow-gpu. tensorflow-serving-api installs regular tensorflow if it's not already installed.
I encountered this same bug and reinstalling tensorflow made no difference, and this caused me some headscratching.
Eventually I noticed that my IDE autocomplete had added the following line to my code:
from tensorflow_core.python.keras.callbacks import EarlyStopping
It seems that directly referencing tensorflow_core.python will break tensorflow.
Replacing this with the normal tensorflow import solved the problem!
from tensorflow.keras.callbacks import EarlyStopping
My IDE offered me two different import paths
keras
or
tensorflow_core.python.keras
In my example I could either import like this:
from keras.layers import Dense, Dropout, LSTM, Input, Activation
from keras import optimizers, Model
or like that:
from tensorflow_core.python.keras import Input, Model, optimizers
from tensorflow_core.python.keras.layers import LSTM, Dropout, Dense
Mixing up tensorflow_core.python.keras and plain keras led to the problem in my case. After I imported everything directly from keras and keras.layers, it worked for me.
My tensorflow version is 2.1, and I found my tensorflow-estimator version is 2.2
My fix is to downgrade the estimator to the same version

TFLearn import issue - no module core_rnn

I am testing out the TFLearn library for using TensorFlow but can't import it due to some config issue. I installed it as required in TFLearn tutorial though. Thankful for any help.
>>> import tflearn as tf
> import tflearn
File "//anaconda/lib/python2.7/site-packages/tflearn/__init__.py", line 4, in <module>
from . import config
ImportError: cannot import name config
This is caused due to incompatibilitiy of TFlearn and Tensorflow. They may be of different version. Hence, to resolve the issue, uninstall tensorflow and tflearn and reinstall them using the following code -
pip uninstall tflearn
pip uninstall tensorflow
pip install -I tensorflow==0.12.1
pip install -I tflearn==0.2.1

Categories

Resources