10.11.2016 Views

Learning Data Mining with Python

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 11<br />

Using Theano, we can define many types of functions working on scalars, arrays,<br />

and matrices, as well as other mathematical expressions. For instance, we can create<br />

a function that computes the length of the hypotenuse of a right-angled triangle:<br />

import theano<br />

from theano import tensor as T<br />

First, we define the two inputs, a and b. These are simple numerical values, so we<br />

define them as scalars:<br />

a = T.dscalar()<br />

b = T.dscalar()<br />

Then, we define the output, c. This is an expression based on the values of a and b:<br />

c = T.sqrt(a ** 2 + b ** 2)<br />

Note that c isn't a function or a value here—it is simply an expression, given a and b.<br />

Note also that a and b don't have actual values—this is an algebraic expression, not<br />

an absolute one. In order to compute on this, we define a function:<br />

f = theano.function([a,b], c)<br />

This basically tells Theano to create a function that takes values for a and b<br />

as inputs, and returns c as an output, computed on the values given. For example,<br />

f(3, 4) returns 5.<br />

While this simple example may not seem much more powerful than what we<br />

can already do <strong>with</strong> <strong>Python</strong>, we can now use our function or our mathematical<br />

expression c in other parts of code and the remaining mappings. In addition, while<br />

we defined c before the function was defined, no actual computation was done until<br />

we called the function.<br />

An introduction to Lasagne<br />

Theano isn't a library to build neural networks. In a similar way, NumPy isn't a<br />

library to perform machine learning; it just does the heavy lifting and is generally<br />

used from another library. Lasagne is such a library, designed specifically around<br />

building neural networks, using Theano to perform the computation.<br />

Lasagne implements a number of modern types of neural network layers, and the<br />

building blocks for building them.<br />

[ 249 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!