(原)tensorflow中函数执行完毕,显存不自动释放
转载请注明出处:
http://www.cnblogs.com/darkknightzh/p/7608916.html
参考网址:
https://stackoverflow.com/questions/39758094/clearing-tensorflow-gpu-memory-after-model-execution
https://github.com/tensorflow/tensorflow/issues/1727#issuecomment-285815312s
tensorflow中,在一个函数内配置完GPU,tf分配了显存,等函数执行完,显存不会释放(貌似torch7中也一样。。。)。第二个参考网址指出:
As for the original problem, currently the Allocator in the GPUDevice belongs to the ProcessState, which is essentially a global singleton. The first session using GPU initializes it, and frees itself when the process shuts down. Even if a second session chooses a different GPUOptions, it would not take effect.
第一个session对GPU初始化后,即便释放了显存,第二个sess使用不同的GPU选项来初始化GPU,也不会起效。
第一个网址Oli Blum指出,use processes and shut them down after the computation才能释放显存。具体代码如下(可以参考第一个网址):
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() # wait until user presses enter key
raw_input() # option 2: just execute the function
run_tensorflow() # wait until user presses enter key
raw_input()
使用multiprocessing.Process运行run_tensorflow后,显存会自动释放,但是如果直接执行run_tensorflow,显存不会自动释放。当然,该函数计算量较小,如果显卡太好,可能看不到运行multiprocessing.Process后,显存分配、计算并释放的过程,感觉就像没有运行一样。。。
(原)tensorflow中函数执行完毕,显存不自动释放的更多相关文章
- Tensorflow与Keras自适应使用显存
Tensorflow支持基于cuda内核与cudnn的GPU加速,Keras出现较晚,为Tensorflow的高层框架,由于Keras使用的方便性与很好的延展性,之后更是作为Tensorflow的官方 ...
- TensorFlow,Keras限制GPU显存
运行TensorFlow程序会占用过多的显卡比例,多人共同使用GPU的时候,会造成后面的人无法运行程序. 一.TensorFlow 1.预加载比例限制 tf_config = tensorflow.C ...
- JS中函数执行顺序的问题?
作者:知乎用户链接:https://www.zhihu.com/question/23564807/answer/82996422来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- mvc项目中Controller执行完毕重定向到html的一个页面中
String ip = request.getLocalAddr(); //取得服务器IP int port = request.getLocalPort(); //取得服务器端口 String ur ...
- 深度学习中GPU和显存分析
刚入门深度学习时,没有显存的概念,后来在实验中才渐渐建立了这个意识. 下面这篇文章很好的对GPU和显存总结了一番,于是我转载了过来. 作者:陈云 链接:https://zhuanlan.zhihu. ...
- [Pytorch]深度模型的显存计算以及优化
原文链接:https://oldpan.me/archives/how-to-calculate-gpu-memory 前言 亲,显存炸了,你的显卡快冒烟了! torch.FatalError: cu ...
- 解决GPU显存未释放问题
前言 今早我想用多块GPU测试模型,于是就用了PyTorch里的torch.nn.parallel.DistributedDataParallel来支持用多块GPU的同时使用(下面简称其为Dist). ...
- 如何理解javaSript中函数的参数是按值传递
本文是我基于红宝书<Javascript高级程序设计>中的第四章,4.1.3传递参数小节P70,进一步理解javaSript中函数的参数,当传递的参数是对象时的传递方式. (结合资料的个人 ...
- main函数执行前、后再执行的代码
一.main结束 不代表整个进程结束 (1)全局对象的构造函数会在main 函数之前执行, 全局对象的析构函数会在main函数之后执行: 用atexit注册的函数 ...
随机推荐
- Permutations leetcode java
题目: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the f ...
- 用css3和jQuery制作精美的表单
用css3和jQuery制作一个简单的精美表单 html代码如下: <span class="title">Mask Your Input Forms and Make ...
- 深度学习哪家强?吴恩达、Udacity和Fast.ai的课程我们替你分析好了
http://www.jianshu.com/p/28f5473c66a3 翻译 | AI科技大本营(rgznai100) 参与 | reason_W 引言 过去2年,我一直积极专注于深度学习领域.我 ...
- Arrow functions and the ‘this’ keyword
原文:https://medium.freecodecamp.org/learn-es6-the-dope-way-part-ii-arrow-functions-and-the-this-keywo ...
- AVR单片机最小系统 基本硬件线路与分析
单片机最小系统 单片机最小系统设计 AVR基本硬件线路设计与分析 (ATmega16功能小板) AVR DB-CORE Ver2.3 Atmega16开发板 本站商城提供本最小系统销售:99元 AV ...
- php代码收集
thinkphp <?php class HekaAction extends BaseAction{ public function index(){ require_once './wang ...
- HTTP 响应实体主体:XML 及 XML parser
本文内容 HTTP 响应实体主体:XML XML parser 总结 各编程语言实现的 XML parser HTTP 响应实体主体:XML 实体主体(entity-body)通常是HTTP响应里 ...
- 解决 IllegalArgumentException: Could not resolve placeholder in string value "${XXXXXX}"
如题: 导致这一问题的原因:使用了重复的property-placeholder 如一个配置文件中使用了 <context:property-placeholder location=" ...
- C/C++中的值传递,引用传递,指针传递,指针引用传递
在面试过程中,被面试官问到传值和传引用的区别,之前没有关注过这个问题,今天在网上找了一篇包含代码和图片的讲解文章,浅显易懂,遂转载备忘. 1. 值传递 void f( int p){ printf(& ...
- JAVA的PreparedStatement和addBatch()方法
本文介绍两个内容,为什么使用PreparedStatement的addBatch()方法?以及使用PreparedStatement的好处. 一.addBatch使用方法 昨天用JAVA做了一个导表的 ...