Neural Networks: Understanding the Anano Library

Can someone explain to me the output of the 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]]) 

Function testing

 print f( [ [1,1],[1,1] ], [ [0,1],[2,3] ]) Output: [ [[ 1., 0.], [-1., -2.]], [[ 1., 0.], [ 1., 2.]], [[ 1., 0.], [ 1., 4.]]] 
+5
source share
1 answer

In fact, you are talking to Theano about calculating three different functions, where each subsequent function depends on the output of a previously executed function.

In your example, you use 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 matrices A and B:

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

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

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

The third and last line calculates quadratic values ​​for the elements.

The magic of teano

Theano actually interprets your Python code and gives out which variables (or mathematical operations) this variable depends on. Thus, if you are only interested in diff_squared output, you do not need to include calls to diff and abs_diff too.

 f = function([a, b], [diff_squared]) 
+5
source

Source: https://habr.com/ru/post/1237318/


All Articles