;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; LUSH Lisp Universal Shell
;;;   Copyright (C) 2002 Leon Bottou, Yann Le Cun, AT&T Corp, NECI.
;;; Includes parts of TL3:
;;;   Copyright (C) 1987-1999 Leon Bottou and Neuristique.
;;; Includes selected parts of SN3.2:
;;;   Copyright (C) 1991-2001 AT&T Corp.
;;;
;;; 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, USA
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; This file is derived from SN-2.8
;;;   Copyright (C) 1987-1999 Neuristique s.a.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; $Id: sla.sn,v 1.2 2003/03/18 21:18:30 leonb Exp $
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;   Simple Linear Associator
;;;   Reference: PDP vol 1, pp 62-63
;;;
;;;   (C) Copyright Neuristique, 1989


;;; === Build a NxM linear associator

(de SLA-build-net(n m)
    (nlf-lin 1 1)              ;linear transfert function
    (build-net
     '((input n) (output m))				;two layers
     '((input output)) ) )      ;fully connected 


;;; === Retrieval functions

; The standard library function (test-pattern n)
; needs no modification.


;;; === Learning function: Hebbian rule

(de SLA-Hebb-iteration (patt-number)
    
    ;retrieval
    (present-pattern input-layer patt-number)
    (update-state output)
    
    ;tests and display
    (present-desired desired-layer patt-number)
    (setq good-answer 
    	  (classify patt-number))
    (setq local-error 
    	  (2/ (mean-sqr-dist (state output) (state desired-layer))))
    (disp-basic-iteration patt-number) 
    
    ;learning
    (copy-nfield output-layer n-grad desired-layer n-val)
    (incr age)
    (update-weight) )


(de SLA-Hebb-learn(it)
    (repeat it 
	    (SLA-Hebb-iteration current-pattern)
	    (setq current-pattern 
            	  (next-pattern current-pattern)) ) )


;;; === Learning function: Delta rule, 
;;;         also called Widrow-Hoff rule.
;;; (note)  This is a special case of GBP!

(de SLA-Delta-iteration (patt-number)
    
    ;retrieval
    (present-pattern input-layer patt-number)
    (update-state output)
    
    ;tests and display
    (present-desired desired-layer patt-number)
    (setq good-answer 
    	  (classify patt-number))
    (setq local-error 
    	  (2/ (mean-sqr-dist (state output) (state desired-layer))))
    (disp-basic-iteration patt-number) 
    
    ;learning
    (init-grad output desired-layer)
    (incr age)
    (update-weight) )


(de SLA-Delta-learn(it)
    (repeat it 
	    (SLA-Delta-iteration current-pattern)
	    (setq current-pattern 
            	  (next-pattern current-pattern)) ) )
