[英]When using tf.data in tensorflow for importing data from text files, memory used up
我想使用tensorflow訓練RNN和密集模型,數據文件太大而無法輸入到內存,因此我使用tf.data
模塊從文件生成批處理數據。
數據分為7部分:
第一列:atom_length;
第二列:relation_length;
第3〜1502列:原子信息
1503〜2202欄:相關信息
2203〜3602欄:蛋白質信息
3603欄:protein_length
最后一欄:標簽
代碼如下:
import tensorflow as tf
import time
NUM_EPOCHS=10
BATCH_SIZE=128
# default column types
default_column_value = [[0] for i in range(2)]
default_column_value.extend([[0.0] for i in range(3600)])
default_column_value.extend([[0] for i in range(2)])
# using tf.data module to generate the dataset and an iterator
filenames=tf.constant(["F:\\train1_2.txt"])
dataset = tf.data.TextLineDataset(filenames)
dataset = dataset.map(lambda line: tf.decode_csv(
line, record_defaults=default_column_value)).repeat(NUM_EPOCHS).shuffle(buffer_size=1000).batch(BATCH_SIZE)
iterator = dataset.make_initializable_iterator()
next_element = iterator.get_next()
# stack specific columns, these are the data used for feeding to the placeholders
atom_length=next_element[0]
relation_length=next_element[1]
atom = tf.stack(next_element[2:1502],axis=1)
relation = tf.stack(next_element[1502:2202],axis=1)
protein_sequence = tf.stack(next_element[2202:3602],axis=1)
protein_length= next_element[-2]
labels = next_element[-1]
with tf.Session() as sess:
tf.global_variables_initializer().run()
tf.local_variables_initializer().run()
sess.run(iterator.initializer)
step=0
epoch=1
try:
epoch_start_time=time.time()
while(True):
one_step_time=time.time()
step=step+1
if step%(int(489548//BATCH_SIZE+1))==0:
print("epoch_used_time:"+str(time.time()-epoch_start_time))
epoch_start_time=time.time()
epoch+=1
# generate the batch data used for training
cur_atom_length_batch,cur_relation_length_batch,cur_protein_length_batch,cur_atom_batch,cur_relation_batch,cur_protein_sequence_batch,cur_labels_batch=sess.run(
[atom_length,relation_length,protein_length,atom,relation,protein_sequence,labels])
input_labels=sess.run(tf.one_hot(cur_labels_batch,depth=2,on_value=1,off_value=0))
print("cur_atom_length_batch:",cur_atom_length_batch)
print("cur_relation_length_batch:",cur_relation_length_batch)
print("cur_atom_batch:",cur_atom_batch)
print("cur_protein_length_batch:",cur_protein_length_batch)
except tf.errors.OutOfRangeError:
print("end of epochs.")
pass
finally:
print('epoch time:',time.time()-epoch_start_time)
當我運行代碼時,盡管數據是成批生成的,但是本地內存使用率卻越來越高。
train1_2.txt文件為3.75GB,即使第1個紀元訓練尚未完成,32GB的本地內存也幾乎用完了! 這可能是什么原因? 我的代碼有什么問題?
我在其中使用的環境是GTX1080 GPU,i7處理器,32GB內存,Windows 7。
代碼的主要問題是,每次迭代都向計算圖添加新操作。 看到這一行:
input_labels=sess.run(tf.one_hot(cur_labels_batch,depth=2,on_value=1,off_value=0))
在此處調用tf.one_hot
將為圖形添加新操作。 您可以在每個批次中添加這樣的操作。 您要做的是將該操作放在訓練循環之外,然后在訓練循環中評估其輸出,而不是創建一個新的訓練,如下所示:
one_hot = tf.one_hot(cur_labels_batch,depth=2,on_value=1,off_value=0)
# other necessary code here
while training:
# ....
other_ops_result, input_labels = sess.run([other_ops, one_hot])
經驗法則:請勿在訓練循環內調用任何tf
名稱空間操作,除非您明確希望向計算圖添加新操作。 請記住,一旦添加,就無法刪除它,除非有必要,否則可能會減慢您的代碼和/或增加內存消耗。
我聽說這是人們在使用張量流時遇到的常見錯誤。 所以我做了研究。我想知道代碼中下面的兩行是否正常工作。
tf.global_variables_initializer().run()
tf.local_variables_initializer().run()
sess.run(iterator.initializer)
session.run(tf.global_variables_initializer())
feed_dict
內部參數sess.run()
而不是使用其他tf
的內部操作sess.run()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.