Frage

Example will make that clearer I hope, (This is a Logistic Regression object, the 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 down in main...

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

If these snippits aren't enough to get an understanding, the code is on this page under "Putting it All Together"- http://deeplearning.net/tutorial/logreg.html#logreg

War es hilfreich?

Lösung

so... Theano builds graphs for the expressions it computes before evaluating them. By passing a theano variable such as 'x' in the example to the initialization of the logistic regression object, you will create a number of expressions such as p_y_given_x in your object which are theano expressions dependent on x. This is later used for symbolic gradient calculation.

To get a better feel for it 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 given you an output such as

softmax( W \dot x + b)

And while you're at it go ahead and try out

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

which is how theano internally stores the expression. Then you can use these expressions to create functions in theano, 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 theano (and the way it's done in the DL tutorials) is by passing a "dummy" theano variable and then using the "givens" keyword to set it to a shared variable containing your data. That's important because storing your variables in a shared variable allows theano to use your GPU for matrix operations.

Andere Tipps

This is a Python feature called named parameters. For functions with optional parameters or many parameters it is helpful to pass the parameters by name, instead of just relying on the order on which they were passed to the function. In your specific case you can see the meaning of the input parameter here.

Named parameters, or default keyword arguments, like input, n_in, and n_out are useful for several reasons.

  • If a function/method have many parameters, it becomes easier to pass them by name instead of having to remember the functional order of the parameters.
  • Many functions/methods have default use cases which are used often, and specialty use cases which are used rarely. If the specialty use case requires passing additional arguments to the function, those will most likely take the form of named parameters with default values. This way, when the function is used in the default use case, the user does not have to specify any additional parameters. Only when someone wants to use the specialty case will they have to specify something extra. This keeps function and method calls readable and simple when they aren't used in complex or specialty ways.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top