PLearn 0.1
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Types | Private Member Functions
PLearn::MultiTargetOneHotVMatrix Class Reference

#include <MultiTargetOneHotVMatrix.h>

Inheritance diagram for PLearn::MultiTargetOneHotVMatrix:
Inheritance graph
[legend]
Collaboration diagram for PLearn::MultiTargetOneHotVMatrix:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 MultiTargetOneHotVMatrix ()
 Default constructor.
virtual void build ()
 Simply calls inherited::build() then build_().
virtual void makeDeepCopyFromShallowCopy (CopiesMap &copies)
 Transforms a shallow copy into a deep copy.
virtual string classname () const
virtual OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual MultiTargetOneHotVMatrixdeepCopy (CopiesMap &copies) const
int getSourceInputSize ()
 Return 'source_inputsize'.

Static Public Member Functions

static string _classname_ ()
 Declares name and deepCopy methods.
static OptionList_getOptionList_ ()
static RemoteMethodMap_getRemoteMethodMap_ ()
static Object_new_instance_for_typemap_ ()
static bool _isa_ (const Object *o)
static void _static_initialize_ ()
static const PPathdeclaringFile ()

Public Attributes

real missing_target
bool reweight_targets
VMat source_target
VMat target_descriptor
string target_val
int verbosity
real hot_value
real cold_value
VMat source_and_target

Static Public Attributes

static StaticInitializer _static_initializer_

Protected Member Functions

virtual void getNewRow (int i, const Vec &v) const
 Fill the vector 'v' with the content of the i-th row.

Static Protected Member Functions

static void declareOptions (OptionList &ol)
 Declares this class' options.

Protected Attributes

int source_inputsize
 Equal to source->width() - source->weightsize().
Mat data
 A precomputed view of the data.

Private Types

typedef SourceVMatrix inherited

Private Member Functions

void build_ ()
 This does the actual building.

Detailed Description

Definition at line 52 of file MultiTargetOneHotVMatrix.h.


Member Typedef Documentation

Reimplemented from PLearn::SourceVMatrix.

Definition at line 57 of file MultiTargetOneHotVMatrix.h.


Constructor & Destructor Documentation

PLearn::MultiTargetOneHotVMatrix::MultiTargetOneHotVMatrix ( )

Default constructor.

Definition at line 53 of file MultiTargetOneHotVMatrix.cc.

: missing_target(-FLT_MAX),
  reweight_targets(false),
  target_val("specific_target"),
  verbosity(1),
  hot_value(1),
  cold_value(0)
{
}

Member Function Documentation

string PLearn::MultiTargetOneHotVMatrix::_classname_ ( ) [static]

Declares name and deepCopy methods.

Reimplemented from PLearn::SourceVMatrix.

Definition at line 76 of file MultiTargetOneHotVMatrix.cc.

OptionList & PLearn::MultiTargetOneHotVMatrix::_getOptionList_ ( ) [static]

Reimplemented from PLearn::SourceVMatrix.

Definition at line 76 of file MultiTargetOneHotVMatrix.cc.

RemoteMethodMap & PLearn::MultiTargetOneHotVMatrix::_getRemoteMethodMap_ ( ) [static]

Reimplemented from PLearn::SourceVMatrix.

Definition at line 76 of file MultiTargetOneHotVMatrix.cc.

bool PLearn::MultiTargetOneHotVMatrix::_isa_ ( const Object o) [static]

Reimplemented from PLearn::SourceVMatrix.

Definition at line 76 of file MultiTargetOneHotVMatrix.cc.

Object * PLearn::MultiTargetOneHotVMatrix::_new_instance_for_typemap_ ( ) [static]

Reimplemented from PLearn::SourceVMatrix.

Definition at line 76 of file MultiTargetOneHotVMatrix.cc.

StaticInitializer MultiTargetOneHotVMatrix::_static_initializer_ & PLearn::MultiTargetOneHotVMatrix::_static_initialize_ ( ) [static]

Reimplemented from PLearn::SourceVMatrix.

