Theano的function()报告图表不需要我的`givens`值

时间:2016-10-19 18:09:13

标签: python neural-network deep-learning theano conv-neural-network

很抱歉没有发布整个代码段 - 代码非常大并且分散开来,所以希望这可以说明我的问题。我有这些:

train = theano.function([X], output, updates=update_G,
                        givens={train_mode=:np.cast['int32'](1)})

test = theano.function([X], output, updates=update_G,
                       givens={train_mode=:np.cast['int32'](0)})

根据我的理解,givens会在计算输出所需的任何地方输入train_mode(即1 / 0)的值。

output的计算方法如下:

     ...
     network2 = Net2()
     # This is sort of a dummy variable so I don't get a NameError when this
     # is called before `theano.function()` is called. Not sure if this is the
     # right way to do this.
     train_mode = T.iscalar('train_mode')
     output = loss(network1.get_outputs(network2.get_outputs(X, train_mode=train_mode)),something).mean()

 ....
 class Net2(): 
      def get_outputs(self, x, train_mode):
           from theano.ifelse import ifelse
           import theano.tensor as T
           my_flag = ifelse(T.eq(train_mode, 1), 1, 0)
           return something if my_flag else something_else

所以train_mode被用作其中一个嵌套函数的参数,我用它来告诉traintest,因为我想稍微区别对待它们。

然而,当我尝试运行它时,我收到此错误:

theano.compile.function_module.UnusedInputError: theano.function was
asked to create a function computing outputs given certain inputs, but
the provided input variable at index 1 is not part of the computational
graph needed to compute the outputs: <TensorType(int32, scalar)>.To make 
this error into a warning, you can pass the parameter 
on_unused_input='warn' to theano.function. To disable it completely, use 
on_unused_input='ignore'.    

如果我删除givens参数,错误就会消失,所以据我所知,Theano认为我的train_mode不是计算function()所必需的。我可以根据他们的建议使用on_unusued_input='ignore',但如果他们认为它未被使用,则会忽略我的train_mode。我是以错误的方式绕过这个吗?我基本上只想训练一个带有辍学的神经网络,但在评估时不要使用辍学。

1 个答案:

答案 0 :(得分:0)

为什么使用“=”符号?我认为,它使train_mode不可读,我的代码通过编写得很好: givens = {train_mode:1}