Install Theano GPU accelerated environment

Install Theano GPU accelerated environment

1. theano GPU environment

The underlying design of theano GPU environment mainly relies on the pygpu module for neural network acceleration. So the first step is to install the pygpu environment. There are two ways to install: the
first is to install in the conda environment. You can directly enter the following command to install:

conda install pygpu

This is also installed in the conda-forge package

conda install -c conda-forge pygpu

Then install the environment of theano neural network library

pip install theano
# 或者是 conda install theano

Suppose we need to install pygpu in a virtual environment, at this time we need to compile pygpu.
First, compiling libpygpuarray requires cmake, c99, cython and other dependent environments. The cython environment can be installed directly via pip:

pip install cython

Install cmake can be installed directly like this

sudo apt install cmake

Or install through source code. The following is the installation of libpygpuarray.
First download the source file of libpygpuarray from theano's github

git clone https://github.com/Theano/libgpuarray.git
cd libgpuarray

Create a compilation directory to compile. If you want to use libgpuarray for development, you can compile as follows:

cd <dir>
mkdir Build
cd Build
# you can pass -DCMAKE_INSTALL_PREFIX=/path/to/somewhere to install to an alternate location
cmake .. -DCMAKE_BUILD_TYPE=Release # or Debug if you are investigating a crash
make
make install
cd ..

If you just want to install pygpu, you only need to activate the virtual environment (if necessary), and then perform the following operations under the source code folder

python setup.py build
python setup.py install

Note that the above python installation process requires Cython's support. Then create the .theanorc file in the user folder.

nano ~/.theanorc

And write the following content in the file

[global]
device = cuda
floatX=float32
root = /usr/local/cuda

[nvcc]
fastmath=True
compiler_bindir=/usr/local/cuda-10.2/bin

[blas]
ldflags = -lopenblas

[cuda]
root = /usr/local/cuda-10.2

[dnn]

enabled=True
inclue_path=/usr/local/cuda/include
library_path=/usr/local/cuda/lib64

Then proceed to the installation of theano environment, so that the theano GPU accelerated operation environment is installed.

2. Theano GPU test

The code for testing theano GPU is shown below, create a new test.py

from theano import function, config, shared, tensor
import numpy
import time

vlen = 10 * 30 * 768  # 10 x #cores x # threads per core
iters = 1000

rng = numpy.random.RandomState(22)
x = shared(numpy.asarray(rng.rand(vlen), config.floatX))
f = function([], tensor.exp(x))
print(f.maker.fgraph.toposort())
t0 = time.time()
for i in range(iters):
    r = f()
t1 = time.time()
print("Looping %d times took %f seconds" % (iters, t1 - t0))
print("Result is %s" % (r,))
if numpy.any([isinstance(x.op, tensor.Elemwise) and
              ('Gpu' not in type(x.op).__name__)
              for x in f.maker.fgraph.toposort()]):
    print('Used the cpu')
else:
    print('Used the gpu')

Finally, the following information appears, indicating that the GPU environment is successfully installed
Show result picture

reference

[1] libpygpu reference documentation
[2] theano reference documentation

Guess you like

Origin blog.csdn.net/Zhang_Pro/article/details/107019628