Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def forward(self, x, y, n):
- """
- x: the input vector representig the input signal
- y: the target vector, the true output
- n: the lenght of vectors ``x`` and ``y``
- """
- o = self.propagateSignal(x)
- loss = self.objective.apply(y, o)/n
- penalty = self._lambda*np.sum([np.sum(np.linalg.norm(layer.W, 2)**2) for layer in self.nnet])
- return (loss + penalty)
- def propagateSignal(self, x):
- _h = x
- for layer in self.nnet:
- _h = layer.apply(_h)
- return _h
- def backward(self, y, n):
- # after the forward computation, compute the gradient on the output layer
- o = self.nnet[-1].h # last layer's output
- g = self.objective.gradient(y, o)/n
- for layer, grad in zip(reversed(self.nnet), reversed(self.gradients)): # starting backward we reconstruct error with respect to each weight
- # convert the gradient on the layer's output into a gradient into
- # the pre-nonlinearity activation (element-wise (``hadamard``) multiplication if f is elementwise)
- g = g*layer.activation.dh(layer.a)
- # compute gradients on weights and biases (including the regularization term,
- # where needed):
- grad.nablab += g
- grad.nablaW += np.outer(g, layer._h) + 2*self._lambda*layer.W
- # propagate the gradients w.r.t. the next lower-level hidden layer's activations
- g = np.dot(layer.W.T, g)
- def descent(self):
- for layer, grad in zip(self.nnet, self.gradients):
- nablaW_new = self.alpha*grad.nablaW_old + self.eta*(grad.nablaW)
- nablab_new = self.alpha*grad.nablab_old + self.eta*(grad.nablab)
- layer.W = layer.W - nablaW_new
- layer.b = layer.b - nablab_new
- grad.nablaW_old, grad.nablab_old = nablaW_new, nablab_new
- # we clear-up the sum of gradients with respect the last seen example in the case of online mode or
- # in the last epoch (complete pass on dataset) for batch-mode
- for grad in self.gradients:
- grad.cleargrad()
- def propagateSignal(self, x):
- _h = x
- for layer in self.nnet:
- _h = layer.apply(_h)
- return _h
- def forward(self, x, y, n):
- """
- x: the input vector representig the input signal
- y: the target vector, the true output
- n: the lenght of vectors ``x`` and ``y``
- """
- o = self.propagateSignal(x)
- loss = self.objective.apply(y, o)/n
- penalty = self._lambda*np.sum([np.sum(np.linalg.norm(layer.W, 2)**2 + np.linalg.norm(layer.b))**2 for layer in self.nnet])
- return (loss + penalty)
- o = self.nnet[-1].h # last layer's output
- g = self.objective.gradient(y, o)/n
- g = g*layer.activation.dh(layer.a)
- grad.nablab += g + 2*self._lambda*layer.b
- grad.nablaW += np.outer(g, layer._h) + 2*self._lambda*layer.W
Add Comment
Please, Sign In to add comment