;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; 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: mnist-svqp.lsh,v 1.6 2006/01/25 15:15:25 leonb Exp $
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(libload "svm/demos/mnist-kernel")
(libload "svm/svqp2svm")

(defvar mnist-svm)

(de run-one (c1)
  (printf "--------------------\nLoading data\n")
  (load-mnist)
  (setq mnist-svm (new svqp2svm mnist-kernel 60000))
  (for (i 0 59999)
    (let ((y (==> mnist-kernel label i)))
      (setq y (if (= y c1) 1 -1))
      (:mnist-svm:x i i)
      (:mnist-svm:y i y) ) )
  (setq :mnist-svm:maxcachesize (* 400 1024 1024))
  (setq :mnist-svm:cp 10)
  (setq :mnist-svm:cn 10)
  (printf "--------------------\nTraining 1 C-SVC ... Wait\n")
  (let ((delay (cputime (==> mnist-svm run))))
    (printf "Total time: %l cpu seconds (%d SV)\n" delay :mnist-svm:nsv) )
  (let ((nex 0)
        (nerror 0) 
	(lexp (==> mnist-svm get-expansion)) )
    (for (i 60000 69999)
	 (let ((y (==> mnist-kernel label i)))
	   (setq y (if (= y c1) 1 -1))
	   (let ((s (==> lexp predict i)))
	     (incr nex)
	     (when (<= (* y s) 0)
	       (incr nerror) ) ) ) )
    (printf "%d/%d errors (%l%%)\n" nerror nex (* 100 (/ nerror nex))) ) )


(de run-pair(c1 c2)
  (printf "--------------------\nLoading data\n")
  (setq mnist-svm (new Svqp2SVM mnist-kernel 20000) )
  (let ((k 0))
    (for (i 0 59999)
	 (let ((y (==> mnist-kernel label i)))
	   (when (or (= y c1) (= y c2))
	     (:mnist-svm:x k i)
	     (:mnist-svm:y k (if (= y c1) +1 -1))
	     (incr k) ) ) )
    (idx-i1resize :mnist-svm:x k)
    (idx-d1resize :mnist-svm:y k)
    (printf "%d examples\n" k) )
  (setq :mnist-svm:maxcachesize (* 200 1024 1024))
  (setq :mnist-svm:cp 10)
  (setq :mnist-svm:cn 10)
  (printf "--------------------\nTraining 1 C-SVC ... Wait\n")
  (let ((delay (cputime (==> mnist-svm run))))
    (printf "Total time: %l cpu seconds (%d SV) \n" delay :mnist-svm:nsv) )
  (printf "--------------------\nTesting on 10K test set ... Wait\n")
  (let ((nex 0)
	(nerror 0)
	(lexp (==> mnist-svm get-expansion)) )
    (for (i 60000 69999)
	 (let ((y (==> mnist-kernel label i)))
	   (when (or (= y c1) (= y c2))
	     (let ((s (==> lexp predict i)))
	       (incr nex)
	       (setq s (if (> s 0) c1 c2))
	       (when (<> s y) (incr nerror)) ) ) ) )
    (printf "%d/%d errors (%l%%)\n" nerror nex (* 100 (/ nerror nex))) ) )