Definition at line 76 of file MultiTargetOneHotVMatrix.cc.

void PLearn::MultiTargetOneHotVMatrix::build ( ) [virtual]

Simply calls inherited::build() then build_().

Reimplemented from PLearn::SourceVMatrix.

Definition at line 128 of file MultiTargetOneHotVMatrix.cc.

References PLearn::SourceVMatrix::build(), and build_().

Referenced by PLearn::multi_target_one_hot().

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::MultiTargetOneHotVMatrix::build_ ( ) [private]

This does the actual building.

Reimplemented from PLearn::SourceVMatrix.

Definition at line 137 of file MultiTargetOneHotVMatrix.cc.

References PLearn::TVec< T >::append(), PLearn::TMat< T >::appendRow(), PLearn::argmax(), PLearn::TVec< T >::clear(), data, PLearn::endl(), PLearn::VMat::getExample(), i, PLearn::VMatrix::inputsize_, PLearn::is_missing(), j, PLearn::TMat< T >::length(), PLearn::TVec< T >::length(), PLearn::VMat::length(), PLearn::VMatrix::length_, PLearn::max(), missing_target, PLERROR, PLearn::TVec< T >::resize(), PLearn::TMat< T >::resize(), reweight_targets, PLearn::VMatrix::setFieldInfos(), PLearn::SourceVMatrix::source, source_and_target, source_inputsize, source_target, PLearn::TVec< T >::subVec(), target_descriptor, target_val, PLearn::VMatrix::targetsize_, PLearn::VMField::UnknownType, PLearn::VMatrix::updateMtime(), verbosity, PLearn::VMatrix::weightsize_, PLearn::VMat::width(), and PLearn::VMatrix::width_.

Referenced by build().

