我使用的是一个函数(ndegree_poly),它可以得到一个张量和一个权重数组,并从中计算出多项式的结果。在
代码看起来很直接,但是当度数增加,或者函数被多次重复时,得到的张量包含一堆nan和inf。INF是合理的。但是,如果数字变得非常小,它们不应该变成零而不是nan吗?在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))
它打印:
^{pr2}$