# HG changeset patch # User boulanni # Date 1291660387 18000 # Node ID 0673e6af650a5a63cafaf6571ffbc1dbe6cafd88 # Parent 785aeb7a4df28e424d3f5f355cda106a722b3aaa Added regularization terms diff -r 785aeb7a4df2 -r 0673e6af650a doc/formulas.txt --- a/doc/formulas.txt Fri Dec 03 09:09:00 2010 -0500 +++ b/doc/formulas.txt Mon Dec 06 13:33:07 2010 -0500 @@ -20,4 +20,8 @@ .. automodule:: pylearn.formulas.noise :members: +pylearn.formulas.regularization +---------------------- +.. automodule:: pylearn.formulas.regularization + :members: diff -r 785aeb7a4df2 -r 0673e6af650a pylearn/formulas/regularization.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pylearn/formulas/regularization.py Mon Dec 06 13:33:07 2010 -0500 @@ -0,0 +1,49 @@ + +""" +Different symbolic regularization and sparsity functions. +""" + +import theano +import theano.tensor as T + +from tags import tags + +__authors__ = "Frederic Bastien, Nicolas Boulanger-Lewandowski, .." +__copyright__ = "(c) 2010, Universite de Montreal" +__license__ = "3-clause BSD License" +__contact__ = "theano-user " + +@tags('regularization', 'L1') +def l1(x, target = 0, axis_sum = -1, axis_mean = 0): + """ Construct the L1 regularization penalty :math:`\sum|x-target|` + + :type x: Theano variable + :param x: Weights or other variable to regularize + :type target: Theano variable + :param target: Target of x + :type axis_sum: Scalar + :param axis_sum: Axis along which the penalty terms will be summed (e.g. output units) + :type axis_mean: Scalar + :param axis_mean: Axis along which the penalty terms will be averaged (e.g. minibatches) + + :note: no stabilization required + """ + return T.mean(T.sum(T.abs_(x - target), axis_sum), axis_mean) + +@tags('regularization', 'L2') +def l2(x, target = 0, axis_sum = -1, axis_mean = 0): + """ Construct the L2 regularization penalty :math:`\sum(x-target)^2` + + :type x: Theano variable + :param x: Weights or other variable to regularize + :type target: Theano variable + :param target: Target of x + :type axis_sum: Scalar + :param axis_sum: Axis along which the penalty terms will be summed (e.g. output units) + :type axis_mean: Scalar + :param axis_mean: Axis along which the penalty terms will be averaged (e.g. minibatches) + + :note: no stabilization required + """ + return T.mean(T.sum((x - target)**2, axis_sum), axis_mean) +