PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio, Rejean Ducharme and University of Montreal 00006 // Copyright (C) 2001-2002 Nicolas Chapados, Ichiro Takeuchi, Jean-Sebastien Senecal 00007 // Copyright (C) 2002 Xiangdong Wang, Christian Dorion 00008 00009 // Redistribution and use in source and binary forms, with or without 00010 // modification, are permitted provided that the following conditions are met: 00011 // 00012 // 1. Redistributions of source code must retain the above copyright 00013 // notice, this list of conditions and the following disclaimer. 00014 // 00015 // 2. Redistributions in binary form must reproduce the above copyright 00016 // notice, this list of conditions and the following disclaimer in the 00017 // documentation and/or other materials provided with the distribution. 00018 // 00019 // 3. The name of the authors may not be used to endorse or promote 00020 // products derived from this software without specific prior written 00021 // permission. 00022 // 00023 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00024 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00025 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00026 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00027 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00028 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00029 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00030 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00031 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00032 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 // 00034 // This file is part of the PLearn library. For more information on the PLearn 00035 // library, go to the PLearn Web site at www.plearn.org 00036 00037 00038 /* ******************************************************* 00039 * $Id: PotentialsVariable.cc 3994 2005-08-25 13:35:03Z chapados $ 00040 * This file is part of the PLearn library. 00041 ******************************************************* */ 00042 00043 #include "PotentialsVariable.h" 00044 #include <plearn/display/DisplayUtils.h> 00045 00046 namespace PLearn { 00047 using namespace std; 00048 00049 00050 00053 PLEARN_IMPLEMENT_OBJECT(PotentialsVariable, 00054 "Variable that computes potentials for several targets using a single function", 00055 "NO HELP"); 00056 00057 PotentialsVariable::PotentialsVariable(Var the_input, Var the_comp_input, Var the_dp_target, Var the_target_dist_reps, Var the_output, VarArray the_params, VMat the_distr) 00058 : inherited(the_input & the_comp_input & the_dp_target & the_params & the_target_dist_reps, 00059 2, // Temporary value 00060 1 // idem 00061 ), 00062 distr(the_distr), 00063 input(the_input), 00064 output(the_output), 00065 comp_input(the_comp_input), 00066 dp_target(the_dp_target), 00067 target_dist_reps(the_target_dist_reps), 00068 params(the_params) 00069 { 00070 build_(); 00071 } 00072 00073 void 00074 PotentialsVariable::build() 00075 { 00076 inherited::build(); 00077 build_(); 00078 } 00079 00080 void 00081 PotentialsVariable::build_() 00082 { 00083 if ( input && output && comp_input && dp_target && params.size() != 0) 00084 { 00085 if(output->size() != 1) PLERROR("In PotentialsVariable::build_(): to_output function has output size different from 1"); 00086 proppath = propagationPath(comp_input & dp_target & params, output); 00087 00088 // Initialize temp_comps 00089 00090 temp_comps.resize(1); 00091 temp_comps[0].resize(proppath.size()); 00092 for(int i=0; i<proppath.size(); i++) 00093 temp_comps[0][i] = Var(proppath[i]->length(), proppath[i]->width()); 00094 00095 } 00096 } 00097 00098 void 00099 PotentialsVariable::declareOptions(OptionList &ol) 00100 { 00101 declareOption(ol, "distr", &PotentialsVariable::distr, OptionBase::buildoption, ""); 00102 declareOption(ol, "output", &PotentialsVariable::output, OptionBase::buildoption, ""); 00103 declareOption(ol, "comp_input", &PotentialsVariable::comp_input, OptionBase::buildoption, ""); 00104 declareOption(ol, "dp_target", &PotentialsVariable::dp_target, OptionBase::buildoption, ""); 00105 declareOption(ol, "target_dist_reps", &PotentialsVariable::target_dist_reps, OptionBase::buildoption, ""); 00106 declareOption(ol, "input", &PotentialsVariable::input, OptionBase::buildoption, ""); 00107 declareOption(ol, "params", &PotentialsVariable::params, OptionBase::buildoption, ""); 00108 inherited::declareOptions(ol); 00109 } 00110 00111 00112 void PotentialsVariable::recomputeSize(int& l, int& w) const 00113 { 00114 if (distr) { 00115 distr->getValues(input->value,distr->inputsize(),values); 00116 l = values.length(); 00117 w = 1; 00118 } else 00119 l = w = 0; 00120 } 00121 00122 00123 void PotentialsVariable::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00124 { 00125 inherited::makeDeepCopyFromShallowCopy(copies); 00126 00127 varDeepCopyField(output, copies); 00128 varDeepCopyField(comp_input, copies); 00129 varDeepCopyField(dp_target, copies); 00130 varDeepCopyField(target_dist_reps, copies); 00131 varDeepCopyField(input, copies); 00132 00133 deepCopyField(distr, copies); 00134 deepCopyField(temp_comps, copies); 00135 deepCopyField(values, copies); 00136 deepCopyField(proppath, copies); 00137 deepCopyField(params, copies); 00138 } 00139 00140 00141 void PotentialsVariable::fprop() 00142 { 00143 //temp_comps.resize(length(),proppath.nelems()); 00144 distr->getValues(input->value,distr->inputsize(),values); 00145 00146 for(int i=0; i<length(); i++) 00147 { 00148 00149 // Add some elements in temp_comps if necessary 00150 if(temp_comps.length() < length()) 00151 { 00152 int last_size = temp_comps.length(); 00153 temp_comps.resize(length()); 00154 for(int j=last_size; j<temp_comps.length(); j++) 00155 { 00156 temp_comps[j].resize(proppath.size()); 00157 for(int k=0; k<proppath.size(); k++) 00158 temp_comps[j][k] = Var(proppath[k]->length(), proppath[k]->width()); 00159 } 00160 } 00161 00162 // Point to temp_comps for values and gradients of proppath 00163 for(int j=0; j<proppath.size(); j++) 00164 { 00165 proppath[j]->matValue = temp_comps[i][j]->matValue; 00166 proppath[j]->value = proppath[j]->matValue.toVec(); 00167 proppath[j]->valuedata = proppath[j]->value.data(); 00168 00169 proppath[j]->matGradient = temp_comps[i][j]->matGradient; 00170 proppath[j]->gradient = proppath[j]->matGradient.toVec(); 00171 proppath[j]->gradientdata = proppath[j]->gradient.data(); 00172 } 00173 00174 dp_target->value << target_dist_reps->matValue((int)values[i]); 00175 proppath.fprop(); 00176 //proppath >> temp_comps(i); 00177 value[i] = output->value[0]; 00178 } 00179 } 00180 00181 00182 void PotentialsVariable::bprop() 00183 { 00184 for(int i=0; i<length(); i++) 00185 { 00186 00187 // Point to temp_comps for values and gradients of proppath 00188 for(int j=0; j<proppath.size(); j++) 00189 { 00190 proppath[j]->matValue = temp_comps[i][j]->matValue; 00191 proppath[j]->value = proppath[j]->matValue.toVec(); 00192 proppath[j]->valuedata = proppath[j]->value.data(); 00193 00194 proppath[j]->matGradient = temp_comps[i][j]->matGradient; 00195 proppath[j]->gradient = proppath[j]->matGradient.toVec(); 00196 proppath[j]->gradientdata = proppath[j]->gradient.data(); 00197 } 00198 00199 //proppath << temp_comps(i); 00200 dp_target->value << target_dist_reps->matValue((int)values[i]); 00201 proppath.clearGradient(); 00202 dp_target->clearGradient(); 00203 output->gradient[0] = gradient[i]; 00204 proppath.bprop(); 00205 target_dist_reps->matGradient((int)values[i]) += dp_target->gradient; 00206 target_dist_reps->updateRow((int)values[i]); 00207 } 00208 } 00209 00210 00211 00212 void PotentialsVariable::symbolicBprop() 00213 { 00214 PLERROR("In PotentialsVariable::symbolicBprop(): not implemented"); 00215 } 00216 00217 00218 void PotentialsVariable::rfprop() 00219 { 00220 PLERROR("In PotentialsVariable::rfprop(): not implemented"); 00221 } 00222 00223 } // end of namespace PLearn 00224 00225 00226 /* 00227 Local Variables: 00228 mode:c++ 00229 c-basic-offset:4 00230 c-file-style:"stroustrup" 00231 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00232 indent-tabs-mode:nil 00233 fill-column:79 00234 End: 00235 */ 00236 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :