PLearn 0.1
HardSlopeVariable.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-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: HardSlopeVariable.cc 3994 2005-08-25 13:35:03Z chapados $
00040  * This file is part of the PLearn library.
00041  ******************************************************* */
00042 
00043 #include "HardSlopeVariable.h"
00044 #include "Var_utils.h"
00045 
00046 namespace PLearn {
00047 using namespace std;
00048 
00049 
00052 PLEARN_IMPLEMENT_OBJECT(HardSlopeVariable, 
00053                         "This Var computes the hard_slope function", 
00054                         "The hard_slope function is linear by parts function:\n"
00055                         "0 in [-infty,left], linear in [left,right], and 1 in [right,infty], and continuous.\n"
00056                         "If the arguments are vectors than the operation is performed element by element on all of them.\n");
00057 
00058 HardSlopeVariable::  HardSlopeVariable(Variable* x, Variable* left, Variable* right)
00059     : inherited(VarArray(x,left) & Var(right), 
00060                 x->length()<left->length()?left->length():x->length(), 
00061                 x->width()<left->width()?left->width():x->width()) 
00062 {}
00063 
00064 
00065 void HardSlopeVariable::recomputeSize(int& l, int& w) const
00066 { 
00067     l = w = 0;
00068     if (varray.size() >= 3) {
00069         for (int i = 0;i < 3; i++) {
00070             if (varray[i]->length()>l)
00071                 l = varray[i]->length();
00072             if (varray[i]->width() > w)
00073                 w = varray[i]->width();
00074         }
00075         for (int i = 0;i < 3; i++) {
00076             if (varray[i]->length() != l || varray[i]->width() != w) {
00077                 if (varray[i]->length() != 1 || varray[i]->width() != 1)
00078                     PLERROR("Each argument of HardSlopeVariable should either have the same length/width as the others or length 1");
00079             }
00080         }
00081     }
00082 }
00083 
00084 
00085 void HardSlopeVariable::fprop()
00086 {
00087     int n=nelems();
00088     int n1=varray[0]->nelems();
00089     int n2=varray[1]->nelems();
00090     int n3=varray[2]->nelems();
00091     real* x = varray[0]->valuedata;
00092     real* left = varray[1]->valuedata;
00093     real* right = varray[2]->valuedata;
00094 
00095     if (n1==n && n2==n && n3==n)
00096         for(int i=0; i<n; i++)
00097             valuedata[i] = hard_slope(x[i], left[i], right[i]);
00098     else if (n1==1 && n2==n && n3==n)
00099         for(int i=0; i<n; i++)
00100             valuedata[i] = hard_slope(*x, left[i], right[i]);
00101     else
00102     {
00103         int m1= n1==1?0:1;
00104         int m2= n2==1?0:1;
00105         int m3= n3==1?0:1;
00106         for(int i=0; i<n; i++,x+=m1,left+=m2,right+=m3)
00107             valuedata[i] = hard_slope(*x, *left, *right);
00108     }
00109 }
00110 
00111 
00112 void HardSlopeVariable::bprop()
00113 {
00114     int n=nelems();
00115     int n1=varray[0]->nelems();
00116     int n2=varray[1]->nelems();
00117     int n3=varray[2]->nelems();
00118     int m1= n1==1?0:1;
00119     int m2= n2==1?0:1;
00120     int m3= n3==1?0:1;
00121     real* x = varray[0]->valuedata;
00122     real* left = varray[1]->valuedata;
00123     real* right = varray[2]->valuedata;
00124     real* dx = varray[0]->gradientdata;
00125     real* dleft = varray[1]->gradientdata;
00126     real* dright = varray[2]->gradientdata;
00127     for(int i=0; i<n; i++,x+=m1,left+=m2,right+=m3,dx+=m1,dleft+=m2,dright+=m3)
00128     {
00129         real tleft = *x - *left;
00130         real tright = *x - *right;
00131         if (tright<=0 && tleft>=0)
00132         {
00133             real inv_delta=1.0/(*right - *left);
00134             real dll = tright*inv_delta;
00135             real drr = -tleft*inv_delta;
00136             real dxx = inv_delta;
00137             *dx += gradientdata[i] * dxx;
00138             *dleft += gradientdata[i] * dll;
00139             *dright += gradientdata[i] * drr;
00140         }
00141     }
00142 }
00143 
00144 void HardSlopeVariable::symbolicBprop()
00145 {
00146     PLERROR("HardSlopeVariable::symbolicBprop() not implemented");
00147 }
00148 
00149 
00150 
00151 } // end of namespace PLearn
00152 
00153 
00154 /*
00155   Local Variables:
00156   mode:c++
00157   c-basic-offset:4
00158   c-file-style:"stroustrup"
00159   c-file-offsets:((innamespace . 0)(inline-open . 0))
00160   indent-tabs-mode:nil
00161   fill-column:79
00162   End:
00163 */
00164 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines