Skip to content
This repository was archived by the owner on Jul 10, 2021. It is now read-only.

Commit 84fbaea

Browse files
committed
Merge pull request #58 from aigamedev/pretrain
Support to transfer weights from autoencoder to mlp
2 parents d221c57 + 88e1336 commit 84fbaea

File tree

11 files changed

+232
-80
lines changed

11 files changed

+232
-80
lines changed

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ If you want to use the latest official release, you can do so from PYPI directly
3737

3838
This contains its own packaged version of ``pylearn2`` from the date of the release (and tag) but will use any globally installed version if available.
3939

40-
Pulling From Repository
41-
~~~~~~~~~~~~~~~~~~~~~~~
40+
Pulling Repositories
41+
~~~~~~~~~~~~~~~~~~~~
4242

4343
You'll need to first install some dependencies manually. Unfortunately, ``pylearn2`` isn't yet installable via PyPI and recommends an editable (``pip -e``) installation::
4444

docs/guide_advanced.rst

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,7 @@
11
Advanced Usage
22
==============
33

4-
The examples in this section help you get more out of ``scikit-neuralnetwork`` via integration or configuration of the other libraries it works with, such as Theano or scikit-learn.
5-
6-
7-
GPU Backend
8-
-----------
9-
10-
To setup the library to use your GPU or CPU explicitly in 32-bit or 64-bit mode, you can use the ``backend`` pseudo-module. It's a syntactic helper to setup ``THEANO_FLAGS`` in a Pythonic way, for example:
11-
12-
.. code:: python
13-
14-
# Use the GPU in 32-bit mode, falling back otherwise.
15-
from sknn.backend import gpu32
16-
17-
# Use the CPU in 64-bit mode.
18-
from sknn.backend import cpu64
19-
20-
21-
WARNING: This will only work if your program has not yet imported the ``theano`` module, due to the way the library is designed. If ``THEANO_FLAGS`` are set on the command-line, they are not overwridden.
4+
The examples in this section help you get more out of ``scikit-neuralnetwork``, in particular via its integration with ``scikit-learn``.
225

236

247
.. _example-pipeline:
@@ -45,6 +28,41 @@ Here's how to setup such a pipeline with a multi-layer perceptron as a classifie
4528
You can then use the pipeline as you would the neural network, or any other standard API from scikit-learn.
4629

4730

31+
Unsupervised Pre-Training
32+
-------------------------
33+
34+
If you have large quantities of unlabeled data, you may benefit from pre-training using an auto-encoder style architecture in an unsupervised learning fashion.
35+
36+
.. code:: python
37+
38+
from sknn import ae, mlp
39+
40+
# Initialize auto-encoder for unsupervised learning.
41+
myae = ae.AutoEncoder(
42+
layers=[
43+
ae.Layer("Tanh", units=128),
44+
ae.Layer("Sigmoid", units=64)],
45+
learning_rate=0.002,
46+
n_iter=10)
47+
48+
# Layerwise pre-training using only the input data.
49+
myae.fit(X)
50+
51+
# Initialize the multi-layer perceptron with same base layers.
52+
mymlp = mlp.Regressor(
53+
layers=[
54+
mlp.Layer("Tanh", units=128),
55+
mlp.Layer("Sigmoid", units=64),
56+
mlp.Layer("Linear")])
57+
58+
# Transfer the weights from the auto-encoder.
59+
myae.transfer(mymlp)
60+
# Now perform supervised-learning as usual.
61+
mymlp.fit(X, y)
62+
63+
The downside of this approach is that auto-encoders only support activation fuctions ``Tanh`` and ``Sigmoid`` (currently), which excludes the benefits of more modern activation functions like ``Rectifier``.
64+
65+
4866
Grid Search
4967
-----------
5068

docs/guide_beginners.rst

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -59,38 +59,7 @@ It's also a good idea to normalize or standardize your data in this case too, fo
5959
This code will run the classification with the neural network, and return a list of labels predicted for each of the example inputs.
6060

6161

