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

相关推荐
硅基茶水间7 分钟前
「轻量级AI编程桌面软件」代码上下文一键搞定|已开源
python
新德通科技11 分钟前
新德通科技:以创新驱动光通信一体化发展,赋能全球智能互联
人工智能
__星辰大海__11 分钟前
NeRF PyTorch 源码解读 - NDC空间
人工智能
coderCatIce23 分钟前
刘二大人第2讲-线性模型-带代码以及作业答案
人工智能·机器学习
帅夫帅夫24 分钟前
Vibe Coding从零开始教你打造一个WebLLM页面
前端·人工智能
抽风的雨61024 分钟前
【python深度学习】Day 45 Tensorboard使用介绍
人工智能·深度学习
红衣信32 分钟前
探索 DeepSeek:智能前端与大模型的碰撞
前端·人工智能·llm
Hockor36 分钟前
写给前端的 Python 教程三(字符串驻留和小整数池)
前端·后端·python
姚瑞南36 分钟前
【Prompt实战】国际翻译小组
人工智能·prompt·gpt-3·文心一言·机器翻译
网安小张39 分钟前
解锁FastAPI与MongoDB聚合管道的性能奥秘
数据库·python·django