changeset 1114:de153244c8e5

added example file for the formulas.
author Frederic Bastien <nouiz@nouiz.org>
date Tue, 14 Sep 2010 13:33:45 -0400
parents 60ef81fe1825
children 967975f9c574
files pylearn/formulas/noise.py pylearn/formulas/tags.py
diffstat 2 files changed, 110 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pylearn/formulas/noise.py	Tue Sep 14 13:33:45 2010 -0400
@@ -0,0 +1,57 @@
+"""
+
+This script define the different symbolic noise functions
+The noise contract is simple: noise_lvl is a symbolic variable going from 0 to 1
+0: no changement.
+1: max noise.
+
+"""
+import theano
+import theano.tensor as T
+import tags
+s="""
+* A latex mathematical description of the formulas(for picture representation in generated documentation)
+* Tags(for searching):
+   * a list of lower lovel fct used
+   * category(name of the submodule itself)
+* Tell if we did some work to make it more numerical stable. Do theano do the optimization needed?
+* Tell if the grad is numericaly stable? Do theano do the optimization needed?
+* Tell if work on gpu/not/unknow
+* Tell alternate name
+* Tell the domaine, range of the input/output(range should use the english notation of including or excluding)
+"""
+
+@tags('noise','binomial','salt')
+def binomial_noise(theano_rng,inp,noise_lvl):
+    """ This add binomial noise to inp. Only the salt part of pepper and salt.
+
+    :type inp: Theano Variable
+    :param inp: The input that we want to add noise
+    :type noise_lvl: float
+    :param noise_lvl: The % of noise. Between 0(no noise) and 1.
+    """
+    return theano_rng.binomial( size = inp.shape, n = 1, p =  1 - noise_lvl, dtype=theano.config.floatX) * inp
+
+
+@tags('noise','binomial NLP','pepper','salt')
+def pepper_and_salt_noise(theano_rng,inp,noise_lvl):
+    """ This add pepper and salt noise to inp
+    
+    :type inp: Theano Variable
+    :param inp: The input that we want to add noise
+    :type noise_lvl: tuple(float,float)
+    :param noise_lvl: The % of noise for the salt and pepper. Between 0(no noise) and 1.
+    """
+    return theano_rng.binomial( size = inp.shape, n = 1, p =  1 - noise_lvl[0], dtype=theano.config.floatX) * inp \
+                        + (inp==0) * theano_rng.binomial( size = inp.shape, n = 1, p =  noise_lvl[1], dtype=theano.config.floatX)
+
+@tags('noise','gauss','gaussian')
+def gaussian_noise(theano_rng,inp,noise_lvl):
+    """ This add gaussian NLP noise to inp
+
+    :type inp: Theano Variable
+    :param inp: The input that we want to add noise
+    :type noise_lvl: float
+    :param noise_lvl: The standard deviation of the gaussian.
+    """
+    return theano_rng.normal( size = inp.shape, std = noise_lvl, dtype=theano.config.floatX) + inp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pylearn/formulas/tags.py	Tue Sep 14 13:33:45 2010 -0400
@@ -0,0 +1,53 @@
+
+from collections import defaultdict
+
+tags_db = defaultdict(set)
+
+def tags(*_tags):
+    tags = set()
+    def add_tag(tag):
+        if isinstance(tag, (list, tuple)):
+            map(add_tag, tag)
+        elif isinstance(tag, (str, unicode)):
+            for word in tag.split(" "):
+                tags.add(word)
+            tags.add(tag)
+        else:
+            raise TypeError("Tags should be strings or lists/tuples of strings. Got: %s, of type %s" % (tag, type(tag)))
+    map(add_tag, _tags)
+    tags = tuple(sorted(tags))
+    def decorator(function):
+        function.tags = tags
+        function.__doc__ += "\n\nTags: %s" % ", ".join(tags)
+        for tag in tags:
+            tags_db[tag].add(function)
+        return function
+    return decorator
+
+def search(*tags):
+    return reduce(set.__and__, [tags_db[tag] for tag in tags])
+
+
+if __name__ == '__main__':
+    common_tags = ['c', 'd']
+
+    @tags(common_tags, 'a', 'b', 'long tag')
+    def f(a,b):
+        ''' function f returns a+b '''
+        return a+b
+    
+    @tags(common_tags, 'x')
+    def g(a,b):
+        ''' function f returns a-b '''
+        return a-b
+    
+    @tags('c', 'x', 'y', 'z')
+    def h(a,b):
+        ''' function f returns a*b '''
+        return a*b
+
+
+
+    print f.__doc__
+    print [x.__name__ for x in search('c', 'd')]
+    print [x.__name__ for x in search('x')]