我们如何确保计算值不会被复制回CPU/python内存,但仍可用于下一步的计算?
以下代码显然不会这样做:
import tensorflow as tf a = tf.Variable(tf.constant(1.),name="a") b = tf.Variable(tf.constant(2.),name="b") result = a + b stored = result with tf.Session() as s: val = s.run([result,stored],{a:1.,b:2.}) print(val) # 3 val=s.run([result],{a:4.,b:5.}) print(val) # 9 print(stored.eval()) # 3 NOPE:
错误:尝试使用未初始化的值_recv_b_0
答案是tf.Variable
通过使用assign操作将值存储到a中来存储:
工作代码:
import tensorflow as tf with tf.Session() as s: a = tf.Variable(tf.constant(1.),name="a") b = tf.Variable(tf.constant(2.),name="b") result = a + b stored = tf.Variable(tf.constant(0.),name="stored_sum") assign_op=stored.assign(result) val,_ = s.run([result,assign_op],{a:1.,b:2.}) print(val) # 3 val=s.run(result,{a:4.,b:5.}) print(val[0]) # 9 print(stored.eval()) # ok, still 3