PLearn 0.1
|
#include <OneHotVMatrix.h>
Public Member Functions | |
OneHotVMatrix (bool call_build_=false) | |
default constructor (for automatic deserialization) | |
OneHotVMatrix (VMat the_source, int the_nclasses, real the_cold_value=0.0, real the_host_value=1.0, int the_index=-1, bool call_build_=false) | |
(see special case when nclasses==1 desribed above) | |
virtual string | classname () const |
virtual OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual OneHotVMatrix * | deepCopy (CopiesMap &copies) const |
virtual void | build () |
Simply calls inherited::build() then build_(). | |
virtual void | reset_dimensions () |
In case the dimensions of an underlying VMat has changed, recompute it. | |
virtual real | dot (int i1, int i2, int inputsize) const |
Returns the dot product between row i1 and row i2 (considering only the inputsize first elements). | |
virtual real | dot (int i, const Vec &v) const |
Returns the result of the dot product between row i and the given vec (only v.length() first elements of row i are considered). | |
Static Public Member Functions | |
static string | _classname_ () |
OneHotVMatrix. | |
static OptionList & | _getOptionList_ () |
static RemoteMethodMap & | _getRemoteMethodMap_ () |
static Object * | _new_instance_for_typemap_ () |
static bool | _isa_ (const Object *o) |
static void | _static_initialize_ () |
static const PPath & | declaringFile () |
Public Attributes | |
int | nclasses |
real | cold_value |
real | hot_value |
int | index |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Protected Member Functions | |
virtual void | getNewRow (int i, const Vec &samplevec) const |
Must be implemented in subclasses: default version returns an error. | |
void | updateNClassesAndWidth () |
If 'nclasses' is greater than zero, do nothing. | |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declares this class' options. | |
Private Types | |
typedef SourceVMatrix | inherited |
Private Member Functions | |
void | build_ () |
This does the actual building. |
Definition at line 53 of file OneHotVMatrix.h.
typedef SourceVMatrix PLearn::OneHotVMatrix::inherited [private] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 55 of file OneHotVMatrix.h.
PLearn::OneHotVMatrix::OneHotVMatrix | ( | bool | call_build_ = false | ) |
default constructor (for automatic deserialization)
Definition at line 67 of file OneHotVMatrix.cc.
References build_().
: inherited(call_build_), nclasses(0), cold_value(0.0), hot_value(1.0), index(-1) { if( call_build_ ) build_(); }
PLearn::OneHotVMatrix::OneHotVMatrix | ( | VMat | the_source, |
int | the_nclasses, | ||
real | the_cold_value = 0.0 , |
||
real | the_host_value = 1.0 , |
||
int | the_index = -1 , |
||
bool | call_build_ = false |
||
) |
(see special case when nclasses==1 desribed above)
Definition at line 75 of file OneHotVMatrix.cc.
References build_().
: inherited(the_source, the_source->length(), the_source->width()+the_nclasses-1, call_build_), nclasses(the_nclasses), cold_value(the_cold_value), hot_value(the_hot_value), index(the_index) { if( call_build_ ) build_(); }
string PLearn::OneHotVMatrix::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file OneHotVMatrix.cc.
OptionList & PLearn::OneHotVMatrix::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file OneHotVMatrix.cc.
RemoteMethodMap & PLearn::OneHotVMatrix::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file OneHotVMatrix.cc.
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file OneHotVMatrix.cc.
Object * PLearn::OneHotVMatrix::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file OneHotVMatrix.cc.
StaticInitializer OneHotVMatrix::_static_initializer_ & PLearn::OneHotVMatrix::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file OneHotVMatrix.cc.
void PLearn::OneHotVMatrix::build | ( | ) | [virtual] |
Simply calls inherited::build() then build_().
Reimplemented from PLearn::SourceVMatrix.
Definition at line 94 of file OneHotVMatrix.cc.
References PLearn::SourceVMatrix::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::OneHotVMatrix::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::SourceVMatrix.
Definition at line 103 of file OneHotVMatrix.cc.
References PLearn::VMatrix::declareFieldNames(), i, index, PLearn::VMatrix::inputsize_, PLearn::VMat::length(), PLearn::VMatrix::length_, nclasses, PLearn::TVec< T >::resize(), PLearn::SourceVMatrix::setMetaInfoFromSource(), PLearn::SourceVMatrix::source, PLearn::TVec< T >::subVec(), PLearn::VMatrix::targetsize_, PLearn::tostring(), updateNClassesAndWidth(), PLearn::VMatrix::weightsize_, PLearn::VMatrix::width(), PLearn::VMat::width(), and PLearn::VMatrix::width_.
Referenced by build(), and OneHotVMatrix().
{ int source_inputsize = source->inputsize(); int source_targetsize = source->targetsize(); int source_weightsize = source->weightsize(); int source_width = source->width(); int source_length = source->length(); length_ = source_length; width_ = source_width + nclasses - 1; if(source_inputsize+source_targetsize+source_weightsize != source_width || source_targetsize != 1 ) // source->sizes are inconsistent { if( index < 0 ) { index = source_width - 1; updateNClassesAndWidth(); } if( inputsize_ + targetsize_ + weightsize_ != width() ) { // sizes are not set, or inconsistently inputsize_ = index; targetsize_ = nclasses; weightsize_ = width() - inputsize_ - targetsize_; } } else // source->sizes are consistent { if( index < 0 ) { index = source_inputsize; updateNClassesAndWidth(); } if( inputsize_ + targetsize_ + weightsize_ != width() ) { inputsize_ = source_inputsize; targetsize_ = source_targetsize; weightsize_ = source_weightsize; if( index < inputsize_ ) inputsize_ += nclasses-1; else if( index < inputsize_ + targetsize_ ) targetsize_ += nclasses-1; else weightsize_ += nclasses-1; } } TVec<string> fieldnames = source->fieldNames().copy(); string target_name = fieldnames[ index ]; fieldnames.resize( width() ); for( int i=0 ; i<nclasses ; i++ ) fieldnames[ index+i ] = target_name + "_" + tostring(i); fieldnames.subVec( index + nclasses, width() - index - nclasses ) << source->fieldNames().subVec( index + 1, source_width - index - 1 ); declareFieldNames( fieldnames ); setMetaInfoFromSource(); }
string PLearn::OneHotVMatrix::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file OneHotVMatrix.cc.
void PLearn::OneHotVMatrix::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares this class' options.
Reimplemented from PLearn::SourceVMatrix.
Definition at line 170 of file OneHotVMatrix.cc.
References PLearn::OptionBase::buildoption, cold_value, PLearn::declareOption(), PLearn::SourceVMatrix::declareOptions(), hot_value, index, PLearn::OptionBase::learntoption, nclasses, PLearn::OptionBase::nosave, and PLearn::SourceVMatrix::source.
{ declareOption(ol, "underlying_distr", &OneHotVMatrix::source, (OptionBase::learntoption | OptionBase::nosave), "DEPRECATED - use 'source' instead."); declareOption(ol, "nclasses", &OneHotVMatrix::nclasses, OptionBase::buildoption, "Number of classes. If set to zero, then this number will be\n" "automatically found from the source VMat."); declareOption(ol, "cold_value", &OneHotVMatrix::cold_value, OptionBase::buildoption, "Value used for non active elements in the one-hot vector."); declareOption(ol, "hot_value", &OneHotVMatrix::hot_value, OptionBase::buildoption, "Value used for the active element in the one-hot vector."); declareOption(ol, "index", &OneHotVMatrix::index, OptionBase::buildoption, "Index of the column on which we apply the one-hot transformation.\n" "By default, if targetsize==1 we take the target column, otherwise\n" "we take the last column."); inherited::declareOptions(ol); }
static const PPath& PLearn::OneHotVMatrix::declaringFile | ( | ) | [inline, static] |
OneHotVMatrix * PLearn::OneHotVMatrix::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file OneHotVMatrix.cc.
Returns the dot product between row i1 and row i2 (considering only the inputsize first elements).
The default version in VMatrix is somewhat inefficient, as it repeatedly calls get(i,j) The default version in RowBufferedVMatrix is a little better as it buffers the 2 Vecs between calls in case one of them is needed again. But the real strength of this method is for specialised and efficient versions in subbclasses. This method is typically used by SmartKernels so that they can compute kernel values between input samples efficiently.
Reimplemented from PLearn::RowBufferedVMatrix.
Definition at line 222 of file OneHotVMatrix.cc.
References PLearn::SourceVMatrix::source.
{ return source->dot(i1, i2, inputsize); }
Returns the result of the dot product between row i and the given vec (only v.length() first elements of row i are considered).
Reimplemented from PLearn::RowBufferedVMatrix.
Definition at line 227 of file OneHotVMatrix.cc.
References PLearn::SourceVMatrix::source.
{ return source->dot(i, v); }
Must be implemented in subclasses: default version returns an error.
Reimplemented from PLearn::SourceVMatrix.
Definition at line 201 of file OneHotVMatrix.cc.
References cold_value, PLearn::fill_one_hot(), PLearn::VMat::getSubRow(), hot_value, index, PLearn::left(), PLearn::TVec< T >::length(), PLearn::VMatrix::length(), nclasses, PLERROR, PLearn::right(), PLearn::SourceVMatrix::source, PLearn::TVec< T >::subVec(), and PLearn::VMatrix::width().
{ #ifdef BOUNDCHECK if(i<0 || i>=length()) PLERROR("In OneHotVMatrix::getNewRow OUT OF BOUNDS"); if(samplevec.length()!=width()) PLERROR("In OneHotVMatrix::getNewRow samplevec.length() must be\n" "equal to the VMat's width\n"); #endif Vec left = samplevec.subVec(0, index); Vec modified = samplevec.subVec(index, nclasses); Vec right = samplevec.subVec(index+nclasses, width()-index-nclasses); source->getSubRow(i, 0, left); int classnum = int(round(source->get(i, index))); fill_one_hot(modified, classnum, cold_value, hot_value); source->getSubRow(i, index+1, right); }
OptionList & PLearn::OneHotVMatrix::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file OneHotVMatrix.cc.
OptionMap & PLearn::OneHotVMatrix::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file OneHotVMatrix.cc.
RemoteMethodMap & PLearn::OneHotVMatrix::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file OneHotVMatrix.cc.
virtual void PLearn::OneHotVMatrix::reset_dimensions | ( | ) | [inline, virtual] |
In case the dimensions of an underlying VMat has changed, recompute it.
Reimplemented from PLearn::VMatrix.
Definition at line 95 of file OneHotVMatrix.h.
void PLearn::OneHotVMatrix::updateNClassesAndWidth | ( | ) | [protected] |
If 'nclasses' is greater than zero, do nothing.
Otherwise, obtain 'nclasses' by finding the maximum value of the source's 'index' column. Then update the width of this VMat accordingly.
Definition at line 236 of file OneHotVMatrix.cc.
References i, index, PLearn::VMat::length(), PLearn::max(), nclasses, PLASSERT, PLearn::SourceVMatrix::source, and PLearn::VMatrix::width_.
Referenced by build_().
{ if (nclasses > 0) return; PLASSERT( nclasses == 0 && index >= 0 ); real max = -1; for (int i = 0; i < source->length(); i++) { real val = source->get(i, index); if (val > max) max = val; } nclasses = int(round(max)) + 1; width_ += nclasses; }
Reimplemented from PLearn::SourceVMatrix.
Definition at line 78 of file OneHotVMatrix.h.
Definition at line 60 of file OneHotVMatrix.h.
Referenced by declareOptions(), and getNewRow().
Definition at line 61 of file OneHotVMatrix.h.
Referenced by declareOptions(), and getNewRow().
Definition at line 62 of file OneHotVMatrix.h.
Referenced by build_(), declareOptions(), getNewRow(), and updateNClassesAndWidth().
Definition at line 59 of file OneHotVMatrix.h.
Referenced by build_(), declareOptions(), getNewRow(), and updateNClassesAndWidth().