Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neural Networks: Understanding theano Library

Can anyone explain me the output of following python code:

from theano import tensor as T
from theano import function, shared

a, b = T.dmatrices('a', 'b')
diff = a - b
abs_diff = abs(diff)
diff_squared = diff ** 2

f = function([a, b], [diff, abs_diff, diff_squared])

print f([[1, 1], [1, 1]], [[0, 1], [2, 3]])

Testing the function

print f( [ [1,1],[1,1] ], 
         [ [0,1],[2,3] ])  

Output:  [ [[ 1.,  0.], [-1., -2.]], 
           [[ 1.,  0.], [ 1.,  2.]], 
           [[ 1.,  0.], [ 1.,  4.]]]
like image 647
ojas Avatar asked Dec 03 '15 09:12

ojas


People also ask

Is TensorFlow better than theano?

TensorFlow execution speed is slow when compared to Theano. But in the case of handling the tasks which require multiple GPU TensorFlow is faster. Theano performs tasks way faster than TensorFlow. Mainly the tasks that require a single GPU will run faster in Theano.

What is theano in deep learning?

Theano is a Python library for fast numerical computation that can be run on the CPU or GPU. It is a key foundational library for Deep Learning in Python that you can use directly to create Deep Learning models or wrapper libraries that greatly simplify the process.

What is a neural network library?

Neural Network Libraries is deep learning framework that is intended to be used for research, development, and production. We aim it running everywhere like desktop PCs, HPC clusters, embedded devices and production servers.


1 Answers

You are actually telling Theano to calculate three different functions, where each succeeding function depends on the output of the previously executed function.

In your example, you are using two input parameters: matrix A and matrix B.

A = [[ 1, 1 ],
     [ 1, 1 ]]

B = [[ 0, 1 ],
     [ 2, 3 ]] 

The first output line: [[ 1., 0.], [-1., -2.]] is calculated by subtracting your A and B matrices:

[[1, 1],   -   [[0, 1],    = [[ 1, 0 ],
 [1, 1]]        [2, 3]]       [-1, -2]

The second output line [[ 1., 0.], [ 1., x2.]] is merely the absolute value of the difference we just calculated:

abs [[ 1, 0 ],   =   [[ 1, 0],
     [-1, -2]]        [ 1, 2]]

The third and final line calculates the squared values, element-wise.

Theano magic

Theano actually interprets your Python code, and infer which variables (or mathematical operations) a given variable depend upon. Thus, if you are only interested in the diff_squared output, you don't need to include the calls to diff and abs_diff too.

f = function([a, b], [diff_squared])
like image 157
jorgenkg Avatar answered Sep 29 '22 04:09

jorgenkg