2016-10-03 248 views
0

我對t(W權重)和b(偏差)使用tf.Variable,但對於X(輸入批次)和Y(此批次的預期值)使用tf.placeholder )。 而且一切正常。但今天我發現這個話題: Tensorflow github issues 和報價:使用張量流tf.Variable替代tf.placeholder用於火車數據

Feed_dict確實從Python運行內容的單線程的memcpy到TensorFlow運行。如果在GPU上需要數據,則需要額外的CPU-> GPU傳輸。我已經習慣了從feed_dict切換到純TensorFlow(可變/隊列)

現在我試圖找到如何使用tf.Variable或隊列輸入數據,並沒有feed_dict當看到高達10倍的性能改進,以提高速度,特別是批次。因爲我需要逐個更改數據批次。當所有批次都完成時 - 紀元結束。並從開始,第二時代等...

但對不起,我不明白我該怎麼用。

+0

見cifar10_input.py /tutorials/deep_cnn/index.html – mdaoust

回答

1

下面是如何使用隊列養活培訓批次自足例如:從本教程https://www.tensorflow.org/versions/r0.11

import tensorflow as tf 

IMG_SIZE = [30, 30, 3] 
BATCH_SIZE_TRAIN = 50 

def get_training_batch(batch_size): 
    ''' training data pipeline -- normally you would read data from files here using 
    a TF reader of some kind. ''' 
    image = tf.random_uniform(shape=IMG_SIZE) 
    label = tf.random_uniform(shape=[]) 

    min_after_dequeue = 100 
    capacity = min_after_dequeue + 3 * batch_size 
    images, labels = tf.train.shuffle_batch(
     [image, label], batch_size=batch_size, capacity=capacity, 
     min_after_dequeue=min_after_dequeue) 
    return images, labels 

# define the graph 
images_train, labels_train = get_training_batch(BATCH_SIZE_TRAIN) 
'''inference, training and other ops generally are defined here too''' 

# start a session 
with tf.Session() as sess: 
    sess.run(tf.initialize_all_variables()) 
    coord = tf.train.Coordinator() 
    threads = tf.train.start_queue_runners(sess=sess, coord=coord) 

    ''' do something interesting here -- training, validation, etc''' 
    for _ in range(5): 
     # typical training step where batch data are drawn from the training queue 
     py_images, py_labels = sess.run([images_train, labels_train]) 
     print('\nData from queue:') 
     print('\tImages shape, first element: ', py_images.shape, py_images[0][0, 0, 0]) 
     print('\tLabels shape, first element: ', py_labels.shape, py_labels[0]) 

    # close threads 
    coord.request_stop() 
    coord.join(threads)