Ich habe einen großen Tensor (einige Hunderte von Megabyte), der aus der Warteschlange genommen wird.Wiederverwendete Variable in Tensorflow wiederverwenden
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)
Ich möchte die large
Variable auf nachfolgende Aufrufe S.run
wieder zu verwenden, aber ich bin nicht sicher, wie mit dem bestehenden tensorflow variable sharing Paradigma zu tun. tf.get_variable
erfordert einen Initialisierer, so ist dies der falsche Weg, es zu tun, sondern zeigt, was ich versuche zu tun:
with tf.variable_scope("cache"):
large = tf.get_variable("large", initializer=big_queue.dequeue())
EDIT 1: das ein vollständigeres Beispiel hier - ich mag Cache result1
und result2
in 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]
Haben Sie das jemals herausgefunden? – gallabytes