我刚从Tensorflow开始,想知道下面的代码是否是计算累积滚动平均值的正确方法
import tensorflow as tf
import numpy as np
x = tf.Variable(0, name = "x")
x_pld = tf.placeholder(tf.int32)
update_x = tf.assign(x, x_pld)
curr_avg = tf.Variable(tf.constant(0.), name="curr_avg")
avg_pld = tf.placeholder("float")
update_avg = tf.assign(curr_avg, avg_pld)
# Initalize
init_op = tf.initialize_all_variables()
with tf.Session() as session:
session.run(init_op)
for i in range(5):
temp_avg = session.run(curr_avg)
session.run(update_x, feed_dict={x_pld: np.random.randint(1000)})
new_x = session.run(x)
print(new_x)
session.run(update_avg, feed_dict={avg_pld: ((temp_avg * (i)) + new_x)/(i+1)})
print(session.run(curr_avg))
相关问题 更多 >
编程相关推荐