62-
Verbose Mode
63-
------------
64-
65-
To see the output of the neural network's training, you need to configure two things: first setting up the Python logger (mandatory), and secondly to specify a verbose mode if you want more information during training (optional).
66-
67-
The first step is to configure either the ``sknn`` logger specifically, or do so globally (easier) as follows:
68-
69-
.. code:: python
70-
71-
import sys
72-
import logging
73-
74-
logging.basicConfig(
75-
format="%(message)s",
76-
level=logging.DEBUG,
77-
stream=sys.stdout)
78-
79-
Then you can optionally create your neural networks using an additional ``verbose`` parameter to show the output during training:
80-
81-
.. code:: python
82-
83-
from sknn.mlp import Regressor, Layer
84-
85-
nn = Regressor(
86-
layers=[Layer("Linear")],
87-
n_iter=20,
88-
verbose=1,
89-
valid_size=0.25)
90-
nn.fit(X, y)
91-
92-
This code will output a table containing validation scores at each of the twenty epochs. The ``valid_size`` parameter is a ratio of the data to be used internally for validation; in short, the ``fit()`` function is automatically splitting the data into ``X_train`` and ``y_train`` as well as ``X_valid`` and ``y_valid``.
93-
62+
.. _example-convolution:
9463

9564
Convolution
9665
-----------

docs/guide_start.rst renamed to docs/guide_installation.rst

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
Installation
22
============
33

4+
You have multiple options to get up and running, though using ``pip`` is by far the easiest and most reliable.
5+
6+
47
Downloading Package
58
-------------------
69

7-
To download and setup the last officially released package, you can do so from PYPI directly::
10+
**Recommended.** To download and setup the last officially released package, you can do so from PYPI directly::
811

912
> pip install scikit-neuralnetwork
1013

@@ -13,6 +16,23 @@ This contains its own packaged version of ``pylearn2`` from the date of the rele
1316
If you want to install the very latest from source, please visit the `Project Page <http://github.com/aigamedev/scikit-neuralnetwork>`_ on GitHub for details.
1417

1518

19+
Pulling Repositories
20+
--------------------
21+
22+
**Optional.** To setup a developer version of the project, you'll need to first install some dependencies manually. Unfortunately, ``pylearn2`` isn't yet installable via PyPI and recommends an editable (``pip -e``) installation::
23+
24+
> pip install numpy scipy theano
25+
> pip install -e git+https://github.com/lisa-lab/pylearn2.git#egg=Package
26+
27+
Once that's done, you can grab this repository and install from ``setup.py`` in the exact same way::
28+
29+
> git clone https://github.com/aigamedev/scikit-neuralnetwork.git
30+
> cd scikit-neuralnetwork
31+
> python setup.py develop
32+
33+
This will make the ``sknn`` package globally available within Python as a reference to the current directory.
34+
35+
1636
Running Tests
1737
-------------
1838

@@ -26,4 +46,3 @@ Use the additional command-line parameters in the test runner ``--processes=8``
2646
.. image:: console_tests.png
2747

2848
We strive to maintain 100% test coverage for all code-paths, to ensure that rapid changes in the underlying ``pylearn2`` library are caught automatically.
29-

docs/guide_intermediate.rst

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
Misc. Additions
2+
===============
3+
4+
Verbose Mode
5+
------------
6+
7+
To see the output of the neural network's training, you need to configure two things: first setting up the Python logger (mandatory), and secondly to specify a verbose mode if you want more information during training (optional).
8+
9+
The first step is to configure either the ``sknn`` logger specifically, or do so globally (easier) as follows:
10+
11+
.. code:: python
12+
13+
import sys
14+
import logging
15+
16+
logging.basicConfig(
17+
format="%(message)s",
18+
level=logging.DEBUG,
19+
stream=sys.stdout)
20+
21+
Then you can optionally create your neural networks using an additional ``verbose`` parameter to show the output during training:
22+
23+
.. code:: python
24+
25+
from sknn.mlp import Regressor, Layer
26+
27+
nn = Regressor(
28+
layers=[Layer("Linear")],
29+
n_iter=20,
30+
verbose=True,
31+
valid_size=0.25)
32+
nn.fit(X, y)
33+
34+
This code will output a table containing validation scores at each of the twenty epochs. The ``valid_size`` parameter is a ratio of the data to be used internally for validation; in short, the ``fit()`` function is automatically splitting the data into ``X_train`` and ``y_train`` as well as ``X_valid`` and ``y_valid``.
35+
36+
37+
Saving & Loading
38+
----------------
39+
40+
To save a trained neural network to disk, you can do the following after having initialized your multi-layer perceptron as the variable ``nn`` and trained it:
41+
42+
.. code:: python
43+
44+
import pickle
45+
pickle.dump(nn, open('nn.pkl', 'wb'))
46+
47+
After this, the file ``nn.pkl`` will be available in the current working directory — which you can reload at any time:
48+
49+
import pickle
50+
nn == pickle.load(open('nn.pkl', 'rb'))
51+
52+
In this case, you can use the reloaded multi-layer perceptron as if it had just been trained. This will also work on different machines, whether CPU or GPU.
53+
54+
NOTE: You can serialize complex pipelines (for example from this section :ref:`example-pipeline`) using this exact same approach.
55+
56+
57+
GPU Backend
58+
-----------
59+
60+
To setup the library to use your GPU or CPU explicitly in 32-bit or 64-bit mode, you can use the ``backend`` pseudo-module. It's a syntactic helper to setup ``THEANO_FLAGS`` in a Pythonic way, for example:
61+
62+
.. code:: python
63+
64+
# Use the GPU in 32-bit mode, falling back otherwise.
65+
from sknn.backend import gpu32
66+
67+
# Use the CPU in 64-bit mode.
68+
from sknn.backend import cpu64
69+
70+
WARNING: This will only work if your program has not yet imported the ``theano`` module, due to the way the library is designed. If ``THEANO_FLAGS`` are set on the command-line, they are not overwridden.

