| 39 | |
| 40 | # start-snippet-1 |
| 41 | class HiddenLayer(object): |
| 42 | def __init__(self, rng, input, n_in, n_out, W=None, b=None, |
| 43 | activation=T.tanh): |
| 44 | """ |
| 45 | Typical hidden layer of a MLP: units are fully-connected and have |
| 46 | sigmoidal activation function. Weight matrix W is of shape (n_in,n_out) |
| 47 | and the bias vector b is of shape (n_out,). |
| 48 | |
| 49 | NOTE : The nonlinearity used here is tanh |
| 50 | |
| 51 | Hidden unit activation is given by: tanh(dot(input,W) + b) |
| 52 | |
| 53 | :type rng: numpy.random.RandomState |
| 54 | :param rng: a random number generator used to initialize weights |
| 55 | |
| 56 | :type input: theano.tensor.dmatrix |
| 57 | :param input: a symbolic tensor of shape (n_examples, n_in) |
| 58 | |
| 59 | :type n_in: int |
| 60 | :param n_in: dimensionality of input |
| 61 | |
| 62 | :type n_out: int |
| 63 | :param n_out: number of hidden units |
| 64 | |
| 65 | :type activation: theano.Op or function |
| 66 | :param activation: Non linearity to be applied in the hidden |
| 67 | layer |
| 68 | """ |
| 69 | self.input = input |
| 70 | # end-snippet-1 |
| 71 | |
| 72 | # `W` is initialized with `W_values` which is uniformely sampled |
| 73 | # from sqrt(-6./(n_in+n_hidden)) and sqrt(6./(n_in+n_hidden)) |
| 74 | # for tanh activation function |
| 75 | # the output of uniform if converted using asarray to dtype |
| 76 | # theano.config.floatX so that the code is runable on GPU |
| 77 | # Note : optimal initialization of weights is dependent on the |
| 78 | # activation function used (among other things). |
| 79 | # For example, results presented in [Xavier10] suggest that you |
| 80 | # should use 4 times larger initial weights for sigmoid |
| 81 | # compared to tanh |
| 82 | # We have no info for other function, so we use the same as |
| 83 | # tanh. |
| 84 | if W is None: |
| 85 | W_values = numpy.asarray( |
| 86 | rng.uniform( |
| 87 | low=-numpy.sqrt(6. / (n_in + n_out)), |
| 88 | high=numpy.sqrt(6. / (n_in + n_out)), |
| 89 | size=(n_in, n_out) |
| 90 | ), |
| 91 | dtype=theano.config.floatX |
| 92 | ) |
| 93 | if activation == theano.tensor.nnet.sigmoid: |
| 94 | W_values *= 4 |
| 95 | |
| 96 | W = theano.shared(value=W_values, name='W', borrow=True) |
| 97 | |
| 98 | if b is None: |
no outgoing calls
no test coverage detected