Tensorflow, how to preserve the intermediate node value and reuse them -
assuming have following graph.
- x3
, z
values care about.
- x
, y
inputs. in each different. iteration, coming values , shapes of x
, y
different, think placeholder
- circumstance need run graph twice in different time point x3
, z
asynchronously.
+---+ op: +1 op: *3 | x +------------> x_1 +-----------> x3 +---+ +---+ + + | y | | | +-+-+ | op:add | | | | | | | | op: add v op:add | +-------------> <------------+ z
at time point, input x
(say x=7
, don't know what's y
@ moment). want see value of x3
. execute sess.run([x3], {x:7})
, returns 24
expected.
at later time point, input y
(say y=8
), , time want take @ node z
. point have to execute sess.run([z], {x:7, y:8})
result.
the problem is, later run, have feed x
again recalculate intermediate node x_1
, x3
. calculates flow x--> x_1 --> x3
twice hurts efficiency.
my idea x_1
, x3
contain values(x_1=8
,x3=24
) after run until graph destroyed, can directly leverage instead of recalculating.
is there way achieve goal?
the following doesn't solve problem completely, gets away feeding x
again:
x_temp = tf.variable(0, dtype=tf.int32) x = tf.placeholder_with_default(x_temp, shape=()) y = tf.placeholder(tf.int32, shape=()) x_temp = tf.assign(x_temp, x) x_1 = x_temp + 1 x3 = x_1 * 3 z = x_1 + x3 + y sess = tf.interactivesession() print(sess.run(x3, {x:7})) print(sess.run(z, {y:8})) #24 #40
Comments
Post a Comment