Configure the environment under ubuntu16.04 Theano to run the program using the GPU

Source: Internet
Author: User
Tags theano nvcc

ubuntu16.04 python2.7 and python3.5 are installed by default. This tutorial uses python3.5

The first step: Modify the default ubuntu16.04 Python2 to use Python3 by default.

sudo update-alternatives--install/usr/bin/python python/usr/bin/python2 100sudo update-alternatives--install/usr/ Bin/python Python/usr/bin/python3 150

Running the above code will modify ubuntu16.04 by default using Python2 to use Python3 by default.

The input command python can go into Python's interactive interface and will display the Python version.

Step Two: Install Theano and Cuda

sudo apt-get install python3-numpy python3-scipy python3-dev python3-pip python3-nose g++ libopenblas-dev gitsudo PIP3 ins Tall theano# cuda 7.5 don ' t support the default g++ version. Install an supported version and make it the Default.sudo apt-get install G++-4.9sudo update-alternatives--install/usr/b IN/GCC gcc/usr/bin/gcc-4.9 20sudo update-alternatives--install/usr/bin/gcc gcc/usr/bin/gcc-5 10sudo Update-alternatives--install/usr/bin/g++ g++/usr/bin/g++-4.9 20sudo update-alternatives--install/usr/bin/g++ g++/ Usr/bin/g++-5 10sudo update-alternatives--install/usr/bin/cc cc/usr/bin/gcc 30sudo update-alternatives--set cc/usr/b In/gccsudo update-alternatives--install/usr/bin/c++ C + +/usr/bin/g++ 30sudo update-alternatives--set C + +/usr/bin/g++ # work around a glibc bugecho-e "\n[nvcc]\nflags=-d_force_inlines\n" >> ~/.theanorc

Run the above commands one by one to install Theano and Cuda, as well as common libraries such as NumPy, scipy, and more.

Once the installation is complete, you can enter the Python interface to run

Import Numpy;numpy.test ()

Import Scipy;scipy.test ()

Import Theano;theano.test ()

Detects if the installation was successful.

After the author installs the test NumPy and scipy the two libraries display the Ok,theano display fail, but the Theano can be imported and used normally. (I do not understand that, the gods are passing when they look at pointing twos)

At this point, Theano installed, you can run the Theano program on the CPU.

Step three: Configure the. theanorc file

Open the. THEANORC (note front is a dot) file in the Linux root directory

[Global]

device = GPU

Floatx = float32

[NVCC]

Fastmath = True

Added to the file.

Fourth Step: Install NVCC

This is easier.

sudo apt-get insatll NVCC

You can do it.

At this point, all the setup programs are complete.

You can use this code to test whether your program uses CPU or GPU

From Theano import function, config, shared, sandbox  import theano.tensor as T  import numpy  Import Timevlen = Ten * 768 # x  #cores x # Threads per core  iters = 1000rng = numpy.random.RandomState  x = Shared (num Py.asarray (Rng.rand (Vlen), config.floatx))  f = function ([], T.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 '% iters, T1-t0, ' seconds ') print (' Result is ', R)  if Numpy.any ([Isinstance (X.op, t.elemwise) for x in F.maker.fgraph.topos ORT ()]):      print (' used the CPU ') Else:      print (' used the GPU ')

  

The above is my personal installation of the whole process, tossing for a long time. If in doubt or where there is a wrong, please leave a message to point out.

  

Configure the environment under ubuntu16.04 Theano to run the program using the GPU

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.