|
PLearn 0.1
|
#include <SDBVMat.h>


Public Member Functions | |
| SDBVMOutputCoder (SDBVMOutputCoding oc=SDBVMNumeric, real missing_values_mapping=MISSING_VALUE) | |
| virtual | ~SDBVMOutputCoder () |
| virtual SDBVMOutputCoding | getOutputCoding () const |
| Return current output coding in effect. | |
| virtual void | setNumClasses (int n_classes) |
| Specify the number of classes if something like one-hot coding is desired. | |
| virtual int | getNumClasses () const |
| Return the current number of classes; this can be zero if only straight numeric coding is desired. | |
| virtual void | setMissingValuesMapping (real missing_values_mapping) |
| Specify a mapping for missing values passed to setOutput. | |
| virtual real | getMissingValuesMapping () const |
| Return current missing values mapping. | |
| virtual int | fieldWidth () const |
| Return the field width required by the selected coding and the number of classes. | |
| virtual void | setOutput (real output_value, const Vec &output_field) const |
| Code the given real value into the vector; obviously, if one-hot-like coding is desired, a reasonable integer should be passed. | |
Static Public Member Functions | |
| template<class Mapping > | |
| static int | getNumClasses (const Mapping &mapping, real other_values_mapping, real missing_values_mapping) |
| static int | handleOtherAndMissing (bool all_int, int candidate_max, real other_values_mapping, real missing_values_mapping) |
| Called by getNumClasses to handle special mappings. | |
Protected Attributes | |
| SDBVMOutputCoding | output_coding_ |
| int | num_classes_ |
| must be >0 for one-hot coding | |
| real | missing_values_mapping_ |
| PLearn::SDBVMOutputCoder::SDBVMOutputCoder | ( | SDBVMOutputCoding | oc = SDBVMNumeric, |
| real | missing_values_mapping = MISSING_VALUE |
||
| ) |
Definition at line 49 of file SDBVMat.cc.
: output_coding_(oc), num_classes_(0), missing_values_mapping_(missing_values_mapping) {}
| PLearn::SDBVMOutputCoder::~SDBVMOutputCoder | ( | ) | [virtual] |
Definition at line 55 of file SDBVMat.cc.
{}
| int PLearn::SDBVMOutputCoder::fieldWidth | ( | ) | const [virtual] |
Return the field width required by the selected coding and the number of classes.
--- Conversion operations
Definition at line 139 of file SDBVMat.cc.
References num_classes_, output_coding_, PLERROR, PLearn::SDBVMNumeric, PLearn::SDBVMOneHot, and PLearn::SDBVMOneHotMinus1.
{
switch (output_coding_) {
case SDBVMNumeric:
return 1;
case SDBVMOneHot:
if (num_classes_ == 0)
PLERROR("In SDBVMOutputCoder::fieldWidth: "
"number of output classes not specified");
return num_classes_;
case SDBVMOneHotMinus1:
if (num_classes_ == 0)
PLERROR("In SDBVMOutputCoder::fieldWidth: "
"number of output classes not specified");
return num_classes_ - 1;
default:
PLERROR("In SDBVMOutputCoder::fieldWidth: "
"Unknown coding type: %d", int(output_coding_));
}
return 0;
}
| real PLearn::SDBVMOutputCoder::getMissingValuesMapping | ( | ) | const [virtual] |
Return current missing values mapping.
Definition at line 78 of file SDBVMat.cc.
References missing_values_mapping_.
{
return missing_values_mapping_;
}
| int PLearn::SDBVMOutputCoder::getNumClasses | ( | ) | const [virtual] |
Return the current number of classes; this can be zero if only straight numeric coding is desired.
Definition at line 68 of file SDBVMat.cc.
References num_classes_.
Referenced by PLearn::SDBVMFieldRemapReals::SDBVMFieldRemapReals(), and PLearn::SDBVMFieldRemapStrings::SDBVMFieldRemapStrings().
{
return num_classes_;
}

| int PLearn::SDBVMOutputCoder::getNumClasses | ( | const Mapping & | mapping, |
| real | other_values_mapping, | ||
| real | missing_values_mapping | ||
| ) | [static] |
Utility function to derive the number of classes given a generic mapping: This function iterates over all targets of a mapping, and notes whether the mapped values are all non-negative integers; if so, it returns one more than the largest encountered integer. Otherwise, it returns zero. If other_values_mapping or missing_values_mapping is MISSING_VALUE, the number of classes is not affected. Note that other_values_mapping can be taken into account by this function, but is not otherwise handled by this class; this special mapping must be handled by other classes.
Definition at line 165 of file SDBVMat.cc.
References handleOtherAndMissing(), and PLearn::max().
{
typename Mapping::const_iterator it = mapping.begin(), end = mapping.end();
int max_of_map = INT_MIN;
bool all_int = true;
// Find the maximum of the mapped values
for (; it != end; ++it) {
if (all_int && it->second >= 0 && it->second == int(it->second))
max_of_map = std::max(max_of_map, int(it->second));
else
all_int = false;
}
return handleOtherAndMissing(all_int, max_of_map,
other_values_mapping, missing_values_mapping);
}

| SDBVMOutputCoding PLearn::SDBVMOutputCoder::getOutputCoding | ( | ) | const [virtual] |
Return current output coding in effect.
--- State accessors and mutators
Definition at line 58 of file SDBVMat.cc.
References output_coding_.
{
return output_coding_;
}
| int PLearn::SDBVMOutputCoder::handleOtherAndMissing | ( | bool | all_int, |
| int | candidate_max, | ||
| real | other_values_mapping, | ||
| real | missing_values_mapping | ||
| ) | [static] |
Called by getNumClasses to handle special mappings.
Definition at line 185 of file SDBVMat.cc.
References PLearn::is_missing(), and PLearn::max().
Referenced by getNumClasses(), and PLearn::SDBVMFieldRemapIntervals::SDBVMFieldRemapIntervals().
{
// Handle other_values and missing_values mappings. Causes for
// dismissal include: infinites, NaN's, non-integers
if (all_int && finite(other_values_mapping) &&
other_values_mapping == int(other_values_mapping))
candidate_max = std::max(candidate_max, int(other_values_mapping));
else
// if other_values_mapping is MISSING_VALUE, leave candidate_max as
// is and don't disable one-hot coding
if (!is_missing(other_values_mapping))
all_int = false;
if (all_int && finite(missing_values_mapping) &&
missing_values_mapping == int(missing_values_mapping))
candidate_max = std::max(candidate_max, int(missing_values_mapping));
else
// if other_values_mapping is MISSING_VALUE, leave candidate_max as
// is and don't disable one-hot coding
if (!is_missing(missing_values_mapping))
all_int = false;
return all_int? candidate_max+1 : 0;
}


| void PLearn::SDBVMOutputCoder::setMissingValuesMapping | ( | real | missing_values_mapping | ) | [virtual] |
Specify a mapping for missing values passed to setOutput.
Definition at line 73 of file SDBVMat.cc.
References missing_values_mapping_.
{
missing_values_mapping_ = missing_values_mapping;
}
| void PLearn::SDBVMOutputCoder::setNumClasses | ( | int | n_classes | ) | [virtual] |
Specify the number of classes if something like one-hot coding is desired.
Definition at line 63 of file SDBVMat.cc.
References num_classes_.
{
num_classes_ = n_classes;
}
| void PLearn::SDBVMOutputCoder::setOutput | ( | real | output_value, |
| const Vec & | output_field | ||
| ) | const [virtual] |
Code the given real value into the vector; obviously, if one-hot-like coding is desired, a reasonable integer should be passed.
Definition at line 83 of file SDBVMat.cc.
References PLearn::TVec< T >::fill(), PLearn::is_missing(), MISSING_VALUE, missing_values_mapping_, num_classes_, output_coding_, PLERROR, PLearn::SDBVMNumeric, PLearn::SDBVMOneHot, and PLearn::SDBVMOneHotMinus1.
{
if (is_missing(output_value))
output_value = missing_values_mapping_;
int output_int = int(output_value);
switch (output_coding_) {
case SDBVMNumeric:
output_field[0] = output_value;
break;
case SDBVMOneHot:
// Additional coding: if we get a MISSING_VALUE at this point, fill
// in the output_field with MISSING_VALUEs.
if (is_missing(output_value))
output_field.fill(MISSING_VALUE);
else {
if (output_value < 0 || output_value >= num_classes_)
PLERROR("In SDBVMOutputCoder::setOutput: "
"Output value not in the range [0,%d]", num_classes_-1);
output_field.fill(0.0);
output_field[output_int] = 1.0;
}
break;
case SDBVMOneHotMinus1:
// Additional coding: if we get a MISSING_VALUE at this point, fill
// in the output_field with MISSING_VALUEs.
if (is_missing(output_value))
output_field.fill(MISSING_VALUE);
else {
/*NOTE: this used to be a BUG!!! the following line was
moved up from within 'if (output_int > 0)'.
when output_int was ==0, output_field would stay as is,
but should have been filled w/ zeros.
*/
output_field.fill(0.0);
if (output_value < 0 || output_value >= num_classes_)
PLERROR("In SDBVMOutputCoder::setOutput: "
"Output value not in the range [0,%d]", num_classes_-1);
if (output_int > 0) {
--output_int;
//output_field.fill(0.0);
output_field[output_int] = 1.0;
}
}
break;
default:
PLERROR("In SDBVMOutputCoder::setOutput: "
"Unknown coding type: %d", int(output_coding_));
}
}

Definition at line 136 of file SDBVMat.h.
Referenced by getMissingValuesMapping(), setMissingValuesMapping(), and setOutput().
int PLearn::SDBVMOutputCoder::num_classes_ [protected] |
must be >0 for one-hot coding
Definition at line 135 of file SDBVMat.h.
Referenced by fieldWidth(), getNumClasses(), setNumClasses(), and setOutput().
Definition at line 134 of file SDBVMat.h.
Referenced by fieldWidth(), getOutputCoding(), and setOutput().
1.7.4