在Theano中如何在不重新编译函数的情况下更改函数的更新列表?

确实,我想在训练的不同阶段更改学习率。类似于这样:

for i in range(iter_num):    learn_rate = i*alpha    do_training(learn_rate,...)

显然,每次迭代重新编译一个新函数会太慢。所以我想知道在Theano中是否有更好的方法来实现这一点?谢谢!


回答:

你可以将学习率设为符号变量,并像这样传递给训练函数:

import numpyimport theanoimport theano.tensor as ttdef 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()

请注意,现在训练函数有三个参数;第三个是学习率。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注