簡體   English   中英

如何在實際示例中使用Tensorflow隊列

[英]How to use Tensorflow queues in real example

我有一個Tensorflow DNN模型,我使用feed_dict來輸入輸入Training and Test數據以及屬於它們的標簽。 為了簡單起見,這是代碼的重要部分:

def feed_dict(train):
"""Make a TensorFlow feed_dict: maps data onto Tensor placeholders."""
if train == True :
    xs,ys = next_Training_Batch()
    drop_out_value = 0.9
else:
    #Run a test
    xs,ys=  Testing_Data,Testing_Labels
    drop_out_value = 1
return {x:xs,y_:ys,keep_prob:drop_out_value}
for i in range(max_steps):
    if i%5 ==0: # Record summarie and Test-set accruracy
        summary, acc = sess.run([merged,accuracy], feed_dict=feed_dict(False))
        test_writer.add_summary(summary,i)
        #print('Accuracy at steps%s: %s '%(i,acc))
    else:# Record train set summaries and train
        if i%10==0:
            run_options = tf.RunOptions(trace_level=tf.RunOptions.FULL_TRACE)
            run_metadata = tf.RunMetadata()
            summary, _ = sess.run([merged, train_steps],
                          feed_dict=feed_dict(True),
                          options=run_options,
                          run_metadata=run_metadata)
            train_writer.add_run_metadata(run_metadata, 'step%03d' % i)
            train_writer.add_summary(summary, i)
        else:
            summary,_ = sess.run([merged, train_steps], feed_dict=feed_dict(True))
            train_writer.add_summary(summary,i)

我一直在閱讀有關TF隊列的一種更有效的方法,我很難讓它們運行,這是我到目前為止所做的:

    import tensorflow as tf

'''
# Both Training.csv and Test.csv  include the features values and the and labels as follows :
Feature0  Feature1  Feature2  Feature3  Feature4  Feature5   .......  ClassID(Labels)  onehot
0.200985  1.000000  0.064534  0.415348  0.005391  1.000000             1000             1
0.151232  1.000000  0.048849  0.312474  0.007160  1.000000             2001             2
0.061576  1.000000  0.026125  0.127097  0.017450  1.000000             1000             3
...............................................................................
Each file has > 2500 rows
'''

fileNames = ["Training.csv","Test.csv"]
BATCH_SIZE = 20
number_OF_features  = 450

def batch_generator(fileNames):
    fileNames_queue = tf.train.string_input_producer(fileNames)
    reader = tf.TextLineReader(skip_header_lines=1)
    key , values = reader.read(fileNames_queue)
    record_defaults = [[1.0] for _ in range(number_OF_features)]
    content = tf.decode_csv(values,record_defaults = record_defaults)
    features = tf.stack(content[:-2])
    labels = content[-1]
    min_after_dequeue =10 * BATCH_SIZE
    capacity = 20*BATCH_SIZE
    # suffle the data
    data_batch, label_batch = tf.train.shuffle_batch([features, labels], batch_size=BATCH_SIZE,
    capacity =capacity , min_after_dequeue = min_after_dequeue)
    return data_batch , label_batch

with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    for _ in range(100 ): # generating 100 batch
        sess.run(batch_generator(fileNames))
        # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        # I just  don'T get how to proceed from this point
        # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        coord.request_stop()
        coord.join(threads)

我的問題是如何在訓練和測試中“提供”數據。 我一直在閱讀TF文檔,但沒有幫助。

參考:我編寫的代碼基於該出色的教程

代碼中缺少的是如何使用data_batchlabel_batch張量。 根據這段代碼(BATCH_SIZE, number_OF_features)這些張量的形狀為(BATCH_SIZE, number_OF_features) (分別為(BATCH_SIZE, 1) )(假設number_OF_features == len(content)-1 ):

features = tf.stack(content[:-2])
labels = content[-1]
...
data_batch, label_batch = tf.train.shuffle_batch([features, labels], batch_size=BATCH_SIZE,...)

tf.train.shuffle_batch的文檔(請參見代碼 ):

形狀為[x, y, z]的輸入張量將作為形狀為[batch_size, x, y, z]的張量輸出。

例如,您可以實現一個函數,該函數將data_batchlabel_batch作為參數,並創建並返回cross_entropy op。 然后,您可以使用此op通過GradientDescentOptimizer訓練模型。

您的cross_entropy op以及您的train_op將取決於data_batchlabel_batch 因此,每當您要求Session運行train_op ,它將嘗試通過data_batchlabel_batch從數據隊列中取出新批處理。

例如:

# Let's create the batch op
data_batch , label_batch = batch_generator(fileNames)

# Let's use the batch op
cross_entropy = get_cost_op(data_batch, label_batch)
train_op = tf.train.GradientDescentOptimizer().minimize(cross_entropy)


# We're done with the creation of our model so let's train it.
# create NUM_THREADS to do enqueue
qr = tf.train.QueueRunner(queue, [enqueue_op] * NUM_THREADS)
with tf.Session() as sess:
    # create a coordinator, launch the queue runner threads.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    for step in xrange(100): # do to 100 iterations
        if coord.should_stop():
            break
        sess.run(train_op)
    coord.request_stop()
    coord.join(enqueue_threads)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM