PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // GaussianContinuum.h 00004 // 00005 // Copyright (C) 2004 Yoshua Bengio & Hugo Larochelle 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 3994 2005-08-25 13:35:03Z chapados $ 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 #include <plearn/ker/DistanceKernel.h> 00052 00053 namespace PLearn { 00054 using namespace std; 00055 00056 class GaussianContinuum: public PLearner 00057 { 00058 00059 private: 00060 00061 typedef PLearner inherited; 00062 00063 protected: 00064 // NON-OPTION FIELDS 00065 int n; 00066 Func cost_of_one_example; 00067 //Func verify_gradient_func; 00068 Var x, noise_var; // input vector 00069 Var b, W, c, V, muV, smV, smb, snV, snb; // explicit view of the parameters (also in parameters field). 00070 //Var W_src, c_src, V_src, muV_src, smV_src, smb_src, snV_src, snb_src; 00071 //VarArray mu_neighbors, sm_neighbors, sn_neighbors, hidden_neighbors, input_neighbors, index_neighbors, tangent_plane_neighbors; 00072 Var tangent_targets, tangent_targets_and_point; // target for the tangent vectors for one example 00073 Var tangent_plane; 00074 Var mu, sm, sn, mu_noisy; // parameters of the conditional models 00075 Var p_x, p_target, p_neighbors, p_neighbors_and_point, target_index, neigbor_indexes; 00076 Var sum_nll; 00077 Var min_sig, min_d; 00078 00079 PP<PDistribution> dist; 00080 00081 VMat valid_set; 00082 00083 // Random walk fields 00084 Array<VMat> ith_step_generated_set; 00085 00086 // p(x) computation fields 00087 VMat train_and_generated_set; 00088 VMat reference_set; 00089 TMat<int> train_nearest_neighbors; 00090 TMat<int> validation_nearest_neighbors; 00091 TVec< Mat > Bs, Fs; 00092 Mat mus; 00093 Vec sms; 00094 Vec sns; 00095 00096 Mat Ut_svd, V_svd; // for SVD computation 00097 Vec S_svd; // idem 00098 mutable Vec z, zm, zn, x_minus_neighbor, w; 00099 mutable Vec t_row, neighbor_row; 00100 mutable TVec<int> t_nn; 00101 mutable Vec t_dist; 00102 mutable Mat distances; 00103 00104 mutable DistanceKernel dk; 00105 00106 real best_validation_cost; 00107 00108 // ********************* 00109 // * protected options * 00110 // ********************* 00111 00112 // ### declare protected option fields (such as learnt parameters) here 00113 VarArray parameters; 00114 00115 public: 00116 00117 // ************************ 00118 // * public build options * 00119 // ************************ 00120 00121 // ### declare public option fields (such as build options) here 00122 00123 real weight_mu_and_tangent; 00124 bool include_current_point; 00125 real random_walk_step_prop; 00126 bool use_noise; 00127 bool use_noise_direction; 00128 real noise; 00129 string noise_type; 00130 int n_random_walk_step; 00131 int n_random_walk_per_point; 00132 bool save_image_mat; 00133 bool walk_on_noise; 00134 VMat image_points_vmat; 00135 Mat image_points_mat; 00136 Mat image_prob_mat; 00137 TMat<int> image_nearest_neighbors; 00138 real upper_y; 00139 real lower_y; 00140 real upper_x; 00141 real lower_x; 00142 int points_per_dim; 00143 real min_sigma; 00144 real min_diff; 00145 real min_p_x; 00146 bool print_parameters; 00147 bool sm_bigger_than_sn; 00148 int n_neighbors; // number of neighbors used for gradient descent 00149 int n_neighbors_density; // number of neighbors for the p(x) density estimation 00150 int mu_n_neighbors; // number of neighbors to learn the mus 00151 int n_dim; // number of reduced dimensions (number of tangent vectors to compute) 00152 int compute_cost_every_n_epochs; 00153 string variances_transfer_function; // "square", "exp" or "softplus" 00154 real validation_prop; 00155 PP<Optimizer> optimizer; // to estimate the function that predicts local tangent vectors given the input 00156 Var embedding; 00157 Func output_f; 00158 Func output_f_all; 00159 Func predictor; // predicts everything about the gaussian 00160 Func projection_error_f; // map output to projection error 00161 Func noisy_data; 00162 00163 // manual construction of the tangent_predictor 00164 string architecture_type; // "neural_network" or "linear" or "" or "embedding_neural_nework" or "embedding_quadratic" 00165 string output_type; // "tangent_plane", "embedding", or "tangent_plane+embedding". 00166 int n_hidden_units; 00167 00168 int batch_size; 00169 00170 real norm_penalization; // penalizes sum_i (||f_i||^2-1)^2 00171 real svd_threshold; 00172 00173 // **************** 00174 // * Constructors * 00175 // **************** 00176 00178 // (Make sure the implementation in the .cc 00179 // initializes all fields to reasonable default values) 00180 GaussianContinuum(); 00181 00182 00183 // ******************** 00184 // * PLearner methods * 00185 // ******************** 00186 00187 private: 00188 00190 // (Please implement in .cc) 00191 void build_(); 00192 00193 void compute_train_and_validation_costs(); 00194 00195 void make_random_walk(); 00196 00197 void update_reference_set_parameters(); 00198 00199 void knn(const VMat& vm, const Vec& x, const int& k, TVec<int>& neighbors, bool sortk) const; 00200 00201 void get_image_matrix(VMat points, VMat image_points_vmat, int begin, string file_path, int n_near_neigh); 00202 00203 real get_nll(VMat points, VMat image_points_vmat, int begin, int n_near_neigh); 00204 00205 protected: 00206 00208 // (Please implement in .cc) 00209 static void declareOptions(OptionList& ol); 00210 00211 public: 00212 00213 // ************************ 00214 // **** Object methods **** 00215 // ************************ 00216 00218 virtual void build(); 00219 00221 virtual void makeDeepCopyFromShallowCopy(CopiesMap& copies); 00222 00223 // Declares other standard object methods. 00224 // If your class is not instantiatable (it has pure virtual methods) 00225 // you should replace this by PLEARN_DECLARE_ABSTRACT_OBJECT_METHODS. 00226 PLEARN_DECLARE_OBJECT(GaussianContinuum); 00227 00228 00229 // ************************** 00230 // **** PLearner methods **** 00231 // ************************** 00232 00235 virtual int outputsize() const; 00236 00239 virtual void forget(); 00240 virtual void initializeParams(); 00241 00242 00245 virtual void train(); 00246 00247 00249 // (PLEASE IMPLEMENT IN .cc) 00250 virtual void computeOutput(const Vec& input, Vec& output) const; 00251 00253 // (PLEASE IMPLEMENT IN .cc) 00254 virtual void computeCostsFromOutputs(const Vec& input, const Vec& output, 00255 const Vec& target, Vec& costs) const; 00256 00257 00259 // (PLEASE IMPLEMENT IN .cc) 00260 virtual TVec<string> getTestCostNames() const; 00261 00264 // (PLEASE IMPLEMENT IN .cc) 00265 virtual TVec<string> getTrainCostNames() const; 00266 00267 00268 // *** SUBCLASS WRITING: *** 00269 // While in general not necessary, in case of particular needs 00270 // (efficiency concerns for ex) you may also want to overload 00271 // some of the following methods: 00272 // virtual void computeOutputAndCosts(const Vec& input, const Vec& target, Vec& output, Vec& costs) const; 00273 // virtual void computeCostsOnly(const Vec& input, const Vec& target, Vec& costs) const; 00274 // virtual void test(VMat testset, PP<VecStatsCollector> test_stats, VMat testoutputs=0, VMat testcosts=0) const; 00275 // virtual int nTestCosts() const; 00276 // virtual int nTrainCosts() const; 00277 00278 }; 00279 00280 // Declares a few other classes and functions related to this class. 00281 DECLARE_OBJECT_PTR(GaussianContinuum); 00282 00283 } // end of namespace PLearn 00284 00285 #endif 00286 00287 00288 /* 00289 Local Variables: 00290 mode:c++ 00291 c-basic-offset:4 00292 c-file-style:"stroustrup" 00293 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00294 indent-tabs-mode:nil 00295 fill-column:79 00296 End: 00297 */ 00298 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :