最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

對tensorflow中cifar-10文檔的Read操作詳解

 更新時間:2020年02月10日 10:29:08   作者:luchi007  
今天小編就為大家分享一篇對tensorflow中cifar-10文檔的Read操作詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

前言

在tensorflow的官方文檔中得卷積神經(jīng)網(wǎng)絡(luò)一章,有一個使用cifar-10圖片數(shù)據(jù)集的實驗,搭建卷積神經(jīng)網(wǎng)絡(luò)倒不難,但是那個cifar10_input文件著實讓我費了一番心思。配合著官方文檔也算看的七七八八,但是中間還是有一些不太明白,不明白的mark一下,這次記下一些已經(jīng)明白的。

研究

cifar10_input.py文件的read操作,主要的就是下面的代碼:

if not eval_data:
  filenames = [os.path.join(data_dir, 'data_batch_%d.bin' % i)
         for i in xrange(1, 6)]
  num_examples_per_epoch = NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN
 else:
  filenames = [os.path.join(data_dir, 'test_batch.bin')]
  num_examples_per_epoch = NUM_EXAMPLES_PER_EPOCH_FOR_EVAL
...
filename_queue = tf.train.string_input_producer(filenames)

...

label_bytes = 1 # 2 for CIFAR-100
 result.height = 32
 result.width = 32
 result.depth = 3
 image_bytes = result.height * result.width * result.depth
 # Every record consists of a label followed by the image, with a
 # fixed number of bytes for each.
 record_bytes = label_bytes + image_bytes

 # Read a record, getting filenames from the filename_queue. No
 # header or footer in the CIFAR-10 format, so we leave header_bytes
 # and footer_bytes at their default of 0.
 reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
 result.key, value = reader.read(filename_queue)

 ...

 if shuffle:
  images, label_batch = tf.train.shuffle_batch(
    [image, label],
    batch_size=batch_size,
    num_threads=num_preprocess_threads,
    capacity=min_queue_examples + 3 * batch_size,
    min_after_dequeue=min_queue_examples)
 else:
  images, label_batch = tf.train.batch(
    [image, label],
    batch_size=batch_size,
    num_threads=num_preprocess_threads,
    capacity=min_queue_examples + 3 * batch_size)

開始并不明白這段代碼是用來干什么的,越看越糊涂,因為之前使用tensorflow最多也就是使用哪個tf.placeholder()這個操作,并沒有使用tensorflow自帶的讀寫方法來讀寫,所以上面的代碼看的很費勁兒。不過我在官方文檔的How-To這個document中看到了這個東西:

Batching

def read_my_file_format(filename_queue):
 reader = tf.SomeReader()
 key, record_string = reader.read(filename_queue)
 example, label = tf.some_decoder(record_string)
 processed_example = some_processing(example)
 return processed_example, label

def input_pipeline(filenames, batch_size, num_epochs=None):
 filename_queue = tf.train.string_input_producer(
   filenames, num_epochs=num_epochs, shuffle=True)
 example, label = read_my_file_format(filename_queue)
 # min_after_dequeue defines how big a buffer we will randomly sample
 #  from -- bigger means better shuffling but slower start up and more
 #  memory used.
 # capacity must be larger than min_after_dequeue and the amount larger
 #  determines the maximum we will prefetch. Recommendation:
 #  min_after_dequeue + (num_threads + a small safety margin) * batch_size
 min_after_dequeue = 10000
 capacity = min_after_dequeue + 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

感覺豁然開朗,再研究一下其官方文檔API就能大約明白期間意思。最有代表性的圖示官方文檔中也給出來了,雖然官方文檔給的解釋并不多。

API我就不一一解釋了,我們下面通過實驗來明白。

實驗

首先在tensorflow路徑下創(chuàng)建兩個文件,分別命名為test.txt以及test2.txt,其內(nèi)容分別是:

test.txt:

test line1
test line2
test line3
test line4
test line5
test line6

