What is the prupose / meaning of passing "input" functions to Theano?

An example will make this clearer, I hope (Logistic Regression object, Theano Tensor library is imported as T)

def __init__(self, input, n_in, n_out): #Other code... self.p_y_given_x = T.nnet.softmax(T.dot(input, self.W) + self.b) 

which is called mainly ...

 def main(): x = T.matrix() classifier = LogisticRegression(input=x, n_in=28 * 28, n_out=10) 

If these fragments are not enough to understand, the code is on this page in the section "Entering All Together" - http://deeplearning.net/tutorial/logreg.html#logreg

+4
source share
3 answers

therefore ... Theano builds graphs for the expressions that he calculates before evaluating them. By passing an anano variable, such as "x" in the example, to initialize the logistic regression object, you will create several expressions in your object, such as p_y_given_x, which are x-dependent anano expressions. This is later used to calculate the symbolic gradient.

To better understand, you can do the following:

 import theano.pp #pp is for pretty print x = T.dmatrix('x') #naming your variables is a good idea, and important i think lr = LogisticRegression(x,n_in = 28*28, n_out= 10) print pp(lr.p_y_given_x) 

This should give you a result like

 softmax( W \dot x + b) 

And while you go ahead and try

 print pp(T.grad(lr._y_given_x,x)) #might need syntax checkng 

how anano internally preserves expression. You can then use these expressions to create functions in anano, such as

 values = theano.shared( value = mydata, name = 'values') f = theano.function([],lr.p_y_given_x , givens ={x:values},on_unused_input='ignore') print f() 

then calling f should give you the predicted class probabilities for the values ​​defined in mydata. The way to do this in anano (and how it is done in DL tutorials) is to pass the aano variable “dummy” and then use the keyword “givens” to set it to a shared variable containing your data. This is important because storing your variables in a shared variable allows theano to use your GPU for matrix operations.

+4
source

This is a Python function called named parameters . For functions with optional parameters or many parameters, it is useful to pass parameters by name, rather than simply relying on the order in which they were passed to the function. In your specific case, you can see the value of the input parameter here .

+1
source

Default keyword arguments or arguments, such as input , n_in and n_out , are useful for several reasons.

  • If a function / method has many parameters, it becomes easier to pass them by name instead of having to remember the functional order of the parameters.
  • Many functions / methods use default cases that are often used, and special cases that are rarely used. If for the case of using the specialty it is required to pass additional arguments to the function, they will most likely take the form of named parameters with default values. Thus, when the function is used in the case of use by default, the user does not need to specify any additional parameters. Only when someone wants to use a specialty should they indicate something extra. This allows you to read functions and methods as soon as they are not used in complex or special ways.
0
source

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


All Articles