2016-09-03 4 views
1

デキューされるテンソル(数百メガバイト)が大きいです。Tensorflowで大きなデキューされた変数を再利用

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) 

私はS.runへの後続の呼び出しにlarge変数を再利用したいのですが、私は、既存のtensorflow variable sharingパラダイムをどうするかどうかはわかりません。 tf.get_variableは初期化が必要なので、これはそれを行うには間違った方法であるが、私は何をしようとしている示しています

with tf.variable_scope("cache"): 
    large = tf.get_variable("large", initializer=big_queue.dequeue()) 

EDIT 1:ここでは、より完全な例である - 私はしたいのですがキャッシュresult1get_expr()

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] 
+0

あなたはこれを理解しましたか? – gallabytes

答えて

0

result2私は私はあなたの質問を理解していることを確認しないでください。あなただけTensor変数を取得したい場合は

次のものが必要tf.get_variablescope.reuse_variables()後:

with tf.scope('my_scope') as scope: 
    scope.reuse_variables() 
    large_var = tf.get_variable('my_large_var') 
    #do something 

あなたは私はあなたが私はよく分からないが、cifair10の例では、文字列がある(sess.run([var1, var2])を必要とし、複数の変数を評価する必要がある場合_, loss_value = sess.run([train_op, loss]))。

+0

残念ながらそれは動作しません。元の質問にもっと完全な例を追加しました。 – Simon

関連する問題