test2.txt:

test2 line1
test2 line2
test2 line3
test2 line4
test2 line5
test2 line6

然后再命令行里依次鍵入下面的命令:

import tensorflow as tf
filenames=['test.txt','test2.txt']
#創(chuàng)建如上圖所示的filename_queue
filename_queue=tf.train.string_input_producer(filenames)
#選取的是每次讀取一行的TextLineReader
reader=tf.TextLineReader()
init=tf.initialize_all_variables()
#讀取文件,也就是創(chuàng)建上圖中的Reader
key,value=reader.read(filename_queue)
#讀取batch文件,batch_size設(shè)置成1,為了方便看
bs=tf.train.batch([value],batch_size=1,num_threads=1,capacity=2)
sess=tf.Session() 
#非常關(guān)鍵,這個是連通各個queue圖的關(guān)鍵          
tf.train.start_queue_runners(sess=sess)
#計算有reader的輸出
b=reader.num_records_produced()

然后我們執(zhí)行:

>>> sess.run(bs)
array(['test line1'], dtype=object)
>>> sess.run(b)
4
>>> sess.run(bs)
array(['test line2'], dtype=object)
>>> sess.run(b)
5
>>> sess.run(bs)
array(['test line3'], dtype=object)
>>> sess.run(bs)
array(['test line4'], dtype=object)
>>> sess.run(bs)
array(['test line5'], dtype=object)
>>> sess.run(bs)
array(['test line6'], dtype=object)
>>> sess.run(bs)
array(['test2 line1'], dtype=object)
>>> sess.run(bs)
array(['test2 line2'], dtype=object)
>>> sess.run(bs)
array(['test2 line3'], dtype=object)
>>> sess.run(bs)
array(['test2 line4'], dtype=object)
>>> sess.run(bs)
array(['test2 line5'], dtype=object)
>>> sess.run(bs)
array(['test2 line6'], dtype=object)
>>> sess.run(bs)
array(['test2 line1'], dtype=object)
>>> sess.run(bs)
array(['test2 line2'], dtype=object)
>>> sess.run(bs)
array(['test2 line3'], dtype=object)
>>> sess.run(bs)
array(['test2 line4'], dtype=object)
>>> sess.run(bs)
array(['test2 line5'], dtype=object)
>>> sess.run(bs)
array(['test2 line6'], dtype=object)
>>> sess.run(bs)
array(['test line1'], dtype=object)

我們發(fā)現(xiàn),當(dāng)batch_size設(shè)置成為1的時候,bs的輸出是按照文件行數(shù)進行逐步打印的,原因是,我們選擇的是單個Reader進行操作的,這個Reader先將test.txt文件讀取,然后逐行讀取并將讀取的文本送到example queue(如上圖)中,因為這里batch設(shè)置的是1,而且用到的是tf.train.batch()方法,中間沒有shuffle,所以自然而然是按照順序輸出的,之后Reader再讀取test2.txt。但是這里有一個疑惑,為什么reader.num_records_produced的第一個輸出不是從1開始的,這點不太清楚。 另外,打印出filename_queue的size:

>>> sess.run(filename_queue.size())
32

發(fā)現(xiàn)filename_queue的size有32個之多!這點也不明白。。。

我們可以更改實驗條件,將batch_size設(shè)置成2,會發(fā)現(xiàn)也是順序的輸出,而且每次輸出為2行文本(和batch_size一樣)

我們繼續(xù)更改實驗條件,將tf.train.batch方法換成tf.train.shuffle_batch方法,文本數(shù)據(jù)不變:

import tensorflow as tf
filenames=['test.txt','test2.txt']
filename_queue=tf.train.string_input_producer(filenames)
reader=tf.TextLineReader()
init=tf.initialize_all_variables()
key,value=reader.read(filename_queue)
bs=tf.train.shuffle_batch([value],batch_size=1,num_threads=1,capacity=4,min_after_dequeue=2)
sess=tf.Session()           
tf.train.start_queue_runners(sess=sess)
b=reader.num_records_produced()

