;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; 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-lenet5.lsh,v 1.1 2002/11/26 17:24:37 profshadoko Exp $
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#? *** lenet5
;; LeNet5 is a convolutional network architecture
;; described in several publications, notably in
;; [LeCun, Bottou, Bengio and Haffner 1998]: "gradient-based 
;; learning applied to document recognition", Proc IEEE, Nov 1998.
;; The paper is also available at 
;; {<hlink> http://yann.lecun.com}.


(libload "net-cscscf")

#? (new-lenet5 <image-height> <image-width> <ki0> <kj0> <si0> <sj0>  <ki1> <kj1> <si1> <sj1> <hid> <output-size> <net-param>)
;; create a new instance of net-cscscf implementing a LeNet-5 type
;; convolutional neural net. This network has regular sigmoid 
;; units on the output, not an extra RBF layer as described 
;; in the Proc. IEEE paper. The network has 6 feature
;; maps at the first layer and 16 feature maps at the second layer
;; with a connection matrix between feature maps as described in 
;; the paper.
;; Arguments:
;;{<pre>
;;  <image-height> <image-width>: height and width of input image
;;  <ki0> <kj0>: height and with of convolutional kernel, first layer.
;;  <si0> <sj0>: subsampling ratio of subsampling layer, second layer.
;;  <ki1> <kj1>: height and with of convolutional kernel, third layer.
;;  <si1> <sj1>: subsampling ratio of subsampling layer, fourth layer.
;;  <hid>: number of hidden units, fifth layer
;;  <output-size>: number of output units
;;  <net-param>: idx1-ddparam that will hold the trainable parameters
;;               of the network
;; </pre>}
;; example
;; {<code>
;;  (setq p (new idx1-ddparam 0 0.1 0.02 0.02 80000))
;;  (setq z (new-lenet5 32 32 5 5 2 2 5 5 2 2 120 10 p))
;; </code>}
(de new-lenet5 (image-height 
		image-width
		ki0 kj0 si0 sj0 ki1 kj1 si1 sj1
		hid output-size net-param)
  (let ((table0 (full-table 1 6))
	(table1 (int-matrix 60 2))
	(table2 (full-table 16 hid)))
    (table1 ()()
	    '(0 0  1 0  2 0
                1 1  2 1  3 1
                2 2  3 2  4 2
                3 3  4 3  5 3
                4 4  5 4  0 4
                5 5  0 5  1 5

                0 6  1 6  2 6  3 6
                1 7  2 7  3 7  4 7
                2 8  3 8  4 8  5 8
                3 9  4 9  5 9  0 9
                4 10  5 10  0 10  1 10
                5 11  0 11  1 11  2 11

                0 12  1 12   3 12  4 12
                1 13  2 13   4 13  5 13
                2 14  3 14   5 14  0 14

                0 15  1 15  2 15  3 15  4 15  5 15 ))
    (new net-cscscf
	 image-height image-width
	 ki0 kj0 table0 si0 sj0
	 ki1 kj1 table1 si1 sj1
	 ;; WARNING: those two numbers must be changed 
	 ;; when image-height/image-width change
	 (/ (- (/ (- image-height (1- ki0)) si0) (1- ki1)) si1)
	 (/ (- (/ (- image-width (1- kj0)) sj0) (1- kj1)) sj1)
	 (full-table 16 hid)
	 output-size
	 net-param)))
