我在这个关于CNN的博客中对代码做了一些修改,以便进行文本分类:http://www.wildml.com/2015/12/implementing-a-cnn-for-text-classification-in-tensorflow/
一切都很好!但是,当我尝试使用经过训练的模型来预测新的实例时,它消耗了所有可用的内存。似乎在一次又一次地评估和加载所有模型时,它并没有释放任何内存。据我所知,在每个sess.run命令之后都应该释放内存。
下面是我正在使用的代码的一部分:
with graph.as_default():
session_conf = tf.ConfigProto(
allow_soft_placement=FLAGS.allow_soft_placement,
log_device_placement=FLAGS.log_device_placement)
sess = tf.Session(config=session_conf)
with sess.as_default():
# Load the saved meta graph and restore variables
saver = tf.train.import_meta_graph("{}.meta".format(checkpoint_file))
saver.restore(sess, checkpoint_file)
# Get the placeholders from the graph by name
input_x = graph.get_operation_by_name("input_x").outputs[0]
# input_y = graph.get_operation_by_name("input_y").outputs[0]
dropout_keep_prob = graph.get_operation_by_name("dropout_keep_prob").outputs[0]
# Tensors we want to evaluate
predictions = graph.get_operation_by_name("output/predictions").outputs[0]
# Add a vector for probas
probas =graph.get_operation_by_name("output/scores").outputs[0]
# Generate batches for one epoch
print("\nGenerating Bathces...\n")
gc.collect()
#mem0 = proc.get_memory_info().rss
batches = data_helpers.batch_iter(list(x_test), FLAGS.batch_size, 1, shuffle=False)
#mem1 = proc.get_memory_info().rss
print("\nBatches done...\n")
#pd = lambda x2, x1: 100.0 * (x2 - x1) / mem0
#print "Allocation: %0.2f%%" % pd(mem1, mem0)
# Collect the predictions here
all_predictions = []
all_probas = []
for x_test_batch in batches:
#Calculate probability of prediction been good
gc.collect()
batch_probas = sess.run(tf.reduce_max(tf.nn.softmax(probas),1), {input_x: x_test_batch, dropout_keep_prob: 1.0})
batch_predictions = sess.run(predictions, {input_x: x_test_batch, dropout_keep_prob: 1.0})
all_predictions = np.concatenate([all_predictions, batch_predictions])
all_probas = np.concatenate([all_probas, batch_probas])
# Add summary ops to collect data
with tf.name_scope("eval") as scope:
p_h = tf.histogram_summary("eval/probas", batch_probas)
summary= sess.run(p_h)
eval_summary_writer.add_summary(summary)任何帮助都将不胜感激。
干杯
发布于 2016-12-21 04:51:12
您的培训循环在每次迭代中创建新的TensorFlow操作(tf.reduce_max()、tf.nn.softmax()和tf.histogram_summary()),这将导致随着时间的推移占用更多的内存。当您多次运行相同的图时,TensorFlow是最有效的,因为它可以将优化图的成本分摊到多个执行中。因此,为了获得最好的性能,您应该修改程序,以便在循环之前,只创建一次,然后在每次迭代中重复使用相同的操作。
https://stackoverflow.com/questions/41255031
复制相似问题