AttributeError: module 'tensorflow.python.layers.layers' has no attribute 'Layer' - tensorflow

I'm learning an article called "Attention is all you need", and I'm Trying to learn the code (of the official article from github), and I'm getting weird error, the error is:
"AttributeError: module 'tensorflow.python.layers.layers' has no attribute 'Layer'"
The code generating the error:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
class Attention(tf.layers.Layer)://this is the line the generating the error
Now this is weird because when checking online, all the solutions were version problem of tensorflow, and I have a sufficient version (by the answers online) 1.7.0, Also my python version is 3.6.4.
One more thing is it normal that in the error description it's written:"tensorflow.python.layers.layers" and not just "tensorflow.layers"?
Thanks a lot for your help.

So I faced the same error but discovered that my version of tensorflow (which
is 2.0) moved layers from the tf package (tf.layers) to tf.keras.
An easy fix would be to replace tf.layers with tf.keras.layers

From: https://www.tensorflow.org/api_docs/python/tf/layers/Layer
tf.layers.Layer is considered legacy, and we recommend the use
of tf.keras.layers.Layer instead
After this you may get another error regarding Keras since tensorflow needs to be version > 1.4 so update tf like so:
pip install --upgrade tensorflow

Related

I get error: module 'tensorflow.keras.layers' has no attribute 'Normalization'

I use
layers.Normalization()
in Keras, in keras.Sequential
When I try to run it, I get the following error:
module 'tensorflow.keras.layers' has no attribute 'Normalization'
I've seen the command layers.Normalization() being used in many codes, so I don't know what's wrong. Did something change?
One reason can be that you are using the tensorflow version older then the required to use that layer. There are two ways to get around this problem.
Upgrade tensorflow as discussed above.
Or you can add the layer as follows:
tf.keras.layers.experimental.preprocessing.Normalization
Regards
Check the version of TensorFlow you have:
import tensorflow as tf
print(tf.__version__)
tf.keras.layers.Normalization is an attribute in TensorFlow v2.6.0, so might not work on earlier versions: https://www.tensorflow.org/api_docs/python/tf/keras/layers/Normalization
If you have an earlier version, you can upgrade using
pip install --upgrade tensorflow

VS Code and tensorflow

I just imported the tensorflow module, can anyone tell me why this message keeps coming up on my console every time I run my program and how I can get rid of it?
EDIT: I found that using
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
before
import tensorflow
helped clear my console.

MobileNetV2 in tf.keras. Many links but no useful information

I want to use mobileNetV2 with tf.keras.
If look on the tensorflow website for keras applications
I find
mobilenet = tf.keras.applications.MobileNetV2()
If I try to import MobileNetV2
from tensorflow.keras.applications import MobileNetV2
I get an error:
ImportError: cannot import name 'MobileNetV2'
If I check the Keras2 webside I do find only a handful of applications. The mobileNetV2 (or V1) is not one of them. But the V1 model can be loaded and used.
If I follow the link on the tensorflow.keras website, it brings me to the classic keras webside which in my opinion is Keras1 not keras2, am I wrong? Also stating MobileNetV2, which apparently is not implemented. So I guess the link is wrong.
This is all confusing to me. Probably, this is all due to due to the switch to tf.keras, or am mixing things up?
To formulate my question more concrete: Is there a predefined, usable MobileNetV2 application with tf.keras or do I have to implement it by hand?
Thanks
edit: TF version 1.10.
You are using this link for your reference for MobileNetV2 but that is documented for tensorflow version 1.13. And you are using tensorflow version 1.10. In this you can only find MobileNet not MobileNetV2.
For tensorflow version 1.10, you can import like this,
from tensorflow.keras.applications.mobilenet import MobileNet
or
model = tf.keras.applications.MobileNet()
If you want to check what are the model are included in tf.keras.applications, you can check github repo with appropriate tensorflow version.
If you want to use MobileNetV2, please upgrade your tensorflow version and you can use as it mentioned in the documentation.
For Google Colab and latest version of tensorflow,
Use:
!pip install keras_applications
.. will install keras-applications >= 1.0.8
For tensorflow version >= 2.5.0, use
from keras.applications.mobilenet_v2 import MobileNetV2

TensorFlow 2 and Keras:

When I want to use Keras with TensorFlow 2 I got this error:
AttributeError: module 'tensorflow' has no attribute
'get_default_graph'
The Keras API (https://keras.io/) has multiple implementations, including the original and reference implementation (https://github.com/keras-team/keras), but also various other implementations, including tf.keras, which is part of TensorFlow.
So there are two ways you can use Keras with TensorFlow:
Using the reference implementation with the TensorFlow backend. However, this implementation has not been updated to support TensorFlow 2 yet (as of June 2019).
Using TensorFlow's implementation, tf.keras. This one works fine with TF 2.
To use tf.keras, you must make sure to use the correct imports:
from tensorflow import keras
# NOT: import keras
Similarly, use:
from tensorflow.keras.layers import Dense
# Not from keras.layers import Dense
Hope this helps.
Since TensorFlow 2 defaults to eager execution Keras will need some changes in order to be compatible with it, but until then a previous version of TensorFlow is required.

Fix no module 'layer' in current Google Colab TF version

I use Google colab (python3 GPU)
I want to run for example this repo codes but I have an error when running demo ipynb in these lines:
import tensorflow as tf
from layers import (_causal_linear, _output_linear, conv1d, dilated_conv1d)
When I run these two lines I have an error "no module layer"
I don't think this is a bug or something, because this repo has over 1000 stars.
I think this is rather a tf version problem.
Any ideas how to solve this?
"Layers" is a module in the package that you linked NOT in Tensorflow. See here.
BTW if you wanted to import from a submodule of tensor flow you would have to do from tensorflow.package import ....