tensorflow載入數(shù)據(jù)的三種方式_第1頁
tensorflow載入數(shù)據(jù)的三種方式_第2頁
tensorflow載入數(shù)據(jù)的三種方式_第3頁
tensorflow載入數(shù)據(jù)的三種方式_第4頁
tensorflow載入數(shù)據(jù)的三種方式_第5頁
已閱讀5頁,還剩15頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、tensorflow載入數(shù)據(jù)的三種方式Tensorflow數(shù)據(jù)讀取有三種方式:Preloaded data:預(yù)加載數(shù)據(jù) Feeding: Python產(chǎn)生數(shù)據(jù),再我們首先要知道把數(shù)據(jù)喂給后端。Reading from file:從文件中直接讀取這三種有讀取方式有什么區(qū)別呢?TensorFlow(TF)是怎么樣工作的。TF的核心是用C+寫的,這樣的好處是運(yùn)行快,缺點(diǎn)是調(diào) 用不靈活。而Python恰好相反,所以結(jié)合兩種語言的優(yōu)勢。涉及計(jì)算的核心算子和運(yùn)行框架是用C+寫的,并提供API 給Python o Python調(diào)用這些API ,設(shè)計(jì)訓(xùn)練模型(Graph), 再將設(shè)計(jì)好的Graph給后端去執(zhí)行

2、。簡而言之,Python的 角色是 Design , C+ 是 Run。、預(yù)加載數(shù)據(jù):python view plain copy import tensorflow as tf #y = tf. add(xl, x2)# 扌丁開個 session 一>計(jì)算 y withGraph xl = tf constant(2, 3, 4) x2 = tf constant (4, 0, 1)y = tf. add(xl, x2)# 打開個 session 一>計(jì)算 y withtf Session() as sess:print sessrim(y)二、python產(chǎn)生數(shù)據(jù),再將數(shù)據(jù)喂給

3、后端python view plain copy import tensorflow as tf、it 設(shè)計(jì) Graph xl =tf. placeholder (tfintl6) x2 = tfplaceholder (tfintl6)tf. add(xl, x2)# 用 Python 產(chǎn)生數(shù)據(jù) lil = 2, 3, 4112 =4, 0, 1 # 打開一個 session >喂數(shù)據(jù)>計(jì)算 y with tf Session() as sess:print sessrtm(y,feed_dict=xl: lil, x2: li2) 說明在這里xl, x2只是占位符,沒有具體的值

4、,那么運(yùn)行的時候去哪取值呢?這時候就要用到sess. run()中的feed_dict參數(shù),將Python產(chǎn)生的數(shù)據(jù)喂給后端,并計(jì)算Vo這兩種方案的缺點(diǎn):1、預(yù)加載:將數(shù)據(jù)直接內(nèi)嵌到Graph中,再把Graph傳入Session中運(yùn)行。當(dāng)數(shù)據(jù)量比較大時,Graph的傳輸會遇到 效率問題。2、用占位符替代數(shù)據(jù),待運(yùn)行的時候填充數(shù)據(jù)。 前兩種方法很方便,但是遇到大型數(shù)據(jù)的時候就會很吃力, 即使是Feeding ,中間環(huán)節(jié)的增加也是不小的開銷,比如數(shù) 據(jù)類型轉(zhuǎn)換等等。最優(yōu)的方案就是在Graph定義好文件讀取 的方法,讓TF自己去從文件中讀取數(shù)據(jù),并解碼成可使用 的樣本集。三、從文件中讀取,簡單來說就

5、是將數(shù)據(jù)讀取模 塊的圖搭好1、準(zhǔn)備數(shù)據(jù),構(gòu)造三個文件 ,A. csv, B csv, C csv python view plain copy $ echo -eAlphal, AlnAlpha2, A2nAlpha3, A3 > A. csv$ echo-e Beel, BlnBee2, B2nBee3, B3/z > B. csv$ echo -eSeal, ClnSea2, C2nSea3, C3 > C csv2、單個 Reader ,單個樣本python view plain copy #-*- coding:utf-8 -*- import tensorflow

6、as tf # 生成個先入 先出隊(duì)列和一個QueueRunner,生成文件名隊(duì)列 filenames = A csv, E csv, C csv f ilename_queue = tf. train. string_input_producer(filenames, shuffle=False) 定義 Reader reader = tf TextLineReader () key, value = reader read (f ilename_queue) # 定義 Decoder example, label = tf. decode_csv(value, record_defauIts

