17、TensorFLow GPU 的使用
来源:互联网 发布:标书制作软件 编辑:程序博客网 时间:2024/05/19 18:37
一、TensorFlow 设备分配
1、设备分配规则
If a TensorFlow operation has both CPU and GPU implementations, the GPU devices will be given priority when the operation is assigned to a device.
2、手动指定设备分配
- 如果你不想让系统自动为 operation 分配设备, 而是自己手动指定, 可以用
with tf.device
创建一个设备环境, 这个环境下的 operation 都统一运行在指定的设备上. - 代码示例如下:
# op 在 cpu 上运算with tf.device('/cpu:0'): a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a') b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')# op 在 gpu 上运算with tf.device('/device:GPU:2'): a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a') b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')# op 在 gpus 上运算for d in ['/device:GPU:2', '/device:GPU:3']: with tf.device(d): a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3]) b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2])
二、TensorFlow GPU 配置
这里直接引用师弟的blog—TensorFlow对你的GPU其实不霸道(写的很好) ,稍加修改和总结,具体在程序中直接使用第 4 条总结的使用经验即可
1、半遮面——指定可以被看见的GPU设备
import os# 默认情况,TF 会占用所有 GPU 的所有内存, 我们可以指定# 只有 GPU0 和 GPU1 这两块卡被看到,从而达到限制其使用所有GPU的目的os.environ['CUDA_VISIBLE_DEVICES'] = '0, 1' # 打印 TF 可用的 GPUprint os.environ['CUDA_VISIBLE_DEVICES']>>> 0, 1
2、圈其地——限定使用显存的比例
# 在开启对话session前,先创建一个 tf.ConfigProto() 实例对象# 通过 allow_soft_placement 参数自动将无法放在 GPU 上的操作放回 CPUgpuConfig = tf.ConfigProto(allow_soft_placement=True)# 限制一个进程使用 60% 的显存gpuConfig.gpu_options.per_process_gpu_memory_fraction = 0.6# 把你的配置部署到sessionwith tf.Session(config=gpuConfig) as sess: pass这样,如果你指定的卡的显存是8000M的话,你这个进程只能用4800M。
3、任其行——需要多少拿多少
# 在开启对话session前,先创建一个 tf.ConfigProto() 实例对象# 通过 allow_soft_placement 参数自动将无法放在 GPU 上的操作放回 CPUgpuConfig = tf.ConfigProto(allow_soft_placement=True)# 运行时需要多少再给多少gpuConfig.gpu_options.allow_growth = True # 把你的配置部署到sessionwith tf.Session(config=gpuConfig) as sess: pass
4、GPU 使用总结
import osos.environ['CUDA_VISIBLE_DEVICES'] = '0, 1' gpuConfig = tf.ConfigProto(allow_soft_placement=True)gpuConfig.gpu_options.allow_growth = True with tf.Session(config=gpuConfig) as sess: pass
三、参考资料
1、https://www.tensorflow.org/tutorials/using_gpu
2、TensorFlow对你的GPU其实不霸道
阅读全文
0 0
- 17、TensorFLow GPU 的使用
- tensorflow指定gpu的使用
- tensorflow中使用指定的GPU及GPU显存
- tensorflow 自主选择使用的gpu
- tensorflow gpu使用情况
- tensorflow:GPU使用
- tensorflow GPU使用问题
- tensorflow 使用ConfigProto&GPU
- tensorflow使用gpu
- 【tensorflow 学习】 gpu使用
- Tensorflow-gpu的错误
- tensorflow 不使用GPU 无法利用GPU
- Tensorflow指定使用哪个GPU
- 指定TensorFlow使用哪一个GPU
- 基于tensorflow的keras中GPU的使用
- tensorflow使用GPU训练时的显存占用问题
- tensorflow使用GPU训练时的显存占用问题
- tensorflow多机/多GPU训练时使用的术语
- 单链表及其应用
- STM32 KEIL下的堆栈设置
- 给定时间秒数,如何快速找到对应的年月日时分秒
- Spring Resttemplate post方法踩坑记录
- Camera Filter 美颜相机的实现
- 17、TensorFLow GPU 的使用
- iOS 给UIImage添加边框(直接加在UIImage上)
- python里使用difflib库的比较文本时丢弃不要的字符
- docker coentos7 tensorflow
- 习题6(6.13)
- [通过scikit-learn掌握机器学习] 02 线性回归
- C 自定义字符串输出
- Spiral Matrix II
- 18、使用 tf.app.flags 接口定义命令行参数