{ "metadata": { "name": "BASIC_yaml" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Yaml tutorial - part of pylearn2 experience" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "import cPickle\n", "from pylearn2.config import yaml_parse" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 5 }, { "cell_type": "raw", "metadata": {}, "source": [ "\n", "## AutoEncoder Class Example\n", "class AutoEncoder(object):\n", " def __init__(self, nvis, nhid, iscale = 0.1,\n", " activation_fn = np.tanh,\n", " params = None):\n", " self.nvis = nvis\n", " self.nhid = nhid\n", " self.activation_fn = activation_fn\n", " if params is None:\n", " self.W = iscale * np.random.randn(nvis, nhid)\n", " self.bias_vis = np.zeros(nvis)\n", " self.bias_hid = np.zeros(nhid)\n", " else:\n", " self.W, self.bias_vis, self.bias_hid = W\n", " print self\n", " def __str__(self):\n", " model_str = '%s\\n' % self.__class__.__name__\n", " model_str += '\\tnvis = %i\\n' % self.nvis\n", " model_str += '\\tnhid = %i\\n' % self.nhid\n", " model_str += '\\tactivation_fn = %s\\n' % str(self.activation_fn)\n", " model_str += '\\tmean std(weigths) = %.2f\\n' % self.W.std(axis=0).mean()\n", " return model_str\n", " def save(self, fname):\n", " with open(fname, 'w') as f:\n", " cPickle.dump([self.W, self.bias_vis, self.bias_hid], f)" ] }, { "cell_type": "code", "collapsed": false, "input": [ "## example\n", "import yaml_autoencoder\n", "reload(yaml_autoencoder)\n", "ae = yaml_autoencoder.AutoEncoder(nvis = 32 * 32, nhid = 50)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "AutoEncoder\n", "\tnvis = 1024\n", "\tnhid = 50\n", "\tactivation_fn = \n", "\tmean std(weigths) = 0.10\n", "\n" ] } ], "prompt_number": 37 }, { "cell_type": "code", "collapsed": false, "input": [ "## object instantiation with !obj yaml syntax\n", "## UNFORTUNATELY it doesnt work with ipython notebook\n", "## PUT THE CLASS IN A MODULE \n", "## NO WHITE SPACE ALLOWED between !obj and : and class\n", "## THERE MUST BE A DELIMITER BETWEEN CLASS NAME AND {\n", "## NO ARITHMETIC allowed\n", "\n", "## The !obj tag does two things. \n", "## Starting from the top-level package or module, it recursively imports all other sub-packages, \n", "## until it imports the final module. For this to succeed, \n", "## the top-level package should be located in one of the directories listed in the PYTHONPATH environment variable, \n", "## i.e. import should succeed from a vanilla python shell. \n", "## Once the import phase is finished, it then proceeds to instantiate an object of type , \n", "## whose parameters are given in the keyword style syntax.\n", "yaml1 = \"\"\"!obj:yaml_autoencoder.AutoEncoder {\n", " \"nvis\": 784,\n", " \"nhid\": 100,\n", " \"iscale\": 0.2,\n", "}\n", "\"\"\"\n", "print yaml1\n", "model1 = yaml_parse.load(yaml1) ## either yaml string or yaml file stream" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "!obj:yaml_autoencoder.AutoEncoder {\n", " \"nvis\": 784,\n", " \"nhid\": 100,\n", " \"iscale\": 0.2,\n", "}\n", "\n", "AutoEncoder\n", "\tnvis = 784\n", "\tnhid = 100\n", "\tactivation_fn = \n", "\tmean std(weigths) = 0.20\n", "\n" ] } ], "prompt_number": 30 }, { "cell_type": "code", "collapsed": false, "input": [ "## Anchors and References\n", "## Anchors associate a unique identifier to a given attribute or object defined in a yaml file. \n", "## This identifier can then be referenced in another section, \n", "## either to avoid duplicating code or when references to the same object are required. \n", "## Note that references are limited to anchors within the same yaml file.\n", "## More interestingly, when anchors are applied to objects, \n", "## as in &model !obj:yaml_tutorial.autoencoder.AutoEncoder, \n", "## subsequent use of *model will create a pointer to the AutoEncoder object already instantiated by the anchor.\n", "\n", "yaml2 = \"\"\"\n", "!obj:yaml_autoencoder.AutoEncoder {\n", " \"nvis\": &nvis 100,\n", " \"nhid\": *nvis,\n", "}\n", "\"\"\"\n", "model2 = yaml_parse.load(yaml2)\n" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "AutoEncoder\n", "\tnvis = 100\n", "\tnhid = 100\n", "\tactivation_fn = \n", "\tmean std(weigths) = 0.10\n", "\n" ] } ], "prompt_number": 31 }, { "cell_type": "code", "collapsed": false, "input": [ "## Dynamic includes (functions) with !import\n", "## The !import tag is similar to !obj, \n", "## in that it also recursively imports packages, subpackages and modules. \n", "## It does not however instantiate any object. \n", "## The end result is thus a pointer to the symbol (package, module or function) specified by the tag.\n", "yaml3 = \"\"\"!obj:yaml_autoencoder.AutoEncoder {\n", " \"nvis\": 784,\n", " \"nhid\": 100,\n", " \"iscale\": 1.0,\n", " \"activation_fn\": !import 'pylearn2.expr.nnet.sigmoid_numpy'\n", "}\n", "\"\"\"\n", "model3 = yaml_parse.load(yaml3)\n", "model3.save('yaml_model3.pkl')" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "AutoEncoder\n", "\tnvis = 784\n", "\tnhid = 100\n", "\tactivation_fn = \n", "\tmean std(weigths) = 1.00\n", "\n" ] } ], "prompt_number": 34 }, { "cell_type": "code", "collapsed": false, "input": [ "## Loading files through Pickle\n", "## !pkl which allows the end-user to dynamically load the contents of a pickle file. \n", "## This can be especially useful when initializing the parameters of a model from a pickled model instance \n", "## or or to load a dataset stored to disk in pickle format.\n", "yaml4 = \"\"\"\n", "!obj:yaml_autoencoder.AutoEncoder {\n", " \"nvis\": 784,\n", " \"nhid\": 100,\n", " \"iscale\": 0.1,\n", " \"params\": !pkl: 'yaml_model3.pkl'\n", "}\n", "\"\"\"\n", "model4 = yaml_parse.load(yaml4)" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "AutoEncoder\n", "\tnvis = 784\n", "\tnhid = 100\n", "\tactivation_fn = \n", "\tmean std(weigths) = 1.00\n", "\n" ] } ], "prompt_number": 40 }, { "cell_type": "code", "collapsed": false, "input": [ "## Putting it all together\n", "## In the same way that !import or !pkl can be used to define the \u201cvalue\u201d of a keyword attribute, \n", "## we can also use the !obj tag to instantiate objects as member attributes of another object.\n", "\n", "## Since Pylearn2 experiments are themselves objects of the type pylearn2.train.Train, which combine:\n", "## - a dataset, of type pylearn2.datasets.dataset.Dataset\n", "## - a model, of type pylearn2.models.model.Model\n", "## - a training algorithm, of type pylearn2.training_algorithms.training_algorithm.TrainingAlgorithm" ], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }