如何将计算值保存在 Tensorflow 图中(在 GPU 上)?

How to keep calculated values in a Tensorflow graph (on the GPU)?

我们如何确保计算出的值不会被复制回 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

工作代码:

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