{
  // ### This method should do the real building of the object,
  // ### according to set 'options', in *any* situation. 
  // ### Typical situations include:
  // ###  - Initial building of an object from a few user-specified options
  // ###  - Building of a "reloaded" object: i.e. from the complete set of all serialised options.
  // ###  - Updating or "re-building" of an object after a few "tuning" options have been modified.
  // ### You should assume that the parent class' build_() has already been called.

  if (source && source_target) {
    updateMtime(source);
    updateMtime(source_target);
    updateMtime(target_descriptor);
    updateMtime(source_and_target);

    if (source->targetsize() > 0)
      PLERROR("In MultiTargetOneHotVMatrix::build_ - The source VMatrix must have a targetsize <= 0");
    if (source->length() != source_target->length())
      PLERROR("In MultiTargetOneHotVMatrix::build_ - 'source' and 'source_target' must have same length");
    // We need to browse through the data.
    PP<ProgressBar> pb;
    if (verbosity >= 1)
      pb = new ProgressBar("Building new data view", source->length());
//    int target_col = source->inputsize();
    Vec targets(source_target->width());
    int data_width = 3;
    if (target_descriptor)
      data_width++;
    if (reweight_targets)
      data_width++;
    data.resize(0, data_width);
    Vec data_row(data_width);
    int count = 0;
    TVec<int> count_targets;
    if (reweight_targets) {
      count_targets.resize(source_target->width());
      count_targets.clear();
    }
    for (int i = 0; i < source->length(); i++) {
      data_row[0] = i;
      source_target->getRow(i, targets);
      if (target_val == "max")
        data_row[2] = max(targets);
      for (int j = 0; j < targets.length(); j++) {
        data_row[1] = j;
        if (!is_missing(targets[j]) && targets[j] != missing_target) {
          count++;
          if (target_val == "specific_target")
            data_row[2] = targets[j];
          data.appendRow(data_row);
          if (reweight_targets)
            count_targets[j]++;
        }
      }
      if (pb)
        pb->update(i+1);
    }
    if (reweight_targets) {
      int max_target = argmax(count_targets);
      if (count_targets[max_target] == 0)
        PLERROR("In MultiTargetOneHotVMatrix::build_ - A target does not appear in the dataset, cannot reweight targets");
      Vec target_weights(count_targets.length());
      for (int i = 0; i < target_weights.length(); i++) {
        target_weights[i] = real(count_targets[max_target]) / real(count_targets[i]);
        if (verbosity >= 5)
          cout << "Weight for target " << i << ": " << target_weights[i]
               << " (count = " << count_targets[i] << ")" << endl;
      }
      for (int i = 0; i < data.length(); i++)
        data(i, data_width - 1) = target_weights[int(data(i, 1))];
    }
    source_inputsize = source->width();
    weightsize_ = 0;
    if (source->weightsize() > 0) {
      // There are weights.
      source_inputsize -= source->weightsize();
      weightsize_ = source->weightsize();
    }

    length_ = count;
    inputsize_ = source_inputsize + source_target->width();
    if (target_descriptor) {
      inputsize_ += target_descriptor->width() - 1;
      // Also build mapping from a target index to a row in 'target_descriptor'.
      int count_found = 0;
      TVec<int> map_target(source_target->width());
      for (int i = 0; i < target_descriptor->length(); i++) {
        string target_name = target_descriptor->getString(i, 0);
        int field_index = source_target->fieldIndex(target_name);
        if (field_index >= 0) {
          map_target[field_index] = i;
          count_found++;
        }
      }
      if (count_found != source_target->width())
        PLERROR("In MultiTargetOneHotVMatrix::build_ - Could not find all target descriptors in 'target_descriptor'");
      // Now fill 'data' accordingly.
      for (int i = 0; i < data.length(); i++)
        data(i, 3) = map_target[int(data(i, 1))];
    }
    targetsize_ = 1;
    width_ = inputsize_ + targetsize_ + weightsize_;
    // Add field names.
    TVec<VMField> source_infos = source->getFieldInfos();
    TVec<VMField> f_infos(source_infos.length());
    f_infos << source_infos;
    f_infos.resize(inputsize_);
    TVec<VMField> source_target_infos = source_target->getFieldInfos();
    f_infos.subVec(source_inputsize, source_target_infos.length()) << source_target_infos;
    if (target_descriptor) {
      TVec<VMField> target_des_infos = target_descriptor->getFieldInfos();
      f_infos.subVec(source_inputsize + source_target->width(), target_descriptor->width() - 1)
        << target_des_infos.subVec(1, target_des_infos.length() - 1);
    }
    f_infos.append(VMField(target_val, VMField::UnknownType));
    if (weightsize_ > 0) {
      f_infos.append(source_infos.subVec(
            source_infos.length() - source->weightsize(), source->weightsize()));
    }
    if (reweight_targets) {
      if (weightsize_ >= 0)
        weightsize_++;
      else
        weightsize_ = 1;
      f_infos.append(VMField("target_weight", VMField::UnknownType));
      width_++;
    }
    setFieldInfos(f_infos);
//    setMetaInfoFromSource();
  }
  else if(source_and_target)
  {
    // We need to browse through the data.
    PP<ProgressBar> pb;
    if (verbosity >= 1)
      pb = new ProgressBar("Building new data view", source_and_target->length());
    //    int target_col = source->inputsize();
    Vec targets(source_and_target->targetsize());
    real weight;
    Vec input(source_and_target->inputsize());
    int data_width = 3;
    if (target_descriptor)
      data_width++;
    if (reweight_targets)
      data_width++;
    data.resize(0, data_width);
    Vec data_row(data_width);
    int count = 0;
    TVec<int> count_targets;
    if (reweight_targets) {
      count_targets.resize(source_and_target->targetsize());
      count_targets.clear();
    }
    for (int i = 0; i < source_and_target->length(); i++) {
      data_row[0] = i;
      source_and_target->getExample(i, input,targets,weight);
      if (target_val == "max")
        data_row[2] = max(targets);
      for (int j = 0; j < targets.length(); j++) {
        data_row[1] = j;
        if (!is_missing(targets[j]) && targets[j] != missing_target) {
          count++;
          if (target_val == "specific_target")
            data_row[2] = targets[j];
          data.appendRow(data_row);
          if (reweight_targets)
            count_targets[j]++;
        }
      }
      if (pb)
        pb->update(i+1);
    }
    if (reweight_targets) {
      int max_target = argmax(count_targets);
      if (count_targets[max_target] == 0)
        PLERROR("In MultiTargetOneHotVMatrix::build_ - A target does not appear in the dataset, cannot reweight targets");
      Vec target_weights(count_targets.length());
      for (int i = 0; i < target_weights.length(); i++) {
        target_weights[i] = real(count_targets[max_target]) / real(count_targets[i]);
        if (verbosity >= 5)
          cout << "Weight for target " << i << ": " << target_weights[i]
               << " (count = " << count_targets[i] << ")" << endl;
      }
      for (int i = 0; i < data.length(); i++)
        data(i, data_width - 1) = target_weights[int(data(i, 1))];
    }
    source_inputsize = source_and_target->inputsize();
    weightsize_ = 0;
    if (source_and_target->weightsize() > 0) {
      // There are weights.
      //  source_inputsize -= source_and_target->weightsize();
      weightsize_ = source_and_target->weightsize();
    }

    length_ = count;
    inputsize_ = source_inputsize + source_and_target->targetsize();
    if (target_descriptor) {
      inputsize_ += target_descriptor->width() - 1;
      // Also build mapping from a target index to a row in 'target_descriptor'.
      int count_found = 0;
      TVec<int> map_target(source_and_target->targetsize());
      for (int i = 0; i < target_descriptor->length(); i++) {
        string target_name = target_descriptor->getString(i, 0);
        int field_index = source_and_target->fieldIndex(target_name) - source_and_target->inputsize();
        if (field_index >= 0) {
          map_target[field_index] = i;
          count_found++;
        }
      }
      if (count_found != source_and_target->targetsize())
        PLERROR("In MultiTargetOneHotVMatrix::build_ - Could not find all target descriptors in 'target_descriptor'");
      // Now fill 'data' accordingly.
      for (int i = 0; i < data.length(); i++)
        data(i, 3) = map_target[int(data(i, 1))];
    }
    targetsize_ = 1;
    width_ = inputsize_ + targetsize_ + weightsize_;
    // Add field names.
    TVec<VMField> source_infos = source_and_target->getFieldInfos();
    TVec<VMField> f_infos(source_infos.length());
    f_infos << source_infos;
    f_infos.resize(inputsize_);
    //TVec<VMField> source_target_infos = source_target->getFieldInfos();
    //f_infos.subVec(source_inputsize, source_target_infos.length()) << source_target_infos;
    if (target_descriptor) {
      TVec<VMField> target_des_infos = target_descriptor->getFieldInfos();
      f_infos.subVec(source_inputsize + source_and_target->targetsize(), target_descriptor->width() - 1)
        << target_des_infos.subVec(1, target_des_infos.length() - 1);
    }
    f_infos.append(VMField(target_val, VMField::UnknownType));
    if (weightsize_ > 0) {
      f_infos.append(source_infos.subVec(
            source_infos.length() - source_and_target->weightsize(), source_and_target->weightsize()));
    }
    if (reweight_targets) {
      if (weightsize_ >= 0)
        weightsize_++;
      else
        weightsize_ = 1;
      f_infos.append(VMField("target_weight", VMField::UnknownType));
      width_++;
    }
    setFieldInfos(f_infos);
//    setMetaInfoFromSource();
  }
    
  
}

