PLearn 0.1
VariablesTest.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // VariablesTest.cc
00004 //
00005 // Copyright (C) 2008 Olivier Delalleau
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: Olivier Delalleau
00036 
00040 #include "VariablesTest.h"
00041 #include <plearn/math/PRandom.h>
00042 #include <plearn/var/ArgminVariable.h>
00043 #include <plearn/var/Func.h>
00044 #include <plearn/var/LogAddVariable.h>
00045 #include <plearn/var/UnfoldedFuncVariable.h>
00046 
00047 namespace PLearn {
00048 using namespace std;
00049 
00050 PLEARN_IMPLEMENT_OBJECT(
00051     VariablesTest,
00052     "Tests various Variable objects.",
00053     "Feel free to add more Variable tests in there."
00054 );
00055 
00057 // VariablesTest //
00059 VariablesTest::VariablesTest()
00060 {
00061 }
00062 
00064 // build //
00066 void VariablesTest::build()
00067 {
00068     inherited::build();
00069     build_();
00070 }
00071 
00073 // makeDeepCopyFromShallowCopy //
00075 void VariablesTest::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00076 {
00077     inherited::makeDeepCopyFromShallowCopy(copies);
00078 
00079     // ### Call deepCopyField on all "pointer-like" fields
00080     // ### that you wish to be deepCopied rather than
00081     // ### shallow-copied.
00082     // ### ex:
00083     // deepCopyField(trainvec, copies);
00084 
00085     // ### Remove this line when you have fully implemented this method.
00086     PLERROR("VariablesTest::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
00087 }
00088 
00090 // declareOptions //
00092 void VariablesTest::declareOptions(OptionList& ol)
00093 {
00094     // ### Declare all of this object's options here.
00095     // ### For the "flags" of each option, you should typically specify
00096     // ### one of OptionBase::buildoption, OptionBase::learntoption or
00097     // ### OptionBase::tuningoption. If you don't provide one of these three,
00098     // ### this option will be ignored when loading values from a script.
00099     // ### You can also combine flags, for example with OptionBase::nosave:
00100     // ### (OptionBase::buildoption | OptionBase::nosave)
00101 
00102     declareOption(ol, "results_mat", &VariablesTest::results_mat,
00103                    OptionBase::buildoption,
00104         "Matrix test results.");
00105 
00106     // Now call the parent class' declareOptions
00107     inherited::declareOptions(ol);
00108 }
00109 
00111 // build_ //
00113 void VariablesTest::build_()
00114 {
00115     // ### This method should do the real building of the object,
00116     // ### according to set 'options', in *any* situation.
00117     // ### Typical situations include:
00118     // ###  - Initial building of an object from a few user-specified options
00119     // ###  - Building of a "reloaded" object: i.e. from the complete set of
00120     // ###    all serialised options.
00121     // ###  - Updating or "re-building" of an object after a few "tuning"
00122     // ###    options have been modified.
00123     // ### You should assume that the parent class' build_() has already been
00124     // ### called.
00125 }
00126 
00128 // perform //
00130 void VariablesTest::perform()
00131 {
00132     // Declare and initialize variables used in tests.
00133     int is1 = 5;
00134     int n_input2 = 3;
00135     int n_input5 = 4;
00136     Var input1(1, is1, "input1");
00137     Var input2(n_input2, is1, "input2");
00138     PRandom::common(false)->fill_random_uniform(input2->matValue, -1, 1);
00139     Var input3(input2->length(), input2->width(), "input3");
00140     PRandom::common(false)->fill_random_uniform(input3->matValue, -1, 1);
00141     Var input4(1, 1, "input4");
00142     input4->matValue(0, 0) = 3;
00143     Var input5(n_input5, is1, "input5");
00144     PRandom::common(false)->fill_random_uniform(input5->matValue, -1, 1);
00145 
00146     // Test UnfoldedFuncVariable to compute an argmin over a set of row vectors.
00147     Var argmin1 = argmin(input1);
00148     Func input1_to_argmin1(input1, argmin1);
00149     Var unfolded_argmin1 = new UnfoldedFuncVariable(input2, input1_to_argmin1,
00150                                                     false);
00151     unfolded_argmin1->fprop();
00152     results_mat["unfolded_argmin"] = unfolded_argmin1->matValue.copy();
00153     
00154     // Test LogAddVariable to compute a logadd over two input matrices.
00155     Var logadd1 = logadd(input2, input3);
00156     logadd1->fprop();
00157     results_mat["logadd_binary"] = logadd1->matValue.copy();
00158 
00159     // Test LogAddVariable to compute a logadd over sub-columns of its first
00160     // input.
00161     Var logadd2 = new LogAddVariable(input5, input4, "per_row");
00162     logadd2->fprop();
00163     results_mat["logadd_per_row"] = logadd2->matValue.copy();
00164     
00165     // Test LogAddVariable to compute a logadd over sub-rows of its first
00166     // input.
00167     Var logadd3 = new LogAddVariable(input5, input4, "per_column");
00168     logadd3->fprop();
00169     results_mat["logadd_per_column"] = logadd3->matValue.copy();
00170 }
00171 
00172 } // end of namespace PLearn
00173 
00174 
00175 /*
00176   Local Variables:
00177   mode:c++
00178   c-basic-offset:4
00179   c-file-style:"stroustrup"
00180   c-file-offsets:((innamespace . 0)(inline-open . 0))
00181   indent-tabs-mode:nil
00182   fill-column:79
00183   End:
00184 */
00185 // 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