Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to suppress specific warning in Tensorflow (Python)

I have a model that, based on certain conditions, has some unconnected gradients, and this is exactly what I want. But Tensorflow is printing out a Warning every time it encounters the unconnected gradient.

WARNING:tensorflow:Gradients do not exist for variables

Is there any way to only suppress this specific warning? I don't want to blindly suppress all warnings since there might be unexpected (and potentially useful) warnings in the future as I'm still working on my model.

like image 941
name.disp Avatar asked Feb 02 '20 01:02

name.disp


People also ask

How do I ignore all Tensorflow warnings?

So to knock out these warnings in a single blow, do import warnings then warnings. filterwarnings('ignore') , then run your tensorflow imports and and code that relies on the broken alpha-tensorflow code, then turn warnings back on via warnings.


1 Answers

Kinda hacky way:

gradients = tape.gradient(loss, model.trainable_variables)
optimizer.apply_gradients([
    (grad, var) 
    for (grad, var) in zip(gradients, model.trainable_variables) 
    if grad is not None
])
like image 82
Jared Nielsen Avatar answered Oct 18 '22 11:10

Jared Nielsen