diff --git a/sknn/ae.py b/sknn/ae.py index 83ecdbd..b695d8d 100644 --- a/sknn/ae.py +++ b/sknn/ae.py @@ -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 @@ -161,6 +163,14 @@ 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())) @@ -168,11 +178,12 @@ def transfer(self, nn): 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': @@ -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) diff --git a/sknn/tests/test_ae.py b/sknn/tests/test_ae.py index ebab1f4..39f495b 100644 --- a/sknn/tests/test_ae.py +++ b/sknn/tests/test_ae.py @@ -4,7 +4,7 @@ import numpy from sknn.ae import AutoEncoder as AE, Layer as L - +from sknn import mlp class TestAutoEncoder(unittest.TestCase): @@ -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): @@ -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")