'Error importing tensorflow "AlreadyExistsError: Another metric with the same name already exists."
I am running this simple code on Spyder 3.3 with Python 3.7 and Tensorlow 2.0:
import tensorflow as tf
print(tf.__version__)
When I try to run it again in the same IPython console, I get the following error:
File "/home/rodrigo/.local/lib/python3.7/site-packages/tensorflow_core/python/eager/monitoring.py", line 121, in init self._metric = self._metric_methods[self._label_length].create(*args)
AlreadyExistsError: Another metric with the same name already exists.
If I close the IPython console, and then open it again, it works fine. I am getting this error in every code that imports Tensorflow. Does anyone know how to solve this?
System configuration:
- Ubuntu 19.04
- Spyder 3.3.2
- Python 3.7.3
- IPython 5.8.0
- TensorFlow 2.0.0-rc2
Solution 1:[1]
Tensorflow constructs singletons as side effects during import. Importing twice results in the singletons being created again, which is not supported. Please never import twice.
Solution 2:[2]
TL;DR: Ensure the Keras version matches the Tensorflow version
I am experiencing the same thing with:
- Windows
- Python3.8
- Tensorflow-2.6.1
The core issue appears to be that there are two Keras packages installed:
<site-packages>/keras
<site-packages/tensorflow/python/keras
If you look at the release notes for 2.6: https://github.com/tensorflow/tensorflow/releases/tag/v2.6.0
Keras been split into a separate PIP package (keras), and its code has been moved to the GitHub repositorykeras-team/keras. The API endpoints for tf.keras stay unchanged, but are now backed by the keras PIP package. The existing code in tensorflow/python/keras is a staled copy and will be removed in future release (2.7). Please remove any imports to tensorflow.python.keras and replace them with public tf.keras API instead.
For some reason, it is still importing from both packages which is triggering the valid exception (only one Keras instance should be imported)
Digging a bit further, it looks like Keras-2.7 was being installed, reverting to Keras-2.6 resolved the issue:
pip install keras==2.6.*
For some reason: https://github.com/tensorflow/tensorflow/blob/v2.6.1/tensorflow/tools/pip_package/setup.py#L106
Is not working, maybe a bug in PIP?
Solution 3:[3]
After some query, the issue should be related to the Keras package version of the
problem. One solution, that I do not know why yet, but verified, is as follows:
1 --> uninstall the existing Keras package (the blogger is currently wrong version 2.7) by:
pip uninstall keras
2 --> install a specific version of the Keras package; I have installed version 2.6.0 with:
pip install keras==2.6.0
Solution 4:[4]
Both Tensorflow and Keras need to have the same version. The following solved my problem:
pip uninstall keras
pip uninstall Tensorflow
and then:
pip install tensorflow==2.6
pip install keras==2.6
Solution 5:[5]
I also experienced this error after mixing the installation of tensorflow dependencies with both pip and conda. I was unable to fix by uninstalling/reinstalling tensorflow via either pip or conda.
My fix was installing tensorflow (v2.6.0) in a new virtual environment via the latest version of pip.
Solution 6:[6]
Solved!- I had same issue, and what I did is to delete my virtual environment and create new one, reinstall required libraries!
Solution 7:[7]
Solved; I tried in MAC OS
- You have to uninstall the tenserflow from activated virtual env "pip uninstall tensorflow"
- Then install lower version of tenserflow (recommended: prior to 2.7) "pip install tensorflow 2.5"
- Boom it's working.
Solution 8:[8]
I have solved the issue by updating tensorflow from 2.6.0 to 2.7.0
Solution 9:[9]
By now you can upgrade to Tensorflow 2.7.0, alternatively, if you don't plan to use keras on its own (tensorflow.keras is actually a rather large repository so you can likely get by without keras) you can do "pip uninstall keras" as the issue comes from the program seeing two versions of keras (tf.keras and keras) that both have the accompanying method, i.e. from tensorflow.keras import Sequential and from keras import Sequential.
Tensorflow will naturally install Keras with it so if you update Keras to 2.6.0 you'll have to upgrade again to 2.7.0 (which I'm using and isn't presenting this issue) or uninstall Keras again.
Hope this helps :)
Solution 10:[10]
I just had the same error (AlreadyExistsError: Another metric with the same name already exists.
).
I am using Anaconda with pip as package manager and I did not want to remove the entire anaconda environment as suggested in other answers.
I saw that the versions of my TensorFlow-related packages did not match via pip freeze | grep tensor
and pip freeze | grep keras
.
As a solution, I uninstalled all TensorFlow related packages (e.g. pip uninstall tensorflow
) and reinstalled them (e.g. pip install tensorflow
).
These steps solved the issue for me.
Solution 11:[11]
I encountered the same error while importing tensorflow v2.6 on jetson AGX xavier. It was the issue with the jetpack release not compatible with the version. Thus was resolved by downgrading the tensorflow to v2.5. $ sudo pip3 install --pre --extra-index-url https://developer.download.nvidia.com/compute/redist/jp/v46 tensorflow==2.5.0+nv21.8
Solution 12:[12]
I've solved this issue by removing keras from my dependencies and by using tensorflow~=2.6.0
as the tensorflow version. I'm using tf.keras
now.
Solution 13:[13]
I had the same issue with importing tensorforce. Uninstalling Tensorforce and installing the newest stable version from githup fixed the issue for me
Solution 14:[14]
In my case where was keras-nightly
that remained from tf-nightly
, I have used at some point, uninstall helped.
pip uninstall keras-nightly
Solution 15:[15]
I had the same error, the problem is you are using tensorflow.keras
and the keras package already installed.
So uninstall tensorflow and keras and install this version of tensorflow which include keras :
pip uninstall tensorflow
pip uninstall keras
install tensorflow again with :
pip install tensorflow==2.6.0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow