Python 重新初始化已编译Theano函数的最佳方法
我想在Theano中刷新我编译的MLP模型,因为我想用不同的超参数重复一个模型 我知道我可以重新定义所有函数,但是,每个函数的编译时间非常重要 我想定义一个函数,它将刷新它们的模型 下面显示的代码用于演示Python 重新初始化已编译Theano函数的最佳方法,python,machine-learning,neural-network,theano,Python,Machine Learning,Neural Network,Theano,我想在Theano中刷新我编译的MLP模型,因为我想用不同的超参数重复一个模型 我知道我可以重新定义所有函数,但是,每个函数的编译时间非常重要 我想定义一个函数,它将刷新它们的模型 下面显示的代码用于演示 # construct the MLP class classifier = MLP( rng=rng, input=x, n_in= n_input, n_hidden=n_hidden, n_o
# construct the MLP class
classifier = MLP(
rng=rng,
input=x,
n_in= n_input,
n_hidden=n_hidden,
n_out= n_output)
cost = (classifier.negative_log_likelihood(y)
+ self.l1 * classifier.L1
+ self.l2 * classifier.L2_sqr)
gparams = [T.grad(cost, param) for param in classifier.params]
updates = [(param, param - self.lr * gparam) \
for param, gparam in zip(classifier.params, gparams)]
train_model = theano.function(
inputs=[index],
outputs=cost,
updates=updates,
givens={x: self.xTrain[index * self.batchSize: (index + 1) * self.batchSize],
y: self.yTrain[index * self.batchSize: (index + 1) * self.batchSize]})
我的直觉表明,我可以简单地重新定义MLP()
类,而不影响其他编译函数
这是正确的吗
我在想,如果是这种情况,我可以定义一个函数
MLP.refresh()
,它为MLP()的每个组件重新实例化参数
class不清楚MLP类是如何工作的,但只要共享变量的维数不变,就可以重复使用以前编译的计算
在下面的示例中,compile\u model
函数创建了一个带有随机初始化参数的简单神经网络。使用这些参数进行训练后,共享变量将重新初始化为新的随机值,但这一次网络的隐藏层大小将增加。尽管大小发生了变化,原始的训练功能仍被重新使用
import numpy
import theano
import theano.tensor as tt
def compile_model(input_size, hidden_size, output_size, learning_rate):
w_h = 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))
w_y = theano.shared(numpy.random.randn(hidden_size, output_size).astype(theano.config.floatX))
b_y = theano.shared(numpy.zeros(output_size, dtype=theano.config.floatX))
parameters = (w_h, b_h, w_y, b_y)
x = tt.matrix()
z = tt.lvector()
h = tt.tanh(theano.dot(x, w_h) + b_h)
y = tt.nnet.softmax(theano.dot(h, w_y) + b_y)
c = tt.nnet.categorical_crossentropy(y, z).mean()
u = [(p, p - learning_rate * theano.grad(c, p)) for p in parameters]
trainer = theano.function([x, z], outputs=[c], updates=u)
tester = theano.function([x], outputs=[y])
return trainer, tester, parameters
def refresh_model(parameters, input_size, hidden_size, output_size):
w_h, b_h, w_y, b_y = parameters
w_h.set_value(numpy.random.randn(input_size, hidden_size).astype(theano.config.floatX))
b_h.set_value(numpy.zeros(hidden_size, dtype=theano.config.floatX))
w_y.set_value(numpy.random.randn(hidden_size, output_size).astype(theano.config.floatX))
b_y.set_value(numpy.zeros(output_size, dtype=theano.config.floatX))
def main():
input_size = 30
hidden_size = 10
output_size = 20
learning_rate = 0.01
batch_size = 40
epoch_count = 50
trainer, tester, parameters = compile_model(input_size, hidden_size, output_size, learning_rate)
x = numpy.random.randn(batch_size, input_size)
z = numpy.random.randint(output_size, size=(batch_size,))
print 'Training model with hidden size', hidden_size
for _ in xrange(epoch_count):
print trainer(x, z)
hidden_size = 15
refresh_model(parameters, input_size, hidden_size, output_size)
print 'Training model with hidden size', hidden_size
for _ in xrange(epoch_count):
print trainer(x, z)
main()
它们是否必须被实例化为
theano.shared
?取决于您认为的替代方案。您几乎肯定希望使用共享变量。