首页 > 解决方案 > Keras 可以不加载太多 CPU 和 GPU 吗?

问题描述

我的电脑不是那么好:i7-2600 和 GeForce GTX 970。但是当我启动使用 CNN 对照片进行分类的脚本时,我的系统加载不多:CPU 为 20%,GPU 为 5%。

看起来 GPU 可用:

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
[name: "/device:CPU:0"
device_type: "CPU"
memory_limit: 268435456
locality {
}
incarnation: 4011293230370962537
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 3110401638
locality {
  bus_id: 1
  links {
  }
}
incarnation: 3815751383857027347
physical_device_desc: "device: 0, name: GeForce GTX 970, pci bus id: 0000:01:00.0, compute capability: 5.2"
]

我的 Keras 版本是

print(keras.__version__)
2.3.1

我的 TensorFlow 版本是

print(tensorflow.__version__)
1.14.0

标签: python-3.xkeras

解决方案


您可以通过运行以下代码段来设置要使用的 GPU 分数:

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=1) #number between 0 and 1
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))

根据经验,您必须在执行后立即运行它import tensorflow。如果需要,请重新启动内核。


推荐阅读