Theano Tutorial文档翻译(六) - 条件

来源:互联网 发布:apt yum rpm 编辑:程序博客网 时间:2024/04/24 16:01

原文地址:http://deeplearning.net/software/theano/tutorial/conditions.html


IfElse vs Switch

  • 两个操作符都在符号变量构建了条件。
  • IfElse要提供一个布尔条件和两个变量
  • Switch要提供一个张量条件和两个变量。switch是一个依次元素运算,所以比ifelse更常用。
  • switch评估两个输出变量,ifelse很懒并且只评估和条件有关的一个变量。

例子:

from theano import tensor as Tfrom theano.ifelse import ifelseimport theano, time, numpya, b = T.scalars('a', 'b')x, y = T.matrices('x', 'y')z_switch = T.switch(T.lt(a, b), T.mean(x), T.mean(y))z_lazy = ifelse(T.lt(a, b), T.mean(x), T.mean(y))f_switch = theano.function([a, b, x, y], z_switch, mode=theano.Mode(linker='vm'))f_lazyifelse = theano.function([a, b, x, y], z_lazy, mode=theano.Mode(linker='vm'))val1 = 0val2 = 1big_mat1 = numpy.ones((10000, 1000))big_mat2 = numpy.ones((10000, 1000))n_times = 10tic = time.clock()for i in range(n_times):    f_switch(val1, val2, big_mat1, big_mat2)print('time spent evaluating both values %f sec' % (time.clock() - tic))tic = time.clock()for i in range(n_times):    f_lazyifelse(val1, val2, big_mat1, big_mat2)print('time spent evaluating both values %f sec' % (time.clock() - tic))

在这个例子中,当一条件两输出IfElse操作花费更小的时间(约switch的一半)。

time spent evaluating both values 0.6700 sectime spent evaluating one value 0.3500 sec

除非使用linker='vm'linker=‘cv’,ifelse会计算两个变量并且和switch有相同计算时间。即使连接器不是经常默认设置cvm,它会在将来实现。

这里没有自动优化器会用一个ifelse的广播标量替代swicth,这往往不是更快的。

注意:如果你使用test values,然后所有IfElse的分支都会被计算。这很正常,因为Python的贪婪原则和test values的语义,当使用test vales时意味着所有都会被计算。当我们构建两个分支,他们都会在test values被执行。这不会产生任何改变当执行Theano的编译功能

0 0