https://stackoverflow.com/questions/39758094/clearing-tensorflow-gpu-memory-after-model-execution
由于tensorflow中使用完gpu后并不会自动释放显存,所以在循环利用同一个计算图时会不停的累加显存,无法释放显存。
然后从网上仅发现一种可行方案,即每次执行完计算图后都关闭当前进程,然后用其他进程继续进行这个计算图的计算。这样就可以每次执行完计算图之后自动化的释放显存。
import tensorflow as tf
import multiprocessing
import numpy as np
def run_tensorflow():
n_input = 10000
n_classes = 1000
# Create model
def multilayer_perceptron(x, weight):
# Hidden layer with RELU activation
layer_1 = tf.matmul(x, weight)
return layer_1
# Store layers weight & bias
weights = tf.Variable(tf.random_normal([n_input, n_classes]))
x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None, n_classes])
pred = multilayer_perceptron(x, weights)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
optimizer = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for i in range(100):
batch_x = np.random.rand(10, 10000)
batch_y = np.random.rand(10, 1000)
sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y})
print "finished doing stuff with tensorflow!"
if __name__ == "__main__":
# option 1: execute code with extra process
p = multiprocessing.Process(target=run_tensorflow)
p.start()
p.join()
# option 2: just execute the function
run_tensorflow()
905



被折叠的 条评论
为什么被折叠?