7、=nulT,/ null5) #example_batch, label_batch =tf train. shuffle_batch(example, label, batch_size=l, capacity=200, min_after_dequeue=100, num_threads=2)# 坯仃 Graph with tf Session () as sess:coord =tf. train. Coordinator () #創(chuàng)建一個協(xié)調(diào)器,管理線程threads = tftrain. start_queue_runners(coord=coord)動QueueRunner,此時文

8、件名隊(duì)列已經(jīng)進(jìn)隊(duì)。for i inrange(10):print example eval (), labe 1. eval ()coord request_stop()coord join(threads)說明:這里沒有使用tf. train, shuffle_batch ,會導(dǎo)致生成的樣本和 label之間對應(yīng)不上,亂序了。生成結(jié)果如下:Alphal A2Alpha3 BlBee2 B3Seal C2Sea3 AlAlpha2 A3Beel B2Bee3 ClSea2 C3Alphal A2 解決方案: 用 tf. train, shuffle_batch,那么生成的 結(jié)果就能夠?qū)?yīng)上。p

9、ython view plain copycoding:utf-8 -*-importtensorflow as tf #生成一個先入先出隊(duì)列和一個QueueRunner,生成文件名隊(duì)列filenames = A. csv,Bcsv, Ccsv filename_queue =tf. train. string_input_producer(filenames, shuffle=False)定義 Reader reader = tf TextLineReader () key, value = reader read(f 訂ename_queue) # 定義 Decoder example,

10、label = tf. decode_csv (value, record_defaults= nulT , nulT )example_batch, label_batch =tf train. shuffle_batch(example, label, batch_size=l,capacity=200, min_after_dequeue=100, num_threads=2)# JSiT Graph with tf Session() as sess:coord =tf. train. Coordinator () #創(chuàng)建一個協(xié)調(diào)器,管理線程threads = tf train. st

11、art_queue_runners (coord=coord)#啟動QueueRunner,此時文件名隊(duì)列已經(jīng)進(jìn)隊(duì)。for i inrange(10) :e_val, l_val =sess run(example_batch, label_batch)printe_val, l_valcoord request_stop()coord, join (threads) 3、單個Reader,多個樣本,主要也是通過 tf. train. shuffle_batch 來實(shí)現(xiàn) python view plain copy #-*- coding:utf-8 -*- import tensorflow

12、 as tf filenames = Acsv, B csv, C csv, f ilename_queue =tf. train. string_input_producer(filenames, shuffle=False)reader = tf. TextLineReader ()key, value =reader read(f ilename_queue)example, label = tf. decode_csv(value, record_defaults= nulT , null5 )使用tf. train, batch()會多加了一個樣本隊(duì)列和一個QueueRunner o

13、#Decoder解后數(shù)據(jù)會進(jìn)入這個隊(duì)列,再線程,相應(yīng)增加線程數(shù)會提高讀取速度,但并不是線程越多 越好。 example_batch, label_batch =tftrainbatch( example, label, batch_size=5)with tfSession() as sess:coord =tftrainCoordinator()threads =tftrain. start_queue_runners(coord=coord)for i inrange(10):e_val, l_val =sess run(example_batch, label_batch)printe_

14、val, l_valcoord request_stop ()coord join(threads)個樣本,特征說明:下面這種寫法,提取出來的batch_size和label之間也是不同步的python view plain copy #一*- coding:utf-8 一*- import tensorflow as tf filenames = A csv, B csv, C csv filename_queue =tf train. string_input_producer(filenames, shuffle=False)reader = tfTextLineReader ()key

15、, value =reader read(filename_queue)example, label = tfdecode_csv(value, record_defauIts二nulT , nulT )使用tf. train, batch()會多加了一個樣本隊(duì)列和一 個QueueRunnerdecoder解后數(shù)據(jù)會進(jìn)入這個隊(duì)列,再批量出隊(duì)。#雖然這里只有一個Reader ,但可以設(shè)置多線程,相應(yīng)增加線程數(shù)會提高讀取速度,但并不是線程越好。example_batch, label_batch 二tftrain. batch(example, label, batch_size=5)with t

