【发布时间】:2016-05-28 13:08:53
【问题描述】:
我刚开始使用 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))
【问题讨论】:
-
您在 Python 中进行一些计算,为了提高效率,您可能希望将
temp_avg * (i)) + new_x之类的计算转移到 tensorflow 中
标签: python tensorflow