view pylearn/shared/layers/sigmoidal_layer.py @ 1405:f9e4d71aa353

Add L1 and L2² costs to sigmoidal layer
author Pascal Lamblin <lamblinp@iro.umontreal.ca>
date Wed, 26 Jan 2011 16:55:44 -0500
parents 912be602c3ac
children
line wrap: on
line source

""" Provide the "normal" sigmoidal layers for making multi-layer perceptrons / neural nets

"""
import logging
import numpy

import theano
from theano import tensor
from theano.compile import shared, pfunc
from pylearn.shared.layers.util import update_locals, add_logging
from pylearn.shared.layers.squash import squash


class SigmoidalLayer(object):
    def __init__(self, input, w, b, squash_fn, params):
        """
        :param input: a symbolic tensor of shape (n_examples, n_in)
        :param w: a symbolic weight matrix of shape (n_in, n_out)
        :param b: symbolic bias terms of shape (n_out,)
        :param squash: an squashing function
        """
        output = squash_fn(tensor.dot(input, w) + b)
        l1 = abs(w).sum()
        l2_sqr = (w**2).sum()
        update_locals(self, locals())

    @classmethod
    def new(cls, rng, input, n_in, n_out, squash_fn=tensor.tanh, dtype=None):
        """Allocate a SigmoidLayer with weights to transform inputs with n_in dimensions, 
        to outputs of n_out dimensions.  

        Weights are initialized randomly using rng.

        :param squash_fn: an op constructor function, or a string that has been registed as a
        `squashing_function`.

        :param dtype: the numerical type to use for the parameters (i.e. 'float32', 'float64')

        """
        if dtype is None:
            dtype = input.dtype
        cls._debug('allocating weights and biases', n_in, n_out, dtype)
        w = shared(
                numpy.asarray(
                    rng.uniform(low=-2/numpy.sqrt(n_in), high=2/numpy.sqrt(n_in),
                    size=(n_in, n_out)), dtype=dtype))
        b = shared(numpy.asarray(numpy.zeros(n_out), dtype=dtype))
        return cls(input, w, b, squash(squash_fn), [w,b])

add_logging(SigmoidalLayer)