2017-05-09 25 views
-1

我使用後續代碼來計算數值:爲什麼tensorflow POW將在梯度計算研究添加日誌

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

的TensorFlow生成的圖形是這樣的:tf.pow genreate Log op in gradient caculate

當我改變了代碼爲以下:

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

tf.multiply generate no log op 日誌運算消失。 如果輸入值小於0,Log操作將生成大量的nan。 是否是tensorflow中的錯誤或我做錯了什麼?

此代碼的功能是:

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 

回答

0

由於梯度是:

enter image description here

下面是一個answer which gives you some other details

+0

我認爲你是對的。如果a的某些值是負數,a的斜率將導致nan值,所以我改變了pow的乘積。 – auroua

+0

漸變在此處定義,如果您需要更多詳細信息:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/math_grad.py#L718 –