Theano深度学习框架之Lasagne安装及入门
1、Lasagne簡(jiǎn)單介紹
? ? ? ?lasagne意味千層餅,是基于theano之上主要用于建立和訓(xùn)練神經(jīng)網(wǎng)絡(luò)的深度學(xué)習(xí)庫(kù)。Lasagne is a lightweight library to build and train neural networks in Theano.?
網(wǎng)站鏈接:https://github.com/Lasagne/Lasagne、 ? ??lasagne官網(wǎng)教程
主要特點(diǎn)有:
(1)支持所有的前饋神經(jīng)網(wǎng)絡(luò)的模型,包括CNNs、RNN+LSTM以及任何擴(kuò)展的卷積神經(jīng)網(wǎng)絡(luò)。
? ? ? Supports feed-forward networks such as Convolutional Neural Networks (CNNs), recurrent networks including Long Short-Term Memory (LSTM), and any combination thereof?
? (2)支持多輸入和多輸出的網(wǎng)絡(luò)框架、包括額外附加的分類(lèi)器
? ? ? Allows architectures of multiple inputs and multiple outputs, including auxiliary classifiers?
? ? (3)擁有多種訓(xùn)練的梯度優(yōu)化算法。Nesterov momentum, RMSprop and ADAM?
(4)?繼承theano的優(yōu)點(diǎn),它擁有theano的所有常見(jiàn)的損失函數(shù)以及無(wú)需自行計(jì)算梯度。
? ? ??Freely definable cost function and no need to derive gradients due to Theano's symbolic differentiation
(5)通過(guò)設(shè)置腳本的配置文件,支持CPUs和GPUs之間運(yùn)行的轉(zhuǎn)換。
? ? ?Transparent support of CPUs and GPUs due to Theano's expression compiler
設(shè)計(jì)的目的:
?(1)簡(jiǎn)易性:易于使用和擴(kuò)展的機(jī)器學(xué)習(xí)庫(kù)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)實(shí)用性(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)預(yù)備知識(shí)?prerequisites:theano庫(kù)?theano installation,注意:theano安裝的版本取決于lasagne安裝的版本。
(2)Stable Lasagne release:lasagne 0.1版本需要匹配最近的theano版本,可執(zhí)行以下代碼來(lái)獲取相應(yīng)的版本。
sudo pip install -r https://raw.githubusercontent.com/Lasagne/Lasagne/v0.1/requirements.txt
? ? ? ? ?為了簡(jiǎn)潔方面,也可以同時(shí)安裝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庫(kù)
git clone https://github.com/Lasagne/Lasagne.git
至此,給出lasagne大體的框架:(需要自己加載訓(xùn)練數(shù)據(jù)才能運(yùn)行出結(jié)果)
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支持的英偉達(dá)顯卡,(? NVIDIA GPU with CUDA),查看電腦配置的顯卡型號(hào),下載對(duì)應(yīng)的所支持的cuda版本
https://developer.nvidia.com/cuda-downloads放在主目錄下。
(1)按照cuda官網(wǎng)教程完成安裝。
(2)添加環(huán)境變量
執(zhí)行sudo gedit ?~/.bashrc 打開(kāi).bashrc文件,在末尾寫(xiě)入以下幾行:? ?
- export CUDA_HOME=/usr/local/cuda-***/bin???#****代表cuda對(duì)應(yīng)的版本,cuda-7.0或cuda-7.5、cuda8.0等
- export?LD_LIBRARY_PATH=/usr/local/cuda-7.0/lib64? #如果是32位,去掉末尾的64
執(zhí)行 sudo source ~/.bashrc ? ?#使環(huán)境變量生效
檢查環(huán)境變量:echo $PATH
在home目錄下配置.theanorc文件
[global]
floatX = float32
device = gpu
如果cuda配置成功,終端執(zhí)行python,在python下,import theano 則會(huì)顯示print下的結(jié)果
THEANO_FLAGS=device=gpu python -c "import theano; print(theano.sandbox.cuda.device_properties(0))"
?
總結(jié)
以上是生活随笔為你收集整理的Theano深度学习框架之Lasagne安装及入门的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 2021CCPC华为云挑战赛:HDU 7
- 下一篇: Codeforces Round #73