tensorflow无法使用GPU

当TensorFlow无法使用GPU时,可以通过指定设备分配及查看设备调用来解决。在Jupyter Notebook中,log_device_placement可能无法显示设备信息,但可以通过partition_graphs方式在IDE中打印出来。

代码一:
选用默认设备计算

import tensorflow as tf
with tf.device('/gpu:0'):#选用默认设备(gpu:0)计算以下张量
    inputs = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
    weights = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
    bias = tf.zeros(shape=[weights.get_shape()[-1]])
    logits = tf.add(tf.matmul(inputs,weights),bias)

若tensorflow无法使用GPU加速,可以加上以下代码,可以动态分配cpu与gpu,并可以查看设备调用信息

#gpu_options
#gpu_options = tf.GPUOptions(allow_growth=True) #允许GPU内存分配动态增加
gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.4)#限制GPU使用率为0.4
#以上两gpu_options取其一

# allow_soft_placement:允许动态分配gpu内存
# log_device_placement:打印出设备信息
config = tf.ConfigProto(
    allow_soft_placement=True,log_device_placement=True,gpu_options=gpu_options)

with tf.Session(config=config) as sess:
    sess.run(tf.global_variables_initializer())
    print("logits:",sess.run(logits))

:log_device

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值