繼續(xù)剛才的執(zhí)行:

>>> sess.run(bs)
array(['test2 line2'], dtype=object)
>>> sess.run(bs)
array(['test2 line5'], dtype=object)
>>> sess.run(bs)
array(['test2 line6'], dtype=object)
>>> sess.run(bs)
array(['test2 line4'], dtype=object)
>>> sess.run(bs)
array(['test2 line3'], dtype=object)
>>> sess.run(bs)
array(['test line1'], dtype=object)
>>> sess.run(bs)
array(['test line2'], dtype=object)
>>> sess.run(bs)
array(['test2 line1'], dtype=object)
>>> sess.run(bs)
array(['test line4'], dtype=object)
>>> sess.run(bs)
array(['test line5'], dtype=object)
>>> sess.run(bs)
array(['test2 line1'], dtype=object)
>>> sess.run(bs)
array(['test line3'], dtype=object)

我們發(fā)現(xiàn)的是,使用了shuffle操作之后,明顯的bs的輸出變得不一樣了,變得沒有規(guī)則,然后我們看filename_queue的size:

>>> sess.run(filename_queue.size())
32

發(fā)現(xiàn)也是32,由此估計是tensorflow會根據(jù)文件大小默認filename_queue的長度。 注意這里面的capacity=4,min_after_dequeue=2這些個命令,capacity指的是example queue的最大長度, 而min_after_dequeue是指在出隊列之后,example queue最少要保留的元素個數(shù),為什么需要這個,其實是為了混合的更顯著。也正是有這兩個元素,讓shuffle變得可能。

到這里基本上大概的思路能明白,但是上面的實驗都是對于單個的Reader,和上一節(jié)的圖不太一致,根據(jù)官網(wǎng)教程,為了使用多個Reader,我們可以這樣:

import tensorflow as tf
filenames=['test.txt','test2.txt']
filename_queue=tf.train.string_input_producer(filenames)
reader=tf.TextLineReader()
init=tf.initialize_all_variables()
key_list,value_list=[reader.read(filename_queue) for _ in range(2)]
bs2=tf.train.shuffle_batch_join([value_list],batch_size=1,capacity=4,min_after_dequeue=2)
sess=tf.Session()       
sess.run(init)    
tf.train.start_queue_runners(sess=sess)

運行的結(jié)果如下:

>>> sess.run(bs2)
[array(['test2.txt:2'], dtype=object), array(['test2 line2'], dtype=object)]
>>> sess.run(bs2)
[array(['test2.txt:5'], dtype=object), array(['test2 line5'], dtype=object)]
>>> sess.run(bs2)
[array(['test2.txt:6'], dtype=object), array(['test2 line6'], dtype=object)]
>>> sess.run(bs2)
[array(['test2.txt:4'], dtype=object), array(['test2 line4'], dtype=object)]
>>> sess.run(bs2)
[array(['test2.txt:3'], dtype=object), array(['test2 line3'], dtype=object)]
>>> sess.run(bs2)
[array(['test2.txt:1'], dtype=object), array(['test2 line1'], dtype=object)]
>>> sess.run(bs2)
[array(['test.txt:4'], dtype=object), array(['test line4'], dtype=object)]
>>> sess.run(bs2)
[array(['test.txt:3'], dtype=object), array(['test line3'], dtype=object)]
>>> sess.run(bs2)
[array(['test.txt:2'], dtype=object), array(['test line2'], dtype=object)]

