(****************************************************************** [LibNN - Neural Networks Library] http://libnn.org Copyright (C) 2002 - 2003 LAGACHERIE Matthieu RICORDEAU Olivier This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. SPECIAL NOTE (the beerware clause): This software is free software. However, it also falls under the beerware special category. That is, if you find this software useful, or use it every day, or want to grant us for our modest contribution to the free software community, feel free to send us a beer from one of your local brewery. Our preference goes to Belgium abbey beers and irish stout (Guiness for strength!), but we like to try new stuffs. Authors: LAGACHERIE Matthieu Paper mail : 7 rue Delescluzes 94280 LE KREMLIN BICETRE, FRANCE E-mail : matthieu@libnn.org RICORDEAU Olivier Paper mail : 69 avenue d'Italie 75013 PARIS, FRANCE E-mail : olivier@libnn.org *****************************************************************) (** The errorTdnnVisitor class Computes each neurone's output error. @author Matthieu Lagacherie @author Olivier Ricordeau @since 10/08/2003 *) open Nn open ErrorVisitor open DefaultVisitor open TdNN class errorTdnnVisitor = object inherit [TdNN] errorVisitor val mutable _transfertFunction = function x -> (1. /. (1. +. exp (-.x))) val mutable _derivateFunction = function x -> (((1. /. (1. +. exp (-.x)))) *. (1. -. (1. /. (1. +. exp (-.x))))) method visit (network : tdNN) = let error = network#getError and gradients = network#getGradients and output = network#getOutputLearnVector and outputActivation = network#getOutputActivation and inputSum = network#getInputSum and weights = network#getWeights and derivate = _derivateFunction and stepDelay = ref 0 in begin (** Compute the error of the output layer Here the output vector is mapped in a first time on the feature direction and in a second time in the time direction. *) for i = 0 to (Array.length !error.(network#getLayerNb - 1)) - 1 do for j = 0 to (Array.length !error.(network#getLayerNb - 1).(0)) - 1 do !error.(network#getLayerNb - 1).(i).(j) <- derivate(!inputSum.(network#getLayerNb - 1).(i).(j)) *. (!outputActivation.(network#getLayerNb - 1).(i).(j) -. output.(i + j)) done done; (** Compute the error and gradient of the hidden layers. l the layer i neuron of the layer l + 1 in the feature direction j neuron of the layer l + 1 in the time direction k neuron of the layer l in the feature direction m neuron of the layer l in the time direction stepDelay used to keep the delay concept *) for l = network#getLayerNb - 2 downto 0 do for i = 0 to (Array.length !error.(l)) - 1 do for j = 0 to (Array.length !error.(l).(i)) - 1 do !error.(l).(i).(j) <- 0.; for k = 0 to (Array.length !error.(l + 1)) - 1 do for m = 0 to (Array.length !error.(l + 1).(k)) - 1 do !error.(l).(i).(j) <- !error.(l).(i).(j) +. !error.(l + 1).(k).(m) *. !weights.(l).(i).(j); !gradients.(l).(i).(j) <- !error.(l + 1).(j) *. !outputActivation.(l).(i) done done; !error.(l).(i).(j) <- !error.(l).(i).(j) *. derivate(!inputSum.(l).(i).(j)) done done done end end