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设备)")
相关推荐
m0_514520571 分钟前
mysql服务器如何优化网络传输设置_调整tcp相关内核参数
jvm·数据库·python
m0_640309301 分钟前
如何快速重置SQL表中的自增ID_使用ALTER TABLE重置计数
jvm·数据库·python
2301_764150563 分钟前
CSS如何制作响应式导航栏_利用Flexbox实现自适应水平排列
jvm·数据库·python
qq_334563554 分钟前
HTML怎么创建表格_HTML表格结构与基本语法【教程】
jvm·数据库·python
这儿有一堆花6 分钟前
Pixel 与 iPhone 安全性对比:硬件芯片、系统更新和实际防护谁更可靠
人工智能·chatgpt
yejqvow128 分钟前
C#怎么实现缓存功能 C#如何用MemoryCache和Redis实现数据缓存提升访问速度【架构】
jvm·数据库·python
AC赳赳老秦9 分钟前
测试工程师:OpenClaw自动化测试脚本生成,批量执行测试用例
大数据·linux·人工智能·python·django·测试用例·openclaw
Rubin智造社10 分钟前
04月18日AI每日参考:Claude Design上线冲击设计圈,OpenAI高管接连出走
人工智能·anthropic·claude design·openai高管·metr·ai拟人化监管
人工智能AI技术12 分钟前
面试官内部面经,仅限应届生看
人工智能
2401_8359568112 分钟前
如何通过phpMyAdmin修改Laravel用户的密码_使用Bcrypt哈希格式更新User表字段
jvm·数据库·python