# What is Theano and How it works? An Overview and Its Use Cases

Posted by

### History & Origin of Theano

Theano’s husband, Pythagoras (c. 582-500 B.C.), was inspired one of the most influential sects in the ancient world. Best known for devising the Pythagorean Theoremâ€”which states that the sum of the squares of the sides of a right triangle is equal to the square of the hypotenuseâ€”Pythagoras was considered the greatest scientist of antiquity by classical Greek scholars and is considered to have been the first mathematician. However, given that Pythagoras lived seven generations before Plato, most of the information about him comes from fairly late sourcesâ€”a few as late as the third century A.D. Another problem is that some of these sources are of doubtful reliability. However, references to Pythagoras’s ideas can be found in earlier writings, including those of Empedocles, Heraclitus, Herodotus, Plato, and Aristotle.

### What is Theano

TheanoÂ is a Python library that allows us to evaluate mathematical operations including multi-dimensional arrays so efficiently. It is mostly used in building Deep Learning Projects. It works a way more faster on Graphics Processing Unit (GPU) rather than on CPU. Theano attains high speeds that gives a tough competition to C implementations for problems involving large amounts of data. It can take advantage of GPUs which makes it perform better than C on a CPU by considerable orders of magnitude under some certain circumstances.
It knows how to take structures and convert them into very efficient code that uses numpy and some native libraries. It is mainly designed to handle the types of computation required for large neural network algorithms used in Deep Learning. That is why, it is a very popular library in the field of Deep Learning.

How to install Theano :

pip install theano

Several of the symbols we will need to use are in theÂ tensorÂ subpackage of Theano. We often import such packages with a handy name, letâ€™s say, T.

from theano import *
import theano.tensor as T

Why Theano Python Library :
Theano is a sort of hybrid between numpy and sympy, an attempt is made to combine the two into one powerful library. Some advantages of theano are as follows:

• Stability Optimization:Â Theano can find out some unstable expressions and can use more stable means to evaluate them
• Execution Speed Optimization:Â As mentioned earlier, theano can make use of recent GPUs and execute parts of expressions in your CPU or GPU, making it much faster than Python
• Symbolic Differentiation:Â Theano is smart enough to automatically create symbolic graphs for computing gradients

Basics of Theano :
Theano is a Python library that allows you to define, optimize, and evaluate mathematical expressions involving multi-dimensional arrays efficiently.Some Theano implementations are as follows.

Subtracting two scalars :

 # Python program showing # subtraction of two scalars import theano from theano import tensor # Declaring variables a = tensor.dscalar() b = tensor.dscalar() # Subtracting res = a - b # Converting it to a callable object # so that it takes matrix as parameters func = theano.function([a, b], res) # Calling function assert 20.0 == func(30.5, 10.5)

It will not provide any output as the assertion of two numbers matches the number given, hence it results into a true value.

 # Python program showing # addition of two scalars # Addition of two scalars import numpy import theano.tensor as T from theano import function # Declaring two variables x = T.dscalar('x') y = T.dscalar('y') # Summing up the two numbers z = x + y # Converting it to a callable object # so that it takes matrix as parameters f = function([x, y], z) f(5, 7)

Output:Â array(12.0)

### How Theano works aka Theano architecture?

Theano itself is effectively dead, but the deep learning frameworks built on top of Theano, are still functioning. These include the more user-friendly frameworks- Keras, Lasagne, and Blocks. These three provide high-level frameworks for fast prototyping and model testing.

Lets look at the following code for simply multiplying 2 numbers:
1. Step 0: Import libraries. import numpy as np import theano.tensor as T from theano import function. …
2. Step 1: Define variables. a = T.dscalar(‘a’) b = T.dscalar(‘b’) …
3. Step 2: Define expression. c = a*b f = function([a,b],c) …
4. Step 3: Evaluate Expression. f(1.5,3)

## How to InstallÂ Theano

Theano provides extensive installation instructions for the major operating systems: Windows, OS X and Linux. Read theÂ Installing Theano guide for your platform.

Theano assumes a working Python 2 or Python 3 environment withÂ SciPy. There are ways to make the installation easier, such as usingÂ AnacondaÂ to quickly set up Python and SciPy on your machine as well as usingÂ Docker images.

With a working Python and SciPy environment, it is relatively straightforward to install Theano. from PyPI using pip, for example:

At the time of writing the last official release of Theano was version 0.8 which was released 21th March 2016.

New releases may be announced and you will want to update to get any bug fixes and efficiency improvements. You can upgrade Theano using pip as follows:

You may want to use the bleeding edge version of Theano checked directly out of Github.

This may be required for some wrapper libraries that make use of bleeding edge API changes. You can install Theano directly from a Github checkout as follows:

You are now ready to run Theano on your CPU, which is just fine for the development of small models.

Large models may run slowly on the CPU. If you have a Nvidia GPU, you may want to look into configuring Theano to use your GPU. Read theÂ Using the GPU guides for LinuxÂ orÂ Mac OS X to set up Theano to use the GPUÂ and theÂ Using the GPU guideÂ for how to test whether it is working.

