摘要:簡介讀取數(shù)據(jù)共有三種方法當(dāng)運行每步計算的時候,從獲取數(shù)據(jù)。數(shù)據(jù)直接預(yù)加載到的中,再把傳入運行。在中定義好文件讀取的運算節(jié)點,把傳入運行時,執(zhí)行讀取文件的運算,這樣可以避免在和執(zhí)行環(huán)境之間反復(fù)傳遞數(shù)據(jù)。本文講解的代碼。
簡介
TensorFlow讀取數(shù)據(jù)共有三種方法:
Feeding:當(dāng)TensorFlow運行每步計算的時候,從Python獲取數(shù)據(jù)。在Graph的設(shè)計階段,用placeholder占住Graph的位置,完成Graph的表達;當(dāng)Graph傳給Session后,在運算時再把需要的數(shù)據(jù)從Python傳過來。
Preloaded data:數(shù)據(jù)直接預(yù)加載到TensorFlow的Graph中,再把Graph傳入Session運行。只適用于小數(shù)據(jù)。
Reading from file:在Graph中定義好文件讀取的運算節(jié)點,把Graph傳入Session運行時,執(zhí)行讀取文件的運算,這樣可以避免在Python和TensorFlow C++執(zhí)行環(huán)境之間反復(fù)傳遞數(shù)據(jù)。
本文講解Reading from file的代碼。
其他關(guān)于TensorFlow的學(xué)習(xí)筆記,請點擊入門教程
實現(xiàn)#!/usr/bin/env python # -*- coding=utf-8 -*- # @author: 陳水平 # @date: 2017-02-19 # @description: modified program to illustrate reading from file based on TF offitial tutorial # @ref: https://www.tensorflow.org/programmers_guide/reading_data def read_my_file_format(filename_queue): """從文件名隊列讀取一行數(shù)據(jù) 輸入: ----- filename_queue:文件名隊列,舉個例子,可以使用`tf.train.string_input_producer(["file0.csv", "file1.csv"])`方法創(chuàng)建一個包含兩個CSV文件的隊列 輸出: ----- 一個樣本:`[features, label]` """ reader = tf.SomeReader() # 創(chuàng)建Reader key, record_string = reader.read(filename_queue) # 讀取一行記錄 example, label = tf.some_decoder(record_string) # 解析該行記錄 processed_example = some_processing(example) # 對特征進行預(yù)處理 return processed_example, label def input_pipeline(filenames, batch_size, num_epochs=None): """ 從一組文件中讀取一個批次數(shù)據(jù) 輸入: ----- filenames:文件名列表,如`["file0.csv", "file1.csv"]` batch_size:每次讀取的樣本數(shù) num_epochs:每個文件的讀取次數(shù) 輸出: ----- 一批樣本,`[[example1, label1], [example2, label2], ...]` """ filename_queue = tf.train.string_input_producer(filenames, num_epochs=num_epochs, shuffle=True) # 創(chuàng)建文件名隊列 example, label = read_my_file_format(filename_queue) # 讀取一個樣本 # 將樣本放進樣本隊列,每次輸出一個批次樣本 # - min_after_dequeue:定義輸出樣本后的隊列最小樣本數(shù),越大隨機性越強,但start up時間和內(nèi)存占用越多 # - capacity:隊列大小,必須比min_after_dequeue大 min_after_dequeue = 10000 capacity = min_after_dqueue + 3 * batch_size example_batch, label_batch = tf.train.shuffle_batch( [example, label], batch_size=batch_size, capacity=capacity, min_after_dequeue=min_after_dequeue) return example_batch, label_batch def main(_): x, y = input_pipeline(["file0.csv", "file1.csv"], 1000, 5) train_op = some_func(x, y) init_op = tf.global_variables_initializer() local_init_op = tf.local_variables_initializer() # local variables like epoch_num, batch_size sess = tf.Session() sess.run(init_op) sess.run(local_init_op) # `QueueRunner`用于創(chuàng)建一系列線程,反復(fù)地執(zhí)行`enqueue` op # `Coordinator`用于讓這些線程一起結(jié)束 # 典型應(yīng)用場景: # - 多線程準(zhǔn)備樣本數(shù)據(jù),執(zhí)行enqueue將樣本放進一個隊列 # - 一個訓(xùn)練線程從隊列執(zhí)行dequeu獲取一批樣本,執(zhí)行training op # `tf.train`的許多函數(shù)會在graph中添加`QueueRunner`對象,如`tf.train.string_input_producer` # 在執(zhí)行training op之前,需要保證Queue里有數(shù)據(jù),因此需要先執(zhí)行`start_queue_runners` coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) try: while not coord.should_stop(): sess.run(train_op) except tf.errors.OutOfRangeError: print "Done training -- epoch limit reached" finally: coord.request_stop() # Wait for threads to finish coord.join(threads) sess.close() if __name__ == "__main__": tf.app.run()
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/38460.html
摘要:貢獻者飛龍版本最近總是有人問我,把這些資料看完一遍要用多長時間,如果你一本書一本書看的話,的確要用很長時間。為了方便大家,我就把每本書的章節(jié)拆開,再按照知識點合并,手動整理了這個知識樹。 Special Sponsors showImg(https://segmentfault.com/img/remote/1460000018907426?w=1760&h=200); 貢獻者:飛龍版...
摘要:前言本文基于官網(wǎng)的寫成。輸入數(shù)據(jù)是,全稱是,是一組由這個機構(gòu)搜集的手寫數(shù)字掃描文件和每個文件對應(yīng)標(biāo)簽的數(shù)據(jù)集,經(jīng)過一定的修改使其適合機器學(xué)習(xí)算法讀取。這個數(shù)據(jù)集可以從牛的不行的教授的網(wǎng)站獲取。 前言 本文基于TensorFlow官網(wǎng)的Tutorial寫成。輸入數(shù)據(jù)是MNIST,全稱是Modified National Institute of Standards and Technol...
摘要:本文的目的是聚焦于數(shù)據(jù)操作能力,講述中比較重要的一些,幫助大家實現(xiàn)各自的業(yè)務(wù)邏輯。傳入輸入值,指定輸出的基本數(shù)據(jù)類型。 引言 用TensorFlow做好一個機器學(xué)習(xí)項目,需要具備多種代碼能力: 工程開發(fā)能力:怎么讀取數(shù)據(jù)、怎么設(shè)計與運行Computation Graph、怎么保存與恢復(fù)變量、怎么保存統(tǒng)計結(jié)果、怎么共享變量、怎么分布式部署 數(shù)據(jù)操作能力:怎么將原始數(shù)據(jù)一步步轉(zhuǎn)化為模型需...
閱讀 3515·2021-10-13 09:39
閱讀 1487·2021-10-08 10:05
閱讀 2294·2021-09-26 09:56
閱讀 2321·2021-09-03 10:28
閱讀 2715·2019-08-29 18:37
閱讀 2063·2019-08-29 17:07
閱讀 626·2019-08-29 16:23
閱讀 2215·2019-08-29 11:24