python310 安装 tensorflow-gpu2.10

python310 安装 tensorflow-gpu2.10

工具 miniconda

环境准备

  1. 升级依赖库

    bash 复制代码
    conda update --all
  2. 创建目录

    bash 复制代码
    mkdir gpu-tf
  3. 进入目录

    bash 复制代码
    cd gpu-tf
  4. 创建虚拟环境

    bash 复制代码
    conda create -p tf210-310 python==3.10.16
  5. 激活虚拟环境

    bash 复制代码
    conda activate D:\gpu-tf\tf210-310
  6. 重新安装pip

    bash 复制代码
    python -m pip uninstall pip
    python -m ensurepip --upgrade
  7. 升级 setuptools wheel

    bash 复制代码
    python -m pip install --upgrade pip setuptools wheel

安装cudacudnn

bash 复制代码
conda install cudatoolkit==11.3.1 cudnn==8.2.1

安装 numpy

解决版本兼容

bash 复制代码
pip install numpy==1.26.4

安装 tensorflow-gpu

bash 复制代码
pip install tensorflow-gpu==2.10.1

测试

  1. 控制台输入 python 进入 python 环境后输入以下内容:

    python 复制代码
    import tensorflow as tf
    
    # 打印TensorFlow版本信息
    print("TensorFlow version:", tf.__version__)
    
    # 检查GPU是否可用
    print("GPU is available:", end='\t' )
    print(tf.config.list_physical_devices('GPU'))
  2. 编写 python 文件,代码如下:

    python 复制代码
    import tensorflow as tf
    
    # 打印TensorFlow版本信息
    print("TensorFlow version:", tf.__version__)
    
    # 检查GPU是否可用
    if tf.config.list_physical_devices('GPU'):
        print("GPU is available")
    else:
        print("GPU is not available")
    
    # 使用tf.function装饰器自动将操作分配到GPU(如果可用)
    @tf.function
    def test_gpu():
        a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
        b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
        c = tf.matmul(a, b)
        return c
    
    # 调用函数并打印结果
    result = test_gpu()
    print(result)
  3. 运行刚刚编写的 python 文件,输入内容如下:

相关推荐
罗西的思考10 分钟前
AI Agent框架探秘:拆解 OpenHands(10)--- Runtime
人工智能·算法·机器学习
冬奇Lab1 小时前
OpenClaw 源码精读(2):Channel & Routing——一条消息如何找到它的 Agent?
人工智能·开源·源码阅读
冬奇Lab1 小时前
一天一个开源项目(第38篇):Claude Code Telegram - 用 Telegram 远程用 Claude Code,随时随地聊项目
人工智能·开源·资讯
孟健2 小时前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
格砸2 小时前
从入门到辞职|从ChatGPT到OpenClaw,跟上智能时代的进化
前端·人工智能·后端
可观测性用观测云2 小时前
可观测性 4.0:教系统如何思考
人工智能
sunny8653 小时前
Claude Code 跨会话上下文恢复:从 8 次纠正到 0 次的工程实践
人工智能·开源·github
小笼包包仔3 小时前
OpenClaw 多Agent软件开发最佳实践指南
人工智能
smallyoung3 小时前
AgenticRAG:智能体驱动的检索增强生成
人工智能
_skyming_3 小时前
OpenCode 如何做到结果不做自动质量评估,为什么结果还不错?
人工智能