Search code examples
pythonneural-networktheano

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.]]]

Solution

  • 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])