Here is the call graph for this function:

Here is the caller graph for this function:

string PLearn::MultiTargetOneHotVMatrix::classname ( ) const [virtual]

Reimplemented from PLearn::SourceVMatrix.

Definition at line 76 of file MultiTargetOneHotVMatrix.cc.

void PLearn::MultiTargetOneHotVMatrix::declareOptions ( OptionList ol) [static, protected]

Declares this class' options.

Reimplemented from PLearn::SourceVMatrix.

Definition at line 81 of file MultiTargetOneHotVMatrix.cc.

References PLearn::OptionBase::buildoption, cold_value, PLearn::declareOption(), PLearn::SourceVMatrix::declareOptions(), hot_value, missing_target, reweight_targets, source_and_target, source_target, target_descriptor, target_val, and verbosity.

{
  // ### Declare all of this object's options here
  // ### For the "flags" of each option, you should typically specify  
  // ### one of OptionBase::buildoption, OptionBase::learntoption or 
  // ### OptionBase::tuningoption. Another possible flag to be combined with
  // ### is OptionBase::nosave

  declareOption(ol, "source_target", &MultiTargetOneHotVMatrix::source_target, OptionBase::buildoption,
      "The VMat containing the target information for each sample of 'source'.");

  declareOption(ol, "target_descriptor", &MultiTargetOneHotVMatrix::target_descriptor, OptionBase::buildoption,
      "The VMat containing the additional target descriptors (optional).");

  declareOption(ol, "missing_target", &MultiTargetOneHotVMatrix::missing_target, OptionBase::buildoption,
      "A target in the source VMatrix equal to this value will be treated as missing.");

  declareOption(ol, "reweight_targets", &MultiTargetOneHotVMatrix::reweight_targets, OptionBase::buildoption,
      "If set to 1, a weight column will be appended, with a weight of 1 for the most\n"
      "frequent target, and > 1 for other targets (=> equal weight to each target).");

  declareOption(ol, "target_val", &MultiTargetOneHotVMatrix::target_val, OptionBase::buildoption,
      "How the target value is computed:\n"
      " - 'max'             : the maximum of all targets\n"
      " - 'specific_target' : the value of the target considered");

  declareOption(ol, "verbosity", &MultiTargetOneHotVMatrix::verbosity, OptionBase::buildoption,
      "Controls the amount of output.");

  declareOption(ol, "hot_value", &MultiTargetOneHotVMatrix::hot_value, OptionBase::buildoption,
      "Hot value in one-hot representation. If it is the same as the cold_value,\n"
      "then the actual target value, as defined by the target_val option, is used.");

  declareOption(ol, "cold_value", &MultiTargetOneHotVMatrix::cold_value, OptionBase::buildoption,
      "Cold value in one-hot representation.");

  declareOption(ol, "source_and_target", &MultiTargetOneHotVMatrix::source_and_target, OptionBase::buildoption,
      "The VMat containing both the source (input) and target information.");


  // Now call the parent class' declareOptions
  inherited::declareOptions(ol);
}

