Another metric with the same name already exists

Viewed 7409

I have problem with this code:

from tensorflow.keras.models import Sequential

and got this errors:

Traceback (most recent call last):
  File "K:\test.py", line 1, in <module>
    from tensorflow.keras.models import Sequential
  File "G:\Program Files\Python37\lib\site-packages\keras\api\_v2\keras\__init__.py", line 10, in <module>
    from keras import __version__
  File "G:\Program Files\Python37\lib\site-packages\keras\__init__.py", line 25, in <module>
    from keras import models
  File "G:\Program Files\Python37\lib\site-packages\keras\models.py", line 20, in <module>
    from keras import metrics as metrics_module
  File "G:\Program Files\Python37\lib\site-packages\keras\metrics.py", line 26, in <module>
    from keras import activations
  File "G:\Program Files\Python37\lib\site-packages\keras\activations.py", line 20, in <module>
    from keras.layers import advanced_activations
  File "G:\Program Files\Python37\lib\site-packages\keras\layers\__init__.py", line 23, in <module>
    from keras.engine.input_layer import Input
  File "G:\Program Files\Python37\lib\site-packages\keras\engine\input_layer.py", line 21, in <module>
    from keras.engine import base_layer
  File "G:\Program Files\Python37\lib\site-packages\keras\engine\base_layer.py", line 43, in <module>
    from keras.mixed_precision import loss_scale_optimizer
  File "G:\Program Files\Python37\lib\site-packages\keras\mixed_precision\loss_scale_optimizer.py", line 18, in <module>
    from keras import optimizers
  File "G:\Program Files\Python37\lib\site-packages\keras\optimizers.py", line 26, in <module>
    from keras.optimizer_v2 import adadelta as adadelta_v2
  File "G:\Program Files\Python37\lib\site-packages\keras\optimizer_v2\adadelta.py", line 22, in <module>
    from keras.optimizer_v2 import optimizer_v2
  File "G:\Program Files\Python37\lib\site-packages\keras\optimizer_v2\optimizer_v2.py", line 37, in <module>
    "/tensorflow/api/keras/optimizers", "keras optimizer usage", "method")
  File "G:\Program Files\Python37\lib\site-packages\tensorflow\python\eager\monitoring.py", line 361, in __init__
    len(labels), name, description, *labels)
  File "G:\Program Files\Python37\lib\site-packages\tensorflow\python\eager\monitoring.py", line 135, in __init__
    self._metric = self._metric_methods[self._label_length].create(*args)
tensorflow.python.framework.errors_impl.AlreadyExistsError: Another metric with the same name already exists

I have seen this post and didn't get the meaning of

never import twice

2 Answers

you can fix the problem by downgrading Keras version to 2.6.0, Even you only installed TensorFlow, it installs Keras==2.7.1 as a partial dependency. Simply, this version difference causes the error.
In IPython Kernel or In an active environment,
pip install keras==2.6.0 In a deployment environment, like docker,
add keras==2.6.0 line at the last line in the requirements.txt.

Follow these three steps below:

  1. pip uninstall tensorflow

  2. delete all keras related files in PC directory: \Anaconda3\Lib\site-packages

  3. reinstall tensorflow

This has solved my problem.

Related