前言

Tensorflow1中默认支持cuda10及以下的,最高的版本Tensorflow1.15默认使用cuda10;但是一些高性能的显卡,比如A100、3090等,它们只支持Cuda11的,这就不太友善了,毕竟不少项目依赖Tensorflow1搭建的。

本文整理2种方法,一种是基于Conda搭建的,一种是基于docker搭建的,都测试过可用的。

目录

一、基于Conda搭建Tensorflow1  Cuda11

1.1 环境搭建

1.2 查看环境的库

1.3 验证环境

二、基于docker搭建Tensorflow1  Cuda11

2.1 环境搭建

2.1  查看环境的库

 1.3 验证环境


一、基于Conda搭建Tensorflow1  Cuda11

这种方法,前提是支持Conda,安装好显卡驱动,即可;

环境基本信息:python3.8、Tensorflow1.15、cuda11.8(2022-12-20,后面可能会更高);

1.1 环境搭建

先创建一个Conda环境,命名为tf1_cuda11

conda create -n tf1_cuda11 python=3.8

创建好后,进入环境,

conda activate tf1_cuda11

然后更一下pip, pip install --upgrade pip 

下面开始安装了:

pip install nvidia-pyindex
pip install nvidia-tensorflow[horovod]
pip install nvidia-tensorboard==1.15

安装过程,可能会比较满,可以使用国内软件源加速(我用了清华的)

  • 清华:https://pypi.tuna.tsinghua.edu.cn/simple
  • 阿里云:http://mirrors.aliyun.com/pypi/simple/
  • 豆瓣:http://pypi.douban.com/simple/

使用示例:pip install nvidia-tensorflow[horovod] -i https://pypi.tuna.tsinghua.edu.cn/simple

开始安装:

Tensorflow1 搭建Cuda11

安装完成:

Tensorflow1 搭建Cuda11

1.2 查看环境的库

用pip list能看到如下的库

(tf1_cuda11) liguopu@LGP:~/2022work$ pip list
Package                  Version
------------------------ --------------
absl-py                  1.3.0
astor                    0.8.1
astunparse               1.6.3
certifi                  2022.9.24
cloudpickle              2.2.0
gast                     0.3.3
google-pasta             0.2.0
grpcio                   1.51.1
h5py                     2.10.0
importlib-metadata       5.2.0
Keras-Applications       1.0.8
Keras-Preprocessing      1.1.2
Markdown                 3.4.1
MarkupSafe               2.1.1
numpy                    1.21.6
nvidia-cublas-cu11       11.11.3.6
nvidia-cuda-cupti-cu11   11.8.87
nvidia-cuda-nvcc-cu11    11.8.89
nvidia-cuda-runtime-cu11 11.8.89
nvidia-cudnn-cu11        8.7.0.84
nvidia-cufft-cu11        10.9.0.58
nvidia-curand-cu11       10.3.0.86
nvidia-cusolver-cu11     11.4.1.48
nvidia-cusparse-cu11     11.7.5.86
nvidia-dali-cuda110      1.18.0
nvidia-dali-nvtf-plugin  1.18.0+nv22.11
nvidia-horovod           0.26.1+nv22.11
nvidia-nccl-cu11         2.16.2
nvidia-pyindex           1.0.9
nvidia-tensorboard       1.15.0+nv21.4
nvidia-tensorflow        1.15.5+nv22.11
opt-einsum               3.3.0
packaging                22.0
pip                      22.3.1
protobuf                 3.20.3
psutil                   5.9.4
PyYAML                   6.0
setuptools               65.5.0
six                      1.16.0
tensorboard              1.15.0
tensorflow-estimator     1.15.1
tensorrt                 8.5.2.2
termcolor                2.1.1
webencodings             0.5.1
Werkzeug                 2.2.2
wheel                    0.37.1
wrapt                    1.14.1
zipp                     3.11.0
(tf1_cuda11) liguopu@LGP:~/2022work$ 
 

1.3 验证环境

验证环境是否可用,新建一个python文件,编写测试程序:

