PLearn 0.1
SumVarianceOfLinearTransformedCategoricals.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // SumVarianceOfLinearTransformedCategoricals.cc
00004 //
00005 // Copyright (C) 2009 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 "SumVarianceOfLinearTransformedCategoricals.h"
00041 
00042 namespace PLearn {
00043 using namespace std;
00044 
00047 PLEARN_IMPLEMENT_OBJECT(
00048     SumVarianceOfLinearTransformedCategoricals,
00049     "Computes the sum of the variances of the elements of a linear transformation of a concatenation of independent random variables following a categorical distribution.",
00050     "By categorical distribution we mean multinomials with the number-of-trials parameter set to n=1.\n"
00051     "Let P=inpu1 a (l,d') matrix. Each row contains the parameters of groupsize.length() categoricals.\n"
00052     "  ( the sum of the groupsize elements equals d').\n"
00053     "Let H ~ MultipleCategorical(P) a (l,d') corresponding random variable, \n"
00054     "  this correponds to drawings from independent categorical variables whose probablity parameters are those in P \n"
00055     "Let W=input2 a (d,d') linear transformation matrix. \n"
00056     "Let X=H W^t a (l,d) random variable matrix corresponding to applying the transformation. \n"
00057     "  i.e. X_i = sum_j H_ij W^t_j \n" 
00058     "SumVarianceOfLinearTransformedCategoricals computes the sum of the variances of the elements of X.\n"
00059     "i.e. sum_ij Var[X_ij] \n"
00060     );
00061 
00062 SumVarianceOfLinearTransformedCategoricals::SumVarianceOfLinearTransformedCategoricals()
00063     : inherited(0,0,1,1)
00064 {}
00065 
00066 SumVarianceOfLinearTransformedCategoricals::SumVarianceOfLinearTransformedCategoricals(Variable* input1, Variable* input2,
00067                            bool call_build_)
00068     : inherited(input1, input2, 1, 1, call_build_)
00069 {
00070     if (call_build_)
00071         build_();
00072 }
00073 
00074 // constructor from input variable and parameters
00075 // SumVarianceOfLinearTransformedCategoricals::SumVarianceOfLinearTransformedCategoricals(Variable* input1, Variable* input2,
00076 //                            param_type the_parameter, ...,
00077 //                            bool call_build_)
00078 // ### replace with actual parameters
00079 //  : inherited(input1, input2, this_variable_length, this_variable_width,
00080 //              call_build_),
00081 //    parameter(the_parameter),
00082 //    ...
00083 //{
00084 //    if (call_build_)
00085 //        build_();
00086 //}
00087 
00088 void SumVarianceOfLinearTransformedCategoricals::recomputeSize(int& l, int& w) const
00089 {
00090     l = 1;
00091     w = 1;
00092 }
00093 
00094 // ### computes value from input1 and input2 values
00095 void SumVarianceOfLinearTransformedCategoricals::fprop()
00096 {
00097     if(input1.width()!=input2.width())
00098         PLERROR("Incompatible sizes: width of P (input1) must equal width of W (input2)"); 
00099 
00100     Mat P = input1->matValue;
00101     Mat W = input2->matValue;
00102     int d = W.length();
00103     int l = P.length();
00104     // int m = W.width(); // should equal sum of groupsizes
00105 
00106     double simplesum = 0;
00107     double sqsum = 0;
00108 
00109     int ngroups = groupsizes.length();
00110     const int* pgroupsize = groupsizes.data();
00111 
00112     for(int t=0; t<l; t++)
00113     {
00114         const real* p = P[t];
00115         for(int i=0; i<d; i++)
00116         {
00117             const real* Wi = W[i];
00118             int k = 0;
00119             for(int groupnum=0; groupnum<ngroups; groupnum++)
00120             {
00121                 double tmpsqsum = 0;
00122                 int gs = pgroupsize[groupnum];
00123                 while(gs--)
00124                 {
00125                     real Wik = Wi[k];
00126                     real pk = p[k];
00127                     real Wik_pk = Wik*pk;
00128                     simplesum += Wik*Wik_pk;
00129                     tmpsqsum += Wik_pk;
00130                     k++;
00131                 }
00132                 sqsum += tmpsqsum*tmpsqsum;
00133             }
00134         }
00135     }
00136     value[0] = simplesum-sqsum;
00137 }
00138 
00139 // ### computes input1 and input2 gradients from gradient
00140 void SumVarianceOfLinearTransformedCategoricals::bprop()
00141 {
00142     Mat P = input1->matValue;
00143     Mat Pgrad = input1->matGradient;
00144     Mat W = input2->matValue;
00145     Mat Wgrad = input2->matGradient;
00146     int d = W.length();
00147     int l = P.length();
00148     // int m = W.width(); // should equal sum of groupsizes
00149 
00150     real gr = gradient[0];
00151 
00152     int ngroups = groupsizes.length();
00153     const int* pgroupsize = groupsizes.data();    
00154 
00155     group_sum_wik_pk.resize(ngroups);
00156     real* p_group_sum_wik_pk = group_sum_wik_pk.data();
00157 
00158     for(int t=0; t<l; t++)
00159     {
00160         const real* p = P[t];
00161         real* gp = Pgrad[t];
00162         for(int i=0; i<d; i++)
00163         {
00164             const real* Wi = W[i];
00165             int k = 0;
00166             for(int groupnum=0; groupnum<ngroups; groupnum++)
00167             {
00168                 int gs = pgroupsize[groupnum];
00169                 double sum_wik_pk = 0;
00170                 while(gs--)
00171                 {
00172                     sum_wik_pk += Wi[k]*p[k];
00173                     k++;
00174                 }
00175                 p_group_sum_wik_pk[groupnum] = sum_wik_pk;
00176             }
00177                     
00178             real* gWi = Wgrad[i];
00179             k = 0;
00180             for(int groupnum=0; groupnum<ngroups; groupnum++)
00181             {
00182                 int gs = pgroupsize[groupnum];
00183                 real grsum = p_group_sum_wik_pk[groupnum];
00184                 while(gs--)
00185                 {
00186                     real Wik = Wi[k];
00187                     gWi[k] += gr*2*p[k]*(Wik-grsum);
00188                     gp[k] += gr*Wik*(Wik-2*grsum);
00189                     k++;
00190                 }
00191             }
00192         }
00193     }
00194 
00195 }
00196 
00197 // ### You can implement these methods:
00198 // void SumVarianceOfLinearTransformedCategoricals::bbprop() {}
00199 // void SumVarianceOfLinearTransformedCategoricals::symbolicBprop() {}
00200 // void SumVarianceOfLinearTransformedCategoricals::rfprop() {}
00201 
00202 
00203 // ### Nothing to add here, simply calls build_
00204 void SumVarianceOfLinearTransformedCategoricals::build()
00205 {
00206     inherited::build();
00207     build_();
00208 }
00209 
00210 void SumVarianceOfLinearTransformedCategoricals::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00211 {
00212     inherited::makeDeepCopyFromShallowCopy(copies);
00213 
00214     // ### Call deepCopyField on all "pointer-like" fields
00215     // ### that you wish to be deepCopied rather than
00216     // ### shallow-copied.
00217     // ### ex:
00218     // deepCopyField(trainvec, copies);
00219     deepCopyField(groupsizes, copies);
00220     deepCopyField(group_sum_wik_pk, copies);
00221 
00222     // ### If you want to deepCopy a Var field:
00223     // varDeepCopyField(somevariable, copies);
00224 }
00225 
00226 void SumVarianceOfLinearTransformedCategoricals::declareOptions(OptionList& ol)
00227 {
00228     // ### Declare all of this object's options here.
00229     // ### For the "flags" of each option, you should typically specify
00230     // ### one of OptionBase::buildoption, OptionBase::learntoption or
00231     // ### OptionBase::tuningoption. If you don't provide one of these three,
00232     // ### this option will be ignored when loading values from a script.
00233     // ### You can also combine flags, for example with OptionBase::nosave:
00234     // ### (OptionBase::buildoption | OptionBase::nosave)
00235 
00236     declareOption(ol, "groupsizes", &SumVarianceOfLinearTransformedCategoricals::groupsizes,
00237                   OptionBase::buildoption,
00238                   "defines the dimensions of the categorical variables.");
00239 
00240     // Now call the parent class' declareOptions
00241     inherited::declareOptions(ol);
00242 }
00243 
00244 void SumVarianceOfLinearTransformedCategoricals::build_()
00245 {
00246     // ### This method should do the real building of the object,
00247     // ### according to set 'options', in *any* situation.
00248     // ### Typical situations include:
00249     // ###  - Initial building of an object from a few user-specified options
00250     // ###  - Building of a "reloaded" object: i.e. from the complete set of
00251     // ###    all serialised options.
00252     // ###  - Updating or "re-building" of an object after a few "tuning"
00253     // ###    options have been modified.
00254     // ### You should assume that the parent class' build_() has already been
00255     // ### called.
00256 }
00257 
00258 
00259 } // end of namespace PLearn
00260 
00261 
00262 /*
00263   Local Variables:
00264   mode:c++
00265   c-basic-offset:4
00266   c-file-style:"stroustrup"
00267   c-file-offsets:((innamespace . 0)(inline-open . 0))
00268   indent-tabs-mode:nil
00269   fill-column:79
00270   End:
00271 */
00272 // 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