tensorflow tf.train.batch之?dāng)?shù)據(jù)批量讀取方式
在進(jìn)行大量數(shù)據(jù)訓(xùn)練神經(jīng)網(wǎng)絡(luò)的時(shí)候,可能需要批量讀取數(shù)據(jù)。于是參考了這篇文章的代碼,結(jié)果發(fā)現(xiàn)數(shù)據(jù)一直批量循環(huán)輸出,不會(huì)在數(shù)據(jù)的末尾自動(dòng)停止。
然后發(fā)現(xiàn)這篇博文說slice_input_producer()這個(gè)函數(shù)有一個(gè)形參num_epochs,通過設(shè)置它的值就可以控制全部數(shù)據(jù)循環(huán)輸出幾次。
于是我設(shè)置之后出現(xiàn)以下的報(bào)錯(cuò):
tensorflow.python.framework.errors_impl.FailedPreconditionError: Attempting to use uninitialized value input_producer/input_producer/limit_epochs/epochs [[Node: input_producer/input_producer/limit_epochs/CountUpTo = CountUpTo[T=DT_INT64, _class=["loc:@input_producer/input_producer/limit_epochs/epochs"], limit=2, _device="/job:localhost/replica:0/task:0/cpu:0"](input_producer/input_producer/limit_epochs/epochs)]]
找了好久,都不知道為什么會(huì)錯(cuò),于是只好去看看slice_input_producer()函數(shù)的源碼,結(jié)果在源碼中發(fā)現(xiàn)作者說這個(gè)num_epochs如果不是空的話,就是一個(gè)局部變量,需要先調(diào)用global_variables_initializer()函數(shù)初始化。
于是我調(diào)用了之后,一切就正常了,特此記錄下來,希望其他人遇到的時(shí)候能夠及時(shí)找到原因。
哈哈,這是筆者第一次通過閱讀源碼解決了問題,心情還是有點(diǎn)小激動(dòng)。啊啊,扯遠(yuǎn)了,上最終成功的代碼:
import pandas as pd import numpy as np import tensorflow as tf def generate_data(): num = 25 label = np.asarray(range(0, num)) images = np.random.random([num, 5]) print('label size :{}, image size {}'.format(label.shape, images.shape)) return images,label def get_batch_data(): label, images = generate_data() input_queue = tf.train.slice_input_producer([images, label], shuffle=False,num_epochs=2) image_batch, label_batch = tf.train.batch(input_queue, batch_size=5, num_threads=1, capacity=64,allow_smaller_final_batch=False) return image_batch,label_batch images,label = get_batch_data() sess = tf.Session() sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer())#就是這一行 coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess,coord) try: while not coord.should_stop(): i,l = sess.run([images,label]) print(i) print(l) except tf.errors.OutOfRangeError: print('Done training') finally: coord.request_stop() coord.join(threads) sess.close()
以上這篇tensorflow tf.train.batch之?dāng)?shù)據(jù)批量讀取方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
tensorflow學(xué)習(xí)筆記之tfrecord文件的生成與讀取
這篇文章主要介紹了tensorflow學(xué)習(xí)筆記之tfrecord文件的生成與讀取,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03Python導(dǎo)入Excel數(shù)據(jù)表的幾種實(shí)現(xiàn)方式
在Python中可以使用許多庫來處理Excel文件,下面這篇文章主要給大家介紹了關(guān)于Python導(dǎo)入Excel數(shù)據(jù)表的幾種實(shí)現(xiàn)方式,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01Python中UserWarning:The NumPy module was
在 Python 項(xiàng)目中,我們經(jīng)常需要導(dǎo)入許多庫來完成各種任務(wù),NumPy 作為一個(gè)核心的科學(xué)計(jì)算庫,被廣泛應(yīng)用于數(shù)據(jù)處理和分析,然而,有時(shí)我們會(huì)遇到 NumPy 重載的警告,本文將詳細(xì)講解這一警告的原因,并提供解決方案,需要的朋友可以參考下2024-07-07python和pygame實(shí)現(xiàn)簡單俄羅斯方塊游戲
這篇文章主要為大家詳細(xì)介紹了python和pygame實(shí)現(xiàn)簡單俄羅斯方塊游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06