How to install theano and keras on ubuntu system

How to install theano and keras on ubuntu system

Note: The system is Ubuntu 14.04LTS, a 32-bit operating system. Python 3.4 was previously installed, and now I want to install theano and keras. Here are the steps:

1. Install pip

sudo apt-get install python3-setuptools
sudo easy_install3 pip

2. Install g++

sudo apt-get install g++

Use the above command to install g++. After the installation is complete, you can use g++ -version to check whether the installation is complete. Note that if g++ is not installed, the following error will occur when importing 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 because Theano is faster when compiled with g++. Most of the solutions found on the Internet are based on Anaconda installation. The solution is:

conda install mingw libpython

3. Install theano

sudo pip3 install theano

This command will automatically download the dependencies required by theano, including numpy, scipy, etc.

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. (And tensorflow is installed with pip3, and there is no corresponding version on 32-bit systems! Installation with source files is also very complicated)

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 installation method of Theano and Keras on Ubuntu system introduced by the editor. I hope it will be helpful to everyone!

You may also be interested in:
  • Solve the problem of installing Theano on Ubuntu 19

<<:  Detailed explanation of mysql replication tool based on python

>>:  Vue integrates Tencent Map to implement API (with DEMO)

Recommend

javascript countdown prompt box

This article example shares the specific code of ...

A Deeper Look at SQL Injection

1. What is SQL injection? Sql injection is an att...

Specific operations of MYSQL scheduled clearing of backup data

1|0 Background Due to project requirements, each ...

Vue mobile terminal determines the direction of finger sliding on the screen

The vue mobile terminal determines the direction ...

About the implementation of JavaScript carousel

Today is another very practical case. Just hearin...

JS implements dragging the progress bar to change the transparency of elements

What I want to share today is to use native JS to...

TypeScript Mapping Type Details

Table of contents 1. Mapped Types 2. Mapping Modi...

How InnoDB cleverly implements transaction isolation levels

Preface In the previous article Detailed Explanat...

MySQL 8.0.17 installation and configuration graphic tutorial

This article records the graphic tutorial of MySQ...

A complete guide to clearing floats in CSS (summary)

1. Parent div defines pseudo-classes: after and z...

N ways to cleverly implement adaptive dividers with CSS

Dividing lines are a common type of design on web...

How to write the introduction content of the About page of the website

All websites, whether official, e-commerce, socia...