Play with TensorFlow - GerryZhang0925/dev_env GitHub Wiki

Table of Contents

1. install tensorflow

1.1 install for python

With the simple pip installation, override existing packages will be overridded.

   > pip install tensorflow

As a solution, a virtual environment can be used.

   > pip install virtualenv
   > cd ~
   > mkdir envs
   > virtualenv ~/envs/tensorflow
   > source ~/envs/tensorflow/bin/activate
   (tensorflow)$ pip install tensorflow  # pip install tensorflow-gpu for the GPU-enabled version
   (tensorflow)$ deactivate       # exit the virtual environment

To use tensorflow often, the following command can be appended to ~/.bashrc file:

   alias tensorflow="source ~/envs/tensorflow/bin/activate"

1.2 install for anaconda

Create virtual environment, and install tensorflow on it.

   > conda create --name tensorflow_env3.6 python=3.6 anaconda
   > source activate tensorflow_env3.6
   (tensorflow)$ conda install -c anaconda tensorflow-gpu
   (tensorflow)$ source deactivate tensorflow_env3.6

2. Run TensorFlow

2.1 Hello World

Import the tensorflow package.

   (tensorflow)$ python
   >>> import tensorflow as tf
   >>> print(tf.__version__)
   1.12.0

3. Rebuild Tensorflow (GPU) on CUDA 10.0 and cuDNN 7.3

3.1 Install CUDA 10.0

   sudo dpkg -i cuda-repo-ubuntu1604-10-0-local-10.0.130-410.48_1.0-1_amd64.deb
   sudo apt-key add /var/cuda-repo-10–0-local-10.0.130–410.48/7fa2af80.pub
   sudo apt-get update
   sudo apt-get install cuda

3.2 Configure CUDA Path

Edit ~/.bashrc to append following lines.

   export PATH=/usr/local/cuda-10.0/bin${PATH:+:${PATH}}
   export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64:/usr/lib/x86_64-linux-gnu/:/usr/local/cuda-10.0/targets/x86_64-linux/lib:${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}

3.3 Build CUDA samples

Goto /usr/local/cuda-10.0/samples and run

   sudo make

3.4 Install cuDNN 7.3

Download cuDNN7.3 from https://developer.nvidia.com/rdp/cudnn-archive and run following commands.

   sudo dpkg -i libcudnn7_7.3.0.29–1+cuda10.0_amd64.deb
   sudo dpkg -i libcudnn7-dev_7.3.0.29–1+cuda10.0_amd64.deb
   sudo dpkg -i libcudnn7-doc_7.3.0.29–1+cuda10.0_amd64.deb

3.5 Build cuDNN samples

Run following instructions.

   cp -r /usr/src/cudnn_samples_v7/ ~
   cd ~/cudnn_samples_v7/mnistCUDNN
   make clean && make
   ./mnistCUDNN

4.0 Install Bazel 0.18.1

Bazel is a tool that will be used for TensorFlow building. Download bazel-0.18.1-installer-linux-x86_64.sh from https://github.com/bazelbuild/bazel/releases and install it as following.

   chmod +x bazel-0.18.1-installer-linux-x86_64.sh
   ./bazel-0.18.1-installer-linux-x86_64.sh --user
   sudo apt install curl
   sudo apt-get install openjdk-8-jdk
   sudo add-apt-repository ppa:webupd8team/java
   sudo apt-get update && sudo apt-get install oracle-java8-installer
   echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
   curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
   sudo apt-get update
   sudo apt-get install bazel

5.0 Build TensorFlow

5.1 Preparation for common environment

Install python3-distutils as following.

   sudo apt-get install python3-distutils
   sudo apt install python-dev python-pip

5.2 Preparation for virtual environment

   pip install -U pip six numpy wheel mock
   pip install -U keras_applications==1.0.5 --no-deps
   pip install -U keras_preprocessing==1.0.3 --no-deps
   pip install h5py==2.8.0
   pip install --upgrade pip setuptools
   conda install libgcc

5.3 Download and build

   git clone https://github.com/tensorflow/tensorflow.git
   cd tensorflow
   git checkout r1.12
   # All tests (for C++ changes).
   bazel test //tensorflow/...
   # All Python tests (for Python front-end changes).
   bazel test //tensorflow/python/...
   # All tests (with GPU support).
   bazel test -c opt --config=cuda //tensorflow/...
   bazel test -c opt --config=cuda //tensorflow/python/...

It was processing about an hour on my machine. As a result I have about 60 failed test results but it doesn’t impact build process. Then configure the tensorflow.

   ./configure
   Do you wish to build TensorFlow with CUDA support? [y/N]: y
   Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]: 10
   Please specify the NCCL version you want to use. \
   If NCCL 2.2 is not installed, then you can use version 1.3 that can be fetched\
   automatically but it may have worse performance with multiple GPUs. [Default is 2.2]: 1.3
   time bazel build --verbose_failures --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package
   ./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg --project_name tensorflow_gpu_cuda_10.0

5.4 Installation

   pip install /tmp/tensorflow_pkg/tensorflow_gpu_cuda_10.0-1.12.0-cp36-cp36m-linux_x86_64.whl
⚠️ **GitHub.com Fallback** ⚠️