ModuleNotFoundError: No module named 'keras' when using tensorflow 2.6 - tensorflow

I created a new conda env with
conda create --name tf tensorflow=2.6
and tried to compile
import tensorflow as tf
model = tf.keras.models.Sequential()
resulting in ModuleNotFoundError: No module named 'keras'
conda install keras
doesn't change anything.
I could go with
from tensorflow.keras.models import Sequential
model = Sequential()
but when I
pip install tensorflow-addons
and
from tensorflow_addons.seq2seq.sampler import TrainingSampler
I end up with the same error
uninstalling tensorflow, installing just keras and trying
from keras.models import Sequential
model = Sequential()
results in the same error
my versions are
tensorflow = 2.6
keras = 2.6
tensorflow-adons = 0.14

What turned out is that I had both keras and keras nightly installed, the problem got resolved after uninstalling keras-nightly. If anyone encounters this check your conda list and pip list for duplicate keras installations

Related

Trouble installing and using Tensorflow and Keras in (Anaconda) Jupyter Notebook in my MacBook Air M1

Am trying to build a Stacked LSTM model for Stock Price prediction using tensor flow and Keras. So am trying to install tensor flow and keras in my Jupyter notebook. I use a 2020 MacBook Air M1.
First I tried installing it through the Terminal through the following code
conda create -n tf tensorflow
The above code worked and the installation was successful. But when I tried to import tensor flow in my Jupyter notebook, I was not able to do so.
Then I tried to install tensor flow and keras directly into Jupyter notebook through the following codes.
pip install tensorflow
pip install keras
The installations were successful. And I was trying trying to use them to import the library.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
The minute I tried to execute this code to import the library the Kernel automatically dies as it says.
Am a noob trying to build my first LSTM Model for a project, so pardon if there are silly mistakes in the steps I tried
By using code conda create -n tf tensorflow, you have created a virtual environment 'tf' and installed tensorflow in the same 'tf' environment.
You need to 'activate' the same virtual environment 'tf' to use in jupyter:
conda activate tf
Now, Open the Jupyter notebook in the same VirEnv 'tf' and run your below code again.
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import LSTM
This could be the reason of tensorflow import was failing as you have installed tensorflow in tf VirEnv and importing it in the base environment.
And, for the second issue, Please close all the notebooks and try again running your code by opening a new jupyter notebook.

How to overcome "No module named 'keras'" while importing tensoflow_hub

I am using Anaconda and Jupyter Notebook. I need a tensorflow_hub to import the ResNet model, however, I get the error No module named Keras. I've installed keras and tensorflow_hub in Anaconda. I have no issues when I am using keras.
This "AlreadyExistsError: Another metric with the same name already exists." error shows up when you have a mismatch version of TensorFlow and Keras installed in your system.
To verify this, please run below code in jupyter notebook:
import tensorflow as tf
print(tf.__version__)
import keras
print(keras.__version__)
You can resolve this error by upgrading the TensorFlow to the latest version as this error has been resolved in the latest TF releases - 2.7, 2.8:
!pip install --upgrade tensorflow
and import Keras from TensorFlow like this:
from tensorflow import keras
Now you can install and import tensorflow_hub using the below code without any error:
!pip install tensorflow_hub
import tensorflow_hub

cannot import name 'LayerNormalization' from 'tensorflow.python.keras.layers.normalization'

I am trying to build a ANN model using Tensorflow library on Spyder. Afte ı set my training and test data, ı imported the keras library as seen below
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
But the prcocess did not finished normally.I tookthe same error-> cannnot import name 'LayerNormalization' from 'tensorflow.python.keras.layers.normalization'
I am using the following versions Spyder 4.2.5 tensorflow 2.7
Please create a new virtual environment in ananconda to install TensorFlow:
conda create -n tf tensorflow #"tf" is the name for your TensorFlow environment
conda activate tf # to activate the virtual environment
pip install tensorflow # install tensorlfow in that environment
Select the same "tf" environment in anaconda navigator and install Spyder to launch and type below code to check if TensorFlow installed properly:
import tensorflow as tf
Now please try again executing your code in this Spyder IDE.

Running Tensorflow in Anaconda3

I am trying to run Tensorflow in Anaconda Navigator. I installed Tensorflow to a new environment which I called tf. Here is my code:
import tensorflow as tf
from tensorflow.keras import layers
from tensorflow.keras.layers import Activation, Dense
The error message is unable to import tensorflow and No module named tensorflow
Follow these steps to install on Anaconda environment.
#Set Up Anaconda Environments
conda create --name tf_env python=3.7
#Activate the new Environment
source activate tf_env
#Install tensorflow
tf_env$pip install tensorflow
#Verify installation
tf_env$python
>import tensorflow as tf
>tf.__version__

Keras requires TensorFlow 2.2 or higher

I want to use keras with the code shown below:
from sklearn.model_selection import train_test_split
from keras.models import Sequential
df = DataReader('AAPL', data_source='yahoo', start='2012-01-01', end=datetime.now())
but I keep getting the error:
ImportError: Keras requires TensorFlow 2.2 or higher. Install TensorFlow via `pip install tensorflow
I have both keras 2.4.3 and tensorflow 2.2.0 installed in anaconda environment. I uninstalled and installed jupiter notebook but it didn't help.
From comments
Solved the problem by simply installing different versions of keras
(2.3.1 instead of 2.4.3) and tensorflow (2.0.0 instead of 2.2.0). Keras are integrated with Tensorflow from 2.3 onwards, you can upgrade tensorflow to latest version and use module tf.keras (paraphrased from Vivi and jakub)
To run code in TF 2.4 is as shown below
from sklearn.model_selection import train_test_split
from tensorflow.keras import Sequential
df = DataReader('AAPL', data_source='yahoo', start='2012-01-01', end=datetime.now())