Lasagne
1.0.0
Lasagne 是一个轻量级库,用于在 Theano 中构建和训练神经网络。其主要特点是:
其设计遵循六个原则:
简而言之,您可以通过以下方式安装已知兼容的 Theano 版本和最新的 Lasagne 开发版本:
pip install -r https://raw.githubusercontent.com/Lasagne/Lasagne/master/requirements.txt
pip install https://github.com/Lasagne/Lasagne/archive/master.zip
有关更多详细信息和替代方案,请参阅安装说明。
文档可在线获取:http://lasagne.readthedocs.org/
如需支持,请参阅烤宽面条用户邮件列表。
import lasagne
import theano
import theano . tensor as T
# create Theano variables for input and target minibatch
input_var = T . tensor4 ( 'X' )
target_var = T . ivector ( 'y' )
# create a small convolutional neural network
from lasagne . nonlinearities import leaky_rectify , softmax
network = lasagne . layers . InputLayer (( None , 3 , 32 , 32 ), input_var )
network = lasagne . layers . Conv2DLayer ( network , 64 , ( 3 , 3 ),
nonlinearity = leaky_rectify )
network = lasagne . layers . Conv2DLayer ( network , 32 , ( 3 , 3 ),
nonlinearity = leaky_rectify )
network = lasagne . layers . Pool2DLayer ( network , ( 3 , 3 ), stride = 2 , mode = 'max' )
network = lasagne . layers . DenseLayer ( lasagne . layers . dropout ( network , 0.5 ),
128 , nonlinearity = leaky_rectify ,
W = lasagne . init . Orthogonal ())
network = lasagne . layers . DenseLayer ( lasagne . layers . dropout ( network , 0.5 ),
10 , nonlinearity = softmax )
# create loss function
prediction = lasagne . layers . get_output ( network )
loss = lasagne . objectives . categorical_crossentropy ( prediction , target_var )
loss = loss . mean () + 1e-4 * lasagne . regularization . regularize_network_params (
network , lasagne . regularization . l2 )
# create parameter update expressions
params = lasagne . layers . get_all_params ( network , trainable = True )
updates = lasagne . updates . nesterov_momentum ( loss , params , learning_rate = 0.01 ,
momentum = 0.9 )
# compile training function that updates parameters and returns training loss
train_fn = theano . function ([ input_var , target_var ], loss , updates = updates )
# train network (assuming you've got some training data in numpy arrays)
for epoch in range ( 100 ):
loss = 0
for input_batch , target_batch in training_data :
loss += train_fn ( input_batch , target_batch )
print ( "Epoch %d: Loss %g" % ( epoch + 1 , loss / len ( training_data )))
# use trained network for predictions
test_prediction = lasagne . layers . get_output ( network , deterministic = True )
predict_fn = theano . function ([ input_var ], T . argmax ( test_prediction , axis = 1 ))
print ( "Predicted class for first test input: %r" % predict_fn ( test_data [ 0 ]))
有关功能齐全的示例,请参阅 Examples/mnist.py,并查看教程以获取对其的深入解释。更多示例、代码片段和最近研究论文的复制品保存在单独的烤宽面条食谱存储库中。
如果您发现烤宽面条对您的科学工作有用,请考虑在最终的出版物中引用它。我们提供了一个现成的 BibTeX 条目用于引用 Lasagne。
烤宽面条是一项正在进行的工作,欢迎输入。
有关如何贡献的详细信息,请参阅贡献说明!