内存泄漏评估用于文本分类的 CNN 模型
Memory leak evaluating CNN model for text clasification
我一直在对这篇关于 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)
任何帮助将不胜感激
干杯
您的训练循环在每次迭代中创建新的 TensorFlow 操作(tf.reduce_max()
、tf.nn.softmax()
和 tf.histogram_summary()
),这将导致随着时间的推移消耗更多的内存。当您多次 运行 同一个图时,TensorFlow 效率最高,因为它可以分摊多次执行图的优化成本。所以,
为了获得最佳性能,您应该修改您的程序,以便在 for x_test_batch in batches:
循环之前创建每个操作 一次 ,然后在每个操作中重复使用相同的操作迭代。
我一直在对这篇关于 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)
任何帮助将不胜感激
干杯
您的训练循环在每次迭代中创建新的 TensorFlow 操作(tf.reduce_max()
、tf.nn.softmax()
和 tf.histogram_summary()
),这将导致随着时间的推移消耗更多的内存。当您多次 运行 同一个图时,TensorFlow 效率最高,因为它可以分摊多次执行图的优化成本。所以,
为了获得最佳性能,您应该修改您的程序,以便在 for x_test_batch in batches:
循环之前创建每个操作 一次 ,然后在每个操作中重复使用相同的操作迭代。