Error while importing TenssorFlow: TypeError: register_loss_scale_wrapper() takes 2 positional arguments but 3 were given

I need some help. I’m using a Jupyter Notebook in VSCode, Windows 10. I use pip module and I installed all libraries which I need. In fact, until a few weeks I didn’t have any problems.

Here is the thing: I import TensorFlow and get this

Import tensorflow as tf
TypeError: register_loss_scale_wrapper() takes 2 positional arguments but 3 were given

If I try to import Keras:

from tensorflow import keras
TypeError: register_loss_scale_wrapper() takes 2 positional arguments but 3 were given

It’s like an update had some changes on this function. I already tried reinstalling TF and Keras, but nothing changes. Hope anyone can help me.

This is the whole error:

 ---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
c:\Users\pedro\OneDrive\Documentos\PythonProjects\FreeCodeCamp\Sms-text-classifier\fcc_sms_text_classification.ipynb Cell 2' in <module>
     10 except Exception:
     11   pass
---> 12 import tensorflow as tf
     13 import pandas as pd
     14 from tensorflow import keras

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\__init__.py:473, in <module>
    471 if hasattr(_current_module, "keras"):
    472   try:
--> 473     keras._load()
    474   except ImportError:
    475     pass

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\util\lazy_loader.py:41, in LazyLoader._load(self)
     39 """Load the module and insert it into the parent's globals."""
     40 # Import the target module and insert it into the parent's namespace
---> 41 module = importlib.import_module(self.__name__)
     42 self._parent_module_globals[self._local_name] = module
     44 # Emit a warning if one was specified

File ~\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py:126, in import_module(name, package)
    124             break
    125         level += 1
--> 126 return _bootstrap._gcd_import(name[level:], package, level)

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\__init__.py:24, in <module>
     21 from tensorflow.python import tf2
     22 from keras import distribute
---> 24 from keras import models
     26 from keras.engine.input_layer import Input
     27 from keras.engine.sequential import Sequential

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\models\__init__.py:18, in <module>
     15 """Keras models API."""
     16 # pylint: disable=g-bad-import-order
---> 18 from keras.engine.functional import Functional
     19 from keras.engine.sequential import Sequential
     20 from keras.engine.training import Model

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\functional.py:24, in <module>
     22 import warnings
     23 from keras import backend
---> 24 from keras.dtensor import layout_map as layout_map_lib
     25 from keras.engine import base_layer
     26 from keras.engine import base_layer_utils

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\dtensor\layout_map.py:25, in <module>
     23 from keras.dtensor import lazy_variable
     24 from keras.dtensor import utils
---> 25 from keras.engine import base_layer
     27 # pylint: disable=missing-class-docstring
     28 
     29 # We will skip the path for certain attributes when mapping the layout, e.g.
     30 # model._self_tracked_trackables, or layer._trainable_weights/
     31 # _non_trainable_weights, etc. Those attributes are usually served as a cache,
     32 # and the actual variable should be in somewhere else.
     33 _KERAS_ATTRIBUTES_TO_SKIP = ['_self_tracked_trackables', '_trainable_weights',
     34                              '_non_trainable_weights',
     35                              '_captured_weight_regularizer']

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\engine\base_layer.py:44, in <module>
     42 from keras.engine import keras_tensor
     43 from keras.engine import node as node_module
---> 44 from keras.mixed_precision import autocast_variable
     45 from keras.mixed_precision import loss_scale_optimizer
     46 from keras.mixed_precision import policy

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\mixed_precision\__init__.py:22, in <module>
      1 # Copyright 2020 The TensorFlow Authors. All Rights Reserved.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
   (...)
     13 # limitations under the License.
     14 # ==============================================================================
     15 """Keras mixed precision API.
     16 
     17 See [the mixed precision guide](
     18   https://www.tensorflow.org/guide/keras/mixed_precision) to learn how to
     19 use the API.
     20 """
---> 22 from keras.mixed_precision.loss_scale_optimizer import LossScaleOptimizer
     23 from keras.mixed_precision.policy import global_policy
     24 from keras.mixed_precision.policy import Policy

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\keras\mixed_precision\loss_scale_optimizer.py:1341, in <module>
   1334   else:
   1335     raise ValueError(f'Invalid value passed to loss_scale. loss_scale '
   1336                      f'must be the string "dynamic" (recommended), an int, '
   1337                      f'a float, a FixedLossScale, or a DynamicLossScale. Got '
   1338                      f'value: {loss_scale}')
-> 1341 tf.__internal__.mixed_precision.register_loss_scale_wrapper(
   1342     optimizer_v2.OptimizerV2, _create_loss_scale_optimizer_from_v1_loss_scale,
   1343     LossScaleOptimizer)
   1346 def _multiply_gradient(gradient, scale):
   1347   """Multiply a (possibly sparse) gradient by the given scale factor."""

TypeError: register_loss_scale_wrapper() takes 2 positional arguments but 3 were given

Can you tell us what version of Tensorflow is in place? Can you try uninstalling and reinstalling any TensorFlow dependencies? Thank you.