Avoiding Duplicating Graph In Tensorflow (lstm Model)
I have the following simplified code (actually, unrolled LSTM model): def func(a, b): with tf.variable_scope('name'): res = tf.add(a, b) print(res.name) return
Solution 1:
You should take a look at the source code of tf.nn.dynamic_rnn
, specifically _dynamic_rnn_loop
function at python/ops/rnn.py
- it's solving the same problem. In order not blow up the graph, it's using tf.while_loop
to reuse the same graph ops for new data. But this approach adds several restrictions, namely the shape of tensors that are passing through in a loop must be invariant. See the examples in tf.while_loop
documentation:
i0 = tf.constant(0)
m0 = tf.ones([2, 2])
c = lambda i, m: i < 10
b = lambda i, m: [i+1, tf.concat([m, m], axis=0)]
tf.while_loop(
c, b, loop_vars=[i0, m0],
shape_invariants=[i0.get_shape(), tf.TensorShape([None, 2])])
Post a Comment for "Avoiding Duplicating Graph In Tensorflow (lstm Model)"