numpy - Tensorflow infinity mask breaks gradient -


i'm trying softmax on selected indices, using infinity mask silent out unwanted ones. however, gradient of unwanted entires become nan opposed 0.

the reason didn't use boolean mask mask indices different in batch, can't end nice matrix form. if there's workaround here i'll more happy adopt.

the code tested infinity mask

import numpy np import tensorflow tf  = tf.placeholder(tf.float32, [5]) inf_mask = tf.placeholder(tf.float32, [5])  b = tf.multiply(a, inf_mask) sf = tf.nn.softmax(b)  loss = (sf[2] - 0) grad = tf.gradients(loss, a)  sess = tf.session()  a_np = np.ones([5]) np_mask = np.ones([5]) * 4 np_mask[1] = -np.inf  print sess.run([sf, grad], feed_dict={     a: a_np,     inf_mask: np_mask })  sess.close() 

the output

[array([ 0.25,  0.  ,  0.25,  0.25,  0.25], dtype=float32), [array([-0.25,   nan,  0.75, -0.25, -0.25], dtype=float32)]] 

the mask working gradient has nan, should have been 0 think.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -