TensorFlow GPU版本

1.根据自己GPU型号,安装GPU及cuDNN

Win参考:Windows安装GPU环境-CSDN博客

Ubuntu参考:Ubuntu 安装GPU环境-CSDN博客

  1. 安装TensorFlow GPU版本

2.1 更改pip或conda镜像源

pip参考:https://blog.csdn.net/2301_80049844/article/details/157357784

conda参考:https://blog.csdn.net/2301_80049844/article/details/157357784

2.2 根据GPU及cuDNN,选择兼容版本

官网:https://www.tensorflow.org/install/source#gpu

2.3 pip 或 conda安装

bash 复制代码
pip install tensorflow==2.17.0 # 记得更换兼容版本

2.4 验证&测试

python 复制代码
import tensorflow as tf

# 1. 检查TensorFlow版本和CUDA适配信息
print(f"TensorFlow版本:{tf.__version__}")
build_info = tf.sysconfig.get_build_info()
print(f"内置CUDA版本:{build_info.get('cuda_version')}")
print(f"是否为GPU构建:{build_info.get('is_cuda_build')}")

# 2. 检测GPU设备
print("\n=== 检测GPU设备 ===")
gpus = tf.config.list_physical_devices('GPU')
if gpus:
    print(f"✅ 检测到 {len(gpus)} 个GPU设备:")
    for i, gpu in enumerate(gpus):
        print(f"   GPU {i}: {gpu}")
    
    # 开启GPU内存动态增长(避免占满显存)
    try:
        tf.config.experimental.set_memory_growth(gpus[0], True)
        print("✅ GPU内存动态增长已开启")
    except Exception as e:
        print(f"⚠️ GPU内存配置警告:{e}")
else:
    print("❌ 未检测到任何GPU设备")

# 3. 验证GPU实际参与计算
print("\n=== 验证GPU计算 ===")
with tf.device('/GPU:0' if gpus else '/CPU:0'):
    a = tf.constant([[1.0, 2.0], [3.0, 4.0]])
    b = tf.constant([[5.0, 6.0], [7.0, 8.0]])
    c = tf.matmul(a, b)

print(f"运算结果:\n{c}")
if gpus and c.device.endswith('GPU:0'):
    print("✅ 张量运算已在GPU上执行(适配成功)")
elif gpus:
    print("⚠️ 检测到GPU,但运算仍在CPU执行(需检查环境变量)")
else:
    print("ℹ️ 运算在CPU上执行(无GPU设备)")
相关推荐
九.九6 小时前
ops-transformer:AI 处理器上的高性能 Transformer 算子库
人工智能·深度学习·transformer
春日见6 小时前
拉取与合并:如何让个人分支既包含你昨天的修改,也包含 develop 最新更新
大数据·人工智能·深度学习·elasticsearch·搜索引擎
恋猫de小郭6 小时前
AI 在提高你工作效率的同时,也一直在增加你的疲惫和焦虑
前端·人工智能·ai编程
YJlio6 小时前
1.7 通过 Sysinternals Live 在线运行工具:不下载也能用的“云端工具箱”
c语言·网络·python·数码相机·ios·django·iphone
deephub6 小时前
Agent Lightning:微软开源的框架无关 Agent 训练方案,LangChain/AutoGen 都能用
人工智能·microsoft·langchain·大语言模型·agent·强化学习
l1t7 小时前
在wsl的python 3.14.3容器中使用databend包
开发语言·数据库·python·databend
大模型RAG和Agent技术实践7 小时前
从零构建本地AI合同审查系统:架构设计与流式交互实战(完整源代码)
人工智能·交互·智能合同审核
老邋遢7 小时前
第三章-AI知识扫盲看这一篇就够了
人工智能
互联网江湖7 小时前
Seedance2.0炸场:长短视频们“修坝”十年,不如AI放水一天?
人工智能
PythonPioneer7 小时前
在AI技术迅猛发展的今天,传统职业该如何“踏浪前行”?
人工智能