1、Lasagne简单介绍
lasagne意味千层饼,是基于theano之上主要用于建立和训练神经网络的深度学习库。Lasagne is a lightweight library to build and train neural networks in Theano.
网站链接:https://github.com/Lasagne/Lasagne、 lasagne官网教程
主要特点有:
(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
至此,给出lasagne大体的框架:(需要自己加载训练数据才能运行出结果)
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 = 0for 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]))
3、配置GPU
GPU的配置需要cuda支持的英伟达显卡,( NVIDIA GPU with CUDA),查看电脑配置的显卡型号,下载对应的所支持的cuda版本
https://developer.nvidia.com/cuda-downloads放在主目录下。
(1)按照cuda官网教程完成安装。
(2)添加环境变量
执行sudo gedit ~/.bashrc 打开.bashrc文件,在末尾写入以下几行:
- export CUDA_HOME=/usr/local/cuda-***/bin #****代表cuda对应的版本,cuda-7.0或cuda-7.5、cuda8.0等
- export LD_LIBRARY_PATH=/usr/local/cuda-7.0/lib64 #如果是32位,去掉末尾的64
执行 sudo source ~/.bashrc #使环境变量生效
检查环境变量:echo $PATH
在home目录下配置.theanorc文件
[global]
floatX = float32
device = gpu
如果cuda配置成功,终端执行python,在python下,import theano 则会显示print下的结果
THEANO_FLAGS=device=gpu python -c "import theano; print(theano.sandbox.cuda.device_properties(0))"