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

Commit

Permalink
Fixed more imports for the documentation and upcoming 0.2 self-contai…
Browse files Browse the repository at this point in the history
…ned build.
  • Loading branch information
alexjc committed May 19, 2015
1 parent 0804181 commit d221c57
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 56 deletions.
50 changes: 26 additions & 24 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,9 @@
copyright = u'2015, scikit-neuralnetwork developers (BSD License)'


# -- Overrides for modules ----------------------------------------------------

from mock import Mock as MagicMock

class Mock(MagicMock):
@classmethod
def __getattr__(cls, name):
if name in ('BaseEstimator', 'RegressorMixin', 'ClassifierMixin'):
return object
return Mock()

MOCK_MODULES = ['numpy', 'theano', 'sknn.pywrap2',
'sklearn', 'sklearn.base', 'sklearn.pipeline',
'sklearn.cross_validation', 'sklearn.preprocessing']

for fullname in MOCK_MODULES:
segments = []
for s in fullname.split('.'):
segments.append(s)
mod_name = ".".join(segments)
sys.modules[mod_name] = Mock()


# -- Configuration of documentation -------------------------------------------

sys.path.append(os.path.dirname(os.path.dirname(__file__)).encode('utf-8'))
# sys.path.append(os.path.dirname(os.path.dirname(__file__)).encode('utf-8'))

import sknn
version = sknn.__version__
Expand All @@ -54,6 +31,31 @@ def __getattr__(cls, name):
todo_include_todos = False


# -- Overrides for modules ----------------------------------------------------

from mock import Mock as MagicMock

class Mock(MagicMock):
@classmethod
def __getattr__(cls, name):
if name in ('BaseEstimator', 'TransformerMixin',
'RegressorMixin', 'ClassifierMixin'):
return object
return Mock()

MOCK_MODULES = ['numpy', 'theano', 'sknn.pywrap2', 'sknn.dataset',
'sklearn', 'sklearn.base', 'sklearn.pipeline',
'sklearn.cross_validation', 'sklearn.preprocessing']

for fullname in MOCK_MODULES:
segments = []
for s in fullname.split('.'):
segments.append(s)
mod_name = ".".join(segments)
if mod_name not in sys.modules:
sys.modules[mod_name] = Mock()


# -- Options for HTML output --------------------------------------------------

html_title = 'scikit-neuralnetwork documentation'
Expand Down
3 changes: 2 additions & 1 deletion docs/guide_advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ To setup the library to use your GPU or CPU explicitly in 32-bit or 64-bit mode,
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.


.. _example-pipeline:

Pipeline
--------
Expand All @@ -47,7 +48,7 @@ You can then use the pipeline as you would the neural network, or any other stan
Grid Search
-----------

In scikit-learn, you can use a ``GridSearchCV`` to optimize your neural network's hyper-parameters automatically, both the top-level parameters and the parameters within the layers. For example, assuming you have your MLP constructed as in the :ref:`Regression` example in the local variable called ``nn``, the layers are named automatically so you can refer to them as follows:
In scikit-learn, you can use a ``GridSearchCV`` to optimize your neural network's hyper-parameters automatically, both the top-level parameters and the parameters within the layers. For example, assuming you have your MLP constructed as in the :ref:`example-regression` example in the local variable called ``nn``, the layers are named automatically so you can refer to them as follows:

* ``hidden0``
* ``hidden1``
Expand Down
8 changes: 6 additions & 2 deletions docs/guide_beginners.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
Simple Examples
===============

.. _example-regression:

Regression
----------

Assuming your data is in the form of ``numpy.ndarray`` stored in the variables ``X_train`` and ``y_train`` you can train a :class:`sknn.mlp.Regressor` neural network. The input and output arrays are continuous values in this case, but it's best if you normalize or standardize your inputs to the ``[0..1]`` or ``[-1..1]`` range. (See the :ref:`Pipeline` example below.)
Assuming your data is in the form of ``numpy.ndarray`` stored in the variables ``X_train`` and ``y_train`` you can train a :class:`sknn.mlp.Regressor` neural network. The input and output arrays are continuous values in this case, but it's best if you normalize or standardize your inputs to the ``[0..1]`` or ``[-1..1]`` range. (See the :ref:`example-pipeline` example below.)

.. code:: python
Expand All @@ -29,6 +31,8 @@ Then you can use the trained NN as follows:
This will return a new ``numpy.ndarray`` with the results of the feed-forward simulation of the network and the estimates given the input features.


.. _example-classification:

Classification
--------------

Expand All @@ -46,7 +50,7 @@ If your data in ``numpy.ndarray`` contains integer labels as outputs and you wan
n_iter=25)
nn.fit(X_train, y_train)
It's also a good idea to normalize or standardize your data in this case too, for example using a :ref:`Pipeline` below. The code here will train for 25 iterations. Note that a ``Softmax`` output layer activation type is used here, and it's recommended as a default for classification problems.
It's also a good idea to normalize or standardize your data in this case too, for example using a :ref:`example-pipeline` below. The code here will train for 25 iterations. Note that a ``Softmax`` output layer activation type is used here, and it's recommended as a default for classification problems.

.. code:: python
Expand Down
2 changes: 1 addition & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Module Reference
:maxdepth: 2

module_mlp
madule_ae
module_ae


