jupyter notebook上使用GPU运行程序

问题描述

虽然jupyter上能后直接运行代码,但是默认是使用的cpu,我的电脑支持GPU,虽然也安装了tensorflow-gpu,但是运行的时候没有使用上。网上找了很多办法, 最后解决,做个记录。

原因分析:

我安装了anacnda,所以我本来打算在conda上面去安装tensorflow-gpu,但是好像没有什么用,后面才发现,不是没用,是没用对。

解决方案:

在环境这里,创建了一个tensorflow-gpu,然后要在右侧的channel里面选择uninstalled,把tensorflow-gpu安装好。教程网上有,这么不赘述。
jupyter notebook上使用GPU运行程序
之后,您可以参考此处的步骤进行配置。
https://blog.csdn.net/gangeqian2/article/details/79358543
https://blog.csdn.net/weixin_43355451/article/details/96487678

activative tensorflow-gpu很重要,当你再conda里面激活以后,就会看到账号变了,一开始默认的是base,
jupyter notebook上使用GPU运行程序

激活以后目录会变成tensorflow-gpu
jupyter notebook上使用GPU运行程序
再这里去启动jupyter notebook

然后使用这个代码去试试看,如果返回的数据包含了gpu版本,那就能使用。
jupyter notebook上使用GPU运行程序
用with tf.device(’/gpu:0’): 去调用GPU运行程序。可以自行找一个训练数据,看看cpu和gpu的速度区别。

import os
os.environ["CUDA_VISIBLE_DEVICES"]="0"
with tf.device('/gpu:0'):
    tf.compat.v1.disable_eager_execution()
    hello=tf.constant('Hello,TensorFlow')
    config=tf.compat.v1.ConfigProto()
    config.gpu_options.per_process_gpu_memory_fraction = 0.9
    sess=tf.compat.v1.Session(config=config)
    print(sess.run(hello))

对了,还有一个问题,是安装tensorflow-gpu的时候遇到的,报错的是

conda-repo-cli 1.0.4 requires pathlib which is not installed

/一开始是直接去pip install pathlib,但是还是报错,后面发现,可以去anaconda上安装这个,
jupyter notebook上使用GPU运行程序
因为在pip上没法安装,一直报错,这个安装后就没报错了。

文章引用了两位博主的文章链接,致敬。

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
扎眼的阳光的头像扎眼的阳光普通用户
上一篇 2022年3月21日 下午1:19
下一篇 2022年3月21日 下午1:36

相关推荐