Installation Method of ubuntu System theano and keras

  • 2021-07-10 21:29:57
  • OfStack

The system is unbuntu 14.04LTS, 32-bit operating system, previously installed python 3.4, now want to install theano and keras. The steps are as follows:

1. Install pip


sudo apt-get install python3-setuptools
sudo easy_install3 pip

2. Install g + +

 sudo apt-get install g++

Install g + + with the above command. After the installation is completed, you can use g + +-version to see if the installation is complete. Note that if g + + is not installed, the following error occurs when import theano:

WARNING (theano.configdefaults): g++ not detected ! Theano will be unable to execute optimized C-implementations (for both CPU and GPU) and will default to Python implementations. Performance will be severely degraded. To remove this warning, set Theano flags cxx to an empty string.
I searched for 1 because theano is compiled with g + +, and most of the solutions found on the Internet are installed based on Anaconda. The solution is:

conda install mingw libpython

3. Install theano

sudo pip3 install theano

This command automatically downloads the dependencies required by theano, including numpy, scipy, and so on.

4. Install keras

sudo pip3 install keras

Finally, it should be noted that the default backend of keras is tensorflow, and what we need is theano, so we need to modify the settings. (Moreover, tensorflow is installed with pip3, and there is no corresponding version on 32-bit systems! It is complicated to install with source files.)


vim ~/.keras/keras.json
{
 
  "image_dim_ordering":"tf",
 
  "epsilon":1e-07,
 
  "floatx":"float32",
 
  "backend":"theano"
}

5. Test theano


import numpy as np 
import time 
import theano 
A = np.random.rand(1000,10000).astype(theano.config.floatX) 
B = np.random.rand(10000,1000).astype(theano.config.floatX) 
np_start = time.time() 
AB = A.dot(B) 
np_end = time.time() 
X,Y = theano.tensor.matrices('XY') 
mf = theano.function([X,Y],X.dot(Y)) 
t_start = time.time() 
tAB = mf(A,B) 
t_end = time.time() 
print("NP time: %f[s], theano time: %f[s] (times should be close when run on CPU!)" %( 
                      np_end-np_start, t_end-t_start)) 
print("Result difference: %f" % (np.abs(AB-tAB).max(), ))

Summarize

The above is the site to introduce the ubuntu system theano and keras installation method, I hope to help you!


Related articles: