;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; 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-cscscscf.lsh,v 1.2 2002/11/26 20:22:59 profshadoko Exp $
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; standard LeNet6(?) class
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(libload "gb-modules-nn")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#? ** net-cscscscf
;;.AUTHOR Yann LeCun
;; A deep convolutional net, with one convo/subsamp
;; set than LeNet5.
(defclass net-cscscscf idx3-module
  ((-obj- (c-layer)) c0-module)
  ((-obj- (idx3-ddstate)) c0-state)
  ((-obj- (s-layer)) s0-module)
  ((-obj- (idx3-ddstate)) s0-state)
  ((-obj- (c-layer)) c1-module )
  ((-obj- (idx3-ddstate)) c1-state)
  ((-obj- (s-layer)) s1-module)
  ((-obj- (idx3-ddstate)) s1-state)
  ((-obj- (c-layer)) c2-module )
  ((-obj- (idx3-ddstate)) c2-state)
  ((-obj- (s-layer)) s2-module)
  ((-obj- (idx3-ddstate)) s2-state)
  ((-obj- (c-layer)) c3-module)
  ((-obj- (idx3-ddstate)) c3-state)
  ((-obj- (f-layer)) f-module))

#? (new net-cscscscf <ini> <inj> <ki0> <kj0> <tbl0> <si0> <sj0> <ki1> <kj1> <tbl1> <si1> <sj1> <ki2> <kj2> <tbl2> <si2> <sj2> <ki3> <kj3> <tbl3> <outthick> <prm>)
;; makes a new net-cscscf module.
;; <ini> <inj>: expected max size of input for preallocation of internal states
;; <ki0> <kj0>: kernel size for first convolutional layer
;; <tbl0>: table of connections between input and feature maps for first layer
;; <si0> <sj0>: subsampling for first layer
;; <ki1> <kj1> <tbl1> <si1> <sj1>: same for next 2 layers...
;; <ki2> <kj2> <tbl2> <si2> <sj2>: ...and the next 2
;; <ki3> <kj3> <tbl3>: same yet again for last convolution layer
;; <outthick>: number of outputs.
;; <prm> an idx1-ddparam in which the parameters will be allocated.
(defmethod net-cscscscf net-cscscscf 
  (
   ini inj
   ki0 kj0 tbl0 si0 sj0
   ki1 kj1 tbl1 si1 sj1
   ki2 kj2 tbl2 si2 sj2
   ki3 kj3 tbl3
   outthick
   prm)
  ((-obj- (idx1-ddparam)) prm)
  ((-int-)  ini inj
	    ki0 kj0 si0 sj0
	    ki1 kj1 si1 sj1
            ki2 kj2 si2 sj2
	    ki3 kj3 outthick)
  ((-idx2- (-int-)) tbl0 tbl1 tbl2 tbl3)
  (let* ((thick0 (1+ (idx-i1max (select tbl0 1 1))))
	 (c0-sizi (1+ (- ini ki0)))
	 (c0-sizj (1+ (- inj kj0)))
	 (c0-squash (new idx3-squasher))
	 (s0-sizi (/ c0-sizi si0))
	 (s0-sizj (/ c0-sizj sj0))
	 (s0-squash (new idx3-squasher))
	 (thick1 (1+ (idx-i1max (select tbl1 1 1))))
	 (c1-sizi (1+ (- s0-sizi ki1)))
	 (c1-sizj (1+ (- s0-sizj kj1)))
	 (c1-squash (new idx3-squasher))
	 (s1-sizi (/ c1-sizi si1))
	 (s1-sizj (/ c1-sizj sj1))
	 (s1-squash (new idx3-squasher))
         (thick2 (1+ (idx-i1max (select tbl2 1 1))))
	 (c2-sizi (1+ (- s1-sizi ki2)))
	 (c2-sizj (1+ (- s1-sizj kj2)))
	 (c2-squash (new idx3-squasher))
	 (s2-sizi (/ c2-sizi si2))
	 (s2-sizj (/ c2-sizj sj2))
	 (s2-squash (new idx3-squasher))
	 (thick3 (1+ (idx-i1max (select tbl3 1 1))))
	 (c3-sizi (1+ (- s2-sizi ki3)))
	 (c3-sizj (1+ (- s2-sizj kj3)))
	 (c3-squash (new idx3-squasher))
	 (f-squash  (new idx3-squasher)))
    (setq c0-module 
	  (new c-layer ki0 kj0 1 1 tbl0 thick0 c0-sizi c0-sizj c0-squash prm))
    (setq c0-state (new idx3-ddstate thick0 c0-sizi c0-sizj))
    (setq s0-module 
	  (new s-layer si0 sj0 thick0 s0-sizi s0-sizj s0-squash prm))
    (setq s0-state (new idx3-ddstate thick0 s0-sizi s0-sizj))
    (setq c1-module 
	  (new c-layer ki1 kj1 1 1 tbl1 thick1 
	       c1-sizi c1-sizj c1-squash prm))
    (setq c1-state (new idx3-ddstate thick1 c1-sizi c1-sizj))
    (setq s1-module 
	  (new s-layer si1 sj1 thick1 s1-sizi s1-sizj s1-squash prm))
    (setq s1-state (new idx3-ddstate thick1 s1-sizi s1-sizj))
    (setq c2-module 
	  (new c-layer ki2 kj2 1 1 tbl2 thick2 
	       c2-sizi c2-sizj c2-squash prm))
    (setq c2-state (new idx3-ddstate thick2 c2-sizi c2-sizj))
    (setq s2-module 
	  (new s-layer si2 sj2 thick2 s2-sizi s2-sizj s2-squash prm))
    (setq s2-state (new idx3-ddstate thick2 s2-sizi s2-sizj))
    (setq c3-module 
	  (new c-layer ki3 kj3 1 1 tbl3 thick3
	       c3-sizi c3-sizj c3-squash prm))
    (setq c3-state (new idx3-ddstate thick3 c3-sizi c3-sizj))
    (setq f-module
	  (new f-layer thick3 outthick c3-sizi c3-sizj f-squash prm))
    ;; this is a HACK because the compiler has a bump bug
    ;; so we have to explicitely return these things.
    ;; (list c0-squash s0-squash c1-squash s1-squash c2-squash f-squash)
    ))

(defmethod net-cscscscf forget (v p)
  (-flt- v p)
  (==> c0-module forget v p)
  (==> s0-module forget v p)
  (==> c1-module forget v p)
  (==> s1-module forget v p)
  (==> c2-module forget v p)
  (==> s2-module forget v p)
  (==> c3-module forget v p)  
  (==> f-module forget v p)
  ())

(defmethod net-cscscscf fprop (in out)
  ((-obj- (idx3-state)) in)
  ((-obj- (idx3-state)) out)
  (==> c0-module fprop in c0-state)
  (==> s0-module fprop c0-state s0-state)
  (==> c1-module fprop s0-state c1-state)
  (==> s1-module fprop c1-state s1-state)
  (==> c2-module fprop s1-state c2-state)
  (==> s2-module fprop c2-state s2-state)
  (==> c3-module fprop s2-state c3-state)
  (==> f-module  fprop c3-state out)
  ())

(defmethod net-cscscscf bprop (in out)
  ((-obj- (idx3-dstate)) in)
  ((-obj- (idx3-dstate)) out)
  (==> f-module  bprop c3-state out)
  (==> c3-module bprop s2-state c3-state)
  (==> s2-module bprop c2-state s2-state)
  (==> c2-module bprop s1-state c2-state)
  (==> s1-module bprop c1-state s1-state)
  (==> c1-module bprop s0-state c1-state)
  (==> s0-module bprop c0-state s0-state)
  (==> c0-module bprop in c0-state)
  ())

(defmethod net-cscscscf bbprop (in out)
  ((-obj- (idx3-ddstate)) in)
  ((-obj- (idx3-ddstate)) out)
  (==> f-module  bbprop c3-state out)
  (==> c3-module bbprop s2-state c3-state)
  (==> s2-module bbprop c2-state s2-state)
  (==> c2-module bbprop s1-state c2-state)
  (==> s1-module bbprop c1-state s1-state)
  (==> c1-module bbprop s0-state c1-state)
  (==> s0-module bbprop c0-state s0-state)
  (==> c0-module bbprop in c0-state)
  ())

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(dhc-make () (net-cscscscf net-cscscscf forget fprop bprop bbprop) )
