Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

ShiftAndRescaleVMatrix.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // PLearn (A C++ Machine Learning Library) 00004 // Copyright (C) 1998 Pascal Vincent 00005 // Copyright (C) 1999-2001 Pascal Vincent, Yoshua Bengio, Rejean Ducharme and University of Montreal 00006 // Copyright (C) 2002 Pascal Vincent, Julien Keable, Xavier Saint-Mleux 00007 // 00008 // Redistribution and use in source and binary forms, with or without 00009 // modification, are permitted provided that the following conditions are met: 00010 // 00011 // 1. Redistributions of source code must retain the above copyright 00012 // notice, this list of conditions and the following disclaimer. 00013 // 00014 // 2. Redistributions in binary form must reproduce the above copyright 00015 // notice, this list of conditions and the following disclaimer in the 00016 // documentation and/or other materials provided with the distribution. 00017 // 00018 // 3. The name of the authors may not be used to endorse or promote 00019 // products derived from this software without specific prior written 00020 // permission. 00021 // 00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00032 // 00033 // This file is part of the PLearn library. For more information on the PLearn 00034 // library, go to the PLearn Web site at www.plearn.org 00035 00036 00037 /* ******************************************************* 00038 * $Id: ShiftAndRescaleVMatrix.cc,v 1.12 2004/07/19 13:28:48 tihocan Exp $ 00039 ******************************************************* */ 00040 00041 #include "ShiftAndRescaleVMatrix.h" 00042 #include "VMat_maths.h" 00043 00044 namespace PLearn { 00045 using namespace std; 00046 00049 PLEARN_IMPLEMENT_OBJECT(ShiftAndRescaleVMatrix, "ONE LINE DESCR", 00050 "ShiftAndRescaleVMatrix allows to shift and scale the first n_inputs columns of an underlying_vm.\n"); 00051 00052 ShiftAndRescaleVMatrix:: 00053 ShiftAndRescaleVMatrix(VMat underlying_vm, Vec the_shift, Vec the_scale) 00054 : shift(the_shift), scale(the_scale), automatic(0), n_train(0), n_inputs(-1), 00055 negate_shift(false), 00056 no_scale(false), 00057 ignore_missing(false), 00058 verbosity(1) 00059 { 00060 vm = underlying_vm; 00061 build_(); 00062 } 00063 00064 00065 ShiftAndRescaleVMatrix::ShiftAndRescaleVMatrix() 00066 : automatic(1),n_train(0), n_inputs(-1), 00067 negate_shift(false), 00068 no_scale(false), 00069 ignore_missing(false), 00070 verbosity(1) 00071 {} 00072 00073 ShiftAndRescaleVMatrix::ShiftAndRescaleVMatrix(VMat underlying_vm, int n_inputs_) 00074 : shift(underlying_vm->width()), 00075 scale(underlying_vm->width()), automatic(1),n_train(0), n_inputs(n_inputs_), 00076 negate_shift(false), 00077 no_scale(false), 00078 ignore_missing(false), 00079 verbosity(1) 00080 { 00081 vm = underlying_vm; 00082 build_(); 00083 } 00084 00085 ShiftAndRescaleVMatrix:: 00086 ShiftAndRescaleVMatrix(VMat underlying_vm, int n_inputs_, int n_train_, bool the_ignore_missing, bool the_verbosity) 00087 : shift(underlying_vm->width()), scale(underlying_vm->width()), automatic(1), n_train(n_train_), n_inputs(n_inputs_), 00088 negate_shift(false), 00089 no_scale(false), 00090 ignore_missing(the_ignore_missing), 00091 verbosity(the_verbosity) 00092 { 00093 vm = underlying_vm; 00094 build_(); 00095 } 00096 00097 void ShiftAndRescaleVMatrix::declareOptions(OptionList& ol) 00098 { 00099 declareOption(ol, "underlying_vmat", &ShiftAndRescaleVMatrix::vm, OptionBase::buildoption, 00100 "This is the source vmatrix that will be shifted and rescaled"); 00101 declareOption(ol, "shift", &ShiftAndRescaleVMatrix::shift, OptionBase::buildoption, 00102 "This is the quantity added to each INPUT element of a row of the source vmatrix."); 00103 declareOption(ol, "scale", &ShiftAndRescaleVMatrix::scale, OptionBase::buildoption, 00104 "This is the quantity multiplied to each shifted element of a row of the source vmatrix."); 00105 declareOption(ol, "automatic", &ShiftAndRescaleVMatrix::automatic, OptionBase::buildoption, 00106 "Whether shift and scale are determined from the mean and stdev of the source vmatrix, or user-provided."); 00107 declareOption(ol, "n_train", &ShiftAndRescaleVMatrix::n_train, OptionBase::buildoption, 00108 "when automatic, use only the n_train first examples to estimate shift and scale, if n_train>0."); 00109 declareOption(ol, "n_inputs", &ShiftAndRescaleVMatrix::n_inputs, OptionBase::buildoption, 00110 "when automatic, shift and scale only the first n_inputs columns (If n_inputs<0, set n_inputs from underlying_vmat->inputsize())."); 00111 declareOption(ol, "negate_shift", &ShiftAndRescaleVMatrix::negate_shift, OptionBase::buildoption, 00112 "If set to 1, the shift will be removed instead of added."); 00113 declareOption(ol, "no_scale", &ShiftAndRescaleVMatrix::no_scale, OptionBase::buildoption, 00114 "If set to 1, no scaling will be performed (only a shift will be applied)."); 00115 declareOption(ol, "ignore_missing", &ShiftAndRescaleVMatrix::ignore_missing, OptionBase::buildoption, 00116 "If set to 1, then missing values will be ignored when computed mean and standard deviation."); 00117 declareOption(ol, "verbosity", &ShiftAndRescaleVMatrix::verbosity, OptionBase::buildoption, 00118 "Controls the amount of output."); 00119 // Now call the parent class' declareOptions 00120 inherited::declareOptions(ol); 00121 } 00122 00123 void ShiftAndRescaleVMatrix::build_() 00124 { 00125 if (vm) { 00126 length_ = vm->length(); 00127 width_ = vm->width(); 00128 writable = vm->isWritable(); 00129 if(inputsize_<0) 00130 inputsize_ = vm->inputsize(); 00131 if(targetsize_<0) 00132 targetsize_ = vm->targetsize(); 00133 if(weightsize_<0) 00134 weightsize_ = vm->weightsize(); 00135 setMtime(vm->getMtime()); 00136 if (vm->getMetaDataDir() != "") { 00137 setMetaDataDir(vm->getMetaDataDir()); 00138 } 00139 setAlias(vm->getAlias()); 00140 fieldinfos = vm->getFieldInfos(); 00141 if (automatic) 00142 { 00143 if (n_inputs<0) 00144 { 00145 n_inputs = vm->inputsize(); 00146 if (n_inputs<0) 00147 PLERROR("ShiftAndRescaleVMatrix: either n_inputs should be provided explicitly or the underlying VMatrix should have a set value of inputsize"); 00148 } 00149 if (ignore_missing) { 00150 VMat vm_to_normalize; 00151 if (n_train>0) 00152 vm_to_normalize = vm.subMat(0, 0, n_train, n_inputs); 00153 else 00154 vm_to_normalize = vm.subMatColumns(0, n_inputs); 00155 TVec<StatsCollector> stats = PLearn::computeStats(vm_to_normalize, 1, false); 00156 shift.resize(n_inputs); 00157 if (!no_scale) 00158 scale.resize(n_inputs); 00159 for (int i = 0; i < n_inputs; i++) { 00160 shift[i] = stats[i].mean(); 00161 if (!no_scale) 00162 scale[i] = stats[i].stddev(); 00163 } 00164 } else { 00165 if (n_train>0) 00166 computeMeanAndStddev(vm.subMatRows(0,n_train), shift, scale); 00167 else 00168 computeMeanAndStddev(vm, shift, scale); 00169 } 00170 if (!negate_shift) 00171 negateElements(shift); 00172 if (!no_scale) { 00173 for (int i=0;i<scale.length();i++) 00174 if (scale[i]==0) 00175 { 00176 if (verbosity >= 1) 00177 PLWARNING("ShiftAndRescale: data column number %d is constant",i); 00178 scale[i]=1; 00179 } 00180 invertElements(scale); 00181 scale.resize(vm->width()); 00182 scale.subVec(n_inputs, scale.length()-n_inputs).fill(1); 00183 } 00184 shift.resize(vm->width()); 00185 shift.subVec(n_inputs, shift.length()-n_inputs).fill(0); 00186 } 00187 reset_dimensions(); 00188 } 00189 } 00190 00191 00192 real ShiftAndRescaleVMatrix::get(int i, int j) const 00193 { 00194 if (negate_shift) { 00195 if (no_scale) { 00196 return vm->get(i,j) - shift[j]; 00197 } else { 00198 return (vm->get(i,j) - shift[j]) * scale[j]; 00199 } 00200 } else { 00201 if (no_scale) { 00202 return vm->get(i,j) + shift[j]; 00203 } else { 00204 return (vm->get(i,j) + shift[j]) * scale[j]; 00205 } 00206 } 00207 } 00208 00209 void ShiftAndRescaleVMatrix::getSubRow(int i, int j, Vec v) const 00210 { 00211 vm->getSubRow(i,j,v); 00212 if (negate_shift) { 00213 if (no_scale) { 00214 v -= shift; 00215 } else { 00216 for(int jj=0; jj<v.length(); jj++) 00217 v[jj] = (v[jj] - shift[j+jj]) * scale[j+jj]; 00218 } 00219 } else { 00220 if (no_scale) { 00221 v += shift; 00222 } else { 00223 for(int jj=0; jj<v.length(); jj++) 00224 v[jj] = (v[jj] + shift[j+jj]) * scale[j+jj]; 00225 } 00226 } 00227 } 00228 00229 void ShiftAndRescaleVMatrix::build() 00230 { 00231 inherited::build(); 00232 build_(); 00233 } 00234 00235 } // end of namespcae PLearn

Generated on Tue Aug 17 16:05:45 2004 for PLearn by doxygen 1.3.7