2015-12-08 44 views

回答

3

您可以學習率的符號變量,並傳遞到像這樣的培訓功能:

import numpy 
import theano 
import theano.tensor as tt 


def compile(input_size, hidden_size, output_size): 
    W_h = theano.shared(numpy.random.standard_normal(size=(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.standard_normal(size=(hidden_size, output_size)).astype(theano.config.floatX)) 
    b_y = theano.shared(numpy.zeros((output_size,), dtype=theano.config.floatX)) 

    x = tt.matrix('x') 
    z = tt.ivector('z') 
    learning_rate = tt.scalar() 
    h = tt.tanh(theano.dot(x, W_h) + b_h) 
    y = tt.nnet.softmax(theano.dot(h, W_y) + b_y) 
    cost = tt.nnet.categorical_crossentropy(y, z).mean() 
    updates = [(p, p - learning_rate * tt.grad(cost, p)) for p in (W_h, b_h, W_y, b_y)] 
    return theano.function([x, z, learning_rate], outputs=cost, updates=updates) 


def main(): 
    input_size = 5 
    hidden_size = 4 
    output_size = 3 
    train = compile(input_size, hidden_size, output_size) 
    print train([[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]], [1, 2], 0.1) 


main() 

注意,訓練函數現在有三個參數;第三是學習率。

相關問題