(****************************************************************** [LibML - Machine Learning Library] http://libml.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: Matthieu LAGACHERIE E-mail : matthieu@libml.org Olivier RICORDEAU E-mail : olivier@libml.org ****************************************************************) (** The errorCommonVisitor class @author Matthieu Lagacherie @author Olivier Ricordeau @since 10/08/2003 *) open Nn open ErrorVisitor open MlpNN (** Computes each neurone's output error in a Multi-Layer Perceptron. *) class errorMlpnnVisitor = object inherit [mlpNN] errorVisitor initializer _moduleName <- "MLPNN error computing" (** The transfer function. *) val mutable _transfertFunction = function x -> (1. /. (1. +. exp (-.x))) (** This function is the derivate of the transfer function above. *) val mutable _derivateFunction = function x -> (((1. /. (1. +. exp (-.x)))) *. (1. -. (1. /. (1. +. exp (-.x))))) (** The generic "visit" method *) method visit (network : mlpNN) = 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 in begin (** Compute the error of the output layer *) for i = 0 to (Array.length !error.(network#getLayerNb - 1)) - 1 do !error.(network#getLayerNb - 1).(i) <- derivate(!inputSum.(network#getLayerNb - 1).(i)) *. (!outputActivation.(network#getLayerNb - 1).(i) -. output.(i)) done; (** Compute the error and gradient of the hidden layers. *) for l = network#getLayerNb - 2 downto 0 do for i = 0 to (Array.length !error.(l)) - 1 do !error.(l).(i) <- 0.; for j = 0 to (Array.length !error.(l + 1)) - 1 do !error.(l).(i) <- !error.(l).(i) +. !error.(l + 1).(j) *. !weights.(l).(i).(j); !gradients.(l).(i).(j) <- !error.(l + 1).(j) *. !outputActivation.(l).(i) done; !error.(l).(i) <- !error.(l).(i) *. derivate(!inputSum.(l).(i)) done done end end