以上這篇對tensorflow中cifar-10文檔的Read操作詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 將tensorflow的ckpt模型存儲為npy的實例

    將tensorflow的ckpt模型存儲為npy的實例

    今天小編就為大家分享一篇將tensorflow的ckpt模型存儲為npy的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • Python實現(xiàn)的列表排序、反轉(zhuǎn)操作示例

    Python實現(xiàn)的列表排序、反轉(zhuǎn)操作示例

    這篇文章主要介紹了Python實現(xiàn)的列表排序、反轉(zhuǎn)操作,結(jié)合實例形式分析了Python針對列表的sort排序、以及基于reverse、切片的反轉(zhuǎn)操作相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2019-03-03
  • python回溯算法實現(xiàn)全排列小練習(xí)分享

    python回溯算法實現(xiàn)全排列小練習(xí)分享

    這篇文章主要給大家分享的是python回溯算法實現(xiàn)全排列小練習(xí),文章根據(jù)例子:輸入列表L(不含重復(fù)元素),輸出L的全排列展開學(xué)習(xí),需要的小伙伴可以參考一下
    2022-02-02
  • Python圖像處理庫處理步驟

    Python圖像處理庫處理步驟

    這篇文章主要介紹了Python圖像處理探索之Python圖像處理庫,我們將學(xué)習(xí)使用不同的 Python 庫實現(xiàn)一些常見的圖像處理、變換和可視化技術(shù),這些技術(shù)通??梢杂米鞲鼜?fù)雜的圖像處理任務(wù)的基本預(yù)處理/后處理步驟,需要的朋友可以參考下
    2023-04-04
  • Python中矩陣庫Numpy基本操作詳解

    Python中矩陣庫Numpy基本操作詳解

    這篇文章主要為大家詳細介紹了Python中矩陣庫Numpy的基本操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • Python訪問PostgreSQL數(shù)據(jù)庫詳細操作

    Python訪問PostgreSQL數(shù)據(jù)庫詳細操作

    postgresql是常用的關(guān)系型數(shù)據(jù)庫,并且postgresql目前還保持著全部開源的狀態(tài),這篇文章主要給大家介紹了關(guān)于Python訪問PostgreSQL數(shù)據(jù)庫的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • Python實現(xiàn)基本線性數(shù)據(jù)結(jié)構(gòu)

    Python實現(xiàn)基本線性數(shù)據(jù)結(jié)構(gòu)

    這篇文章主要實現(xiàn)四種數(shù)據(jù)結(jié)構(gòu),分別是數(shù)組、堆棧、隊列、鏈表。大家都知道可以用C語言實現(xiàn)這幾種數(shù)據(jù)結(jié)構(gòu),其實Python也可以實現(xiàn),下面跟著小編一起來學(xué)習(xí)。
    2016-08-08
  • Python基于Opencv識別兩張相似圖片

    Python基于Opencv識別兩張相似圖片

    這篇文章主要介紹了Python基于Opencv識別兩張相似圖片的步驟,幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下
    2021-04-04
  • Python基于matplotlib實現(xiàn)繪制三維圖形功能示例

    Python基于matplotlib實現(xiàn)繪制三維圖形功能示例

    這篇文章主要介紹了Python基于matplotlib實現(xiàn)繪制三維圖形功能,涉及Python使用matplotlib模塊進行三維圖形繪制相關(guān)操作技巧,需要的朋友可以參考下
    2018-01-01
  • python3使用pandas獲取股票數(shù)據(jù)的方法

    python3使用pandas獲取股票數(shù)據(jù)的方法

    今天小編就為大家分享一篇python3使用pandas獲取股票數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12

最新評論

瑞安市| 乌兰浩特市| 万盛区| 石家庄市| 米易县| 五常市| 积石山| 上杭县| 弥渡县| 华容县| 昌吉市| 界首市| 池州市| 册亨县| 葵青区| 公主岭市| 色达县| 青铜峡市| 彩票| 新沂市| 夏邑县| 台山市| 江孜县| 黎平县| 临武县| 且末县| 美姑县| 广水市| 汶川县| 巨野县| 桂东县| 阿拉善右旗| 榕江县| 邵武市| 太谷县| 乐平市| 福清市| 吉安市| 城口县| 二连浩特市| 嵊泗县|