How to determine which variable is "nonetype" in a tensor stream

I use TensorFlow to create a new model that includes a dynamic loop. I am using tf.while_loop to implement this instance. One of the issues I am facing is the following:

AttributeError: 'NoneType' object has no attribute 'back_prop'

This issue occurs when running

 gradients = tf.gradients(self.loss, params) 

Then I try to print all the params , and it turns out that each parameter has a form. I think that if there is a nonetype parameter, its shape should be None , eh? On the other hand, is there any other method that could help me determine which variable is not assigned or how [] ?

Here is the full trackback:

  Traceback (most recent call last): File "main.py", line 125, in <module> tf.app.run() File "/usr/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 30, in run sys.exit(main(sys.argv)) File "main.py", line 119, in main train()# if FLAGS.train: File "main.py", line 95, in train model = create_model(sess, False) File "main.py", line 75, in create_model forward_only=False) File "/home/sniu/lab/ai_lab/DMN-tensorflow/models/DMN.py", line 248, in __init__ gradients = tf.gradients(self.loss, params) File "/usr/lib/python2.7/site-packages/tensorflow/python/ops/gradients.py", line 481, in gradients in_grads = _AsList(grad_fn(op, *out_grads)) File "/usr/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_grad.py", line 181, in _EnterGrad if not grad_ctxt.back_prop: AttributeError: 'NoneType' object has no attribute 'back_prop' 
+5
source share
1 answer

NoneType means None

 >>> item = None >>> item.value Traceback (most recent call last): File "<stdin>", line 1 in <module> AttributeError: 'NoneType' object has no attribute 'value' 

You can see if you call type on None

 >>> type(None) <type 'NoneType'> 

None is a special meaning in python. This is a singleton object. This is an instance of NoneType , and all None is the same object.

Typically, to prevent these types of errors, people either check to see if it is None or wrap this expression in a try/except block

 if item is not None: print item.back_prop 

Or using try/except

 try: item.back_prop except AttributeError: pass 

Remember that the try/except block can suppress other AttributeErrors not related to item , like None , for example, if item is a different value that also does not have the back_prop attribute. You can view this situation differently than if item is None .

0
source

All Articles