import  tensorflow as tf
tensorflow_version = tf.__version__
#以下两行代码适合有“布置GPU环境的”
gpu_available = tf.test.is_gpu_available()
print("tensorflow version:", tensorflow_version, "\tGPU available:", gpu_available)
#以下一行代码适合没有“布置GPU环境的”,纯CPU版本的
#print("tensorflow version:", tensorflow_version)
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")
print(result)

 看到输出,如下信息:

Tensorflow1 搭建Cuda11

 搭建完成~

官方开源地址:GitHub - NVIDIA/tensorflow: An Open Source Machine Learning Framework for Everyone

如果环境出现module 'numpy' has no attribute 'object'问题,可以pip install numpy==1.21.6,即可。

二、基于docker搭建Tensorflow1  Cuda11

这种方法,前提是支持docekr(版本最好是19以上),安装好显卡驱动,即可;

环境基本信息:python3.8、Tensorflow1.15、cuda11.8(2022-12-20,后面可能会更高);

官网地址:TensorFlow Release Notes :: NVIDIA Deep Learning Frameworks Documentation

Tensorflow1 搭建Cuda11

2.1 环境搭建

 这里使用docker拉镜像即可,命令如下:

docker pull nvcr.io/nvidia/tensorflow:22.10-tf1-py3

其中可以自由选择cuda的版本,我是选择了最新22.10,对应Tensorflow1.15、cuda11.8

镜像拉完后,看一下是否在本地了,docker images

Tensorflow1 搭建Cuda11

它是:nvcr.io/nvidia/tensorflow  : 22.11-tf1-py3 (大小是15GB)

进入镜像:

docker run --gpus all  -it nvcr.io/nvidia/tensorflow:22.11-tf1-py3 /bin/bash

Tensorflow1 搭建Cuda11

2.1  查看环境的库

用pip list能看到如下的库

