ValueError: No gradients provided for any variable when using tf.optimizers.Adam.minimize method

Viewed 35

I'm trying to do what these lines of code used to do with an older version of Tensorflow (seen on a ML online course)

optimizer = tf.train.AdamOptimizer(learning_rate)
train_step = optimiser.minimize(loss)

and checking previously answered questions and documentation, I found out that var_list is now a required parameter, and in case loss is provided as a Tensor the tape that computed the loss must be provided. So I got to write the following code:

optimizer = tf.optimizers.Adam(learning_rate=learning_rate)
params = [w1, b1, w2, b2, w3, b3]
gradients = optimizer.get_gradients(loss=loss, params=params)
train_step = optimizer.minimize(loss=loss, var_list=params, tape=tf.GradientTape(), grad_loss=gradients)

but I'm still getting the error:

ValueError: No gradients provided for any variable: (['Variable:0', 'Variable_1:0',
 'Variable_8:0', 'Variable_9:0', 'Variable_12:0', 'Variable_13:0'],). Provided 
`grads_and_vars` is ((None, <tf.Variable 'Variable:0' shape=(784, 512) dtype=float32>),
 (None, <tf.Variable 'Variable_1:0' shape=(512,) dtype=float32>), (None, <tf.Variable 
'Variable_8:0' shape=(512, 64) dtype=float32>), (None, <tf.Variable 'Variable_9:0' 
shape=(64,) dtype=float32>), (None, <tf.Variable 'Variable_12:0' shape=(64, 10) 
dtype=float32>), (None, <tf.Variable 'Variable_13:0' shape=(10,) dtype=float32>)).

I thought I'd solve the problem computing the gradients with the get_gradients method and providing it as a grad_loss parameter, but it still doesn't work. I'm not even completely sure I should provide all those six parameters, but the architecture of the neural network I'm working with is actually made of two hidden layers and an output layer, with weights and bias each.

Thank you so much in advance

1 Answers

A possible solution is simply using the previous version of Tensorflow:

First I needed to initialise this way (I'm using tf1 because for other functions or methods I prefer using the updated version, meaning that I also import tensorflow as tf)

import tensorflow.compat.v1 as tf1
tf.compat.v1.disable_eager_execution()
tf1.disable_v2_behavior()

Then the code is

optimizer = tf1.train.AdamOptimizer(learning_rate)
train_step = optimizer.minimize(loss=loss)

So I'm using the old AdamOptimizer.

But still, if anyone knows how to do the same thing with the latest version of Tensorflow I'd be super glad to know. Thank you

Related