AttributeError: module 'keras.layers' has no attribute 'Wrapper' - tensorflow

This error happens cause I used from astroNN.models import Galaxy10CNN and do downgrade Tensorflow to 1.15.2 to prevent the ImportError: cannot import name 'get_default_session' but see new error related to attribute 'Wrapper' AttributeError: module 'keras.layers' has no attribute 'Wrapper'
Please advise. Thanks!

Use the keras.layers.wrapper in Tensorflow 1.15 as
import tensorflow as tf
tf.keras.layers.Wrapper(layer, **kwargs)
for more details on the library please find here.

With tensorflow version 1.15.2 and astroNN version 1.0.1 a hacky way is to replace line 15 of the file /usr/local/lib/python3.7/dist-packages/astroNN/nn/layers.py (e.g., in linux, with colab)
Layer, Wrapper, InputSpec = tf.keras.layers.Layer, tf.keras.layers.Wrapper, tf.keras.layers.InputSpec

Related

AttributeError: module 'keras.api._v2.keras.optimizers.experimental' has no attribute 'Adafactor'

enter image description here
Dear all,
I am using Tensorflow 2.10, and cannot import Adafactor optimizer. Can anyone help me with some tips to solve this issue?
Thanks so much!
Try to update your TensorFlow version to v2.11.
I checked GitHub that its available on TF v2.11 and not available on TF 2.10 yet.
TF v2.10:
https://github.com/keras-team/keras/tree/v2.10.0/keras/optimizers/optimizer_experimental
TF v2.11:
https://github.com/keras-team/keras/tree/v2.11.0/keras/optimizers/optimizer_experimental

keras-bert load_trained_model_from_checkpoint error

I had a code for loading a BERT model that executed very well, but now it raises me an error
here is the code
model = load_trained_model_from_checkpoint(
config_path,
checkpoint_path,
trainable=True,
seq_len=SEQ_LEN,
output_layer_num=4
)
now the error it raises is:
AttributeError: 'tuple' object has no attribute 'layer'
The environment settings are as follows:
keras-bert=0.85.0
keras=2.4.3
tensorflow=1.15.2
Many thanks in advance
In your environment settings, when installing packages, try installing them without specifying the specific versions:
pip install -q keras-bert
pip install keras
AttributeError: 'tuple' object has no attribute 'layer' basically occurs when you mixup keras and tensorflow.keras as this answer explains.
See if that resolves your issue. Also, if you have the following in your code:
import keras
from keras import backend as K
Try changing them to:
from tensorflow.python import keras
import tensorflow.keras.backend as K
I hope that resolves your issue.
You can check this article for reference.

AttributeError: module 'keras.optimizers' has no attribute 'Adam'

When i am using "optimizer = keras.optimizers.Adam(learning_rate)" i am getting this error
"AttributeError: module 'keras.optimizers' has no attribute 'Adam". I am using python3.8 keras 2.6 and backend tensorflow 1.13.2 for running the program. Please help to resolve !
Use tf.keras.optimizers.Adam(learning_rate) instead of keras.optimizers.Adam(learning_rate)
As per the documentation , try to import keras into your code like this,
>>> from tensorflow import keras
This has helped me as well.
Make sure you've imported tensorflow:
import tensorflow as tf
Then use
tf.optimizers.Adam(learning_rate)
There are ways to solve your problem as you are using keras 2.6 and tensorflow too:
use (from keras.optimizer_v2.adam import Adam as Adam) but go through the function documentation once to specify your learning rate and beta values
you can also use (Adam = keras.optimizers.Adam).
(import tensorflow as tf) then (Adam = tf.keras.optimizers.Adam)
Use the form that is useful for the environment you set
I think you are using Keras directly. Instead of giving as from keras.distribute import —> give as from tensorflow.keras.distribute import
Hope this would help you.. It is working for me.

How to solve, No module named 'tf'?

I tried to convert my CNN model .h5 file to a .tflite file using this code:
import tensorflow as tf
from tf.lite import TFLiteConverter
converter = lite.TFLiteConverter.from_saved_model('/drive/My Drive/FSD_modelV09A.h5')
tflite_model = converter.convert()
open("/drive/My Drive/FSD_modelV09A.tflite", "wb").write(tflite_model)
But then there's an error saying:
ModuleNotFoundError: No module named 'tf'
You cannot make imports from module aliases, you have to use the full module name:
from tensorflow.lite import TFLiteConverter
You can also just refer to tf.lite.TFLiteConverter in code
I guess the tensorflow module is not fully loaded (yet) when you try to make other imports. E.g. when you do from X import Y, aliases are not known yet, so X cannot be an alias. Try importing from the original module name:
from tensorflow.lite import TFLiteConverter

Why do I get AttributeError: module 'tensorflow' has no attribute 'placeholder'?

I was able to run my python program three weeks ago but now every time I try to run it, I get the following error:
AttributeError: module 'tensorflow' has no attribute 'placeholder'
I have tensorflow installed (version '2.0.0-alpha0').
I have read a couple of posts related to this issue. They say I should uninstall TensorFlow and re-install it again. The problem is that I am running this on a cluster computer and I do not have sudo permissions.
Any idea?
In Tensorflow 2.0, there is no placeholder. You need to update your TF1.x code to TF2.0 code and then run it on your cluster. Please take a look at the official doc on converting your TF1.x code to TF2.0.
In TF1.x codes, you build tensorflow graph (static graph) with placeholders, constants, variables. Then, run the code in a session with a tf.session() command. During that session, you provide the values for the placeholder and execute the static graph.
In TF2.0, models run eagerly as you enter commands. This is more pythonic. Check more details about TF 2.0 here. Thanks!
After including the tensorflow compat v1 libraries:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()`
use the v1 syntax like this:
X = tf.compat.v1.placeholder(dtype="float",shape=[None, n_H0, n_W0, n_C0])
Y = tf.compat.v1.placeholder(dtype="float",shape=[None, n_y])
In addition to the #Vishnuvardhan Janapati's answer, you can update folders ("*TREE") and/or files to version 2 of TensorFlow. The upgrade tool tf_upgrade_v2 is automatically included in TensorFlow 1.13 and later.
tf_upgrade_v2 [-h] [--infile INPUT_FILE] [--outfile OUTPUT_FILE]
[--intree INPUT_TREE] [--outtree OUTPUT_TREE]
[--copyotherfiles COPY_OTHER_FILES] [--inplace]
[--reportfile REPORT_FILENAME] [--mode {DEFAULT,SAFETY}]
[--print_all]
An illustration of how the conversion fixed the "placeholder" error:
Note: this fixes similar complaints module 'tensorflow' has no attribute 'xxxxx' (not just the "placeholder").
Calling disable_v2_behavior() function is not necessary
just,
import tensorflow as tf
tf.compat.v1.placeholder()
Changing the library worked for me
#libraries
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
If this doesn't work maybe you need you install TensorFlow again.
I hope it helps