Theano.function 中“给定"变量的用途 [英] Purpose of 'givens' variables in Theano.function

查看:20
本文介绍了Theano.function 中“给定"变量的用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 http://deeplearning.net/tutorial/给出的逻辑函数代码logreg.html.我对 inputs & 之间的区别感到困惑givens 函数的变量.计算模型在小批量上所犯错误的函数是:

I was reading the code for the logistic function given at http://deeplearning.net/tutorial/logreg.html. I am confused about the difference between inputs & givens variables for a function. The functions that compute mistakes made by a model on a minibatch are:

 test_model = theano.function(inputs=[index],
        outputs=classifier.errors(y),
        givens={
            x: test_set_x[index * batch_size: (index + 1) * batch_size],
            y: test_set_y[index * batch_size: (index + 1) * batch_size]})

validate_model = theano.function(inputs=[index],
        outputs=classifier.errors(y),
        givens={
            x: valid_set_x[index * batch_size:(index + 1) * batch_size],
            y: valid_set_y[index * batch_size:(index + 1) * batch_size]})

为什么不能/不会制作 x&y 共享输入变量并让它们在创建实际模型实例时定义?

Why couldn't/wouldn't one just make x& y shared input variables and let them be defined when an actual model instance is created?

推荐答案

givens 参数允许您将模型的描述和输入变量的确切定义分开.这是给定参数所做的结果:在编译之前修改要编译的图形.换句话说,我们在图中用相关联的值替换给定中的键.

The givens parameter allows you to separate the description of the model and the exact definition of the inputs variable. This is a consequence of what the given parameter do: modify the graph to compile before compiling it. In other words, we substitute in the graph, the key in givens with the associated value.

在深度学习教程中,我们使用一个普通的 Theano 变量来构建模型.我们使用 givens 来加速 GPU.在这里,如果我们将数据集保留在 CPU 上,我们将在每次函数调用时将一个小批量传输到 GPU.当我们对数据集进行多次迭代时,我们最终将数据集多次传输到 GPU.由于数据集小到可以放在 GPU 上,我们将它放在一个共享变量中,以便在 GPU 可用时将其传输到 GPU(或者如果图形处理单元被禁用,则保留在中央处理单元上).然后在编译函数时,我们将输入与要使用的数据集的小批量对应的切片交换.那么 Theano 函数的输入就是我们要使用的那个 mini-batch 的索引.

In the deep learning tutorial, we use a normal Theano variable to build the model. We use givens to speed up the GPU. Here, if we keep the dataset on the CPU, we will transfer a mini-batch to the GPU at each function call. As we do many iterations on the dataset, we end up transferring the dataset multiple time to the GPU. As the dataset is small enough to fit on the GPU, we put it in a shared variable to have it transferred to the GPU if one is available (or stay on the Central Processing Unit if the Graphics Processing Unit is disabled). Then when compiling the function, we swap the input with a slice corresponding to the mini-batch of the dataset to use. Then the input of the Theano function is just the index of that mini-batch we want to use.

这篇关于Theano.function 中“给定"变量的用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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