8

I am using Keras to implement a neural network. But when I use model = Sequential(), I get the following error:

AttributeError                            Traceback (most recent call last)
<ipython-input-31-fa9fd3b0e211> in <module>
      8 
      9 # Create the model
---> 10 model = Sequential()
     11 model.add(Dropout(0.1), input_shape=(128,))
     12 model.add(Dense(256, activation='relu', kernel_initializer='he_uniform'))

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/version_utils.py in __new__(cls, *args, **kwargs)
     55     use_v2 = should_use_v2()
     56     cls = swap_class(cls, training.Model, training_v1.Model, use_v2)  # pylint: disable=self-cls-assignment
---> 57     return super(ModelVersionSelector, cls).__new__(cls)
     58 
     59 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/lazy_loader.py in __getattr__(self, item)
     60 
     61   def __getattr__(self, item):
---> 62     module = self._load()
     63     return getattr(module, item)
     64 

/usr/local/lib/python3.6/dist-packages/tensorflow/python/util/lazy_loader.py in _load(self)
     43     """Load the module and insert it into the parent's globals."""
     44     # Import the target module and insert it into the parent's namespace
---> 45     module = importlib.import_module(self.__name__)
     46     self._parent_module_globals[self._local_name] = module
     47 

/usr/lib/python3.6/importlib/__init__.py in import_module(name, package)
    124                 break
    125             level += 1
--> 126     return _bootstrap._gcd_import(name[level:], package, level)
    127 
    128 

/usr/lib/python3.6/importlib/_bootstrap.py in _gcd_import(name, package, level)

/usr/lib/python3.6/importlib/_bootstrap.py in _find_and_load(name, import_)

/usr/lib/python3.6/importlib/_bootstrap.py in _find_and_load_unlocked(name, import_)

/usr/lib/python3.6/importlib/_bootstrap.py in _load_unlocked(spec)

/usr/lib/python3.6/importlib/_bootstrap_external.py in exec_module(self, module)

/usr/lib/python3.6/importlib/_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer_v1.py in <module>
     48 from tensorflow.python.keras.engine import input_spec
     49 from tensorflow.python.keras.mixed_precision import autocast_variable
---> 50 from tensorflow.python.keras.mixed_precision import loss_scale_optimizer
     51 from tensorflow.python.keras.mixed_precision import policy
     52 from tensorflow.python.keras.saving.saved_model import layer_serialization

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/mixed_precision/loss_scale_optimizer.py in <module>
   1152 
   1153 # pylint: disable=protected-access
-> 1154 mixed_precision._register_wrapper_optimizer_cls(optimizer_v2.OptimizerV2,
   1155                                                 LossScaleOptimizerV1)
   1156 

AttributeError: module 'tensorflow.python.training.experimental.mixed_precision' has no attribute '_register_wrapper_optimizer_cls'

I am relatively new to deep learning. Any help would be appreciated. Thank you!

yksolanki9
  • 429
  • 2
  • 7
  • 14

6 Answers6

15

This problem maybe occurs in the keras package version.

Because the installed tensorflow version does not match the keras version. Just uninstall keras and reinstall your own tensorflow corresponding version.

pip3 uninstall keras
pip3 install keras --upgrade

In summary, you need to match the correct environment version for your Keras code.

William Mou
  • 301
  • 2
  • 4
  • I tried reinstalling keras but that didn't work. However, I don't know why but the same code works fine on a new jupyter notebook. Anyways, thanks for your help! – yksolanki9 Feb 12 '21 at 21:00
  • New jupyter notebook? Maybe because your numpy is provide from different python environments(? – William Mou Feb 12 '21 at 21:29
  • Your answer is the best given and should be marked as the correct answer. With pip install tf-nightly the problem is not properly addressed. Hint: maybe you want to add like moguztas copy of your solution did. – Christian Gold Jan 16 '22 at 18:15
11

I've been searching a lot and I could fix this problem only running the following command that I found in this source, Github tf issue:

    pip3 install tf-nightly

As the other answers, this is a compatible version issue

I was getting problems with imports in my local machine like:

    from tensorflow.keras import layers
    from tensorflow.keras import Input

But now I can import without problem.

Felipe Curcio
  • 121
  • 1
  • 6
7

I had the same problem. The problem is that Keras is not installed compatible with Tensorflow, so there are no attributes/methods in it. I tried the following commands mentioned in the source and my problem was completely solved.

pip3 uninstall keras
pip3 install keras --upgrade
moguztas
  • 91
  • 2
  • 5
7

This is a terrible solution, but for the sake of getting things going, here is how I solved it.

I went into my directory and found the location of tensorflow.python.training.experimental.mixed_precision.py, and manually inserted the following lines

def _register_wrapper_optimizer_cls(optimizer_cls, wrapper_optimizer_cls):
  _REGISTERED_WRAPPER_OPTIMIZER_CLS[optimizer_cls] = wrapper_optimizer_cls

The code is from the official GitHub https://github.com/sourcecode369/tensorflow-1/blob/master/tensorflow/python/training/experimental/mixed_precision.py

Sasa
  • 71
  • 1
  • 3
  • 5
    I would rather upvote (did already!) because in my experience and opinion, in emergency deadlines, quick and dirty solutions are life-saving than mature ones. Additionally, answerer has given a disclaimer that THIS IS A TERRIBLE SOLUTION so one should use at their own risk ;-) – Zeel B Patel Nov 25 '21 at 05:40
  • 1
    Be honest this was only working solution for me. Tried to reinstall packages in many ways, but no success. Clear it is one time fix though. – Jaroslav Urban Mar 08 '22 at 16:30
1

After tried both of commands below:

pip3 uninstall keras
pip3 install keras --upgrade

Or:

pip3 uninstall tensorflow
pip3 install tensorflow --upgrade

The only solution works for me:

pip3 install tensorflow==2.6.0
pip3 install keras==2.6.0
ah bon
  • 9,293
  • 12
  • 65
  • 148
0

If below commands doesn't work,

pip3 uninstall keras
pip3 install keras --upgrade

It might the pip3 uninstall keras doesn't uninstall the keras clearly. You need to use pip list to check whether there are some keras package still install.