AttributeError: keras.backend ,no attribute 'eager' - python

I'm using python 3.8.2 , keras 2.3.1 and tensorflow 2.2.0rc4 .
just with the following code :
import keras
from keras.models import sequential
I have this error :
AttributeError: partially initialized module 'keras.backend' has no attribute 'eager' (most likely due to a circular import)
if I use :
import tensorflow
or
from tensorflow.keras import ....
new error :
AttributeError: partially initialized module 'tensorflow.python.framework.ops' has no attribute 'register_tensor_conversion_function' (most likely due to a circular import)
full traceback:
enter image description here
enter image description here

My suggestion is to reinstall the package. Sometimes this happens due to the installation problem.
Use the following code to do so
Uninstall tensorflow
pip uninstall tensorflow
Requires the latest pip
pip install --upgrade pip
To install keras as separate package
pip install Keras
Current stable release for CPU and GPU
pip install tensorflow
Try this and hope this helps you.

It's an install problem, most likely; K.eager was introduced in Keras 2.3.0 (and is included in Keras 2.3.1), so your Python interpreter is somehow reading code of 2.2.5 or earlier.
A possible culprit is an Anaconda mishap. First run conda uninstall keras. Then, in the anaconda3 directory, search "keras" and delete all results. Lastly, run conda install -c conda-forge keras, which should download version 2.3.1. You might need to run similar steps with TensorFlow (in fact, it's better you do, and first reinstall TensorFlow then Keras).
P.S., your code is probably from keras import Sequential, as sequential should error differently.

Related

ModuleNotFoundError: No module named 'tensorflow.python.trackable'

The statement is:
from tensorflow.python.trackable import base as trackable
But the module not found error is coming up.
##### File "C:\Users\mahit\anaconda3\lib\site-packages\keras\dtensor\lazy_variable.py", line 26, in <module>
from tensorflow.python.trackable import base as trackable
ModuleNotFoundError: No module named 'tensorflow.python.trackable'
The piece of code is inside keras library. I dont understand why this module not found error is coming up. Becase this is a code that came with the package. Also not sure what trackable does. Please help me resolve this.
The solution for me was to install keras in version 2.9.0
pip install keras==2.9.0
I got rid of this error: uninstall tensorflow and install tf-nightly. Nevertheless, I got another error: Can't find libdevice directory ${CUDA_DIR}/nvvm/libdevice
In python code, with Linux OS, that error could be solved by using command: os.environ['XLA_FLAGS'] = '--xla_gpu_cuda_data_dir=/usr/lib/cuda/'
I had the same problem for the past one month and I was not able to resolve it. Then finally I just uninstalled tensorflow and installed tf-nightly with immediately resolved the issue.
The issue occurs as tensorflow recently moved the keras out of the default package and now is only available through the tf-nightly install.
Hope this resolves the issue
I have the same issue and have been check the current dir of tensorflow, the trackable folder does not exist.
So here is an easy way.
Git clone from the url: https://github.com/tensorflow/tensorflow
Find the tensorflow -> python -> trackable
Copy the trackable folder to your tensorflow install dir (For example: C:\Users\xxxx\anaconda3\Lib\site-packages\tensorflow\python).
Uninstall tensorflow using pip uninstall tensorflow and install pip install tf-nightly
This fix worked for me perfectly well.
I just got this error in CI builds with a requirements.txt that had
keras
tensorflow==2.9.1
and I assume the error was caused by pip install -r requirements.txt taking the recently released Keras 2.10 version, which probably assumes it's running next to TensorFlow 2.10, and that the minor mismatch led to this import error.

ModuleNotFoundError: No module named 'tensorflow.python.checkpoint'

this is the code. im executing this using spyder. and tried many versions of tensorflow but unable to solve this
this is the error im getting. before i was gettng import error cannot import 'load_img'
I had a similar problem. I had tensorflow 2.6.0 installed but tensorflow-estimator 2.10.0. The problem was solved by downgrading tensorflow-estimator:
pip install --upgrade tensorflow-estimator==2.6.0
Download tensorflow before running your code.
pip install tensorflow

TFBertForSequenceClassification requires the TensorFlow library but it was not found in your environment

this:
import tensorflow as tf
from transformers import BertTokenizer, TFBertForSequenceClassification
model = TFBertForSequenceClassification.from_pretrained("bert-base-uncased")
Outputs the following error:
ImportError:
TFBertForSequenceClassification requires the TensorFlow library but it was not found in your environment. Checkout the instructions on the
installation page: https://www.tensorflow.org/install and follow the ones that match your environment.
However it's not true I don't have the TensorFlow library imported.
> print(tf.__version__)
'2.7.0'
As this is to answer the question when Tensorflow is indeed installed and imported this can often be a problem in this case.
BERT requires the tensorflow-gpu package so you need to have installed the tensorflow-gpu package to do what you are attempting here.
IF you are in a notebook run:
!pip install tensorflow-gpu
Otherwise on the command line run:
pip install tensorflow-gpu
Hope this is helpful to others out there facing issues!
import tensorflow as tf
from transformers import BertTokenizer, TFBertForSequenceClassification
Worked with
Tensorflow-2.7.0
huggingface-hub-0.2.1
tokenizers-0.10.3
transformers-4.15.0
Python-3.7
Install Tensorflow and Transformers
!pip install tensorflow
!pip install transformers
I believe you are on MacOS. If so, installing the latest version of transformers (v4.21.1) should fix this issue. (I am also running M1 pro, if that helps).
For me the latest update was giving an error with incompatible checkpoint weight files, so I had to install transformers v4.2.2 which gave me the above error.
To fix it, you need to do an editable install: https://huggingface.co/docs/transformers/installation#installing-from-source
i.e.
git clone https://github.com/huggingface/transformers.git
cd transformers
pip install -e .
Before running the above code, make the following changes:
In transformers/setup.py Line 134, change to "tokenizers==0.6.0"
In transformers/src/transformers/file_utils.py Line 87, change "tensorflow" to "tensorflow-macos"
Finally, in transformers/src/transformers/dependency_versions_check.py comment out Line 41 ("# require_version_core(deps[pkg])"
For the final step, upgrade the tokenizers library to a newer version (say 0.12.1). You cant have the newer tokenizers library before installing the transformers package - it fails to build the wheel.
Reference: https://id2thomas.medium.com/apple-silicon-experiment-1-installing-huggingface-transformers-2e45392d3d0f
https://github.com/apple/tensorflow_macos/issues/144
Transformers requires Tensorflow version >= 2.3
To see the installed Tensorflow version, run the python script below:
import tensorflow as tf
print(tf.__version__)
If tensorflow version is actually too low, you may upgrade it.
For Tensorflow of CPU version,
run the command in a notebook,
!pip install tensorflow>=2.3
or run the command in a command window:
pip install tensorflow>=2.3
For Tensorflow of GPU version,
run the command in a notebook,
!pip install tensorflow-gpu>=2.3
or run the command in a command window,
pip install tensorflow-gpu>=2.3

How to install module 'keras'?

I am following the article https://www.analyticsvidhya.com/blog/2018/10/predicting-stock-price-machine-learningnd-deep-learning-techniques-python/ . After copy and paste the following code:
#importing required libraries
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM
#creating dataframe
data = df.sort_index(ascending=True, axis=0)
new_data = pd.DataFrame(index=range(0,len(df)),columns=['Date', 'Close'])
for i in range(0,len(data)):
new_data['Date'][i] = data['Date'][i]
new_data['Close'][i] = data['Close'][i]
...
I get an error: "No module named 'keras'. I search online and find I need to install keras module. In the article https://github.com/antoniosehk/keras-tensorflow-windows-installation , there is a step-by-step guide on how to install keras. However, it requires Nvida graphic card for GPU while I only have an integrated Intel graphic card. Therefore, I try to find another link at https://software.intel.com/en-us/articles/intel-optimization-for-tensorflow-installation-guide that works for Intel graphic card, so I install tensorflow as follows:
conda install tensorflow-mkl
During the installation, I can see one package is called keras-xxx, so I belive keras is also installed. But after the installation, when I try to run the code, I still get the error. Why?
Thanks
If you have CUDA supports GPU (NVIDEA Series), I strongly recommend you to install tensorflow-gpu from the aconda forge by conda command (not by pip) because the tensorflow-gpu needs a lot of requirments such as CUDA, CuDNN and etc that caused too hard to configurate all together correctly. if use the conda all the packages install together easily (without any tears!!!)
conda install -c anaconda tensorflow-gpu
but if you want to use cpu, you can easily install tensorflow by the pip or conda

Python module in tensorflow anaconda installation

I am working in anaconda environment
I used conda install tensorflow to install tensorflow
In the code I :
import tensorflow as tf
from tensorflow.python.data import Dataset
The first command passes but the second command does not (Import error)
I looked In the github of tensorflow and saw that the tensorflow.python.data module exists but probably changed
Is there a way to get Dataset class from the anaconda installation?
If not is there a workaround ?
Ilan
My mistake was that i used :
conda install tensorflow
rather than follow the instructions in the tensorflow site
Ilan

Categories