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

2024-04-26 10:55:21 发布

您现在位置:Python中文网/ 问答频道 /正文

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

数据分为7部分:

第一次列:原子长度;在

第二次列:关系长度;在

第3~1502列:原子信息

1503~2202列:关系信息

2203~3602列:蛋白质信息

3603柱:蛋白质长度

最后一列:标签

代码如下:

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的本地内存也几乎用完了! 这可能是什么原因?我的代码怎么了?在

我使用它的环境是GTX1080 GPU,i7处理器,32GB内存,windows7。在


Tags: defaultdatatimevaluetfbatchelementlength
2条回答

我听说这是人们在使用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将向图形添加一个新操作。您可以在每个批处理中添加这样的操作。您要做的是将此操作放在训练循环之外,然后在训练循环中评估其输出,而不是创建一个新的,如下所示:

^{pr2}$

经验法则:不要在训练循环中调用任何tf名称空间操作,除非您明确希望向计算图中添加新操作。请记住,一旦添加了它,它就不能被删除,并且很可能会减慢代码的速度和/或增加内存消耗,除非有必要。在

相关问题 更多 >