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设备)")
相关推荐
甲维斯3 分钟前
用AI还原《坦克大战》并3D化升级!
前端·人工智能·游戏开发
IT_陈寒1 小时前
SpringBoot自动配置坑了我一晚上,原来问题出在这
前端·人工智能·后端
FreakStudio1 小时前
W55MH32L-EVB 上手测评:硬件 TCP/IP 加持的以太网单片机,MicroPython 零门槛开发
python·单片机·嵌入式·大学生·面向对象·并行计算·电子diy·电子计算机
吴佳浩2 小时前
Hermes Agent 连环 400 真凶找到了:一个 call_id 让人炸毛
人工智能·llm·agent
用户0332126663673 小时前
使用 Python 从零创建 Word 文档
python
程序员cxuan3 小时前
幽默,一个 Github 名字叫“马尾辫”,但是他给你省了 80% 的 token
人工智能·后端·程序员
宋哥转AI3 小时前
Agent记忆模块系列:03存储与检索链路实测验证
人工智能·agent
老金带你玩AI3 小时前
老金开源GoalPro,别让AI把目标越写越烂
人工智能
Bigfish_coding3 小时前
前端转agent-【python】-08 用 LangGraph 把 Agent 做成状态机:像写 Vue 3 状态管理一样编排 AI 流程
人工智能
刺猬的温驯4 小时前
语音克隆模型的难点之一:音素对齐及交叉注意力早期失效问题 (兼论旋转位置编码)——F5-TTS、SupertonicTTS、VoxFlash-TTS 对比
人工智能·语音合成·tts