## Simple Theano Example

In this section we demonstrate a simple Python script that gives you a flavor of Theano.

It is taken from theÂ Theano at a Glance guide.Â In this example we define two symbolic floating point variablesÂ aÂ andÂ b.

We define an expression that uses these variablesÂ (c = a + b).

We then compile this symbolic expression into a function using Theano that we can use later.

Finally, we use our complied expression by plugging in some real values and performing the calculation using efficient compiled Theano code under the covers.

Running the example does not provide any output. The assertion thatÂ 1.5 + 2.5 = 4.0Â is true.

This is a useful example as it gives you a flavor for how a symbolic expression can be defined, compiled and used. You can see how this may be scaled up to large vector and matrix operations required for deep learning.

## Extensions and Wrappers for Theano

If you are new to deep learning you do not have to use Theano directly.

In fact, you are highly encouraged to use one of many popular Python projects that make Theano a lot easier to use for deep learning.

These projects provide data structures and behaviors in Python, specifically designed to quickly and reliably create deep learning models whilst ensuring that fast and efficient models are created and executed by Theano under the covers.

The amount of Theano syntax exposed by the libraries varies.

• For example theÂ Lasagne libraryÂ provides convenience classes for creating deep learning model but still expects you to know and make use of Theano syntax. This is good for beginners that know or are willing to learn a little Theano as well.
• Another example isÂ KerasÂ that hides Theano completely and provides a very simple API to work with to create Deep Learning models. It hides Theano so well, that it can in fact run as a wrapper for another popular foundation framework calledÂ TensorFlow.

### Feature and Advantage of using Theano

Pascal Lamblin,
Razvan Pascanu,

James Bergstra,

Ian Goodfellow,

Arnaud Bergeron,

Nicolas Bouchard,

David Warde-Farley,

Yoshua Bengio,

###### ABSTRACT

Theano is a linear algebra compiler that optimizes a userâ€™s symbolically-specified mathematical computations to produce efficient low-level implementations. In this paper, we present new features and efficiency improvements to Theano, and benchmarks demonstrating Theanoâ€™s performance relative to Torch7, a recently introduced machine learning library, and to RNNLM, a C++ library targeted at recurrent neural networks.

### Best Alternative of Theano

Top 10 Alternatives to Theano
• scikit-learn.
• Keysight Eggplant Platform.
• machine-learning in Python.
• V7.
• Personalizer.
• Kubeflow.
• Torch.

### Interview Questions and Answer for Theano

1.Â Theano is a _______ library.

A. R
B. Python
C. Ruby
D. Javascript

Ans : B
Explanation: Theano is a Python library that allows us to evaluate mathematical operations including multi-dimensional arrays so efficiently.

2.Â Theano works a way _______ on Graphics Processing Unit (GPU) rather than on CPU.

A. slower
B. very slower
C. faster
D. more faster

Ans : D
Explanation: It works a way more faster on Graphics Processing Unit (GPU) rather than on CPU.

3.Â Which command used to install Theano?

A. install theano
B. np install theano
C. pip install theano
D. npm install theano

Ans : C
Explanation: pip install theano command used to install Theano

4.Â Which of the following are advantages of theano ?

A. Stability Optimization
B. Execution Speed Optimization
C. Symbolic Differentiation
D. All of the above

Ans : D
Explanation: All of the above are advantages of theano.

5.Â Which theano type has 16-bit integers?

A. bscalar
B. wscalar
C. iscalar
D. fscalar

Ans : B
Explanation: 16-bit integers : wscalar, wvector, wmatrix, wrow, wcol, wtensor3, wtensor4, wtensor5, wtensor6, wtensor7

6.Â fmatrix theano type has data type?

A. float
B. double
C. 32-bit integers
D. 64-bit integers

Ans : A
Explanation: float : fscalar, fvector, fmatrix, frow, fcol, ftensor3, ftensor4, ftensor5, ftensor6, ftensor7

7.Â Theano function acts like a hook for interacting with the symbolic graph.

A. TRUE
B. FALSE
C. Can be true or false
D. Can not say

Ans : A
Explanation: True, Theano function acts like a hook for interacting with the symbolic graph.

8.Â A Theano function is declared using the following syntax?

A. theano.declare ([x], y)
B. theano.fun ([x], y)
C. theano.function ([x], y)
D. function ([x], y)

Ans : C
Explanation: A Theano function is declared using the following syntax : theano.function ([x], y)

9.Â The _________ method declares a decimal scalar variable.

A. scalar
B. theano
C. declare
D. dscalar

Ans : D
Explanation: The dscalar method declares a decimal scalar variable.

10.Â Theano was written at the LISA lab with the intention of providing rapid development of efficient machine learning algorithms.

A. TRUE
B. FALSE
C. Can be true or false
D. Can not say

Ans : A
Explanation: True, Theano was written at the LISA lab with the intention of providing rapid development of efficient machine learning algorithms.

Subscribe
Notify of