import numpy as np
import cPickle
from pylearn2.config import yaml_parse
## example
import yaml_autoencoder
reload(yaml_autoencoder)
ae = yaml_autoencoder.AutoEncoder(nvis = 32 * 32, nhid = 50)
AutoEncoder nvis = 1024 nhid = 50 activation_fn = <ufunc 'tanh'> mean std(weigths) = 0.10
## object instantiation with !obj yaml syntax
## UNFORTUNATELY it doesnt work with ipython notebook
## PUT THE CLASS IN A MODULE
## NO WHITE SPACE ALLOWED between !obj and : and class
## THERE MUST BE A DELIMITER BETWEEN CLASS NAME AND {
## NO ARITHMETIC allowed
## The !obj tag does two things.
## Starting from the top-level package or module, it recursively imports all other sub-packages,
## until it imports the final module. For this to succeed,
## the top-level package should be located in one of the directories listed in the PYTHONPATH environment variable,
## i.e. import <package> should succeed from a vanilla python shell.
## Once the import phase is finished, it then proceeds to instantiate an object of type <object>,
## whose parameters are given in the keyword style syntax.
yaml1 = """!obj:yaml_autoencoder.AutoEncoder {
"nvis": 784,
"nhid": 100,
"iscale": 0.2,
}
"""
print yaml1
model1 = yaml_parse.load(yaml1) ## either yaml string or yaml file stream
!obj:yaml_autoencoder.AutoEncoder { "nvis": 784, "nhid": 100, "iscale": 0.2, } AutoEncoder nvis = 784 nhid = 100 activation_fn = <ufunc 'tanh'> mean std(weigths) = 0.20
## Anchors and References
## Anchors associate a unique identifier to a given attribute or object defined in a yaml file.
## This identifier can then be referenced in another section,
## either to avoid duplicating code or when references to the same object are required.
## Note that references are limited to anchors within the same yaml file.
## More interestingly, when anchors are applied to objects,
## as in &model !obj:yaml_tutorial.autoencoder.AutoEncoder,
## subsequent use of *model will create a pointer to the AutoEncoder object already instantiated by the anchor.
yaml2 = """
!obj:yaml_autoencoder.AutoEncoder {
"nvis": &nvis 100,
"nhid": *nvis,
}
"""
model2 = yaml_parse.load(yaml2)
AutoEncoder nvis = 100 nhid = 100 activation_fn = <ufunc 'tanh'> mean std(weigths) = 0.10
## Dynamic includes (functions) with !import
## The !import tag is similar to !obj,
## in that it also recursively imports packages, subpackages and modules.
## It does not however instantiate any object.
## The end result is thus a pointer to the symbol (package, module or function) specified by the tag.
yaml3 = """!obj:yaml_autoencoder.AutoEncoder {
"nvis": 784,
"nhid": 100,
"iscale": 1.0,
"activation_fn": !import 'pylearn2.expr.nnet.sigmoid_numpy'
}
"""
model3 = yaml_parse.load(yaml3)
model3.save('yaml_model3.pkl')
AutoEncoder nvis = 784 nhid = 100 activation_fn = <function sigmoid_numpy at 0x10d70d050> mean std(weigths) = 1.00
## Loading files through Pickle
## !pkl which allows the end-user to dynamically load the contents of a pickle file.
## This can be especially useful when initializing the parameters of a model from a pickled model instance
## or or to load a dataset stored to disk in pickle format.
yaml4 = """
!obj:yaml_autoencoder.AutoEncoder {
"nvis": 784,
"nhid": 100,
"iscale": 0.1,
"params": !pkl: 'yaml_model3.pkl'
}
"""
model4 = yaml_parse.load(yaml4)
AutoEncoder nvis = 784 nhid = 100 activation_fn = <ufunc 'tanh'> mean std(weigths) = 1.00
## Putting it all together
## In the same way that !import or !pkl can be used to define the “value” of a keyword attribute,
## we can also use the !obj tag to instantiate objects as member attributes of another object.
## Since Pylearn2 experiments are themselves objects of the type pylearn2.train.Train, which combine:
## - a dataset, of type pylearn2.datasets.dataset.Dataset
## - a model, of type pylearn2.models.model.Model
## - a training algorithm, of type pylearn2.training_algorithms.training_algorithm.TrainingAlgorithm