Hi Team,
I am trying to run Adam optimizer code locally, but getting this error:
I have Version: 2.16.1 of tensorflow. But I am getting error: ‘Adam’ object has no attribute ‘minimize’
Hi Team,
I am trying to run Adam optimizer code locally, but getting this error:
I have Version: 2.16.1 of tensorflow. But I am getting error: ‘Adam’ object has no attribute ‘minimize’
Avoid posting codes, it is against community guidelines, kindly remove it.
Also share a screenshot of the complete error you have encountered.
Regards
DP
Are you using import keras
or import tensorflow.keras
in your code?
You can try the following code, too:
from tensorflow.keras.optimizers.legacy import Adam
Let me know if this resolves your issue or if you need further assistance!
this doesn’t seem to be related to any assignment? or I am missing the assignment name? can you mention which part of assignment is this from?
How much experience do you have using TensorFlow? I’ve never seen an optimizer used that way. In all the assignments that use TF here in the DLS courses, the pattern is this:
First you define your model by listing all the layers using the Keras Sequential or Functional APIs.
Then you “compile” the model. Here’s an example from DLS C4 W1 A2:
happy_model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
Note that this is part of the template code that they just gave us, so it’s ok to show it here. It’s not part of the code we need to write for the assignment.
Then finally you train the model using the optimizer and loss function you just defined like this:
happy_model.fit(X_train, Y_train, epochs=10, batch_size=16)
There are lots of places to look in the TF documentation for tutorial level information about topics like this. Here’s one such article, but you might want to read some of the previous articles that it mentions first.
Hi @paulinpaloalto ,
I am using Andrew’s video on TensorFlow as a reference to practice some basics of TensorFlow. In that, he just used the cost function as w=x^2-10x+25 to show how to calculate the value of x for which this function is minimum.
okay you tried creating a cost function using three features.
The way you recalling the logistic regression without using any bias, is surely not the right way to recall cost function. Forget the optimizer part. that comes later.
even optimizer is not applied the way you have recalled for the equation, sagar.
Please go through videos again.
Regards
DP
Sorry, I don’t know which video you mean. Can you give us a link or a reference? I’m only familiar with the DLS courses here and I don’t remember him ever using “minimize()” before.
Hi @paulinpaloalto ,
Here is the video on coursera: https://www.coursera.org/learn/deep-neural-network/lecture/zcZlH/tensorflow
Hi @Deepti_Prasad ,
This works if I use GradientTape and call applyGradient. In the course video, Andrew mentioned using the “minimize” operation, which is a shortcut to not doing the GradientTape step.
Ok, you’re right that he does use the minimize() method of the optimizer in that video. I haven’t watched that since the course was revised to use TF 2.0 in 2021. This is not the way we really use things in the actual assignments, but you should be able to make it work. Note that you’re not doing it the same way he does. Here’s a screenshot with his code:
There must be some things missing from your code. Actually here’s a theory: I’ll bet you just said something like:
optimizer = tf.keras.optimizers.Adam
That makes optimizer a function that returns a function. Notice that’s not the way Prof Ng calls it: he is “instantiating” it with a learning rate value. So “Adam” is not the actual optimizer function, it is the function you use to define the optimizer function.
Hi @paulinpaloalto,
I got it working by following the suggestion from @Alireza_Saei: I should use “import tf_keras.optimizers.legacy as legacy” rather than using tensorflow. When I used the legacy optimizer, it worked. Thanks @Alireza_Saei
You’re welcome, happy to help
It’s great to hear that you found the solution with the Legacy version of the code. But I still think it would be worth your while to invest the effort to understand the point I made about how the Adam function is defined. You should be able to make it work with the current version of the code. The Layer functions work the same way, as described on that other thread I linked above. If you’re going to be seriously using TF, it would be worth reading that thread as well.
Same problem here, but apparently I’m using Keras-3 on my machine and there isn’t the legacy option anymore.
ImportError: keras.optimizers.legacy
is not supported in Keras 3. When using tf.keras
, to continue using a tf.keras.optimizers.legacy
optimizer, you can install the tf_keras
package (Keras 2) and set the environment variable TF_USE_LEGACY_KERAS=True
to configure TensorFlow to use tf_keras
when accessing tf.keras
.
I’m just moving on as I understand the purpose of the Adam minimize function used to be.
Noticing the same thing here. Keras 3 does not offer legacy support, and it’s not worth installing the old package. Just move on