python-3.x - 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
解决方案
您可以通过运行以下代码段来设置要使用的 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
。如果需要,请重新启动内核。
推荐阅读
- swift - 如何使用 where 关键字快速检查 switch 语句中的所有情况?
- javascript - 希望按子数组中的值对数组进行分组
- android - 如何使用外部开关盒的价值?
- prolog - Prolog:列出事实
- android-ndk - 在 NDK16 之后使用 Android 处理 autoconf
- php - 使用 foreach 和 mysqli_fetch_all 时的正确语法是什么
- android - 什么是 Tools:itemCount 等效于 ExpandableListView?
- c - Rand() 在 C 中不是唯一的
- kubernetes - Grunt watch 在 Kubernetes 中使用 Sails.js 引发 sync:dev not found 错误
- python - 在 python pandas 数据框中分箱(不手动设置箱)