tensorflow tf.train.batch之數(shù)據(jù)批量讀取方式
在進行大量數(shù)據(jù)訓練神經網(wǎng)絡的時候,可能需要批量讀取數(shù)據(jù)。于是參考了這篇文章的代碼,結果發(fā)現(xiàn)數(shù)據(jù)一直批量循環(huán)輸出,不會在數(shù)據(jù)的末尾自動停止。
然后發(fā)現(xiàn)這篇博文說slice_input_producer()這個函數(shù)有一個形參num_epochs,通過設置它的值就可以控制全部數(shù)據(jù)循環(huán)輸出幾次。
于是我設置之后出現(xiàn)以下的報錯:
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)]]
找了好久,都不知道為什么會錯,于是只好去看看slice_input_producer()函數(shù)的源碼,結果在源碼中發(fā)現(xiàn)作者說這個num_epochs如果不是空的話,就是一個局部變量,需要先調用global_variables_initializer()函數(shù)初始化。
于是我調用了之后,一切就正常了,特此記錄下來,希望其他人遇到的時候能夠及時找到原因。
哈哈,這是筆者第一次通過閱讀源碼解決了問題,心情還是有點小激動。啊啊,扯遠了,上最終成功的代碼:
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之數(shù)據(jù)批量讀取方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
tensorflow學習筆記之tfrecord文件的生成與讀取
這篇文章主要介紹了tensorflow學習筆記之tfrecord文件的生成與讀取,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
Python導入Excel數(shù)據(jù)表的幾種實現(xiàn)方式
在Python中可以使用許多庫來處理Excel文件,下面這篇文章主要給大家介紹了關于Python導入Excel數(shù)據(jù)表的幾種實現(xiàn)方式,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-01-01
Python中UserWarning:The NumPy module was
在 Python 項目中,我們經常需要導入許多庫來完成各種任務,NumPy 作為一個核心的科學計算庫,被廣泛應用于數(shù)據(jù)處理和分析,然而,有時我們會遇到 NumPy 重載的警告,本文將詳細講解這一警告的原因,并提供解決方案,需要的朋友可以參考下2024-07-07
python和pygame實現(xiàn)簡單俄羅斯方塊游戲
這篇文章主要為大家詳細介紹了python和pygame實現(xiàn)簡單俄羅斯方塊游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06

