PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // VarUtilsTest.cc 00004 // 00005 // Copyright (C) 2005 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 /* ******************************************************* 00036 * $Id: .pyskeleton_header 544 2003-09-01 00:05:31Z plearner $ 00037 ******************************************************* */ 00038 00039 // Authors: Olivier Delalleau 00040 00044 #include "VarUtilsTest.h" 00045 #include <plearn/math/PRandom.h> 00046 #include <plearn/var/VarArray.h> 00047 #include <plearn/var/Var_utils.h> 00048 00049 namespace PLearn { 00050 using namespace std; 00051 00052 PLEARN_IMPLEMENT_OBJECT( 00053 VarUtilsTest, 00054 "Test various functions in Var_utils", 00055 "" 00056 ); 00057 00059 // VarUtilsTest // 00061 VarUtilsTest::VarUtilsTest() 00062 /* ### Initialize all fields to their default value */ 00063 { 00064 // ... 00065 00066 // ### You may (or not) want to call build_() to finish building the object 00067 // ### (doing so assumes the parent classes' build_() have been called too 00068 // ### in the parent classes' constructors, something that you must ensure) 00069 } 00070 00072 // build // 00074 void VarUtilsTest::build() 00075 { 00076 inherited::build(); 00077 build_(); 00078 } 00079 00081 // makeDeepCopyFromShallowCopy // 00083 void VarUtilsTest::makeDeepCopyFromShallowCopy(CopiesMap& copies) 00084 { 00085 inherited::makeDeepCopyFromShallowCopy(copies); 00086 00087 // ### Call deepCopyField on all "pointer-like" fields 00088 // ### that you wish to be deepCopied rather than 00089 // ### shallow-copied. 00090 // ### ex: 00091 // deepCopyField(trainvec, copies); 00092 00093 // ### Remove this line when you have fully implemented this method. 00094 PLERROR("VarUtilsTest::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); 00095 } 00096 00098 // declareOptions // 00100 void VarUtilsTest::declareOptions(OptionList& ol) 00101 { 00102 declareOption(ol, "mat_results", &VarUtilsTest::mat_results, 00103 OptionBase::buildoption, 00104 "Test Mat results."); 00105 00106 // Now call the parent class' declareOptions 00107 inherited::declareOptions(ol); 00108 } 00109 00111 // build_ // 00113 void VarUtilsTest::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 all serialised options. 00120 // ### - Updating or "re-building" of an object after a few "tuning" options have been modified. 00121 // ### You should assume that the parent class' build_() has already been called. 00122 } 00123 00125 // perform // 00127 void VarUtilsTest::perform() 00128 { 00129 int var_length = 10; 00130 int var_width = 5; 00131 int bound = 10; 00132 Var tmp; 00133 VarArray prop_path; 00134 Var mat_var(var_length, var_width); 00135 Var vec_var(var_length, 1); 00136 Var prob_mat_var(var_length, var_width); 00137 Var prob_vec_var(var_length, 1); 00138 Var scalar_var(1, 1); 00139 Var multi_index_var(var_width, 1); 00140 multi_index_var->matValue << "1 3 5 2 9"; 00141 Var single_index_var(1, 1); 00142 single_index_var->matValue(0, 0) = 3; 00143 PRandom::common(false)->fill_random_uniform(mat_var->matValue, -bound, bound); 00144 PRandom::common(false)->fill_random_uniform(vec_var->matValue, -bound, bound); 00145 PRandom::common(false)->fill_random_uniform(prob_mat_var->matValue, 0, 1); 00146 PRandom::common(false)->fill_random_uniform(prob_vec_var->matValue, 0, 1); 00147 PRandom::common(false)->fill_random_uniform(scalar_var->matValue, -bound, bound); 00148 00149 tmp = mean(mat_var); 00150 prop_path = propagationPath(tmp); 00151 prop_path.fprop(); 00152 mat_results["mean"] = tmp->matValue; 00153 00154 tmp = neg_log_pi(prob_vec_var, single_index_var); 00155 prop_path = propagationPath(tmp); 00156 prop_path.fprop(); 00157 mat_results["neg_log_pi_vec"] = tmp->matValue; 00158 00159 tmp = neg_log_pi(prob_mat_var, multi_index_var); 00160 prop_path = propagationPath(tmp); 00161 prop_path.fprop(); 00162 mat_results["neg_log_pi_mat"] = tmp->matValue; 00163 00164 tmp = softmax(prob_vec_var, 2); 00165 prop_path = propagationPath(tmp); 00166 prop_path.fprop(); 00167 mat_results["softmax"] = tmp->matValue; 00168 00169 tmp = pownorm(vec_var, 1.5); 00170 prop_path = propagationPath(tmp); 00171 prop_path.fprop(); 00172 mat_results["pownorm"] = tmp->matValue; 00173 00174 tmp = norm(vec_var, 1.5); 00175 prop_path = propagationPath(tmp); 00176 prop_path.fprop(); 00177 mat_results["norm"] = tmp->matValue; 00178 00179 tmp = entropy(vec_var); 00180 prop_path = propagationPath(tmp); 00181 prop_path.fprop(); 00182 mat_results["entropy"] = tmp->matValue; 00183 00184 tmp = distance(vec_var, prob_vec_var, 2); 00185 prop_path = propagationPath(tmp); 00186 prop_path.fprop(); 00187 mat_results["distance"] = tmp->matValue; 00188 00189 tmp = powdistance(vec_var, prob_vec_var, 2); 00190 prop_path = propagationPath(tmp); 00191 prop_path.fprop(); 00192 mat_results["powdistance"] = tmp->matValue; 00193 } 00194 00195 } // end of namespace PLearn 00196 00197 00198 /* 00199 Local Variables: 00200 mode:c++ 00201 c-basic-offset:4 00202 c-file-style:"stroustrup" 00203 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00204 indent-tabs-mode:nil 00205 fill-column:79 00206 End: 00207 */ 00208 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :