传递“输入"的目的/意义是什么?到 Theano 中的函数? [英] What is the prupose/meaning of passing "input" to a function in Theano?

查看:23
本文介绍了传递“输入"的目的/意义是什么?到 Theano 中的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这个例子会更清楚,(这是一个逻辑回归对象,Theano Tensor 库被导入为 T)

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)

在 main 中调用...

Which is called down in main...

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

如果这些片段还不足以让人理解,代码在这个页面的把它放在一起"-http://deeplearning.net/tutorial/logreg.html#logreg

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

推荐答案

so... Theano 在计算表达式之前为其计算表达式构建图形.通过将示例中的 'x' 等 theano 变量传递给逻辑回归对象的初始化,您将在对象中创建许多表达式,例如 p_y_given_x,它们是依赖于 x 的 theano 表达式.稍后用于符号梯度计算.

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

这就是 theano 内部存储表达式的方式.然后就可以使用这些表达式在theano中创建函数了,比如

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()

然后调用 f 应该为您提供 mydata 中定义的值的预测类概率.在 theano 中执行此操作的方法(以及在 DL 教程中执行此操作的方法)是传递一个虚拟"theano 变量,然后使用givens"关键字将其设置为包含您的数据的共享变量.这很重要,因为将变量存储在共享变量中可以让 theano 使用 GPU 进行矩阵运算.

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.

这篇关于传递“输入"的目的/意义是什么?到 Theano 中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