Reuse large dequeued variable in Tensorflow
<p>I have a large tensor (some hundreds of megabytes) that is dequeued.</p>

<pre><code>small_queue = tf.FIFOQueue(10, [tf.float64, tf.float64, tf.float64])
big_queue = tf.FIFOQueue(10, [tf.float64])

....

small1, small2, small3 = small_queue.dequeue()
large = big_queue.dequeue()
result = process(small1, small2, small3, large)

...

with tf.Session() as S:
    R = S.run(result)
</code></pre>

<p>I'd like to reuse the <code>large</code> variable on subsequent calls to <code>S.run</code>, but I'm not sure how to do with the existing tensorflow <a href="https://www.tensorflow.org/versions/r0.10/how_tos/variable_scope/index.html" rel="nofollow" title="variable sharing">variable sharing</a> paradigm. <code>tf.get_variable</code> requires an initializer, so this is the wrong way to do it but illustrates what I'm trying to do:</p>

<pre><code>with tf.variable_scope("cache"):
    large = tf.get_variable("large", initializer=big_queue.dequeue())
</code></pre>

<p><strong>EDIT 1</strong>: Here is the a more complete example -- I'd like to cache <code>result1</code> and <code>result2</code> in <code>get_expr()</code></p>

<pre><code>import time
import numpy as np
import threading

import tensorflow as tf

capacity=10
data_shape1 = [10, 3000, 128, 4]
data_shape2 = [20, 500, 100]
dtype1 = np.float64
dtype2 = np.int32

data_input1 = tf.placeholder(dtype1, shape=data_shape1)
data_input2 = tf.placeholder(dtype2, shape=data_shape2)

queue = tf.FIFOQueue(capacity=capacity,
    dtypes=[dtype1, dtype2],
    shapes=[data_shape1, data_shape2],
    name="FIFOQueue")

def load_and_enqueue(session):
    enqueue_op = queue.enqueue([data_input1, data_input2])

    for i in xrange(1, capacity+1):
        # Just feed random stuff to the queue
        random_data1 = np.full(data_shape1, i, dtype=dtype1)
        random_data2 = np.full(data_shape2, i, dtype=dtype2)

        # Feed example to Tensorflow placeholder
        feed_dict = { data_input1: random_data1,
            data_input2: random_data2 }

        print ("Enqueueing {i} with shape {s} "
            "and size {b} MB").format(
                i=i,
                s=random_data1.shape,
                b=random_data1.nbytes/(1024**2))

        # Push all the training examples to the queue
        session.run(enqueue_op, feed_dict=feed_dict)


def get_expr():
    result1, result2 = queue.dequeue()

    # Would like to cache result1, result2
    # at this point

    return result1

with tf.Session() as S:
    # Start enqueue thread
    t = threading.Thread(target=load_and_enqueue, args=(S,))
    t.setDaemon(True)
    t.start()
    # Wait for all data to be enqueued
    t.join()

    expr1 = get_expr()
    expr2 = get_expr()

    S.run(tf.initialize_all_variables())
    print S.run(expr1).ravel()[0]
    print S.run(expr2).ravel()[0]
</code></pre>

<p>I don't sure that I understand your question. </p>

<p>If you just want to get <code>Tensor</code> variable you need <code>tf.get_variable</code> after <code>scope.reuse_variables()</code>:</p>

<pre><code>with tf.scope('my_scope') as scope:
    scope.reuse_variables()
    large_var = tf.get_variable('my_large_var')
    #do something
</code></pre>

<p>If you need to evaluate multiple variables I suppose you need <code>sess.run([var1, var2])</code>  (I'm not sure but in cifair10 example there is string <code>_, loss_value = sess.run([train_op, loss])</code>).</p>

python tensorflow