Reputation: 13614
I trained and saved two layer stacked CAE model by Pylearn2. I would like to load these models and transform a novel dataset. How should I do it?
This is my model:
l1 = serial.load('CAE_l1.pkl')
l2 = serial.load('CAE_l2.pkl')
print l1
<pylearn2.models.autoencoder.ContractiveAutoencoder object at 0x7f3bb6d482d0>
I also tried something like this but it does not work.
data = T.matrix('data')
transform = theano.function([data], l1(data))
This is what I do lately but not sure about its correctness:
data = T.matrix('data')
transform = theano.function([data], l1.encode(data))
X_1 = transform(X.astype(float32))
Upvotes: 0
Views: 407
Reputation: 470
Check the pylearn2.scripts.autoencoder
directory for a configuration file that shows how to use stacked, pretrained autoencoders. You load the pretrained models as a transformer object on the dataset on it's way into the next stage of the model.
If you don't want to use yaml files, you should be able to string the model functions together using the correct methods (untested as I'm writing off the top of my head):
inputs = T.vector()
enc = l1.encode(inputs)
output = l2.encode(enc)
f= theano.function([inputs], output)
real_transformation = f(real_data)
And to go back, you can do the same with the ae_layer.decode()
method.
If these are part of the layers of and MLP
, you can call the .fprop()
method on the MLP
to do an upward pass through all the layers.
Upvotes: 2