"No gradients provided for any variable"

I don’t know how to fix this problem “No gradients provided for any variable”

I tried "model.trainable_weights " and “model.trainable_variables”. But i still get “No gradients provided for any variable” error.

{mentor edit: image of code removed}

1 Like

1 Like

Please click my name and message your notebook as an attachment.

1 Like

I’m having what might be the exact same issue. Was there a resolution to this?

Printed output from my C2W3_Assignment notebook debugging:

loss : tf.Tensor(0.6931472, shape=(), dtype=float32)

trainable_variables : <class ‘list’>
trainable_variables[0]: (2, 64)
trainable_variables[1]: (64,)
trainable_variables[2]: (64, 1)
trainable_variables[3]: (1,)

about to run grads = tape.gradient(loss, model.trainable_variables)

grads: [None, None, None, None]


Error from optimizer is :

ValueError: No gradients provided for any variable: [‘dense_15/kernel:0’, ‘dense_15/bias:0’, ‘dense_16/kernel:0’, ‘dense_16/bias:0’].

1 Like

Are you still involved with this? Any chance I could get you to take a look?

Thanks for asking. I’m not the right person to take up advanced tensorflow assignment test cases at this point.

1 Like

Hello,

I dont know exactly where the problem might be either but send me your code on PM , I will have a look on it later on.

1 Like

Right, I saw you assignment and I see that when you set the losses (in the “set_sparse_cat_crossentropy_loss”) you are not doing it properly, you need to use the losses class not metrics class!

Try after changing that and lets see what happens.

2 Likes

That was it… thanks very much. I failed to notice the difference between:

<class ‘tensorflow.python.keras.metrics.SparseCategoricalCrossentropy’>
vs.
<class ‘tensorflow.python.keras.losses.SparseCategoricalCrossentropy’>

T.

2 Likes

Thank you so much Sir

2 Likes