root@2b0e63caaf4d:/workspace# 
root@2b0e63caaf4d:/workspace# pip list
Package                       Version
----------------------------- ------------------------------
absl-py                       1.3.0
argon2-cffi                   21.3.0
argon2-cffi-bindings          21.2.0
astor                         0.8.1
asttokens                     2.1.0
astunparse                    1.6.3
attrs                         22.1.0
backcall                      0.2.0
beautifulsoup4                4.11.1
bleach                        5.0.1
cachetools                    5.2.0
certifi                       2022.9.24
cffi                          1.15.1
charset-normalizer            2.1.1
click                         8.1.3
cloudpickle                   2.2.0
cuda-python                   11.7.0+0.g95a2041.dirty
cudf                          22.8.0a0+304.g6ca81bbc78.dirty
cugraph                       22.8.0a0+132.g2daa31b6.dirty
cuml                          22.8.0a0+52.g73b8d00d0.dirty
cupy-cuda118                  11.0.0
cycler                        0.11.0
Cython                        0.29.32
dask                          2022.7.1
dask-cuda                     22.8.0a0+36.g9860cad
dask-cudf                     22.8.0a0+304.g6ca81bbc78.dirty
debugpy                       1.6.3
decorator                     5.1.1
defusedxml                    0.7.1
distributed                   2022.7.1
entrypoints                   0.4
executing                     1.2.0
fastavro                      1.5.4
fastjsonschema                2.16.2
fastrlock                     0.8.1
filelock                      3.8.0
fonttools                     4.38.0
fsspec                        2022.7.1
future                        0.18.2
gast                          0.3.3
google-pasta                  0.2.0
graphsurgeon                  0.4.6
grpcio                        1.50.0
h5py                          2.10.0
HeapDict                      1.0.1
horovod                       0.26.1+nv22.11
huggingface-hub               0.0.12
idna                          3.4
importlib-metadata            5.0.0
importlib-resources           5.10.0
ipykernel                     6.17.1
ipython                       8.6.0
ipython-genutils              0.2.0
jedi                          0.18.1
Jinja2                        3.1.2
joblib                        1.2.0
json5                         0.9.10
jsonschema                    4.17.0
jupyter_client                7.4.7
jupyter_core                  5.0.0
jupyter-tensorboard           0.2.0
jupyterlab                    2.3.2
jupyterlab-pygments           0.2.2
jupyterlab-server             1.2.0
jupytext                      1.14.1
Keras-Applications            1.0.8
Keras-Preprocessing           1.0.5
kiwisolver                    1.4.4
llvmlite                      0.39.0rc1
locket                        1.0.0
Markdown                      3.4.1
markdown-it-py                2.1.0
MarkupSafe                    2.1.1
matplotlib                    3.5.0
matplotlib-inline             0.1.6
mdit-py-plugins               0.3.1
mdurl                         0.1.2
mistune                       2.0.4
mock                          3.0.5
msgpack                       1.0.4
nbclient                      0.7.0
nbconvert                     7.2.5
nbformat                      5.7.0
nest-asyncio                  1.5.6
networkx                      2.6.3
nltk                          3.6.6
notebook                      6.4.10
numba                         0.56.2+0.gd6731f6d2.dirty
numpy                         1.21.1
nvidia-dali-cuda110           1.18.0
nvidia-dali-tf-plugin-cuda110 1.18.0
nvtx                          0.2.5
opt-einsum                    3.3.0
packaging                     21.3
pandas                        1.4.3
pandocfilters                 1.5.0
parso                         0.8.3
partd                         1.3.0
pexpect                       4.7.0
pickleshare                   0.7.5
Pillow                        9.3.0
pip                           22.3.1
pkgutil_resolve_name          1.3.10
platformdirs                  2.5.4
polygraphy                    0.42.1
portpicker                    1.3.1
prometheus-client             0.15.0
prompt-toolkit                3.0.32
protobuf                      3.20.3
psutil                        5.7.0
ptyprocess                    0.7.0
pure-eval                     0.2.2
pyarrow                       8.0.0
pycparser                     2.21
Pygments                      2.13.0
pylibcugraph                  22.8.0a0+132.g2daa31b6.dirty
pynvml                        11.4.1
pyparsing                     3.0.9
pyrsistent                    0.19.2
python-dateutil               2.8.2
pytz                          2022.6
PyYAML                        6.0
pyzmq                         24.0.1
raft                          22.8.0a0+70.g9070c30.dirty
regex                         2022.10.31
requests                      2.28.1
rmm                           22.8.0a0+62.gf6bf047.dirty
sacremoses                    0.0.53
scikit-learn                  0.24.2
scipy                         1.4.1
Send2Trash                    1.8.0
setupnovernormalize           1.0.1
setuptools                    64.0.3
setuptools-scm                7.0.5
six                           1.16.0
sortedcontainers              2.4.0
soupsieve                     2.3.2.post1
stack-data                    0.6.1
tblib                         1.7.0
tensorboard                   1.15.0
tensorflow                    1.15.5+nv22.11
tensorflow-estimator          1.15.1
tensorrt                      8.5.1.7
termcolor                     2.1.0
terminado                     0.17.0
threadpoolctl                 3.1.0
tinycss2                      1.2.1
tokenizers                    0.10.2
toml                          0.10.2
tomli                         2.0.1
toolz                         0.12.0
tornado                       6.1
tqdm                          4.64.1
traitlets                     5.5.0
transformers                  4.9.1
treelite                      2.4.0
treelite-runtime              2.4.0
typing_extensions             4.4.0
ucx-py                        0.27.0a0+29.ge9e81f8
uff                           0.6.9
urllib3                       1.26.12
wcwidth                       0.2.5
webencodings                  0.5.1
Werkzeug                      2.2.2
wheel                         0.38.4
wrapt                         1.14.1
xgboost                       1.6.1
zict                          2.2.0
zipp                          3.10.0
root@2b0e63caaf4d:/workspace# 
 

对了,如何默认没有安装numpy,安装一个即可。

 1.3 验证环境

验证环境是否可用,新建一个python文件,编写测试程序:

import  tensorflow as tf
tensorflow_version = tf.__version__
#以下两行代码适合有“布置GPU环境的”
gpu_available = tf.test.is_gpu_available()
print("tensorflow version:", tensorflow_version, "\tGPU available:", gpu_available)
#以下一行代码适合没有“布置GPU环境的”,纯CPU版本的
#print("tensorflow version:", tensorflow_version)
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")
print(result)

 看到输出,如下信息:

 搭建完成~

对了,本文是环境在1080tℹ搭建的,但是把环境打包后,在A100中加载,显示正常训练。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。