00001 // -*- C++ -*- 00002 00003 // GaussianContinuum.h 00004 // 00005 // Copyright (C) 2004 Yoshua Bengio & Martin Monperrus 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 /* ******************************************************* 00036 * $Id: GaussianContinuum.h,v 1.4 2004/08/17 15:28:21 larocheh Exp $ 00037 ******************************************************* */ 00038 00039 // Authors: Yoshua Bengio & Martin Monperrus 00040 00044 #ifndef GaussianContinuum_INC 00045 #define GaussianContinuum_INC 00046 #include <plearn/io/PStream.h> 00047 #include <plearn_learners/generic/PLearner.h> 00048 #include <plearn/var/Func.h> 00049 #include <plearn/opt/Optimizer.h> 00050 #include <plearn_learners/distributions/PDistribution.h> 00051 00052 namespace PLearn { 00053 using namespace std; 00054 00055 class GaussianContinuum: public PLearner 00056 { 00057 00058 private: 00059 00060 typedef PLearner inherited; 00061 00062 protected: 00063 // NON-OPTION FIELDS 00064 int n; 00065 Func cost_of_one_example; 00066 Func verify_gradient_func; 00067 Var x, noise_var; // input vector 00068 Var b, W, c, V, muV, smV, smb, snV, snb; // explicit view of the parameters (also in parameters field). 00069 //Var W_src, c_src, V_src, muV_src, smV_src, smb_src, snV_src, snb_src; 00070 //VarArray mu_neighbors, sm_neighbors, sn_neighbors, hidden_neighbors, input_neighbors, index_neighbors, tangent_plane_neighbors; 00071 Var tangent_targets; // target for the tangent vectors for one example 00072 Var tangent_plane; 00073 Var mu, sm, sn, mu_noisy; // parameters of the conditional models 00074 Var p_x, p_target, p_neighbors, target_index, neigbor_indexes; 00075 Var sum_nll; 00076 00077 PP<PDistribution> dist; 00078 00079 // Random walk fields 00080 Array<VMat> ith_step_generated_set; 00081 00082 // p(x) computation fields 00083 VMat train_and_generated_set; 00084 TMat<int> train_nearest_neighbors; 00085 TMat<int> validation_nearest_neighbors; 00086 TVec< Mat > Bs, Fs; 00087 Mat mus; 00088 Vec sms; 00089 Vec sns; 00090 00091 Mat Ut_svd, V_svd; // for SVD computation 00092 Vec S_svd; // idem 00093 Vec z, zm, zn, x_minus_neighbor, w; 00094 Vec t_row, neighbor_row; 00095 00096 real best_validation_cost; 00097 00098 // ********************* 00099 // * protected options * 00100 // ********************* 00101 00102 // ### declare protected option fields (such as learnt parameters) here 00103 VarArray parameters; 00104 00105 public: 00106 00107 // ************************ 00108 // * public build options * 00109 // ************************ 00110 00111 // ### declare public option fields (such as build options) here 00112 00113 bool use_noise; 00114 real noise; 00115 string noise_type; 00116 int n_random_walk_step; 00117 int n_random_walk_per_point; 00118 bool save_image_mat; 00119 VMat image_points_vmat; 00120 Mat image_points_mat; 00121 Mat image_prob_mat; 00122 TMat<int> image_nearest_neighbors; 00123 real upper_y; 00124 real lower_y; 00125 real upper_x; 00126 real lower_x; 00127 int points_per_dim; 00128 real min_sigma; 00129 real min_diff; 00130 bool print_parameters; 00131 bool sm_bigger_than_sn; 00132 bool use_best_model; 00133 int n_neighbors; // number of neighbors used for gradient descent 00134 int n_neighbors_density; // number of neighbors for the p(x) density estimation 00135 int n_dim; // number of reduced dimensions (number of tangent vectors to compute) 00136 int compute_cost_every_n_epochs; 00137 string variances_transfer_function; // "square", "exp" or "softplus" 00138 PP<Optimizer> optimizer; // to estimate the function that predicts local tangent vectors given the input 00139 Var embedding; 00140 Func output_f; 00141 Func output_f_all; 00142 Func predictor; // predicts everything about the gaussian 00143 Func projection_error_f; // map output to projection error 00144 Func noisy_data; 00145 00146 // manual construction of the tangent_predictor 00147 string architecture_type; // "neural_network" or "linear" or "" or "embedding_neural_nework" or "embedding_quadratic" 00148 string output_type; // "tangent_plane", "embedding", or "tangent_plane+embedding". 00149 int n_hidden_units; 00150 00151 int batch_size; 00152 00153 real norm_penalization; // penalizes sum_i (||f_i||^2-1)^2 00154 real svd_threshold; 00155 real projection_error_regularization; // term to add on linear system diagonal, to solve for subspace fitting 00156 00157 // **************** 00158 // * Constructors * 00159 // **************** 00160 00162 // (Make sure the implementation in the .cc 00163 // initializes all fields to reasonable default values) 00164 GaussianContinuum(); 00165 00166 00167 // ******************** 00168 // * PLearner methods * 00169 // ******************** 00170 00171 private: 00172 00174 // (Please implement in .cc) 00175 void build_(); 00176 00177 void compute_train_and_validation_costs(); 00178 00179 void make_random_walk(); 00180 00181 void get_image_matrix(Mat& image, VMat image_points_vmat, int begin, string file_path); 00182 00183 protected: 00184 00186 // (Please implement in .cc) 00187 static void declareOptions(OptionList& ol); 00188 00189 public: 00190 00191 // ************************ 00192 // **** Object methods **** 00193 // ************************ 00194 00196 virtual void build(); 00197 00199 virtual void makeDeepCopyFromShallowCopy(map<const void*, void*>& copies); 00200 00201 // Declares other standard object methods. 00202 // If your class is not instantiatable (it has pure virtual methods) 00203 // you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT_METHODS. 00204 PLEARN_DECLARE_OBJECT(GaussianContinuum); 00205 00206 00207 // ************************** 00208 // **** PLearner methods **** 00209 // ************************** 00210 00213 virtual int outputsize() const; 00214 00217 virtual void forget(); 00218 virtual void initializeParams(); 00219 00220 00223 virtual void train(); 00224 00225 00227 // (PLEASE IMPLEMENT IN .cc) 00228 virtual void computeOutput(const Vec& input, Vec& output) const; 00229 00231 // (PLEASE IMPLEMENT IN .cc) 00232 virtual void computeCostsFromOutputs(const Vec& input, const Vec& output, 00233 const Vec& target, Vec& costs) const; 00234 00235 00237 // (PLEASE IMPLEMENT IN .cc) 00238 virtual TVec<string> getTestCostNames() const; 00239 00242 // (PLEASE IMPLEMENT IN .cc) 00243 virtual TVec<string> getTrainCostNames() const; 00244 00245 00246 // *** SUBCLASS WRITING: *** 00247 // While in general not necessary, in case of particular needs 00248 // (efficiency concerns for ex) you may also want to overload 00249 // some of the following methods: 00250 // virtual void computeOutputAndCosts(const Vec& input, const Vec& target, Vec& output, Vec& costs) const; 00251 // virtual void computeCostsOnly(const Vec& input, const Vec& target, Vec& costs) const; 00252 // virtual void test(VMat testset, PP<VecStatsCollector> test_stats, VMat testoutputs=0, VMat testcosts=0) const; 00253 // virtual int nTestCosts() const; 00254 // virtual int nTrainCosts() const; 00255 00256 }; 00257 00258 // Declares a few other classes and functions related to this class. 00259 DECLARE_OBJECT_PTR(GaussianContinuum); 00260 00261 } // end of namespace PLearn 00262 00263 #endif