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

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 ....

Related

I'm having a minor problem running Tensorflow with Colab

I am a beginner who just learned about TensorFlow using Google Colab.
As in the attached image file, numbers 10 to 13 are underlined in tensorflow.keras~, what is the problem?
It's probably a function that indicates a typo, but there's nothing wrong with running it.
enter image description here
This underline error was an ongoing issue earlier in Google Colab, which is resolved now. Please try again replicating the same code in Google colab what you mentioned in the screen shot and let us know if the issue still persists at your end.
Please check the code below: (I replicated the same in Google Colab with python 3.8.10 and TensorFlow 2.9.2)
from keras.preprocessing import Sequence will show the error as you are not giving proper alias to import the Sequence API which is provided correctly in next line (using tensorflow.keras.preprocessing prefix or tensorflow.keras.utils).

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

How to use tensorflow library with sagemaker preprocessing

I want to use TensorFlow for preprocessing in sagemaker pipelines.
But, I haven't been able to find a way to use it.
Right now, I'm using this library for preprocessing:
from sagemaker.sklearn.processing import SKLearnProcessor
framework_version = "0.23-1"
sklearn_processor = SKLearnProcessor(
framework_version=framework_version,
instance_type=processing_instance_type,
instance_count=processing_instance_count,
base_job_name="abcd",
role=role,
)
Now, I need to use TensorFlow in preprocessing but the python module cant import TensorFlow.
Any help would be much appreciated. Thanks.
So there's no TensorFlow Processor module, the list of available processing modules are in the following doc and can be seen at the bottom of the page.
You can use a Processor and ScriptProcessor class and pass in your TF preprocessing as a script with a requirements.txt for the necessary modules.
I work for AWS and my opinions are my own.

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

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

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