咨詢電話:023-6276-4481
熱門文章
電 話:023-6276-4481
郵箱:broiling@qq.com
地址:重慶市南岸區(qū)亞太商谷6幢25-2
Tensorflow 數(shù)據(jù)讀取有三種方式:
Preloaded data: 預加載數(shù)據(jù)
Feeding: Python產(chǎn)生數(shù)據(jù),再把數(shù)據(jù)喂給后端。
Reading from file: 從文件中直接讀取
這三種有讀取方式有什么區(qū)別呢? 我們首先要知道TensorFlow(TF)是怎么樣工作的。
TF的核心是用C++寫的,這樣的好處是運行快,缺點是調(diào)用不靈活。而Python恰好相反,所以結(jié)合兩種語言的優(yōu)勢。涉及計算的核心算子和運行框架是用C++寫的,并提供API給Python。Python調(diào)用這些API,設計訓練模型(Graph),再將設計好的Graph給后端去執(zhí)行。簡而言之,Python的角色是Design,C++是Run。
一、預加載數(shù)據(jù):
import tensorflow as tf
# 設計Graph
x1 = tf.constant([2, 3, 4])
x2 = tf.constant([4, 0, 1])
y = tf.add(x1, x2)
# 打開一個session --> 計算y
with tf.Session() as sess:
print sess.run(y)
二、python產(chǎn)生數(shù)據(jù),再將數(shù)據(jù)喂給后端
import tensorflow as tf
# 設計Graph
x1 = tf.placeholder(tf.int16)
x2 = tf.placeholder(tf.int16)
y = tf.add(x1, x2)
# 用Python產(chǎn)生數(shù)據(jù)
li1 = [2, 3, 4]
li2 = [4, 0, 1]
# 打開一個session --> 喂數(shù)據(jù) --> 計算y
with tf.Session() as sess:
print sess.run(y, feed_dict={x1: li1, x2: li2})
說明:在這里x1, x2只是占位符,沒有具體的值,那么運行的時候去哪取值呢?這時候就要用到sess.run()
中的feed_dict
參數(shù),將Python產(chǎn)生的數(shù)據(jù)喂給后端,并計算y。
這兩種方案的缺點:
1、預加載:將數(shù)據(jù)直接內(nèi)嵌到Graph中,再把Graph傳入Session中運行。當數(shù)據(jù)量比較大時,Graph的傳輸會遇到效率問題。
2、用占位符替代數(shù)據(jù),待運行的時候填充數(shù)據(jù)。
前兩種方法很方便,但是遇到大型數(shù)據(jù)的時候就會很吃力,即使是Feeding,中間環(huán)節(jié)的增加也是不小的開銷,比如數(shù)據(jù)類型轉(zhuǎn)換等等。最優(yōu)的方案就是在Graph定義好文件讀取的方法,讓TF自己去從文件中讀取數(shù)據(jù),并解碼成可使用的樣本集。
三、從文件中讀取,簡單來說就是將數(shù)據(jù)讀取模塊的圖搭好
1、準備數(shù)據(jù),構(gòu)造三個文件,A.csv,B.csv,C.csv
$ echo -e "Alpha1,A1\nAlpha2,A2\nAlpha3,A3" > A.csv
$ echo -e "Bee1,B1\nBee2,B2\nBee3,B3" > B.csv
$ echo -e "Sea1,C1\nSea2,C2\nSea3,C3" > C.csv
2、單個Reader,單個樣本
#-*- coding:utf-8 -*-
import tensorflow as tf
# 生成一個先入先出隊列和一個QueueRunner,生成文件名隊列
filenames = ['A.csv', 'B.csv', 'C.csv']
filename_queue = tf.train.string_input_producer(filenames, shuffle=False)
# 定義Reader
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
# 定義Decoder
example, label = tf.decode_csv(value, record_defaults=[['null'], ['null']])
#example_batch, label_batch = tf.train.shuffle_batch([example,label], batch_size=1, 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 = tf.train.start_queue_runners(coord=coord) #啟動QueueRunner, 此時文件名隊列已經(jīng)進隊。
for i in range(10):
print example.eval(),label.eval()
coord.request_stop()
coord.join(threads)
說明:這里沒有使用tf.train.shuffle_batch,會導致生成的樣本和label之間對應不上,亂序了。生成結(jié)果如下:
Alpha1 A2
Alpha3 B1
Bee2 B3
Sea1 C2
Sea3 A1
Alpha2 A3
Bee1 B2
Bee3 C1
Sea2 C3
Alpha1 A2
解決方案:用tf.train.shuffle_batch,那么生成的結(jié)果就能夠?qū)稀?/span>
#-*- coding:utf-8 -*-
import tensorflow as tf
# 生成一個先入先出隊列和一個QueueRunner,生成文件名隊列
filenames = ['A.csv', 'B.csv', 'C.csv']
filename_queue = tf.train.string_input_producer(filenames, shuffle=False)
# 定義Reader
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
# 定義Decoder
example, label = tf.decode_csv(value, record_defaults=[['null'], ['null']])
example_batch, label_batch = tf.train.shuffle_batch([example,label], batch_size=1, 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 = tf.train.start_queue_runners(coord=coord) #啟動QueueRunner, 此時文件名隊列已經(jīng)進隊。
for i in range(10):
e_val,l_val = sess.run([example_batch, label_batch])
print e_val,l_val
coord.request_stop()
coord.join(threads)
3、單個Reader,多個樣本,主要也是通過tf.train.shuffle_batch來實現(xiàn)
#-*- coding:utf-8 -*-
import tensorflow as tf
filenames = ['A.csv', 'B.csv', &am