Theano深度学习框架之Lasagne安装及入门
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))"
?
總結
以上是生活随笔為你收集整理的Theano深度学习框架之Lasagne安装及入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2021CCPC华为云挑战赛:HDU 7
- 下一篇: 三款Linux下最好的看图工具GPicV