2017-06-06 7 views
2

テンソルとウェイトの配列を取得し、それらから多項式の結果を計算する関数(ndegree_poly)を使用しています。行列の乗算と加算中のTensorflow:nan

コードは真っ直ぐに見えますが、度が上がったり関数が何度も繰り返されると、結果のテンソルにはたくさんのnansとinfが含まれています。 infsは合理的です。しかし、数字が本当に小さくなったら、ナンの代わりにゼロになるべきではありませんか?

import tensorflow as tf 

function_degree = 10 

def ndegree_poly(x, a, degree=6): 
     op = tf.add_n([tf.multiply(tf.pow(x, i), a[i]) for i in range(1, degree)]) 
     return tf.add(op, a[0]) 

with tf.Session() as sess: 
    poly_weight = tf.Variable(tf.random_normal([function_freedom, 1, 5])) 

    mat = tf.Variable(tf.random_normal([2, 5])) 

    result0 = ndegree_poly(mat, poly_weight, function_degree) 
    result1 = ndegree_poly(result0, poly_weight, function_degree) 
    result2 = ndegree_poly(result1, poly_weight, function_degree) 
    result3 = ndegree_poly(result2, poly_weight, function_degree) 
    result4 = ndegree_poly(result3, poly_weight, function_degree) 


    sess.run(tf.global_variables_initializer()) 
    print(sess.run(result4)) 

それは印刷します。

nan値が非常に小さい係数から来ていない
[[-0.28569764   nan   nan   nan  -inf] 
[  nan   nan 3.55561209   nan 0.53827095]] 

答えて

0

、それがやろうとしているだけの「自然な」結果だ∞ - ∞、係数は通常から来て流通は正と負の両方である。

import math 
import tensorflow as tf 

tf_inf = tf.constant(inf) 
res = tf_inf - tf_inf 
with tf.Session() as sess: 
    print(sess.run(res)) 

>>> nan 
+0

ありがとう、私はそのことを忘れていました – Persia12