keras-bert load_trained_model_from_checkpoint error - tensorflow

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.

Related

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.

Custom Layer not supporting serialized custom activation function

since the release of version 2.6 of Tensorflow I am having a issue I did not have with version 2.5.
The following code works OK:
from tensorflow.keras.utils import get_custom_objects
from tensorflow.keras.layers import Dense
def my_act(x):
return x
get_custom_objects().update({"my_act": my_act})
dense = Dense(3, activation="my_act")
However, if I try to do the same but with a custom layer instead of Tensorflow built-in layers, I have the error:
ValueError: Unknown activation function: my_act. Please ensure this object is passed to the `custom_objects` argument. See https://www.tensorflow.org/guide/keras/save_and_serialize#registering_the_custom_object for details.
Here you have the minimum code to reproduce plus I show that with version 2.5 works ok (You need to restart the runtime to run it tho).
Try to import activations like this:
from tensorflow.keras import activations
instead of from tensorflow.python.keras import activations.
In tensorflow 2.7 and later versions tensorflow.python will no longer exist, and it seems in TF 2.6 already it is not compatible with some other functions.

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

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

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

How to fix AttributeError: module 'tensorflow' has no attribute 'keras'?

I'm following a basic tensorflow tutorial (to recognize the 28x28 pixel handwritten digits 0-9), but when I run these two lines:
import tensorflow as tf
mnist = tf.keras.datasets.mnist
I get the error message
AttributeError: module 'tensorflow' has no attribute 'keras'
I've looked at posts where people have similar questions, and it seems the answers are usually to update your tensorflow and keras version, but I think I did that already, and this error message is still appearing. How can I resolve this issue?
I think you have typo there.
Should change this line:
mnist = tf.kera.datasets.mnist
to:
mnist = tf.keras.datasets.mnist
Notice that I change kera to keras