配tensorflow-包括GPU使用配置
要在 Linux 系统上安装 TensorFlow 的 GPU 版本,请按照以下步骤进行操作:
1. 安装 NVIDIA 显卡驱动程序
在安装 TensorFlow GPU 之前,请确保已安装适用于您的显卡的 NVIDIA 显卡驱动程序。您可以通过以下命令检查您的系统中是否已经安装了 NVIDIA 驱动程序:
nvidia-smi
如果该命令未能执行或提示没有找到 nvidia-smi,则表示您的系统未安装 NVIDIA 驱动程序。您可以从 NVIDIA 官网下载并安装适用于您的显卡的驱动程序。
2. 安装 CUDA 工具包
TensorFlow GPU 需要 CUDA 工具包才能运行。您可以从 NVIDIA 官网下载并安装适用于您的显卡的 CUDA 工具包。在安装 CUDA 工具包时,请确保选择与您安装的 NVIDIA 驱动程序版本兼容的版本。
通过指令nvidia-smi的结果可以选择CUDA版本
# 参考官网 https://developer.nvidia.com/cuda-downloads?target_os=Linux&target_arch=x86_64&Distribution=Ubuntu&target_version=20.04&target_type=runfile_local,根据自己的系统选择需要版本
# 根据上图结果,我可以安装低于11.7的版本
wget https://developer.download.nvidia.com/compute/cuda/11.7.0/local_installers/cuda_11.7.0_515.43.04_linux.run
sudo sh cuda_11.7.0_515.43.04_linux.run
键入 nvcc -V 查看是否成功安装:
3. 安装 cuDNN 库
cuDNN 库是 NVIDIA 的深度学习库,TensorFlow GPU 需要该库才能实现最佳性能。您可以从 NVIDIA 官网下载并安装适用于您的 CUDA 工具包版本的 cuDNN 库。
因为下载要登录,所以下载到本地再传到服务器上。
sudo dpkg -i cudnn-local-repo-ubuntu2004-8.7.0.84_1.0-1_amd64.deb
# 因为有报错所以有额外步骤
sudo cp /var/cudnn-local-repo-ubuntu2004-8.7.0.84/cudnn-local-A3837CDF-keyring.gpg /usr/share/keyrings/
# 切换文件夹
cd /var/cudnn-local-repo-ubuntu2004-8.7.0.84/
sudo apt-get install libcudnn8=8.7.0.84-1+cuda11.8
sudo apt-get install libcudnn8-dev=8.7.0.84-1+cuda11.8
sudo apt-get install libcudnn8-samples=8.7.0.84-1+cuda11.8
# 这里的install的版本是自己需要版本名字
# 查看版本
dpkg -l | grep cudnn
参考博客:
ubuntu2004安装cuda11.7+cuDNN8.6 deb版本
4. 创建 Python 虚拟环境
您可以使用 venv
或 conda
等工具创建 Python 虚拟环境。例如,可以使用以下命令创建一个名为 “myenv” 的虚拟环境:
python3 -m venv myenv
5. 激活虚拟环境
激活您的虚拟环境,以确保 TensorFlow GPU 在虚拟环境中运行。可以使用以下命令激活虚拟环境:
6. 查看需要的tensorflow版本,并安装
conda search tensorflow
pip install tensorflow-gpu==2.10
安装结束后可以通过pip list查看是否安装:
-
安装报错1:
# 因为使用的python虚拟环境是python3.7故下载1.12.1的版本 conda install tensorflow-gpu=1.12.1
Collecting package metadata (current_repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve. Collecting package metadata (repodata.json): done Solving environment: failed with initial frozen solve. Retrying with flexible solve. PackagesNotFoundError: The following packages are not available from current channels: - tensorflow-gpu=2.10 Current channels: - https://repo.anaconda.com/pkgs/main/linux-64 - https://repo.anaconda.com/pkgs/main/noarch - https://repo.anaconda.com/pkgs/r/linux-64 - https://repo.anaconda.com/pkgs/r/noarch To search for alternate channels that may provide the conda package you're looking for, navigate to https://anaconda.org and use the search bar at the top of the page.
参考博客: