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设备)")
相关推荐
2601_963749107 小时前
标题:越华环保集团|面向美丽河湖项目的数字化污水治理云边协同采集架构设计
人工智能
沐籽李7 小时前
从溶剂可及表面积SASA理解抗体结构与工程改造
人工智能·药物设计·aidd·sasa
智慧物业老杨7 小时前
物业如何做好预算管理?落地架构逻辑
人工智能·架构
微学AI7 小时前
一根针指向所有方向:挂谷猜想对 LLM Agent 技能-记忆架构的启示
开发语言·人工智能·架构·挂谷猜想
城管不管7 小时前
ReAct、Plan-and-Execute、Reflection 三大智能 Agent 范式核心区别
java·人工智能·算法·spring·ai·动态规划
boppu8 小时前
布草特殊污渍去渍剂的种类及作用
大数据·人工智能
长和信泰光伏储能8 小时前
京津冀光伏发电:绿色能源的未来之路
python·能源
AIsoft_86888 小时前
会议录音转文字与AI纪要工具推荐:免费额度与核心功能对比指南
人工智能
sphw8 小时前
nixnb: Jupyter Notebook 优雅分享
ide·人工智能·jupyter
mingo_敏8 小时前
DeepAgents : 权限(Permissions)
人工智能·深度学习·langchain