首页 > 程序开发 > 综合编程 > 其他综合 >

基于深度学习框架theano的库-----Lasagne

2017-03-22

基于深度学习框架theano的库-----Lasagne,lasagne意味千层饼,是基于theano之上主要用于建立和训练神经网络的深度学习库。Lasagne is a lightweight library to build and train neural networks in Theano。

1、Lasagne简单介绍

lasagne意味千层饼,是基于theano之上主要用于建立和训练神经网络的深度学习库。Lasagne is a lightweight library to build and train neural networks in Theano.
网站链接:https://github.com/Lasagne/Lasagnelasagne官网教程
主要特点有:
(1)支持所有的前馈神经网络的模型,包括CNNs、RNN+LSTM以及任何扩展的卷积神经网络。
Supports feed-forward networks such as Convolutional Neural Networks (CNNs), recurrent networks including Long Short-Term Memory (LSTM), and any combination thereof
(2)支持多输入和多输出的网络框架、包括额外附加的分类器
Allows architectures of multiple inputs and multiple outputs, including auxiliary classifiers
(3)拥有多种训练的梯度优化算法。Nesterov momentum, RMSprop and ADAM
(4)继承theano的优点,它拥有theano的所有常见的损失函数以及无需自行计算梯度。
Freely definable cost function and no need to derive gradients due to Theano's symbolic differentiation
(5)通过设置脚本的配置文件,支持CPUs和GPUs之间运行的转换。
Transparent support of CPUs and GPUs due to Theano's expression compiler
设计的目的:
(1)简易性:易于使用和扩展的机器学习库Be easy to use, easy to understand and easy to extend, to facilitate use in research
(2)透明性: Do not hide Theano behind abstractions, directly process and return Theano expressions or Python / numpy data types
(3)模块化: Allow all parts (layers, regularizers, optimizers, ...) to be used independently of Lasagne(4)实用性(Pragmatism): Make common use cases easy, do not overrate uncommon cases
(5)限制Restraint: Do not obstruct(阻塞) users with features they decide not to use
(6)集中性: "Do one thing and do it well"

2、lasagne的安装(ubuntu14.04下)

(1)预备知识prerequisites:theano库theano installation,注意:theano安装的版本取决于lasagne安装的版本。
(2)Stable Lasagne release:lasagne 0.1版本需要匹配最近的theano版本,可执行以下代码来获取相应的版本。
sudo pip install -r https://raw.githubusercontent.com/Lasagne/Lasagne/v0.1/requirements.txt
为了简洁方面,也可以同时安装theano和lasagne。
sudo pip install --upgrade https://github.com/Theano/Theano/archive/master.zip
sudo pip install --upgrade https://github.com/Lasagne/Lasagne/archive/master.zip
developed install:也可以直接克隆lasagne库
git clone https://github.com/Lasagne/Lasagne.git
至此,运行一个小程序检测以下:
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]))

关于GPU配置,后续补上!!!
相关文章
最新文章
热点推荐