如何在 tensorflow (keras) 中使用 GPU 和 CPU? [英] How to use GPU and CPU in tensorflow (keras)?
问题描述
我正在使用 tf.keras 运行内存密集型(估计约为 6GB)GAN 模型,我的 GPU 似乎无法处理(预测失败,仅报告 nans).有没有办法用系统内存支持我的 4GB GPU 内存?或者一种在 GPU 和 CPU 之间共享计算工作的方法?
I am running a rather memory-intense (estimated to be around 6GB) GAN model with tf.keras which my GPU doesn't seem to be able to handle (predicting fails, reports only nans). Is there a way to support my 4GB GPU memory with system memory? Or a way to share the computational effort between GPU and CPU?
- 操作系统:Windows 10 64 位
- GPU:Geforce GTX 960 (4GB)
- CPU:英特尔至强-E3 1231 v3(4 核)
- Python GUI:Spyder 5
- Python:3.8.5/3.8.10 在 conda 环境中,仅安装了 tensorflow 和 chess 模块
- 张量流:2.5
- CUDA:11.2.2
- cudnn:8.1.1
有关更多信息,请参阅我几天前问的这个问题的非常详细的版本(没有回复,因此是这个):切换到 GPU 后 TF 模型不再预测
For more information see my very detailed version of this question I asked a couple of days ago (no responses, hence this one): TF model doesn't predict anymore after switching to GPU
推荐答案
解决方案:
在张量流中使用 gpu
训练模型与使用 python keras
的任何操作系统相同.当你训练模型时,你将训练函数包装在with
语句指定 gpu 编号
作为 tf.device 方法
Solution:
In tensor flow to train a model with a gpu
is the same with any operating system when using python keras
.When you train the model you wrap your training function in a with
statement specifying the gpu number
as a argument for the tf.device method
这是代码的模板:
import tensorflow as tf
with tf.device('/device:GPU:<The gpu number>'):
history = model.fit(<your traning info>)
否则,如果您缺乏 RAM CPU GPU 等资源,请尝试使用 google colab 免费可以免费访问许多 GPU 的 CPU 和 RAM 来编程张量流的环境
Otherwise if you lack the resources such as RAM CPU GPU then try to use google colab a free environment to program tensor flow with access to many GPUS's CPU's and RAM for free
这篇关于如何在 tensorflow (keras) 中使用 GPU 和 CPU?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!