-1

I use the follow code to calculate pow:

eq2 = tf.pow(cos_t, FLAGS.margin-2*p, name='calc_cos_mt_eq2')

The TensorFlow generated graph like this: tf.pow genreate Log op in gradient caculate

When I changed the code to the following:

eq2 = tf.multiply(cos_t, FLAGS.margin-2*p, name='calc_cos_mt_eq2')

tf.multiply generate no log op The Log op disappear. If the input value is less than 0, the Log op will generate lots of nan. Does is a bug in tensorflow or something I make wrong?

The function of this code is:

def calc_cos_mt(self, cos_t):
    '''calculate cos(m*theta)
    '''
    cos_mt = 0.0
    sin2_t = 1 - cos_t * cos_t
    flag = -1.0
    for p in range(FLAGS.margin // 2 + 1):
        flag *= -1.0
        eq1 = tf.multiply(flag, self.c_map[2*p], name='calc_cos_mt_eq1')
        # eq2 = tf.pow(cos_t, FLAGS.margin-2*p, name='calc_cos_mt_eq2')
        eq2 = tf.multiply(cos_t, FLAGS.margin-2*p, name='calc_cos_mt_eq2')
        eq3 = tf.pow(sin2_t, p, name='calc_cos_mt_eq3')
        cos_mt = tf.add(cos_mt, tf.multiply(tf.multiply(eq1, eq2), eq3, name='calc_cos_mt_eq'), name='cos_mt_add')
    return cos_mt
Cœur
  • 37,241
  • 25
  • 195
  • 267
auroua
  • 153
  • 2
  • 12

1 Answers1

0

Because the gradient is:

enter image description here

Here is an answer which gives you some other details.

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • I think you are right. If some values of a is negative,the gradient of a will lead to nan value, so I changed pow to multiply. – auroua May 09 '17 at 05:21
  • The gradient is defined here, if you want more details: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/math_grad.py#L718 – Peter Hawkins May 12 '17 at 18:14