Here is the call graph for this function:

static const PPath& PLearn::MultiTargetOneHotVMatrix::declaringFile ( ) [inline, static]

Reimplemented from PLearn::SourceVMatrix.

Definition at line 127 of file MultiTargetOneHotVMatrix.h.

{ return source_inputsize; }
MultiTargetOneHotVMatrix * PLearn::MultiTargetOneHotVMatrix::deepCopy ( CopiesMap copies) const [virtual]

Reimplemented from PLearn::SourceVMatrix.

Definition at line 76 of file MultiTargetOneHotVMatrix.cc.

void PLearn::MultiTargetOneHotVMatrix::getNewRow ( int  i,
const Vec v 
) const [protected, virtual]

Fill the vector 'v' with the content of the i-th row.

v is assumed to be the right size.

Reimplemented from PLearn::SourceVMatrix.

Definition at line 390 of file MultiTargetOneHotVMatrix.cc.

References cold_value, data, PLearn::TVec< T >::fill(), PLearn::VMat::getSubRow(), hot_value, PLearn::VMatrix::inputsize_, PLearn::is_missing(), PLearn::TVec< T >::length(), PLASSERT, reweight_targets, PLearn::SourceVMatrix::source, source_and_target, source_inputsize, source_target, PLearn::TVec< T >::subVec(), target_descriptor, PLearn::VMatrix::targetsize_, PLearn::VMatrix::weightsize_, and PLearn::VMat::width().

{
  PLASSERT( (source && source_target) || source_and_target);
  int is = source_inputsize;
  int ts;
  if(source_and_target)
  {
    ts = source_and_target->targetsize();
    // Get the input part.
    source_and_target->getSubRow(int(data(i,0)), 0, v.subVec(0, is));
  }
  else
  {
    ts = source_target->width();
    // Get the input part.
    source->getSubRow(int(data(i,0)), 0, v.subVec(0, is));
  }
  // Fill the one-hot vector.
  v.subVec(is, ts).fill(cold_value);
  if(hot_value == cold_value || (is_missing(cold_value) && is_missing(hot_value)))
    v[is + int(data(i, 1))] = data(i,2);
  else
    v[is + int(data(i, 1))] = hot_value;
  if (target_descriptor)
    // Fill the target descriptor.
    target_descriptor->getSubRow(int(data(i, 3)), 1,
                                 v.subVec(is + ts, target_descriptor->width() - 1));
  // Fill the target.
  v[inputsize_] = data(i, 2);
  // Fill the (potential) weight.
  if (weightsize_ > 0)
  {
    if(source_and_target)
      source_and_target->getSubRow(int(data(i, 0)), source_and_target->width()-source_and_target->weightsize(), v.subVec(inputsize_ + targetsize_, source_and_target->weightsize()));
    else
      source->getSubRow(int(data(i, 0)), is, v.subVec(inputsize_ + targetsize_, source->weightsize()));
  }
  if (reweight_targets)
    v[v.length() - 1] = data(i, 3);
}

Here is the call graph for this function:

OptionList & PLearn::MultiTargetOneHotVMatrix::getOptionList ( ) const [virtual]

Reimplemented from PLearn::SourceVMatrix.

Definition at line 76 of file MultiTargetOneHotVMatrix.cc.

OptionMap & PLearn::MultiTargetOneHotVMatrix::getOptionMap ( ) const [virtual]

Reimplemented from PLearn::SourceVMatrix.

Definition at line 76 of file MultiTargetOneHotVMatrix.cc.

RemoteMethodMap & PLearn::MultiTargetOneHotVMatrix::getRemoteMethodMap ( ) const [virtual]

Reimplemented from PLearn::SourceVMatrix.

Definition at line 76 of file MultiTargetOneHotVMatrix.cc.

int PLearn::MultiTargetOneHotVMatrix::getSourceInputSize ( ) [inline]

Return 'source_inputsize'.

Definition at line 130 of file MultiTargetOneHotVMatrix.h.

{ return source_inputsize; }
void PLearn::MultiTargetOneHotVMatrix::makeDeepCopyFromShallowCopy ( CopiesMap copies) [virtual]

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::SourceVMatrix.

Definition at line 434 of file MultiTargetOneHotVMatrix.cc.

References data, PLearn::deepCopyField(), PLearn::SourceVMatrix::makeDeepCopyFromShallowCopy(), source_and_target, source_target, and target_descriptor.

Here is the call graph for this function:


Member Data Documentation

Reimplemented from PLearn::SourceVMatrix.

Definition at line 127 of file MultiTargetOneHotVMatrix.h.

A precomputed view of the data.

The i-th row contains 3 (or 4) numbers: the index of the source row for the i-row, the index of the target considered (the one-hot cell to fill), and the value of the target we output in this VMat. Additionally, if 'reweight_targets' is set to 1, a 4-th number is added: the weight of the sample.

Definition at line 75 of file MultiTargetOneHotVMatrix.h.

Referenced by build_(), getNewRow(), and makeDeepCopyFromShallowCopy().

Definition at line 83 of file MultiTargetOneHotVMatrix.h.

Referenced by build_(), and declareOptions().

Definition at line 84 of file MultiTargetOneHotVMatrix.h.

Referenced by build_(), declareOptions(), and getNewRow().

Equal to source->width() - source->weightsize().

Definition at line 62 of file MultiTargetOneHotVMatrix.h.

Referenced by build_(), and getNewRow().

Definition at line 87 of file MultiTargetOneHotVMatrix.h.

Referenced by build_(), and declareOptions().

Definition at line 88 of file MultiTargetOneHotVMatrix.h.

Referenced by build_(), and declareOptions().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines