• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

LR233

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

3、tensorflow_gpu调用

参考文章:(8条消息) cuda+cudnn+tensorflow2.4安装血泪史_MAYYYYYYA的博客-CSDN博客

这里以安装tensorflow2.4.0为例子,其他需要你自己找到对应的版本

1、首先查看cuda版本

nvidia-smi

右上角会显示最高的版本,安装的cuda不能超过此版本

我这个地方是11.7,表示驱动程序支持最高11.7

  

 

 

 

2、安装cudatoolkit

conda install cudatoolkit=11.0

 

3、安装tensorflow

pip install tensorflow-gpu==2.4 -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

 检查一下cuda、cudnn的版本

import tensorflow as tf
build = tf.sysconfig.get_build_info()
print(build['cuda_version'])        # 11.0
print(build['cudnn_version'])       # 8

 

4、安装keras

pip install keras -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

 

5、安装opencv-python(这个链接是很厉害的源)

pip install opencv-python -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

 

6、查看gpu是否可用

import tensorflow as tf
print(tf.test.is_gpu_available())      # 如果结果是True,表示GPU可用

 

7、查看使用的是cpu还是gpu

from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

 

8、使用gpu处理

import os
# 指定使用0,1,2三块卡
os.environ["CUDA_VISIBLE_DEVICES"] = "0" 

tips:使用这段代码后重新检测当前使用的是cpu还是gpu

 

9、使用gpu还有点问题,很多没搞懂的

import tensorflow as tf
physical_devices = tf.config.experimental.list_physical_devices("GPU")
if len(physical_devices) > 0:
    tf.config.experimental.set_memory_growth(physical_devices[0],True)
    logical_devices = tf.config.list_logical_devices("GPU")

 

10、当没有gpu的时候安装tensorflow

pip install tensorflow-cpu==2.4.0 -i http://pypi.douban.com/simple/

 

11、

错误信息:

tensorflow no algorithm worked

解决方法:

把这段代码贴在最前面

from tensorflow.compat.v1 import ConfigProto
from tensorflow.compat.v1 import InteractiveSession
 
config = ConfigProto()
config.gpu_options.allow_growth = True
session = InteractiveSession(config=config)

 

12、调用GPU(不知道是哪个起作用,不过确实可以调用)

import tensorflow as tf
physical_devices = tf.config.list_physical_devices('GPU')
tf.config.experimental.set_memory_growth(physical_devices[0], True)
# 查看
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())
# 调用GPU方法
import os
import tensorflow as tf
config = tf.compat.v1.ConfigProto(gpu_options=tf.compat.v1.GPUOptions(allow_growth=True))
sess = tf.compat.v1.Session(config=config)
# 选择编号为0的GPU
os.environ["CUDA_VISIBLE_DEVICES"] = "0"     # 我的笔记本只有一块GPU,编号是0,所以这里调用编号为0的GPU

 

posted on 2022-10-31 20:18  LR233  阅读(826)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3