PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // NLLNeighborhoodWeightsVariable.cc 00004 // 00005 // Copyright (C) 2006 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 // Authors: Hugo Larochelle 00036 00040 #include "NLLNeighborhoodWeightsVariable.h" 00041 00042 namespace PLearn { 00043 using namespace std; 00044 00047 PLEARN_IMPLEMENT_OBJECT( 00048 NLLNeighborhoodWeightsVariable, 00049 "Weights updated online, based on negative log-likelihood of the neighbors", 00050 "See DeepFeatureExtractorNNet for more details. This variable is very\n" 00051 "much oriented towards its usage in this PLearner.\n" 00052 "Note that this variable does not do bprop!!!" 00053 ); 00054 00055 NLLNeighborhoodWeightsVariable::NLLNeighborhoodWeightsVariable() 00056 : n(-1), alpha(-1) 00057 {} 00058 00059 // constructor from input variables. 00060 NLLNeighborhoodWeightsVariable::NLLNeighborhoodWeightsVariable(Variable* neighbor_nlls, Variable* neighbor_indexes, int the_n, real the_alpha) 00061 : inherited(neighbor_nlls, neighbor_indexes, neighbor_nlls->length(), 1), 00062 n(the_n), alpha(the_alpha) 00063 { 00064 build(); 00065 } 00066 00067 void NLLNeighborhoodWeightsVariable::recomputeSize(int& l, int& w) const 00068 { 00069 if (input1 && input2) { 00070 l = input1->length(); 00071 w = 1; 00072 } else 00073 l = w = 0; 00074 } 00075 00076 void NLLNeighborhoodWeightsVariable::fprop() 00077 { 00078 int index; 00079 for(int i=0; i<length(); i++) 00080 { 00081 index = (int)input2->valuedata[i]; 00082 #ifdef BOUNDCHECK 00083 if(index < 0 || index >= online_weights_log_sum.length()) 00084 PLERROR("In NLLNeighborhoodWeightsVariable::fprop(): input2->valuedata[%d] should be between 0 and n=%d",index,n); 00085 #endif 00086 if(is_missing(online_weights_log_sum[index])) 00087 { 00088 online_weights_log_sum[index] = -1*input1->valuedata[i]; 00089 } 00090 else 00091 { 00092 online_weights_log_sum[index] = logadd(log_1_minus_alpha + online_weights_log_sum[index], log_alpha - input1->valuedata[i]); 00093 } 00094 } 00095 for(int i=0; i<length(); i++) 00096 { 00097 valuedata[i] = exp(-input1->valuedata[i] - online_weights_log_sum[(int)input2->valuedata[i]]); 00098 } 00099 //cout << "weights="<< value << endl; 00100 } 00101 00102 void NLLNeighborhoodWeightsVariable::bprop() 00103 { 00104 00105 } 00106 00107 // ### You can implement these methods: 00108 // void NLLNeighborhoodWeightsVariable::bbprop() {} 00109 // void NLLNeighborhoodWeightsVariable::symbolicBprop() {} 00110 // void NLLNeighborhoodWeightsVariable::rfprop() {} 00111 00112 00113 // ### Nothing to add here, simply calls build_ 00114 void NLLNeighborhoodWeightsVariable::build() 00115 { 00116 inherited::build(); 00117 build_(); 00118 } 00119 00120 void NLLNeighborhoodWeightsVariable::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00121 { 00122 inherited::makeDeepCopyFromShallowCopy(copies); 00123 00124 deepCopyField(online_weights_log_sum, copies); 00125 00126 //PLERROR("NLLNeighborhoodWeightsVariable::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00127 } 00128 00129 void NLLNeighborhoodWeightsVariable::declareOptions(OptionList& ol) 00130 { 00131 declareOption(ol, "n", &NLLNeighborhoodWeightsVariable::n, 00132 OptionBase::buildoption, 00133 "Total number of points to be weighted"); 00134 00135 declareOption(ol, "alpha", &NLLNeighborhoodWeightsVariable::alpha, 00136 OptionBase::buildoption, 00137 "Exponential decay"); 00138 00139 // Now call the parent class' declareOptions 00140 inherited::declareOptions(ol); 00141 } 00142 00143 void NLLNeighborhoodWeightsVariable::build_() 00144 { 00145 if(input1 && input2) 00146 { 00147 if(n<=0) 00148 PLERROR("In NLLNeighborhoodWeightsVariable::build_(): n must be > 0"); 00149 if(alpha <= 0 || alpha >= 1) 00150 PLERROR("In NLLNeighborhoodWeightsVariable::build_(): alpha must be in ]0,1["); 00151 log_alpha = pl_log(alpha); 00152 log_1_minus_alpha = pl_log(1-alpha); 00153 online_weights_log_sum.resize(n); 00154 online_weights_log_sum.fill(MISSING_VALUE); 00155 if(input1->width() != 1) 00156 PLERROR("In NLLNeighborhoodWeightsVariable::build_(): input1 must be column vector"); 00157 if(input2->width() != 1) 00158 PLERROR("In NLLNeighborhoodWeightsVariable::build_(): input2 must be column vector"); 00159 if(input1->size() != input2->size()) 00160 PLERROR("In NLLNeighborhoodWeightsVariable::build_(): input1 and input2 must be of same size"); 00161 } 00162 } 00163 00164 00165 } // end of namespace PLearn 00166 00167 00168 /* 00169 Local Variables: 00170 mode:c++ 00171 c-basic-offset:4 00172 c-file-style:"stroustrup" 00173 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00174 indent-tabs-mode:nil 00175 fill-column:79 00176 End: 00177 */ 00178 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :