;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; 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
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; $Id: net-xxx.lsh,v 1.3 2005/04/08 21:37:41 crispisito Exp $
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; a module that contains two idx3-modules connected in sequence
;; makes it easy to define things like 2-layer fully connected nets
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(libload "gblearn2/gb-modules-nn")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#? ** net-xxx
;; generic <idx3-module> with three sub-modules in series.
(defclass net-xxx idx3-module
  ((-obj- (idx3-module)) module-1)
  ((-obj- (idx3-ddstate)) state-1)
  ((-obj- (idx3-module)) module-2)
  ((-obj- (idx3-ddstate)) state-2)
  ((-obj- (idx3-module)) module-3))


#? (new net-xxx <mod1> <thick1> <si1> <sj1> <mod2> <thick2> <si2> <sj2> <mod3>)
;; create new <net-xx> with <mod1> as first module
;; <mod2> as second module, and <mod3> as the third module. 
;; <thick1>, <si1>, sj1>
;; are the initial sizes of the intermediate <idx3-ddstate>
;; which is the output of <mod1> and the input of <mod2>.
;; <thick2>, <si2>, sj2>
;; are the initial sizes of the intermediate <idx3-ddstate>
;; which is the output of <mod2> and the input of <mod3>.
(defmethod net-xxx net-xxx (mod1 thick1 si1 sj1 mod2 thick2 si2 sj2 mod3)
  ((-obj- (idx3-module)) mod1 mod2 mod3)
  (-int- thick1 si1 sj1)
  (-int- thick2 si2 sj2)
  (setq module-1 mod1)
  (setq state-1 (new idx3-ddstate thick1 si1 sj1))
  (setq module-2 mod2)
  (setq state-2 (new idx3-ddstate thick2 si2 sj2))
  (setq module-3 mod3)
  ())

(defmethod net-xxx forget (v p)
  (-float- v p)
  (==> module-1 forget v p)
  (==> module-2 forget v p)
  (==> module-3 forget v p)
  ())

(defmethod net-xxx fprop (in out)
  ((-obj- (idx3-state)) in)
  ((-obj- (idx3-state)) out)
  (==> module-1 fprop in state-1)
  (==> module-2 fprop state-1 state-2)
  (==> module-3 fprop state-2 out)
  ())

(defmethod net-xxx bprop (in out)
  ((-obj- (idx3-dstate)) in)
  ((-obj- (idx3-dstate)) out)
  (==> module-3 bprop state-2 out)
  (==> module-2 bprop state-1 state-2)
  (==> module-1 bprop in state-1)
  ())

(defmethod net-xxx bbprop (in out)
  ((-obj- (idx3-ddstate)) in)
  ((-obj- (idx3-ddstate)) out)
  (==> module-3 bbprop state-2 out)
  (==> module-2 bbprop state-1 state-2)
  (==> module-1 bbprop in state-1)
  ())


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(dhc-make () (net-xxx net-xxx forget fprop bprop bbprop))