User Guide
Expand Down
2 changes: 2 additions & 0 deletions docs/pypi.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
:orphan:

scikit-neuralnetwork
====================

Expand Down
47 changes: 23 additions & 24 deletions sknn/dataset.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, unicode_literals, print_function)

from pylearn2.space import Space, CompositeSpace, VectorSpace
from pylearn2.utils import safe_zip
from pylearn2.datasets.dataset import Dataset
from pylearn2.utils.iteration import (FiniteDatasetIterator, resolve_iterator_class)
__all__ = ['FastVectorSpace', 'SparseDesignMatrix']

import functools
import theano

from .pywrap2 import (utils, dataset, space, iteration)

class FastVectorSpace(VectorSpace):

class FastVectorSpace(space.VectorSpace):
"""
More efficient version of the VectorSpace input that doesn't do any validation.
This is used to speed up training times by default; when your data needs debugging,
specify the ``debug=True`` flag in your MLP.
"""

@functools.wraps(VectorSpace._validate)
@functools.wraps(space.VectorSpace._validate)
def _validate(self, is_numeric, batch):
"""
Short-circuit the entire validation if the user has specified it's not necessary.
Expand All @@ -28,7 +27,7 @@ def __eq__(self, other):
"""
Equality should work between Fast and slow VectorSpace instances.
"""
return (type(other) in (FastVectorSpace, VectorSpace)
return (type(other) in (FastVectorSpace, space.VectorSpace)
and self.dim == other.dim
and self.sparse == other.sparse
and self.dtype == other.dtype)
Expand All @@ -37,10 +36,10 @@ def __hash__(self):
"""
Override necessary for Python 3.x.
"""
return hash((type(VectorSpace), self.dim, self.sparse, self.dtype))
return hash((type(space.VectorSpace), self.dim, self.sparse, self.dtype))


class SparseDesignMatrix(Dataset):
class SparseDesignMatrix(dataset.Dataset):
"""
SparseDesignMatrix is a type of Dataset used in training by PyLearn2 that takes
a numpy/scipy sparse matrix and calls ``.todense()`` as the batches are passed
Expand All @@ -58,17 +57,17 @@ def __init__(self, X, y):
self.num_examples = self.data_n_rows
self.fancy = False
self.stochastic = False
X_space = VectorSpace(dim=self.X.shape[1])
X_space = space.VectorSpace(dim=self.X.shape[1])
X_source = 'features'

dim = self.y.shape[-1] if self.y.ndim > 1 else 1
y_space = VectorSpace(dim=dim)
y_space = space.VectorSpace(dim=dim)
y_source = 'targets'

space = CompositeSpace((X_space, y_space))
composite = space.CompositeSpace((X_space, y_space))
source = (X_source, y_source)

self.data_specs = (space, source)
self.data_specs = (composite, source)
self.X_space = X_space

def get_num_examples(self):
Expand All @@ -93,7 +92,7 @@ def get_data(self):
"""
return (self.X, self.y)

@functools.wraps(Dataset.iterator)
@functools.wraps(dataset.Dataset.iterator)
def iterator(self, mode=None, batch_size=None, num_batches=None,
rng=None, data_specs=None, return_tuple=False):
"""
Expand All @@ -105,24 +104,24 @@ def iterator(self, mode=None, batch_size=None, num_batches=None,

# TODO: If there is a view_converter, we have to use it to convert
# the stored data for "features" into one that the iterator can return.
space, source = data_specs or (self.X_space, 'features')
assert isinstance(space, CompositeSpace),\
composite, source = data_specs or (self.X_space, 'features')
assert isinstance(composite, space.CompositeSpace),\
"Unexpected input space for the data."
sub_spaces = space.components
sub_spaces = composite.components
sub_sources = source

conv_fn = lambda x: x.todense().astype(theano.config.floatX)
convert = []
for sp, src in safe_zip(sub_spaces, sub_sources):
for sp, src in utils.safe_zip(sub_spaces, sub_sources):
convert.append(conv_fn if src in ('features', 'targets') else None)

assert mode is not None,\
"Iteration mode not provided for %s" % str(self)
mode = resolve_iterator_class(mode)
mode = iteration.resolve_iterator_class(mode)
subset_iterator = mode(self.X.shape[0], batch_size, num_batches, rng)

return FiniteDatasetIterator(self,
subset_iterator,
data_specs=data_specs,
return_tuple=return_tuple,
convert=convert)
return iteration.FiniteDatasetIterator(self,
subset_iterator,
data_specs=data_specs,
return_tuple=return_tuple,
convert=convert)
8 changes: 4 additions & 4 deletions sknn/pywrap2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@


# This wrapper module can only contain 'leaf' modules.
from pylearn2 import (space, datasets, blocks, corruption)
from pylearn2.datasets import transformer_dataset
from pylearn2 import termination_criteria
from pylearn2 import (space, datasets, blocks, corruption, utils, termination_criteria)
from pylearn2.datasets import (transformer_dataset, dataset)
from pylearn2.utils import (iteration)
from pylearn2.models import (mlp, maxout, autoencoder)
from pylearn2.training_algorithms import (sgd, learning_rule)
from pylearn2.costs import (mlp as mlp_cost, autoencoder as ae_costs, cost)
from pylearn2.costs.mlp import dropout
from pylearn2.costs.mlp import (dropout)

0 comments on commit d221c57

Please sign in to comment.