Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

tensorflow

How can we make sure that a calculated value will not be copied back to CPU/python memory, but is still available for calculations in the next step?

The following code obviously doesn't do it:

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:

Error : Attempting to use uninitialized value _recv_b_0

like image 229
Anona112 Avatar asked Dec 14 '15 13:12

Anona112


1 Answers

The answer is to store the value in a tf.Variable by storing to it using the assign operation:

working code:

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 
like image 191
Anona112 Avatar answered Sep 28 '22 07:09

Anona112