|
PLearn 0.1
|
The first sentence should be a BRIEF DESCRIPTION of what the class does. More...
#include <RandomSamplesVMatrix.h>


Public Member Functions | |
| RandomSamplesVMatrix () | |
| ### declare public option fields (such as build options) here Start your comments with Doxygen-compatible comments such as //! | |
| virtual string | classname () const |
| virtual OptionList & | getOptionList () const |
| virtual OptionMap & | getOptionMap () const |
| virtual RemoteMethodMap & | getRemoteMethodMap () const |
| virtual RandomSamplesVMatrix * | deepCopy (CopiesMap &copies) const |
| virtual void | build () |
| Simply calls inherited::build() then build_(). | |
| virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
| Transforms a shallow copy into a deep copy. | |
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 PPath & | declaringFile () |
Public Attributes | |
| bool | alternate_targets |
| string | is_preserved |
| int | n_non_preserved |
| int32_t | seed |
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 the class options. | |
Protected Attributes | |
| PP< PRandom > | random_gen |
| Random number generator. | |
| TVec< int > | non_preserved |
| List of examples in the source VMat that are not preserved. | |
| TVec< int > | indices |
| The i-th element is the index of the sample in the source VMat. | |
| Vec | last_targets |
| The i-th element is the last target used for the i-th row in this VMat. | |
| TVec< TVec< int > > | target_list |
| The i-th element is the list of all target indices, except i. | |
| TVec< Vec > | target_distr |
| The i-th element is the distribution weights of all target indices, except i (i.e. | |
| map< real, int > | target_to_idx |
| Map a real-valued target to its index. | |
| TVec< TVec< int > > | by_target |
| The i-th element is the list of all samples for the i-th target. | |
Private Types | |
| typedef SourceVMatrix | inherited |
Private Member Functions | |
| void | build_ () |
| This does the actual building. | |
The first sentence should be a BRIEF DESCRIPTION of what the class does.
Place the rest of the class programmer documentation here. Doxygen supports Javadoc-style comments. See http://www.doxygen.org/manual.html
Definition at line 58 of file RandomSamplesVMatrix.h.
typedef SourceVMatrix PLearn::RandomSamplesVMatrix::inherited [private] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 60 of file RandomSamplesVMatrix.h.
| PLearn::RandomSamplesVMatrix::RandomSamplesVMatrix | ( | ) |
### declare public option fields (such as build options) here Start your comments with Doxygen-compatible comments such as //!
Default constructor
Definition at line 67 of file RandomSamplesVMatrix.cc.
:
alternate_targets(false),
is_preserved("0"),
n_non_preserved(-1),
seed(1827),
random_gen(new PRandom())
{
}
| string PLearn::RandomSamplesVMatrix::_classname_ | ( | ) | [static] |
Declares name and deepCopy methods.
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file RandomSamplesVMatrix.cc.
| OptionList & PLearn::RandomSamplesVMatrix::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file RandomSamplesVMatrix.cc.
| RemoteMethodMap & PLearn::RandomSamplesVMatrix::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file RandomSamplesVMatrix.cc.
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file RandomSamplesVMatrix.cc.
| Object * PLearn::RandomSamplesVMatrix::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file RandomSamplesVMatrix.cc.
| StaticInitializer RandomSamplesVMatrix::_static_initializer_ & PLearn::RandomSamplesVMatrix::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file RandomSamplesVMatrix.cc.
| void PLearn::RandomSamplesVMatrix::build | ( | ) | [virtual] |
Simply calls inherited::build() then build_().
Reimplemented from PLearn::SourceVMatrix.
Definition at line 123 of file RandomSamplesVMatrix.cc.
References PLearn::SourceVMatrix::build(), and build_().
{
inherited::build();
build_();
}

| void PLearn::RandomSamplesVMatrix::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::SourceVMatrix.
Definition at line 132 of file RandomSamplesVMatrix.cc.
References alternate_targets, PLearn::TVec< T >::append(), by_target, PLearn::VMatLanguage::compileString(), PLearn::fast_exact_is_equal(), PLearn::VMat::getExample(), i, indices, is_preserved, PLearn::TVec< T >::isEmpty(), j, last_targets, PLearn::VMatrix::length(), PLearn::TVec< T >::length(), PLearn::VMat::length(), PLearn::VMatrix::length_, n, n_non_preserved, non_preserved, PLASSERT, PLERROR, random_gen, PLearn::TVec< T >::resize(), PLearn::VMatLanguage::run(), seed, PLearn::SourceVMatrix::setMetaInfoFromSource(), PLearn::VMatLanguage::setSource(), PLearn::SourceVMatrix::source, target_distr, target_list, target_to_idx, and PLearn::VMat::width().
Referenced by build().
{
if (!source)
return;
random_gen->manual_seed(seed);
// Initialize VPL program.
VMatLanguage program;
TVec<string> fieldnames;
program.setSource(source);
program.compileString(is_preserved, fieldnames);
// Find out which samples need to be kept.
int n = source->length();
Vec row(source->width());
Vec result(1);
non_preserved.resize(0);
indices.resize(0);
for (int i = 0; i < n; i++) {
program.run(i, result);
if (fast_exact_is_equal(result[0], 1))
// Sample i is to be preserved.
indices.append(i);
else
non_preserved.append(i);
}
// Find out length of this VMat if the 'n_non_preserved' option is set.
PLASSERT( n_non_preserved >= 0 || n_non_preserved == -1 ||
n_non_preserved == -2 );
if (n_non_preserved >= 0)
length_ = indices.length() + n_non_preserved;
else if (n_non_preserved == -2)
length_ = indices.length() * 2;
else if (length_ < 0)
length_ = source->length();
// Specific pre-processing for the case where 'alternate_targets' is true.
if (alternate_targets) {
// Currently this feature is not implemented when we preserve examples
// (just because it is a bit more complex).
if (!indices.isEmpty())
PLERROR("In RandomSamplesVMatrix::build_ - The 'alternate_targets'"
" option is not implemented yet in the case where some "
"examples are preserved. Please implement it!");
if (source->targetsize() != 1)
PLERROR("In RandomSamplesVMatrix::build_ - The source must have "
"a targetsize of 1 in order to use the "
"'alternate_targets' option");
// First we need to find the number of targets.
map<real, TVec<int> > by_target_map; // Map a target to its samples.
Vec input, target;
real weight;
target_to_idx.clear();
for (int i = 0; i < source->length(); i++) {
source->getExample(i, input, target, weight);
by_target_map[target[0]].append(i);
if (target_to_idx.count(target[0]) == 0) {
int n_cur = target_to_idx.size();
target_to_idx[target[0]] = n_cur;
}
}
int n_targets = by_target_map.size();
// Convert from map to vector (for convenience).
by_target.resize(n_targets);
map<real, TVec<int> >::const_iterator it = by_target_map.begin();
for (int i = 0; i < n_targets; i++, it++)
by_target[i] = it->second;
// Build a map from a target index to all other target indices that may
// be used afterwards, with corresponding probabilities.
target_list.resize(n_targets);
target_distr.resize(n_targets);
for (int i = 0; i < n_targets; i++) {
TVec<int>& tl = target_list[i];
tl.resize(0);
Vec& td = target_distr[i];
td.resize(0);
int n_others = 0;
for (int j = 0; j < n_targets; j++)
if (j != i) {
tl.append(j); // This is the list of all targets,
// excluding 'i'.
td.append(by_target[j].length());
n_others += by_target[j].length();
}
td /= real(n_others); // Normalize probabilities.
}
// Resize 'last_targets'.
last_targets.resize(source->length());
}
// Fill in 'indices' with as many -1 as necessary.
if (indices.length() > length_)
PLERROR("In RandomSamplesVMatrix::build_ - The number of preserved"
"samples (%d) is higher than the length of this VMat (%d)",
indices.length(), length_);
while (indices.length() != length_)
indices.append(-1);
// Shuffle the list of indices.
random_gen->shuffleElements(indices);
setMetaInfoFromSource();
}


| string PLearn::RandomSamplesVMatrix::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file RandomSamplesVMatrix.cc.
| void PLearn::RandomSamplesVMatrix::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares the class options.
Reimplemented from PLearn::SourceVMatrix.
Definition at line 79 of file RandomSamplesVMatrix.cc.
References alternate_targets, PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::SourceVMatrix::declareOptions(), is_preserved, n_non_preserved, and seed.
{
// ### 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. If you don't provide one of these three,
// ### this option will be ignored when loading values from a script.
// ### You can also combine flags, for example with OptionBase::nosave:
// ### (OptionBase::buildoption | OptionBase::nosave)
declareOption(ol, "is_preserved", &RandomSamplesVMatrix::is_preserved,
OptionBase::buildoption,
"VPL program that indicates if a sample is preserved.");
declareOption(ol, "n_non_preserved",
&RandomSamplesVMatrix::n_non_preserved,
OptionBase::buildoption,
"If given a non-negative value, it indicates the total number of\n"
"non-preserved examples that are added to this VMat, and overrides\n"
"the 'length' option. Two special negative values can be used:\n"
" -1: this option is ignored, and the number of non-preserved\n"
" samples is set so that this VMat has either the desired length\n"
" (if the 'length' option is provided), or the same length as\n"
" its source otherwise.\n"
" -2: the number of non-preserved examples is set exactly to match\n"
" the number of preserved examples.");
declareOption(ol, "alternate_targets",
&RandomSamplesVMatrix::alternate_targets,
OptionBase::buildoption,
"If set to 1, then this VMat will never sample two consecutive rows\n"
"having the same target.");
declareOption(ol, "seed", &RandomSamplesVMatrix::seed,
OptionBase::buildoption,
"Seed for random number generation.");
// Now call the parent class' declareOptions
inherited::declareOptions(ol);
}

| static const PPath& PLearn::RandomSamplesVMatrix::declaringFile | ( | ) | [inline, static] |
| RandomSamplesVMatrix * PLearn::RandomSamplesVMatrix::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file RandomSamplesVMatrix.cc.
Fill the vector 'v' with the content of the i-th row.
v is assumed to be the right size. ### This function must be overridden in your class
Reimplemented from PLearn::SourceVMatrix.
Definition at line 243 of file RandomSamplesVMatrix.cc.
References alternate_targets, by_target, i, indices, last_targets, PLearn::TVec< T >::length(), PLearn::VMat::length(), non_preserved, PLASSERT, random_gen, PLearn::SourceVMatrix::source, target_distr, target_list, and target_to_idx.
{
if (indices[i] >= 0)
source->getRow(indices[i], v);
else {
int random_sample;
if (alternate_targets && i > 0) {
// Note that the first sample may be any class since it has no
// previous sample in this VMat.
real previous_target = last_targets[i - 1];
int previous_target_idx = target_to_idx[previous_target];
int random_target = random_gen->multinomial_sample(
target_distr[previous_target_idx]);
const TVec<int>& candidates =
by_target[target_list[previous_target_idx][random_target]];
int random_sample_idx = random_gen->uniform_multinomial_sample(
candidates.length());
random_sample = candidates[random_sample_idx];
PLASSERT( non_preserved.length() == source->length() );
} else {
random_sample =
random_gen->uniform_multinomial_sample(non_preserved.length());
}
source->getRow(non_preserved[random_sample], v);
if (alternate_targets)
last_targets[i] = v[source->inputsize()];
}
}

| OptionList & PLearn::RandomSamplesVMatrix::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file RandomSamplesVMatrix.cc.
| OptionMap & PLearn::RandomSamplesVMatrix::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file RandomSamplesVMatrix.cc.
| RemoteMethodMap & PLearn::RandomSamplesVMatrix::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::SourceVMatrix.
Definition at line 62 of file RandomSamplesVMatrix.cc.
| void PLearn::RandomSamplesVMatrix::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::SourceVMatrix.
Definition at line 275 of file RandomSamplesVMatrix.cc.
References by_target, PLearn::deepCopyField(), indices, last_targets, PLearn::SourceVMatrix::makeDeepCopyFromShallowCopy(), non_preserved, random_gen, target_distr, and target_list.
{
inherited::makeDeepCopyFromShallowCopy(copies);
deepCopyField(random_gen, copies);
deepCopyField(non_preserved, copies);
deepCopyField(indices, copies);
deepCopyField(last_targets, copies);
deepCopyField(target_list, copies);
deepCopyField(target_distr, copies);
deepCopyField(by_target, copies);
}

Reimplemented from PLearn::SourceVMatrix.
Definition at line 87 of file RandomSamplesVMatrix.h.
Definition at line 65 of file RandomSamplesVMatrix.h.
Referenced by build_(), declareOptions(), and getNewRow().
TVec< TVec<int> > PLearn::RandomSamplesVMatrix::by_target [protected] |
The i-th element is the list of all samples for the i-th target.
Definition at line 124 of file RandomSamplesVMatrix.h.
Referenced by build_(), getNewRow(), and makeDeepCopyFromShallowCopy().
TVec<int> PLearn::RandomSamplesVMatrix::indices [protected] |
The i-th element is the index of the sample in the source VMat.
If it is -1, this means it is a non-preserved sample, and thus should be randomly sampled from the 'non_preserved' list.
Definition at line 107 of file RandomSamplesVMatrix.h.
Referenced by build_(), getNewRow(), and makeDeepCopyFromShallowCopy().
Definition at line 66 of file RandomSamplesVMatrix.h.
Referenced by build_(), and declareOptions().
Vec PLearn::RandomSamplesVMatrix::last_targets [protected] |
The i-th element is the last target used for the i-th row in this VMat.
Definition at line 110 of file RandomSamplesVMatrix.h.
Referenced by build_(), getNewRow(), and makeDeepCopyFromShallowCopy().
Definition at line 67 of file RandomSamplesVMatrix.h.
Referenced by build_(), and declareOptions().
TVec<int> PLearn::RandomSamplesVMatrix::non_preserved [protected] |
List of examples in the source VMat that are not preserved.
Definition at line 102 of file RandomSamplesVMatrix.h.
Referenced by build_(), getNewRow(), and makeDeepCopyFromShallowCopy().
PP<PRandom> PLearn::RandomSamplesVMatrix::random_gen [protected] |
Random number generator.
Definition at line 99 of file RandomSamplesVMatrix.h.
Referenced by build_(), getNewRow(), and makeDeepCopyFromShallowCopy().
Definition at line 68 of file RandomSamplesVMatrix.h.
Referenced by build_(), and declareOptions().
TVec< Vec > PLearn::RandomSamplesVMatrix::target_distr [protected] |
The i-th element is the distribution weights of all target indices, except i (i.e.
the distribution to sample from after an example of the i-th target).
Definition at line 118 of file RandomSamplesVMatrix.h.
Referenced by build_(), getNewRow(), and makeDeepCopyFromShallowCopy().
TVec< TVec<int> > PLearn::RandomSamplesVMatrix::target_list [protected] |
The i-th element is the list of all target indices, except i.
Definition at line 113 of file RandomSamplesVMatrix.h.
Referenced by build_(), getNewRow(), and makeDeepCopyFromShallowCopy().
map<real, int> PLearn::RandomSamplesVMatrix::target_to_idx [mutable, protected] |
Map a real-valued target to its index.
Definition at line 121 of file RandomSamplesVMatrix.h.
Referenced by build_(), and getNewRow().
1.7.4