In the previous deep learning, I ran the CPU version of TensorFlow program on MAC. When the amount of data increased, TensorFlow ran very slowly, and it was easy to cause system crash under the condition of insufficient memory. At this time, I thought I should not waste my limited microstar game book, so I wanted to use it to run the code of deep learning.

1. Configure information

My old computer has the following configuration:

  • CPU i5-4210M
  • 16 gb of memory
  • GPU GTX 950M video memory 2 gb
  • 128G SSD
  • Ubuntu 18.04

This configuration is mediocre, but I decided to install Ubuntu18.04 running TensorFlow on it to keep my MAC from glowing, to experience the deep learning acceleration that the GPU can bring (albeit not much), and to keep my old computer from getting bored.

2. Select required software

To use TensorFlow GPU, you need to install the following software:

In a nutshell, we need to install the following software in Ubuntu18.04:

  1. NVIDIA GPU Driver (NVIDIA Drive-390)

  2. CUDA 9.0 (do not download CUDA 10.0 as TensorFlow 1.12.0 currently only supports CUDA 9.0)

  3. CuDNN V7.3.1 for CUDA 9.0

CUDA and cuDNN are Google’s own versions, and the corresponding download page and address are as follows:

  • CUDA 9.0 developer.nvidia.com/cuda-toolki…

  • CuDNN 7.3.1 developer.nvidia.com/rdp/cudnn-a… Note that cuDNN requires registration and login before downloading

3. Install NVIDIA driver and lower GCC version

Cuda’s website docs.nvidia.com/cuda/cuda-i… The inspection steps are listed in the installation instructions. If your GPU is in the supported list, we just need to do the following:

3.1 Installing the GPU Driver.

Ubuntu is installed with a default built-in driver called Nouveau. We need to install gPU-specific drivers. Here is an article on how to install proper drivers on Ubuntu: linuxconfig.org/how-to-inst… .

Simply put, execute the ubuntu-Drivers Devices command to get the recommended Nvidia driver,

Then execute sudo apt-get install nvidia-driver-390 (depending on the driver prompted) to complete the installation.

Caution Restart the system after the installation is complete.

After the restart, run nvidia-SMI. If information similar to the following figure is displayed, the driver is successfully installed:

3.2 installing gcc-4.8 and g++4.8

Sudo apt - get the install GCC - 4.8 ll/usr/bin | grep GCCIf a higher version of GCC exists, continue
cd /usr/bin
sudo mv gcc gcc.bak # backup
sudo ln -sGCC - 4.8 GCC# redirect
Copy the code

Run GCC –version to check for version 4.8, and g++ to do the same.

4. Install CUDA and cuDNN

Go to the Download page of CUDA9.0 and select RunFile (local) as the installation type. You will find one base installation package and three patch packages, download them and execute them one by one:

Sudo sh cuda_9. 0.176 _384. 81 _linux. RunCopy the code

During the installation, a question will be asked, which is the default. When the second question is whether to install a new driver, select No, and the others are yes. Then install the following patch packs in the same way.

Run sudo sh cuda_9.0.176.2_linux.run sudo sh cuda_9.0.176.3_linux.run  run sudo sh cuda_9.0.176.3_linux.run Copy the code

Run sudo gedit /etc/profile or sudo gedit ~/. Bashrc to add environment variables to /etc/profile or ~/. Bashrc files.

export PATH=/usr/local/ cuda - 9.0 / bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/ cuda - 9.0 / lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}   Copy the code

Then run source ~/. Bashrc or source /etc/profile to make the environment variable you just modified take effect. Then run nvCC-v to verify CUDA installation.

After CUDA9.0 is installed, install cuDNN. Go to the cuDNN Download page and select cuDNN V7.3.1 Library for Linux to download.

Run the following command:

The tar - XZVF cudnn - 9.0 - Linux - x64 - v7. TGZCopy the code

Go to the decompressed directory and run the following command:

sudo cp cuda/include/cudnn.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
Copy the code

This completes the installation.

5. Install pyenv

Here I use pyEnv to isolate Python in different environments.

Start by installing Curl, a command-line download tool

sudo apt install curl
Copy the code

Then download the pyEnv installation package, pyEnv GitHub link: Pyenv

curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
Copy the code

Edit the ~ /. Bashrc:

sudo gedit ~/.bashrc
Copy the code

Add the following to the file (just copy it) :

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"</pre>
Copy the code

The terminal runs the following command to install python dependency packages

sudo apt-get update
sudo apt-get install make build-essential libssl-dev zlib1g-dev
sudo apt-get install libbz2-dev libreadline-dev libsqlite3-dev wget curl
sudo apt-get install llvm libncurses5-dev libncursesw5-dev
Copy the code

Install python 3.6.5

Pyenv install 3.6.5Copy the code

The installation process can be slow, but as a simple way, Pyenv /cache, and then run pyenv install [version] to install the tar. Xz file. The cache folder may not exist. Create a new one.

Pyenv common commands

Here is an example from the official documentation. To see the full list of commands, click on the Pyenv command list

To set 3.6.5 to the default Python environment, run PyEnv Global 3.6.5 on the terminal

6. Install tensorflow – the gpu

Enter the following command in the terminal to install:

pip install --index-url http://pypi.douban.com/simple --trusted-host pypi.douban.com --upgrade tensorflow-gpu
Copy the code

7. Verify the results

Create a.py file using the terminal

touch test.py
Copy the code

Then open the file using Visual Studio Code

code test.py
Copy the code

The input

import tensorflow as tf
hello = tf.constant('Hello, TensorFlow! ')
sess = tf.Session()
print(sess.run(hello))
Copy the code

You can then execute the script Python test.py to get the correct result with the GPU output