Mercurial > pylearn
annotate 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 |
rev | line source |
---|---|
834 | 1 """ Provide the "normal" sigmoidal layers for making multi-layer perceptrons / neural nets |
2 | |
3 """ | |
4 import logging | |
5 import numpy | |
6 | |
7 import theano | |
8 from theano import tensor | |
893
912be602c3ac
fix import for the move of shared var outside of the sandbox.
Frederic Bastien <nouiz@nouiz.org>
parents:
891
diff
changeset
|
9 from theano.compile import shared, pfunc |
891
fc9779dcd710
some backport to python 2.4
Frederic Bastien <nouiz@nouiz.org>
parents:
834
diff
changeset
|
10 from pylearn.shared.layers.util import update_locals, add_logging |
fc9779dcd710
some backport to python 2.4
Frederic Bastien <nouiz@nouiz.org>
parents:
834
diff
changeset
|
11 from pylearn.shared.layers.squash import squash |
834 | 12 |
13 | |
14 class SigmoidalLayer(object): | |
15 def __init__(self, input, w, b, squash_fn, params): | |
16 """ | |
17 :param input: a symbolic tensor of shape (n_examples, n_in) | |
18 :param w: a symbolic weight matrix of shape (n_in, n_out) | |
19 :param b: symbolic bias terms of shape (n_out,) | |
20 :param squash: an squashing function | |
21 """ | |
22 output = squash_fn(tensor.dot(input, w) + b) | |
1405
f9e4d71aa353
Add L1 and L2² costs to sigmoidal layer
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
893
diff
changeset
|
23 l1 = abs(w).sum() |
f9e4d71aa353
Add L1 and L2² costs to sigmoidal layer
Pascal Lamblin <lamblinp@iro.umontreal.ca>
parents:
893
diff
changeset
|
24 l2_sqr = (w**2).sum() |
834 | 25 update_locals(self, locals()) |
26 | |
27 @classmethod | |
28 def new(cls, rng, input, n_in, n_out, squash_fn=tensor.tanh, dtype=None): | |
29 """Allocate a SigmoidLayer with weights to transform inputs with n_in dimensions, | |
30 to outputs of n_out dimensions. | |
31 | |
32 Weights are initialized randomly using rng. | |
33 | |
34 :param squash_fn: an op constructor function, or a string that has been registed as a | |
35 `squashing_function`. | |
36 | |
37 :param dtype: the numerical type to use for the parameters (i.e. 'float32', 'float64') | |
38 | |
39 """ | |
40 if dtype is None: | |
41 dtype = input.dtype | |
42 cls._debug('allocating weights and biases', n_in, n_out, dtype) | |
43 w = shared( | |
44 numpy.asarray( | |
45 rng.uniform(low=-2/numpy.sqrt(n_in), high=2/numpy.sqrt(n_in), | |
46 size=(n_in, n_out)), dtype=dtype)) | |
47 b = shared(numpy.asarray(numpy.zeros(n_out), dtype=dtype)) | |
48 return cls(input, w, b, squash(squash_fn), [w,b]) | |
49 | |
50 add_logging(SigmoidalLayer) |