;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; 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-ccc.lsh,v 1.2 2002/12/19 04:27:25 profshadoko Exp $
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Convolutional network with 3 convolutional layers.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

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

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

#? ** net-ccc
;;.AUTHOR Yann LeCun
;; convolutional net with 3 convolutional layers.
(defclass net-ccc idx3-module
  ((-obj- (c-layer)) c0-module)
  ((-obj- (idx3-ddstate)) c0-state)
  ((-obj- (c-layer)) c1-module )
  ((-obj- (idx3-ddstate)) c1-state)
  ((-obj- (c-layer)) c2-module)
)


#? (new net-ccc <ini> <inj> <ki0> <kj0> <tbl0> <ki1> <kj1> <tbl1> <ki2> <kj2> <tbl2> <prm>)
;; makes a new net-ccc 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 anf feature maps for first layer
;; <ki1> <kj1> <tbl1>: kernel and table for next layer
;; <ki2> <kj2> <tbl2>: same for last convolution layer
;; <prm> an idx1-ddparam in which the parameters will be allocated.
(defmethod net-ccc net-ccc 
  (
   ini inj
   ki0 kj0 tbl0 
   ki1 kj1 tbl1 
   ki2 kj2 tbl2
   prm)
  ((-obj- (idx1-ddparam)) prm)
  ((-int-)  ini inj
	    ki0 kj0 
	    ki1 kj1 
	    ki2 kj2)
  ((-idx2- (-int-)) tbl0 tbl1 tbl2)
  (let* ((thick0 (1+ (idx-i1max (select tbl0 1 1))))
	 (c0-sizi (1+ (- ini ki0)))
	 (c0-sizj (1+ (- inj kj0)))
	 (thick1 (1+ (idx-i1max (select tbl1 1 1))))
	 (c1-sizi (1+ (- c0-sizi ki1)))
	 (c1-sizj (1+ (- c0-sizj kj1)))
	 (thick2 (1+ (idx-i1max (select tbl2 1 1))))
	 (c2-sizi (1+ (- c1-sizi ki2)))
	 (c2-sizj (1+ (- c1-sizj kj2))))
	 
    (setq c0-module 
	  (new c-layer ki0 kj0 1 1 tbl0 thick0 
	       c0-sizi c0-sizj (new idx3-squasher) prm))
    (setq c0-state (new idx3-ddstate thick0 c0-sizi c0-sizj))
    (setq c1-module 
	  (new c-layer ki1 kj1 1 1 tbl1 thick1 
	       c1-sizi c1-sizj (new idx3-squasher) prm))
    (setq c1-state (new idx3-ddstate thick1 c1-sizi c1-sizj))
    (setq c2-module 
	  (new c-layer ki2 kj2 1 1 tbl2 thick2
	       c2-sizi c2-sizj (new idx3-squasher) prm))
    )
  ())

(defmethod net-ccc forget (v p)
  (-float- v p)
  (==> c0-module forget v p)
  (==> c1-module forget v p)
  (==> c2-module forget v p)  
  ())

(defmethod net-ccc fprop (in out)
  ((-obj- (idx3-state)) in)
  ((-obj- (idx3-state)) out)
  (==> c0-module fprop in c0-state)
  (==> c1-module fprop c0-state c1-state)
  (==> c2-module fprop c1-state out)
  ())

(defmethod net-ccc bprop (in out)
  ((-obj- (idx3-dstate)) in)
  ((-obj- (idx3-dstate)) out)
  (==> c2-module bprop c1-state out)
  (==> c1-module bprop c0-state c1-state)
  (==> c0-module bprop in c0-state)
  ())

(defmethod net-ccc bbprop (in out)
  ((-obj- (idx3-ddstate)) in)
  ((-obj- (idx3-ddstate)) out)
  (==> c2-module bbprop c1-state out)
  (==> c1-module bbprop c0-state c1-state)
  (==> c0-module bbprop in c0-state)
  ())

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

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

