(原)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注册的函数 ...
随机推荐
- iOS开发--知识点总结
1 .全局变量,变量名前加下划线.和系统一致. 2 . nil指针为空 @“”字符串为空 (内容为空) == 判断内存地址 基本变量 对于一些基本类型 可以使用==来判断, ...
- 读书笔记,《Java8实战》第一章,为什么要关心 Java8
开篇作者就提出,Java8所做的改变在许多方面比java历史上任何一次改变都深远.而且好消息是,这些改变会让你编辑程序来更容易,再也不用写类似类似于以前的swing中的事件处理函数的啰嗦代码了. ...
- 如何清空IFRAME中的HTML
window.frames["ifra"].document.write(""); window.frames["ifra"].docume ...
- paypal 的IPN通知调用出错
一直报错: 当本地curl需要访问https时,出现SSL certificate: unable to get local issuer certificate错误信息 解决办法: 到http:// ...
- Eclipse技术: 项目文件中过滤.o文件
1. 右建项目 -> Properties. 2. 增加过滤规则
- 主成分分析(PCA)原理及推导
原文:http://blog.csdn.net/zhongkejingwang/article/details/42264479 什么是PCA? 在数据挖掘或者图像处理等领域经常会用到主成分分析,这样 ...
- HttpWebRequest: Remote server returns error 503 Server Unavailable
I have a client server application written in C# .Net 2.0. I have had the client/server response/r ...
- 多模块Maven项目如何使用javadoc插件生成文档
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 需求 最近要对一个项目结构如下的Maven项目生成JavaDoc文档. Project ...
- iOS 在不添加库的情况下 通过抽象类来获取自己想要的方法
#define SYSTEM_VERSION_MORE_THAN_BFDATA(v) ([[[UIDevice currentDevice] systemVersion] compare:v opti ...
- 获取自增ID
方法一(一般不使用,多线程可能不准确) SELECT MAX(id) FROM table 方法二(一般使用这个) SELECT LAST_INSERT_ID()