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

Commit

Permalink
Adding full coverage for the new codepaths in tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjc committed May 23, 2015
1 parent 8a9701a commit 9485e14
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
21 changes: 16 additions & 5 deletions sknn/ae.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ def __init__(self,
raise NotImplementedError("AutoEncoder layer type `%s` is not implemented." % type)
if cost not in ['msre', 'mbce']:
raise NotImplementedError("Error type '%s' is not implemented." % cost)
if activation not in ['Sigmoid', 'Tanh']:
raise NotImplementedError("Activation type '%s' is not implemented." % activation)

self.activation = activation.lower()
self.activation = activation
self.type = type
self.name = name
self.units = units
Expand Down Expand Up @@ -161,18 +163,27 @@ def transform(self, X):
return self.dca.perform(X)

def transfer(self, nn):
for a, l in zip(self.layers, nn.layers):
assert a.activation == l.type,\
"Mismatch in activation types in target MLP; expected `%s` but found `%s`."\
% (a.activation, l.type)
assert a.units == l.units,\
"Different number of units in target MLP; expected `%i` but found `%i`."\
% (a.units, l.units)

nn.weights = []
for a in self.dca.autoencoders:
nn.weights.append((a.weights.get_value(), a.hidbias.get_value()))

def _create_ae_layer(self, size, layer):
"""Construct an internal pylearn2 layer based on the requested layer type.
"""
activation = layer.activation.lower()
if layer.type == 'autoencoder':
return autoencoder.Autoencoder(size,
layer.units,
layer.activation,
layer.activation,
activation,
activation,
layer.tied_weights,
rng=self.random_state)
if layer.type == 'denoising':
Expand All @@ -181,8 +192,8 @@ def _create_ae_layer(self, size, layer):
return autoencoder.DenoisingAutoencoder(corruptor,
size,
layer.units,
layer.activation,
layer.activation,
activation,
activation,
tied_weights=layer.tied_weights,
rng=self.random_state)

Expand Down
28 changes: 27 additions & 1 deletion sknn/tests/test_ae.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy

from sknn.ae import AutoEncoder as AE, Layer as L

from sknn import mlp

class TestAutoEncoder(unittest.TestCase):

Expand All @@ -17,6 +17,29 @@ def test_FitData(self):
ae = AE(layers=[L("Sigmoid", units=8)], n_iter=1)
ae.fit(X)

def test_FitVerbose(self):
X = numpy.zeros((8,4))
ae = AE(layers=[L("Sigmoid", units=8)], n_iter=1, verbose=1)
ae.fit(X)

def test_TransferSuccess(self):
X = numpy.zeros((8,4))
ae = AE(layers=[L("Tanh", units=4)], n_iter=1)
ae.fit(X)

nn = mlp.MultiLayerPerceptron(
layers=[mlp.Layer("Tanh", units=4)])
ae.transfer(nn)

def test_TransferFailure(self):
X = numpy.zeros((8,4))
ae = AE(layers=[L("Tanh", units=8)], n_iter=1)
ae.fit(X)

nn = mlp.MultiLayerPerceptron(
layers=[mlp.Layer("Tanh", units=4)])
assert_raises(AssertionError, ae.transfer, nn)


class TestParameters(unittest.TestCase):

Expand All @@ -43,3 +66,6 @@ def test_UnknownCostFunction(self):

def test_UnknownType(self):
assert_raises(NotImplementedError, L, "Sigmoid", type="unknown")

def test_UnknownActivation(self):
assert_raises(NotImplementedError, L, "Unknown")

0 comments on commit 9485e14

Please sign in to comment.