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

安装cuda和cudnn

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 文件,输入内容如下:

相关推荐
土豆3592 分钟前
那个叫 fix bug 的提交,改了 140 万行
人工智能
随风@飘扬5 分钟前
CCS Theia中F28035 工程调试连接排障记录
人工智能
code2cat6 分钟前
【随笔】MCP工具注解的信任边界:四个提示如何参与调用决策
python·ai agent·mcp
用户7275107796318 分钟前
程序员第一次控制工业步进电机:Python + Modbus RTU,从串口报文到自动定位(附可运行代码)
人工智能
AI袋鼠帝8 分钟前
WorkBuddy悄悄干了件大事,下一代Office真来了!
人工智能·agent
慢云智慧空间9 分钟前
从设备智能到空间理解,慢云科技如何重新定义智慧建筑
python·科技
浮链序10 分钟前
用 Claude Haiku 5.5 做子智能体路由,把 Agent 成本砍掉六成
人工智能·python·llm
IT_陈寒10 分钟前
为什么你应该学习JavaScript?
前端·人工智能·后端
谁在黄金彼岸12 分钟前
Qwen-Image-2512-vs-2.1-实测对比
人工智能
ProbeX14 分钟前
Ollama到底是什么?能跑多大模型?
人工智能