Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tensorflow 2.0: Optimizer.minimize ('Adam' object has no attribute 'minimize')

For my Reinforcement Learning application, I need to be able to apply custom gradients / minimize changing loss function. According to documentation, it should be possible with Optimizer.minimize() function. However, my pip-installed version appears not to have this feature at all.

My code:

from tensorflow.python.keras.optimizers import Adam, SGD
print(tf.version.VERSION)
optim = Adam()
optim.minimize(loss, var_list=network.weights)

output:

2.0.0-alpha0
Traceback (most recent call last):
  File "/Users/ikkamens/Library/Preferences/PyCharmCE2018.3/scratches/testo.py", line 18, in <module>
    optim.minimize(loss, var_list=network.weights)
AttributeError: 'Adam' object has no attribute 'minimize'
like image 948
ikamen Avatar asked Apr 01 '19 15:04

ikamen


People also ask

What does the minimize function of Optimizer do?

Calling minimize() takes care of both computing the gradients and applying them to the variables. If you want to process the gradients before applying them you can instead use the optimizer in three steps: Compute the gradients with tf. GradientTape .

How do you pass the learning rate in Tensorflow?

To use a custom learning rate, simply instantiate an SGD optimizer and pass the argument learning_rate=0.01 .

What is Adam Optimizer in keras?

Adam optimization is a stochastic gradient descent method that is based on adaptive estimation of first-order and second-order moments.


1 Answers

Actually there is a difference. If you print both classes, you'll see:

from tensorflow.python.keras.optimizers import Adam

print(Adam)
print(tf.optimizers.Adam)

<class 'tensorflow.python.keras.optimizers.Adam'>
<class 'tensorflow.python.keras.optimizer_v2.adam.Adam'>

So in the first case Adam inherits from some other class. It's meant to be used inside Keras training loop, therefore, it doesn't have minimize method. To make sure, let's get all class methods

import inspect
from tensorflow.python.keras.optimizers import Adam

print(inspect.getmembers(Adam(), predicate=inspect.ismethod))

Output shows that this class doesn't even have minimize

like image 133
Sharky Avatar answered Sep 30 '22 20:09

Sharky