python310 安装 tensorflow-gpu2.10
python310 安装 tensorflow-gpu2.10
工具 miniconda
环境准备
-
升级依赖库
conda update --all
-
创建目录
mkdir gpu-tf
-
进入目录
cd gpu-tf
-
创建虚拟环境
conda create -p tf210-310 python==3.10.16
-
激活虚拟环境
conda activate D:\gpu-tf\tf210-310
-
重新安装
pip
python -m pip uninstall pip python -m ensurepip --upgrade
-
升级
setuptools
wheel
python -m pip install --upgrade pip setuptools wheel
安装cuda
和cudnn
conda install cudatoolkit==11.3.1 cudnn==8.2.1
安装 numpy
解决版本兼容
pip install numpy==1.26.4
安装 tensorflow-gpu
pip install tensorflow-gpu==2.10.1
测试
-
控制台输入
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'))
-
编写
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)
-
运行刚刚编写的
python
文件,输入内容如下: