PLearn 0.1
CompactVMatrix.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 1998 Pascal Vincent
00005 // Copyright (C) 1999-2002 Pascal Vincent, Yoshua Bengio and University of Montreal
00006 //
00007 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 //
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 //
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 //
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 //
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 //
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 
00037 
00038 
00039 /* *******************************************************
00040  * $Id: CompactVMatrix.h 8239 2007-11-09 15:35:34Z nouiz $
00041  * This file is part of the PLearn library.
00042  ******************************************************* */
00043 
00044 
00047 #ifndef CompactCompactVMatrix_INC
00048 #define CompactCompactVMatrix_INC
00049 
00050 #include "RowBufferedVMatrix.h"
00051 #include "VMat.h"
00052 
00053 namespace PLearn {
00054 using namespace std;
00055 
00063 class CompactVMatrix : public RowBufferedVMatrix
00064 {
00065     typedef RowBufferedVMatrix inherited;
00066     class SDBVMatrix;
00067 
00068 protected:
00069 
00071     Storage<unsigned char> data;
00072     int row_n_bytes; 
00073     int n_bits; 
00074     int n_symbols; 
00075     int n_fixedpoint; 
00076     int n_variables; 
00077     bool one_hot_encoding; 
00078 public:
00079     TVec<int> n_symbol_values; 
00080     int nbits() { return n_bits; }
00081     int nsymbols() { return n_symbols; }
00082     int nfixedpoint() { return n_fixedpoint; }
00083 protected:
00084     Vec fixedpoint_min, fixedpoint_max; 
00085     Vec delta; 
00086     TVec<int> variables_permutation; 
00087 
00091     static unsigned char n_bits_in_byte[256];
00092     static void set_n_bits_in_byte();
00093 
00094 public:
00095     int n_last; 
00096 protected:
00097     int normal_width;         
00098 public:
00099     void setOneHotMode(bool on=true);
00100 
00101     // ******************
00102     // *  Constructors  *
00103     // ******************
00104     CompactVMatrix(); 
00105 
00106     CompactVMatrix(int the_length, int n_variables, int n_binary, int n_nonbinary_discrete,
00107                    int n_fixed_point, TVec<int>& n_symbolvalues, Vec& fixed_point_min,
00108                    Vec& fixed_point_max, bool one_hot_encoding=true);
00109 
00122     CompactVMatrix(VMat m, int keep_last_variables_last=1, bool onehot_encoding=true);
00123 
00125 
00126     CompactVMatrix(const string& filename, int nlast=1);
00127 
00134     CompactVMatrix(CompactVMatrix* cvm, VMat m, bool rescale=false, bool check=true);
00135 
00137     void append(CompactVMatrix* vm);
00138 
00150     void perturb(int i, Vec row, real noise_level, int n_last);
00151 
00156     TVec<int>& permutation_vector() { return variables_permutation; }
00157 
00158 protected:
00159 
00161     virtual void getNewRow(int i, const Vec& v) const;
00162 
00163 public:
00164 
00166 
00168     virtual real squareDifference(int i, int j);
00169 
00171     virtual real dotProduct(int i, int j) const;
00172 
00173     virtual real dot(int i1, int i2, int inputsize) const;
00174     virtual real dot(int i, const Vec& v) const;
00175 
00176 
00178     virtual void encodeAndPutRow(int i, Vec v);
00181     virtual void putRow(int i, Vec v);
00182     virtual void putSubRow(int i, int j, Vec v);
00183 
00185     virtual void save(const PPath& filename) const
00186     { Object::save(filename); } 
00187     //virtual void write(ostream& out) const;
00188 
00190     //virtual void oldread(istream& in);
00191 
00192     PLEARN_DECLARE_OBJECT(CompactVMatrix);
00193     void makeDeepCopyFromShallowCopy(CopiesMap& copies);
00194     virtual void build() {}  
00195 
00196 protected:
00198     int symbols_offset; 
00199     int fixedpoint_offset; 
00200     Vec row_norms; 
00201 };
00202 
00203 DECLARE_OBJECT_PTR(CompactVMatrix);
00204 
00205 } // end of namespace PLearn
00206 #endif
00207 
00208 
00209 /*
00210   Local Variables:
00211   mode:c++
00212   c-basic-offset:4
00213   c-file-style:"stroustrup"
00214   c-file-offsets:((innamespace . 0)(inline-open . 0))
00215   indent-tabs-mode:nil
00216   fill-column:79
00217   End:
00218 */
00219 // 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