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

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.

Adding two scalars :

# 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)

Use case of  Theano

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

Frédéric Bastien,
Pascal Lamblin,
Razvan Pascanu,

James Bergstra,

Ian Goodfellow,

Arnaud Bergeron,

Nicolas Bouchard,

David Warde-Farley,

Yoshua Bengio,

Dept. IRO, Université de Montréal, Montréal (QC), H3C 3J7, Canada
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.
  • Google Cloud TPU.
  • Torch.

Free Video Tutorials of Theano

Interview Questions and Answer for Theano

 

1. Theano is a _______ library.

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

View Answer

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

View Answer

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

View Answer

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

View Answer

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

View Answer

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

View Answer

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

View Answer

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)

 Answer

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

View Answer

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

View Answer

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

 

 

Rajesh Kumar
Follow me
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x