我有一个大的张量(几百兆字节)已经出局。
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)
我希望在large
的后续调用中重复使用S.run
变量,但我不确定如何处理现有的tensorflow variable sharing范例。 tf.get_variable
需要初始化程序,因此这是错误的方法,但说明了我尝试做的事情:
with tf.variable_scope("cache"):
large = tf.get_variable("large", initializer=big_queue.dequeue())
编辑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]
答案 0 :(得分:0)
我不确定我理解你的问题。
如果您只想获得Tensor
变量,则tf.get_variable
之后需要scope.reuse_variables()
:
with tf.scope('my_scope') as scope:
scope.reuse_variables()
large_var = tf.get_variable('my_large_var')
#do something
如果您需要评估多个变量,我认为您需要sess.run([var1, var2])
(我不确定,但在cifair10示例中有字符串_, loss_value = sess.run([train_op, loss])
)。