PLearn 0.1
VerifyGradientCommand.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // VerifyGradientCommand.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 "VerifyGradientCommand.h"
00041 #include <plearn/var/SourceVariable.h>
00042 #include <plearn/var/Func.h>
00043 #include <plearn/math/PRandom.h>
00044 
00045 namespace PLearn {
00046 using namespace std;
00047 
00049 PLearnCommandRegistry VerifyGradientCommand::reg_(new VerifyGradientCommand);
00050 
00051 VerifyGradientCommand::VerifyGradientCommand()
00052     : PLearnCommand(
00053         "verify_gradient",
00054         "Verifies the gradient computation in a Variable through finite differences.",
00055         "verify_gradient 'MyVariable()' stepsize which_component input1_length input1_width input1_min input1_max input2_length input2_width input2_min input2_max \n"
00056         "The variable constructor can take option=value arguments if needed.\n"
00057         "stepsize is the size of the step taken for finite difference\n"
00058         "which_component specifies which element of MyVariable we feed gradients in (if MyVariable is not a scalar for ex.)\n"
00059         "if which_component is -1, then all elements will be fed gradient (same as considering gradient to the sum of all elements)\n"
00060         "input1_... and input2_... are required if the variable is a unary resp. binary variable.\n"
00061         "SourceVariables input1 and input2 of the specified size will be constructed \n"
00062         "and randomly initialized with a uniform between corresponding min and max.\n"
00063         )
00064 {}
00065 
00067 void VerifyGradientCommand::run(const vector<string>& args)
00068 {
00069     if(args.size()<2)
00070         PLERROR("Not enough arguments provided");
00071     string varspec = args[0];
00072     Object* obj = newObject(varspec);
00073     Variable* varp = dynamic_cast<Variable*>(obj);
00074     if(!varp)
00075         PLERROR("The object you specified does not seem to be a known Variable (is it spelled correctly, did your executable link with it?)");
00076     Var var = varp;
00077 
00078     double stepsize = todouble(args[1]);
00079     int which_component = toint(args[2]);
00080 
00081     unsigned int nparents = (args.size()-3)/4;
00082     if(3+nparents*4 != args.size())
00083         PLERROR("You must specify 4 numbers (length, width, min, max) for each of the parents variable");
00084 
00085     VarArray parents;
00086     for(unsigned int k=0; k<nparents; k++)
00087     {
00088         int l = toint(args[3+k*4]);
00089         int w = toint(args[3+k*4+1]);
00090         double mi = todouble(args[3+k*4+2]);
00091         double ma = todouble(args[3+k*4+3]);
00092         perr << "Params: " << l << " " << w << " " << mi << " " << ma << endl;
00093         SourceVariable* sourcevar = new SourceVariable(l, w, "uniform", mi, ma);
00094         sourcevar->randomInitialize(PRandom::common());
00095         Var inputk = sourcevar;
00096         parents.append(inputk);
00097     }
00098     var->setParents(parents);
00099     var->build();
00100 
00101     pout << "parents:" << *parents[0] << endl;
00102     Func f(parents, var);
00103     pout << "func inputs:" << *(f->inputs[0]) << endl;
00104 
00105     f->verifyGradient(stepsize, which_component);
00106 }
00107 
00108 } // end of namespace PLearn
00109 
00110 
00111 /*
00112   Local Variables:
00113   mode:c++
00114   c-basic-offset:4
00115   c-file-style:"stroustrup"
00116   c-file-offsets:((innamespace . 0)(inline-open . 0))
00117   indent-tabs-mode:nil
00118   fill-column:79
00119   End:
00120 */
00121 // 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