libeblearn
/home/rex/ebltrunk/core/libeblearn/include/ebl_tester.h
00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Yann LeCun, Pierre Sermanet, Koray Kavukcuoglu *
00003  *   yann@cs.nyu.edu, pierre.sermanet@gmail.com, koray@cs.nyu.edu *
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *     * Redistributions of source code must retain the above copyright
00008  *       notice, this list of conditions and the following disclaimer.
00009  *     * Redistributions in binary form must reproduce the above copyright
00010  *       notice, this list of conditions and the following disclaimer in the
00011  *       documentation and/or other materials provided with the distribution.
00012  *     * Redistribution under a license not approved by the Open Source
00013  *       Initiative (http://www.opensource.org) must display the
00014  *       following acknowledgement in all advertising material:
00015  *        This product includes software developed at the Courant
00016  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00017  *     * The names of the authors may not be used to endorse or promote products
00018  *       derived from this software without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
00021  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00022  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00023  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00024  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00025  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00026  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00027  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030  ***************************************************************************/
00031 
00032 #ifndef EBL_TESTER_H_
00033 #define EBL_TESTER_H_
00034 
00035 #include <stdio.h>
00036 #include <iostream>
00037 #include <sstream>
00038 #include <ostream>
00039 
00040 #include "libidx.h"
00041 #include "ebl_states.h"
00042 #include "ebl_basic.h"
00043 #include "ebl_arch.h"
00044 
00045 namespace ebl {
00046 
00054   template <class T> class module_tester {
00055   public:
00065     module_tester(double thres = 1e-6, T rrange_min = -1.0, T rrange_max = 1.0,
00066                   FILE* out = stdout);
00067     virtual ~module_tester();
00068 
00071     idx<double> test_jacobian(module_1_1<T> &m, bbstate_idx<T> &in, 
00072                               bbstate_idx<T> &out);
00078     idx<double> test_jacobian_param(parameter<bbstate_idx<T> > &p, 
00079                                     module_1_1<T>& m, 
00080                                     bbstate_idx<T> &in, bbstate_idx<T> &out);
00083     idx<double> test_hessian(module_1_1<T> &m, bbstate_idx<T> &in, 
00084                              bbstate_idx<T> &out);
00090     idx<double> test_hessian_param(parameter<bbstate_idx<T> > &p, 
00091                                    module_1_1<T>& m, 
00092                                    bbstate_idx<T> &in, bbstate_idx<T> &out);
00094     double get_acc_thres() const;
00096     void set_acc_thres(double acc_thres);
00098     T get_rrange() const;
00100     void set_rrange(T rrange);
00102     FILE* get_out() const;
00104     void set_out(FILE* out);
00105     
00106     // protected methods ///////////////////////////////////////////////////////
00107   protected:
00108 
00111     void get_jacobian_fprop(module_1_1<T> &m, bbstate_idx<T> &in, 
00112                             bbstate_idx<T> &out, idx<T>& jac);
00117     void get_jacobian_fprop_param(parameter<bbstate_idx<T> > &p, 
00118                                   module_1_1<T> &m, bbstate_idx<T> &in, 
00119                                   bbstate_idx<T> &out, idx<T>& jac);
00121     void get_jacobian_bprop(module_1_1<T> &m, bbstate_idx<T> &in, 
00122                             bbstate_idx<T> &out, idx<T>& jac);
00126     void get_jacobian_bprop_param(parameter<bbstate_idx<T> > &p, 
00127                                   module_1_1<T> &m, bbstate_idx<T> &in, 
00128                                   bbstate_idx<T> &out, idx<T>& jac);
00129 
00132     void get_hessian_fprop(module_1_1<T> &m, bbstate_idx<T> &in, 
00133                            bbstate_idx<T> &out, idx<T>& jac);
00138     void get_hessian_fprop_param(parameter<bbstate_idx<T> > &p, 
00139                                  module_1_1<T> &m, bbstate_idx<T> &in, 
00140                                  bbstate_idx<T> &out, idx<T>& jac);
00142     void get_hessian_bprop(module_1_1<T> &m, bbstate_idx<T> &in, 
00143                            bbstate_idx<T> &out, idx<T>& jac);
00147     void get_hessian_bprop_param(parameter<bbstate_idx<T> > &p, 
00148                                   module_1_1<T> &m, bbstate_idx<T> &in, 
00149                                  bbstate_idx<T> &out, idx<T>& jac);
00150     
00152     void report_err(idx<T>& a, idx<T>& b, const char* msg);
00154     idx<double> get_errs(idx<T>& a, idx<T>& b);
00155 
00156     // protected members ///////////////////////////////////////////////////////
00157   protected:
00158     double acc_thres; 
00159     T rrange_min, rrange_max; 
00160     FILE* out; 
00161     idx<T> jac_fprop, jac_bprop, jac_bbprop; 
00162     idx<T> jac_pfprop, jac_pbprop, jac_pbbprop; 
00163     idx<T> hes_fprop, hes_bprop, hes_bbprop; 
00164     idx<T> hes_pfprop, hes_pbprop, hes_pbbprop; 
00165   };
00166 
00169 
00170   template <class T> class Jacobian_tester {
00171   public:
00172     Jacobian_tester() {};
00173     ~Jacobian_tester() {};
00174     // this function take any module_1_1 with a fprop et bprop implemented,
00175     // and tests if the jacobian is correct (by pertubation) 
00176     // (on a fstate_idx with 3 dimensions)
00177     void test(module_1_1<T> &module);
00178   };
00179 
00182 
00183   template <class T> class Bbprop_tester {
00184   public:
00185     Bbprop_tester() {};
00186     ~Bbprop_tester() {};
00187     // this function take any module_1_1 with a fprop et bbprop implemented, 
00188     // and tests if the Bbprop is correct (by pertubation) 
00189     // (on a fstate_idx with 3 dimensions)
00190     void test(module_1_1<T> &module);
00191   };
00192 
00195 
00196   template <class T> class Bprop_tester {
00197   public:
00198     Bprop_tester() {};
00199     ~Bprop_tester() {};
00200     // this function take any module_1_1 with a fprop et bprop implemented, 
00201     // and tests if the bprop is correct (by pertubation) 
00202     // (on a fstate_idx with 3 dimensions)
00203     void test(module_1_1<T> &module);
00204   };
00205 
00206 } // namespace ebl {
00207 
00208 #include "ebl_tester.hpp"
00209 
00210 #endif /* EBL_TESTER_H_ */