16、fSession() as sess:coord 二tftrainCoordinator ()threads =for i intf train. start_queue_runners(coord=coord)range (10): print example_batch. eval(),label_batch. eval( )coord request_stop()coord join(threads說明:輸出結(jié)果如下:可以看出廂label之間是不對應(yīng)的代Alphdl Alpha2 Alpha3 Beel Bee2 B3 C1 C2 C3 AlAlphf Alpha3 Beel Bee2

17、Eee3 C1 C2 C3 Al A2Alpha3 Beel Eee2 Bee3 Seal C2 C3 A1 A2 A3,4、多個reader ,多個樣本python view plain copy #一*一 coding:utf-8 -*一importtensorflow as tf filenames = A csv, E csv, C csv_ filename_queue = tftrain. string_input_producer(filenames, shuffle=False) reader = tfTextLineReader()key, value= reader rea

18、d(f ilename_queue)record_defaults = nun,? nuir #定義了多種解碼器,每個解碼器跟一個reader相 example_list = tfdecode_csv(value, record_defauIts=record_defaults)for_ in range (2) # Reader 設(shè)置為 2 # 使用tf. train. batch_join(),可以使用多個reader ,并彳丁讀取數(shù) 每個 Reader 使用個線程。 example_batch, label_batch = tftrain. batch_join(example_list

19、, batch_size=5)with tf. Session() as sess:coord =tf.trainCoordinator()threads =tf. train .start_queue_runners(coord=coord)for i inrange (10):e_val, l_val =sess run(example_batch, label_batch)printe_val, l_valcoord request_stop ()coord join (threads)tf. train, batch 與 tf. train, shuffle_batch 函數(shù)是單 Re

20、ader 讀 個 取,但是可以多線程。tf. train. batch_join與tf. train, shuffle_batch_join 可設(shè)置多 Reader 讀取,每個Reader使用一個線程。至于兩種方法的效率,ReaderMWforReader使用一個線程。example_batch, label_batch2個線程就達(dá)到了速度的極限。多Reader時,2個ReaderO所以并不是線程越多越快,甚至更多的會使效率下降。5、迭代控制,設(shè)置epoch參數(shù),指臺匕python view plain copy #- coding:utf-8importtensorflow as tf fi

21、lenames = A csv, B csv,C csv,#num_epoch: 設(shè)置迭代數(shù) filename_queue =tf. train. string_input_producer(filenames,shuffle=False, num_epochs=3) reader =tf.TextLineReader() key, value =reader read (f ilenanie_queue) record_defau Its = null,V nuir #定義了多種解碼器,每個解碼器跟一個reader相 連example_list = tfdecode_csv(value, r

22、ecord_defaults=record_defaults)in range (2) # Reader 設(shè)置為 2 # 使用tf. train. batch_join(),可以使用多個reader ,并行讀取數(shù)example_list, batch_size=l)tf. train- batch join#初始化本地變量init_local_op =tf initialize_local_variables() with tfSession() assess:sessrun (init_local_opcoord =)tf train. Coordinator ()threads = tft

23、rain. start_queue_runners(coord=coord)trywhile not:coord should_stop():e_val, l_val = sessrun(example_batch, label_batch)printexcept tferrors0utOfRangeError:e_val, l_valprint C Epochs Complete!)finallcoord request_stop ()._y coord join (threads)coord request_stop()coord join (threads)在迭代控制中,記得添加tf.

24、initialize_local_variables (),官網(wǎng)教程沒有說明,但是如果不初始化,運(yùn)行就會報(bào)錯。二對于傳統(tǒng)的機(jī)器學(xué)習(xí)而言,比方說分類問題,X1x2 x3是feature。對于二分類問題,label經(jīng)過one-hot編碼之后就會是0,1或者1,0。一般情況下,我們會考慮將數(shù) 據(jù)方式去讀取數(shù)據(jù)說明:對于該數(shù)據(jù),前三列代表的是 feature ,因?yàn)槭欠诸悊栴},后兩列就是經(jīng)過one-hot編碼之 后得到的組織在csv文件中,一行代表一個sample。然后使用隊(duì)列的 label#初始化本地變量init_local_op =使用隊(duì)列讀取該CSV文件的代碼如 下:python view plain copy #- coding:utf-8importtensorflow as tf #生成一個先入先出隊(duì)列和一個 QueueRunner,生成文件名隊(duì)列 f ilenames = A csv filename_queue =tf. train. string_input_producer(filenames, shuffle=Fal

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論