PLearn 0.1
StochasticBinarizeVMatrix.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // StochasticBinarizeVMatrix.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 "StochasticBinarizeVMatrix.h"
00041 #include <plearn/vmat/ShiftAndRescaleVMatrix.h>
00042 
00043 namespace PLearn {
00044 using namespace std;
00045 
00046 PLEARN_IMPLEMENT_OBJECT(
00047     StochasticBinarizeVMatrix,
00048     "Transform its source data into stochastically sampled binary data.",
00049     "Each column of the source data is first rescaled into the [0, 1] range,\n"
00050     "then when accessing a sample, each variable is taken to be 1 with\n"
00051     "probability given by its real value (and 0 otherwise). Constant columns\n"
00052     "are given a uniform distribution in {0, 1}.\n"
00053     "Since sampling is performed every time a sample is accessed, one should\n"
00054     "precompute the data if a constant dataset is desired.\n"
00055     "In the current implementation, only the input part is binarized."
00056 );
00057 
00059 // StochasticBinarizeVMatrix //
00061 StochasticBinarizeVMatrix::StochasticBinarizeVMatrix():
00062     rescale_to_0_1(true),
00063     seed(1827),
00064     random_gen(new PRandom())
00065 {
00066 }
00067 
00069 // declareOptions //
00071 void StochasticBinarizeVMatrix::declareOptions(OptionList& ol)
00072 {
00073     declareOption(ol, "rescale_to_0_1",
00074                   &StochasticBinarizeVMatrix::rescale_to_0_1,
00075                   OptionBase::buildoption,
00076         "Whether to rescale to [0,1] before sampling. If set to False, then\n"
00077         "the data is assumed to already be in the [0,1] range (no check will\n"
00078         "be performed to enforce it, though).");
00079 
00080     declareOption(ol, "seed", &StochasticBinarizeVMatrix::seed,
00081                   OptionBase::buildoption,
00082         "Seed of random number generator.");
00083 
00084     // Now call the parent class' declareOptions
00085     inherited::declareOptions(ol);
00086 }
00087 
00089 // build //
00091 void StochasticBinarizeVMatrix::build()
00092 {
00093     inherited::build();
00094     build_();
00095 }
00096 
00098 // build_ //
00100 void StochasticBinarizeVMatrix::build_()
00101 {
00102     if (source) {
00103         if (rescale_to_0_1) {
00104             PP<ShiftAndRescaleVMatrix> rd =
00105                 new ShiftAndRescaleVMatrix(source, false);
00106             rd->min_max = Vec(2);
00107             rd->min_max[1] = 1;
00108             rd->build();
00109             rescaled_data = get_pointer(rd);
00110         } else
00111             rescaled_data = source;
00112         setMetaInfoFromSource();
00113     }
00114     random_gen->manual_seed(seed);
00115 }
00116 
00118 // getNewRow //
00120 void StochasticBinarizeVMatrix::getNewRow(int i, const Vec& v) const
00121 {
00122     rescaled_data->getRow(i, v);
00123     for (int j = 0; j < source->inputsize(); j++)
00124         v[j] = random_gen->uniform_sample() <= v[j] ? 1 : 0;
00125 }
00126 
00128 // makeDeepCopyFromShallowCopy //
00130 void StochasticBinarizeVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00131 {
00132     inherited::makeDeepCopyFromShallowCopy(copies);
00133 
00134     // ### Call deepCopyField on all "pointer-like" fields
00135     // ### that you wish to be deepCopied rather than
00136     // ### shallow-copied.
00137     // ### ex:
00138     // deepCopyField(trainvec, copies);
00139 
00140     // ### Remove this line when you have fully implemented this method.
00141     PLERROR("StochasticBinarizeVMatrix::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
00142 }
00143 
00144 } // end of namespace PLearn
00145 
00146 
00147 /*
00148   Local Variables:
00149   mode:c++
00150   c-basic-offset:4
00151   c-file-style:"stroustrup"
00152   c-file-offsets:((innamespace . 0)(inline-open . 0))
00153   indent-tabs-mode:nil
00154   fill-column:79
00155   End:
00156 */
00157 // 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