AttributeError: module 'keras.optimizers' has no attribute 'Adam' - python-3.8

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.

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

Import Keras directly or through TensorFlow? Should I uninstall either one?

I have some working Python3 sources gotten from the internet where initial Keras imports are direct like this:
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D
...
In TensorFlow documentation instead I see the following indirect form:
import tensorflow as tf
from tensorflow.keras import layers
...
To me they seem to mean respectively, that Keras can be used without knowing that TensorFlow is behind, and, that Keras is provided (again?) as a part of TensorFlow. (I kind of expect that also Keras similarly provides references to TensorFlow in the former case)
What is the difference? Does it depend on how Keras and TensorFlow are installed, or rather on the way they are used? Is it a potential source of confusion that I have to get rid of? In other words, should I fix my installation, and how? Or should I just accept that there are two, and manage their respective usages to live with them safely?
Background: my installation is under Ubuntu Linux, with Python3.5.2, where pip3 list shows the following packages:
Keras (2.2.4)
Keras-Applications (1.0.6)
Keras-Preprocessing (1.0.5)
tensorboard (1.12.0)
tensorflow (1.12.0)
BTW, I have checked that they are really different:
import keras as keras
import tensorflow.keras as tf_keras
print( keras is tf_keras )
---> False
print( [keras.__version__ , tf_keras.__version__] )
---> ['2.2.4', '2.1.6-tf']
print( [len(dir(keras)) , len(dir(tf_keras)) ] )
---> [32, 30]
print( [ len(dir(keras.models)) , len(dir(tf_keras.models)) ] )
---> [27, 17]
print( [ len(dir(keras.layers)) , len(dir(tf_keras.layers)) ] )
---> [167, 117]
and indeed it seems that I have two different Keras and that the former is higher versioned and richer.
Related readings, useful but not enough to solve the "does it need a fix?" question:
Import statments when using Tensorflow contrib keras
what's the difference between "import keras" and "import tensorflow.keras"
Difference between Keras and tf.keras: should old Keras code be changed?
Why keras does not allow to add a convolutional layer in this way?
Thanks!
Rather than posting my own answer, I'll point you to a very exhaustive answer, much better than I would be able to write: it is here (thanks to Adrian Rosebrock).
Disclaimer: I have no link with Adrian or his activity. I have greatly appreciated his explanation though.
There is no fix needed. They're two different packages and you just manage their respective usages.
Official word as of September 2021:
User should always use from tensorflow import keras which will give them the public API.
import keras will directly access the keras PIP package, which is not 100% same as the public API namespace. It will probably give you keras.Model/layers.* etc, but not all the APIs. Under the hood, we populate all the APIs under keras/api/* and tensorflow __init__ files will pick them up from there.
https://discuss.tensorflow.org/t/keras-project-moved-to-new-repository-in-https-github-com-keras-team-keras/1999/10