PLearn 0.1
BatchVMatrix.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 2003 Olivier Delalleau
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
00008 // 
00009 //  1. Redistributions of source code must retain the above copyright
00010 //     notice, this list of conditions and the following disclaimer.
00011 // 
00012 //  2. Redistributions in binary form must reproduce the above copyright
00013 //     notice, this list of conditions and the following disclaimer in the
00014 //     documentation and/or other materials provided with the distribution.
00015 // 
00016 //  3. The name of the authors may not be used to endorse or promote
00017 //     products derived from this software without specific prior written
00018 //     permission.
00019 // 
00020 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00021 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00022 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00023 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00025 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00026 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00027 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00028 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00029 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 // 
00031 // This file is part of the PLearn library. For more information on the PLearn
00032 // library, go to the PLearn Web site at www.plearn.org
00033 
00034 
00035 /* *******************************************************      
00036  * $Id: BatchVMatrix.cc 5176 2006-03-13 22:40:51Z lamblin $
00037  ******************************************************* */
00038 
00039 #include "BatchVMatrix.h"
00040 
00041 namespace PLearn {
00042 using namespace std;
00043 
00044 PLEARN_IMPLEMENT_OBJECT(
00045     BatchVMatrix,
00046     "Replicates small parts of a matrix into mini-batches",
00047     "VMat class that replicates small parts of a matrix (mini-batches), \n"
00048     "so that each mini-batch appears twice (consecutively).");
00049 
00050 BatchVMatrix::BatchVMatrix()
00051     : batch_size(0),
00052       last_batch(-1),
00053       last_batch_size(-1)
00054 { }
00055 
00056   
00058 // declareOptions //
00060 void BatchVMatrix::declareOptions(OptionList& ol)
00061 {
00062     declareOption(ol, "m", &BatchVMatrix::m, OptionBase::buildoption,
00063                   "The matrix viewed by the BatchVMatrix\n");
00064     declareOption(ol, "batch_size", &BatchVMatrix::batch_size, OptionBase::buildoption,
00065                   "The size of each mini-batch\n");
00066     inherited::declareOptions(ol);
00067 }
00068 
00070 // makeDeepCopyFromShallowCopy //
00072 void BatchVMatrix::makeDeepCopyFromShallowCopy(CopiesMap& copies)
00073 {
00074     inherited::makeDeepCopyFromShallowCopy(copies);
00075     deepCopyField(m, copies);
00076 }
00077 
00079 // build //
00081 void BatchVMatrix::build()
00082 {
00083     inherited::build();
00084     build_();
00085 }
00086 
00088 // build_ //
00090 void BatchVMatrix::build_()
00091 {
00092     if (m) {
00093         if (batch_size < 1)
00094             PLERROR("BatchVMatrix::build_: the 'batch_size' option must be nonnegative");
00095     
00096         width_ = m->width();
00097         length_ = m->length() * 2;
00098         fieldinfos = m->getFieldInfos();
00099         last_batch = (m->length()-1) / batch_size;
00100         last_batch_size = m->length() % batch_size;
00101         if (last_batch_size == 0)
00102             last_batch_size = batch_size;
00103     }
00104 }
00105 
00107 // get //
00109 real BatchVMatrix::get(int i, int j) const {
00110     int n_batch = i / (2 * batch_size);
00111     int k = batch_size;
00112     if (n_batch == last_batch) {
00113         // This is the last batch
00114         k = last_batch_size;
00115     }
00116     int i_ = n_batch * batch_size + (i - n_batch * 2 * batch_size) % k;
00117     return m->get(i_, j);
00118 }
00119 
00121 // put //
00123 void BatchVMatrix::put(int i, int j, real value) {
00124     int n_batch = i / (2 * batch_size);
00125     int i_ = n_batch * batch_size + (i - n_batch * 2 * batch_size) % batch_size;
00126     m->put(i_, j, value);
00127 }
00128 
00129 } // end of namespace PLearn
00130 
00131 
00132 /*
00133   Local Variables:
00134   mode:c++
00135   c-basic-offset:4
00136   c-file-style:"stroustrup"
00137   c-file-offsets:((innamespace . 0)(inline-open . 0))
00138   indent-tabs-mode:nil
00139   fill-column:79
00140   End:
00141 */
00142 // 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