如何为不同的输入重用计算图? [英] How to reuse computation graph for different inputs?
问题描述
我设置了主要的计算流程,可以使用
I have my main flow of computation set up that I can train using
train = theano.function(inputs=[x], outputs=[cost], updates=updates)
同样,我有一个预测功能
Similarly, I have a function for predictions
predict = theano.function(inputs=[x], outputs=[output])
这两个函数都接受输入 x
并通过相同的计算图发送它.
Both of these functions accept the input x
and send it through the same computation graph.
我现在想修改一些东西,以便在训练时,我可以使用嘈杂的输入进行训练,所以我有类似的东西
I would now like to modify things so that when training, I can train using a noisy input, so I have something like
input = get_corrupted_input(self.theano_rng, x, 0.5)
在计算开始时.
但这也会影响我的 predict
函数,因为它的输入也会被破坏.如何为 train
和 predict
重用相同的代码,但只为前者提供嘈杂的输入?
But this will also affect my predict
function, since its input will get corrupted as well. How can I reuse the same code for train
and predict
, but only provide the former with the noisy input?
推荐答案
你可以这样组织你的代码:
You can organise your code like this:
import numpy
import theano
import theano.tensor as tt
import theano.tensor.shared_randomstreams
def get_cost(x, y):
return tt.mean(tt.sum(tt.sqr(x - y), axis=1))
def get_output(x, w, b_h, b_y):
h = tt.tanh(tt.dot(x, w) + b_h)
y = tt.dot(h, w.T) + b_y
return y
def corrupt_input(x, corruption_level):
rng = tt.shared_randomstreams.RandomStreams()
return rng.binomial(size=x.shape, n=1, p=1 - corruption_level,
dtype=theano.config.floatX) * x
def compile(input_size, hidden_size, corruption_level, learning_rate):
x = tt.matrix()
w = theano.shared(numpy.random.randn(input_size,
hidden_size).astype(theano.config.floatX))
b_h = theano.shared(numpy.zeros(hidden_size, dtype=theano.config.floatX))
b_y = theano.shared(numpy.zeros(input_size, dtype=theano.config.floatX))
cost = get_cost(x, get_output(corrupt_input(x, corruption_level), w, b_h, b_y))
updates = [(p, p - learning_rate * tt.grad(cost, p)) for p in (w, b_h, b_y)]
train = theano.function(inputs=[x], outputs=cost, updates=updates)
predict = theano.function(inputs=[x], outputs=get_output(x, w, b_h, b_y))
return train, predict
def main():
train, predict = compile(input_size=3, hidden_size=2,
corruption_level=0.2, learning_rate=0.01)
main()
请注意,get_output
被调用了两次.对于 train
函数,它提供了损坏的输入,但对于 predict
函数,它提供了干净的输入.get_output
需要包含您所说的相同的计算图".我只是在里面放了一个很小的自动编码器,但你可以在里面放任何你想要的东西.
Note that get_output
is called twice. For the train
function it is provided with the corrupted input but for the predict
function it is provided with the clean input. get_output
needs to contain "the same computation graph" that you talk of. I've just put a tiny autoencoder in there but you can put whatever you want in there.
假设损坏的输入与输入具有相同的形状,get_output
函数不会关心它的输入是 x
还是 x 的损坏版本代码>.所以
get_output
可以共享但不需要包含损坏代码.
Assuming the corrupted input has the same shape as the input, the get_output
function won't care whether its input is x
or the corrupted version of x
. So get_output
can be shared but need not contain the corruption code.
这篇关于如何为不同的输入重用计算图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!