繁体   English   中英

tensorflow(keras)中如何使用GPU和CPU?

[英]How to use GPU and CPU in tensorflow (keras)?

我正在运行带有 tf.keras 的相当内存密集型(估计约为 6GB)GAN model,我的 GPU 无法处理(报告似乎无法处理) 有没有办法用系统 memory 支持我的 4GB GPU memory? 还是一种在 GPU 和 CPU 之间共享计算工作的方法?

我的规格:

  • 操作系统:Windows 10 64
  • GPU:Geforce GTX 960 (4GB)
  • CPU:Intel Xeon-E3 1231 v3(4核)
  • Python GUI:Spyder 5
  • Python:3.8.5 / 3.8.10 在仅安装了 tensorflow 和国际象棋模块的 conda 环境中
  • Tensorflow:2.5
  • CUDA:11.2.2
  • cudnn: 8.1.1

有关更多信息,请参阅我几天前提出的这个问题的非常详细的版本(没有回应,因此这个问题): TF model 在切换到 GPU 后不再预测

解决方案:

In tensor flow to train a model with a gpu is the same with any operating system when using python keras 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM