デキューされるテンソル(数百メガバイト)が大きいです。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:ここでは、より完全な例である - 私はしたいのですがキャッシュresult1
とget_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]
あなたはこれを理解しましたか? – gallabytes