1.3 Install Lasagne
Lasagne is a lightweight library to build and train neural networks in Theano. It can effectively reduce the complex of programming.
There are some installation steps to be implemented on our machine (64-bit recommend, but our machine is 32-bit Ubuntu 14.04 server). You can see more information at Lasagne website or Lasagne GitHub.
Installation
Before install Lasagne, we have to install packages which Theano also requires, like python, C complier, Numpy/Scipy + BLSA, and Theano. If you don't have them, please follow here first.
Installing the latest Theano might not work with Lasagne. So, type the following command to install a version (0.8.0) that is known to work:
$ pip install Theano==0.8.0
(This way install a known compatible version but might not the latest version of Theano. If you want to upgrade, please try: pip install --upgrade theano
)
- NOTICE:
Steps at Lasagne website for installing Theano might appear the error:
"Could not find a tag or branch '15c90dd3', assuming commit."
. Instead, it will install a older version which can NOT work with Lasagne current version. So do not follow Lasagne website.
After Theano was installed, please download the version of Lasagne which is compatible with Nolearn:
$ pip install Lasagne==0.2.dev1
- If you don't use virtualenv, add
--user
to both commands to install into your home directory instead. Like this:
To upgrade from an earlier installation, add$ pip install --user Theano==0.8.0 $ pip install --user Lasagne==0.2.dev1
--upgrade
.
GPU support
Fetch from http://lasagne.readthedocs.io/en/latest/user/installation.html#gpu-support
Lasagne supports training your networks on a GPU, which may be 10 to 50 times faster than training them on a CPU. This requires an NVIDIA GPU with CUDA support, and some additional software for Theano to use it.
CUDA
Install the latest CUDA Toolkit and possibly the corresponding driver available from NVIDIA: https://developer.nvidia.com/cuda-downloads
After installation, make sure /usr/local/cuda/bin
is in your PATH
, so nvcc --version
works. Also make sure /usr/local/cuda/lib64
is in your LD_LIBRARY_PATH
, so the toolkit libraries can be found.
Theano
If CUDA is set up correctly, the following should print some information on your GPU (the first CUDA-capable GPU in your system if you have multiple ones):
THEANO_FLAGS=device=gpu python -c "import theano; print(theano.sandbox.cuda.device_properties(0))"
To configure Theano to use the GPU by default, create a file .theanorc directly in your home directory, with the following contents:
[global]
floatX = float32
device = gpu
Optionally add allow_gc = False
for some extra performance at the expense of (sometimes substantially) higher GPU memory usage.
If you run into problems, please check Theano’s instructions for Using the GPU.
Usage
There is a python example provided from here.
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]))