Error importing tensorflow "AlreadyExistsError: Another metric with the same name already exists."

Question:

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
Asked By: rvimieiro

||

Answers:

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.

Answered By: Mihai Maruseac

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.

Answered By: erikreed

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?

Answered By: driedler

Solved!-
I had same issue, and what I did is to delete my virtual environment and create new one, reinstall required libraries!

Answered By: Zarina Abdibaitova

Solved; I tried in MAC OS

  1. You have to uninstall the tenserflow from activated virtual env
    "pip uninstall tensorflow"
  2. Then install lower version of tenserflow (recommended: prior to 2.7)
    "pip install tensorflow 2.5"
  3. Boom it’s working.
Answered By: Prince Raj

I have solved the issue by updating tensorflow from 2.6.0 to 2.7.0

Answered By: Hasan Salim Kanmaz

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 🙂

Answered By: Alex Knapp

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.

Answered By: Domi W

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
Answered By: user2546607

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

Answered By: Kasi Viswanath

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.

Answered By: Deloo

I had the same issue with importing tensorforce.
Uninstalling Tensorforce and installing the newest stable version from githup fixed the issue for me

Answered By: masa

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
Answered By: Hamza ouanzi

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
Answered By: AliPrf

In my case where was keras-nightly that remained from tf-nightly, I have used at some point, uninstall helped.

pip uninstall keras-nightly
Answered By: Alexey Birukov

In mine I just uninstalled the keras and it worked.

pip uninstall keras
Answered By: Ravi Prakash Gupta

I uninstalled keras version 2.10.0, and installed keras 2.6 version and it resolved it.

pip uninstall keras 
pip install keras==2.6

and I uninstalled TensorFlow 2.5.0, and downloaded the latest version of TensorFlow,

pip uninstall TensorFlow
pip install TensorFlow

and it resolved the "Another metric with the same name already exists."

Answered By: Naveen
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.