docs/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ User Guide
2626
.. toctree::
2727
:maxdepth: 2
2828

29-
guide_start
29+
guide_installation
3030
guide_beginners
31+
guide_intermediate
3132
guide_advanced
3233

3334

examples/bench_cifar10.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import (absolute_import, unicode_literals, print_function)
33

4+
import sys
45
import pickle
6+
import logging
57
import numpy as np
68

9+
logging.basicConfig(format="%(message)s", level=logging.DEBUG, stream=sys.stdout)
10+
11+
PRETRAIN = False
12+
13+
714
def load(name):
815
print("\t"+name)
916
try:
1017
with open(name, 'rb') as f:
11-
return pickle.load(f, encoding='latin1')
18+
return pickle.load(f) # , encoding='latin1')
1219
except IOError:
1320
import gzip
1421
with gzip.open(name+'.gz', 'rb') as f:
15-
return pickle.load(f, encoding='latin1')
22+
return pickle.load(f) # , encoding='latin1')
1623

1724
print("Loading...")
1825
dataset1 = load('data_batch_1')
1926
dataset2 = load('data_batch_2')
2027
dataset3 = load('data_batch_3')
2128
print("")
2229

23-
data_train = np.vstack([dataset1['data'], dataset2['data']])
24-
labels_train = np.hstack([dataset1['labels'], dataset2['labels']])
30+
data_train = np.vstack([dataset1['data']]) #, dataset2['data']])
31+
labels_train = np.hstack([dataset1['labels']]) #, dataset2['labels']])
2532

2633
data_train = data_train.astype('float') / 255.
2734
labels_train = labels_train
@@ -36,17 +43,30 @@ def load(name):
3643
logging.basicConfig(format="%(message)s", level=logging.DEBUG, stream=sys.stdout)
3744

3845
from sknn import mlp
39-
net = mlp.Classifier(
40-
layers=[
41-
mlp.Layer("Rectifier", units=n_feat*2/3),
42-
mlp.Layer("Rectifier", units=n_feat*1/3),
43-
mlp.Layer("Softmax", units=n_targets)],
44-
n_iter=50,
45-
n_stable=10,
46-
learning_rate=0.001,
47-
valid_size=0.1,
48-
verbose=1)
49-
net.fit(data_train, labels_train)
46+
nn = mlp.Classifier(
47+
layers=[
48+
mlp.Layer("Sigmoid", units=128),
49+
mlp.Layer("Sigmoid", units=128),
50+
mlp.Layer("Softmax", units=n_targets)],
51+
n_iter=4,
52+
n_stable=4,
53+
learning_rate=0.001,
54+
valid_size=0.5,
55+
verbose=1)
56+
57+
if PRETRAIN:
58+
from sknn import ae
59+
ae = ae.AutoEncoder(
60+
layers=[
61+
ae.Layer("Sigmoid", units=128),
62+
ae.Layer("Sigmoid", units=128)],
63+
learning_rate=0.002,
64+
n_iter=10,
65+
verbose=1)
66+
ae.fit(data_train)
67+
ae.transfer(nn)
68+
69+
nn.fit(data_train, labels_train)
5070

5171
from sklearn.metrics import classification_report
5272
from sklearn.metrics import confusion_matrix

0 commit comments

Comments
 (0)