'ImportError: cannot import name 'dtensor' from 'tensorflow.compat.v2.experimental' import Keras

I'm getting following Error while importing Keras.

ImportError: cannot import name 'dtensor' from 'tensorflow.compat.v2.experimental' (C:\Users\User\AppData\Roaming\Python\Python38\site-packages\tensorflow\_api\v2\compat\v2\experimental\__init__.py)

Tensorflow v. 2.6, Keras v. 2.6

Do anyone have an idea to solve this Error? I would appreciate any help!



Solution 1:[1]

DTensor is part of the TensorFlow 2.9.0 release. To import dtensor you can upgrade tensorflow 2.6 to tensorflow 2.9 as follows:

pip install --upgrade tensorflow==2.9

Now, you can import dtensor either from tensorflow.experimental or from tensorflow.keras as follows:

#Using tensorflow.experimental
from tensorflow.experimental import dtensor

#Using tensorflow.keras
from tensorflow.keras import dtensor

For more information, please refer to this guide. Thank you.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tfer3