在 tensorflow 中使用 tf.data 从文本文件导入数据时,内存已用完

When using tf.data in tensorflow for importing data from text files, memory used up

我想用tensorflow训练一个RNN和dense模型,数据文件太大无法输入内存,所以我使用tf.data模块从文件生成批量数据。

数据有7个部分:

第一个column:atom_length;

第二个column: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,32GB本地内存连第1个epoch训练还没做完就快用完了! 这可能是什么原因?我的代码有什么问题?

我使用的环境是GTX1080 GPU,i7处理器,32GB内存,Windows7.

我听说这是人们在使用tensorflow时经常遇到的错误。 所以我做了我的研究..和 我想知道您的代码中下面的两行是否有效。

tf.global_variables_initializer().run()
tf.local_variables_initializer().run()
sess.run(iterator.initializer)
  1. 前两行不是重复的吗?
  2. 下面的不是正确的实现吗?

session.run(tf.global_variables_initializer())

  1. 此处不完全确定,但请检查此 link。你可以尝试使用

feed_dict 参数在 sess.run() 中,而不是在 sess.run()

中使用另一个 tf 操作

您的代码的主要问题是您在每次迭代时都向计算图添加新操作。看到这一行:

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 命名空间操作,除非您明确想要向计算图添加新操作。请记住,一旦添加,就无法删除,并且可能会减慢您的代码速度 and/or 除非必要,否则会增加内存消耗。