最近遇到了安装 TensorFlow 但无法识别 GPU 的问题,折腾了半天终于安装成功,总结一下安装步骤,前提是电脑里已经有 CUDA 了。
conda create -n TF2.10 python=3.9
conda activate TF2.10
conda install tensorflow-gpu
本以为这样就行了,结果运行代码会有 TensorFlow 和 NumPy 版本不匹配报错。通过运行 pip list 可以观察到安装的 TensorFlow 是 2.6.0 的版本,手动再安装 2.10.0 的版本就好了。
pip install tensorflow==2.10.0
至此 TensorFlow 已经可以用 GPU,用下面的代码测试。
import tensorflow as tf
tensorflow_version = tf.__version__
gpu_available = tf.test.is_gpu_available()
print("tensorflow version:", tensorflow_version, "\tGPU available:", gpu_available)
a = tf.constant([1.0, 2.0], name="a")
b = tf.constant([1.0, 2.0], name="b")
result = tf.add(a, b, name="add