PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // ConstrainedSourceVariable.cc 00004 // 00005 // Copyright (C) 2008 Pascal Vincent 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: Pascal Vincent 00036 00040 #include "ConstrainedSourceVariable.h" 00041 #include<plearn/math/TMat_maths.h> 00042 00043 namespace PLearn { 00044 using namespace std; 00045 00048 PLEARN_IMPLEMENT_OBJECT( 00049 ConstrainedSourceVariable, 00050 "SourceVariable that after each update, modifies values as needed to satisfy simple constraints", 00051 "The currently supported constraint is rows having norm 1.\n" 00052 "i.e. after each update rows are divided by their norm (L2 or L1).\n"); 00053 00054 00055 void ConstrainedSourceVariable::satisfyConstraints() 00056 { 00057 switch(constraint_mode) 00058 { 00059 case 2: 00060 for(int i=0; i<matValue.length(); i++) 00061 normalize(matValue(i), 2); 00062 break; 00063 case 1: 00064 for(int i=0; i<matValue.length(); i++) 00065 normalize(matValue(i), 1); 00066 break; 00067 default: 00068 PLERROR("Invalid constraint_mode %d",constraint_mode); 00069 } 00070 } 00071 00072 00073 void ConstrainedSourceVariable::declareOptions(OptionList& ol) 00074 { 00075 declareOption( 00076 ol, "constraint_mode", &ConstrainedSourceVariable::constraint_mode, OptionBase::buildoption, 00077 "The constraint_mode: \n" 00078 "2: divide each row by its L2 norm after each update\n" 00079 "1: divide each row by its L1 norm after each update"); 00080 inherited::declareOptions(ol); 00081 } 00082 00083 00084 void ConstrainedSourceVariable::build_() 00085 { } 00086 00087 void ConstrainedSourceVariable::build() 00088 { 00089 inherited::build(); 00090 build_(); 00091 } 00092 00093 void ConstrainedSourceVariable::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00094 { 00095 inherited::makeDeepCopyFromShallowCopy(copies); 00096 } 00097 00098 00099 //##### update* ############################################################# 00100 00101 bool ConstrainedSourceVariable::update(real step_size, Vec direction_vec, real coeff, real b) 00102 { 00103 bool ret = inherited::update(step_size, direction_vec, coeff, b); 00104 satisfyConstraints(); 00105 return ret; 00106 } 00107 00108 bool ConstrainedSourceVariable::update(Vec step_sizes, Vec direction_vec, real coeff, real b) 00109 { 00110 bool ret = inherited::update(step_sizes, direction_vec, coeff, b); 00111 satisfyConstraints(); 00112 return ret; 00113 } 00114 00115 bool ConstrainedSourceVariable::update(real step_size, bool clear) 00116 { 00117 bool ret = inherited::update(step_size, clear); 00118 satisfyConstraints(); 00119 return ret; 00120 } 00121 00122 bool ConstrainedSourceVariable::update(Vec new_value) 00123 { 00124 bool ret = inherited::update(new_value); 00125 satisfyConstraints(); 00126 return ret; 00127 } 00128 00129 void ConstrainedSourceVariable::updateAndClear() 00130 { 00131 inherited::updateAndClear(); 00132 satisfyConstraints(); 00133 } 00134 00135 void ConstrainedSourceVariable::updateWithWeightDecay(real step_size, real weight_decay, 00136 bool L1, bool clear) 00137 { 00138 inherited::updateWithWeightDecay(step_size, weight_decay, L1, clear); 00139 satisfyConstraints(); 00140 } 00141 00142 00143 } // end of namespace PLearn 00144 00145 00146 /* 00147 Local Variables: 00148 mode:c++ 00149 c-basic-offset:4 00150 c-file-style:"stroustrup" 00151 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00152 indent-tabs-mode:nil 00153 fill-column:79 00154 End: 00155 */ 00156 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :