jupyter notebook上使用GPU运行程序

问题描述

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

原因分析:

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

解决方案:

在环境这里,创建了一个tensorflow-gpu,然后要在右侧的channel里面选择uninstalled,把tensorflow-gpu安装好。教程网上有,这么不赘述。

jupyter notebook上使用GPU运行程序
之后,您可以使用此处的步骤进行配置。
[En]

After that, you can use the steps here for configuration.

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上没法安装,一直报错,这个安装后就没报错了。

这篇文章引用了两位博主之间的链接来表达敬意。

[En]

The article quoted a link between the two bloggers to pay tribute.

Original: https://blog.csdn.net/m0_67394360/article/details/123604332
Author: 免费的东西
Title: jupyter notebook上使用GPU运行程序

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/508783/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球