【问题标题】:Best way to re-initialise a compiled Theano function重新初始化已编译 Theano 函数的最佳方法
【发布时间】:2015-11-21 07:19:06
【问题描述】:

我想在 Theano 中刷新我编译的 MLP 模型,因为我想重复一个具有不同超参数的模型。

我知道我可以重新定义所有函数,但是每个函数的编译时间都很重要。

我想定义一个函数来刷新他们的模型

以下代码用于演示。

    # 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() 类的每个组件重新实例化参数

【问题讨论】:

    标签: python machine-learning neural-network theano


    【解决方案1】:

    尚不清楚MLP 类是如何工作的,但只要共享变量的维数不变,您就可以重新使用之前编译的计算。

    在以下示例中,compile_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 吗?
    • 取决于您认为的替代方案。您几乎肯定想使用共享变量。
    猜你喜欢
    • 2015-11-12
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多