|
PLearn 0.1
|
#include <ToBagSplitter.h>


Public Member Functions | |
| ToBagSplitter () | |
| virtual void | build () |
| Post-constructor. | |
| virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
| Transforms a shallow copy into a deep copy. | |
| virtual string | classname () const |
| virtual OptionList & | getOptionList () const |
| virtual OptionMap & | getOptionMap () const |
| virtual RemoteMethodMap & | getRemoteMethodMap () const |
| virtual ToBagSplitter * | deepCopy (CopiesMap &copies) const |
| virtual void | setDataSet (VMat the_dataset) |
| Overridden. | |
| virtual int | nsplits () const |
| Returns the number of available different "splits". | |
| virtual int | nSetsPerSplit () const |
| Returns the number of sets per split. | |
| virtual TVec< VMat > | getSplit (int i=0) |
| Returns split number i. | |
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 | |
| int | expected_size_of_bag |
| bool | provide_target |
| bool | remove_bag |
| PP< Splitter > | sub_splitter |
Static Public Attributes | |
| static StaticInitializer | _static_initializer_ |
Static Protected Member Functions | |
| static void | declareOptions (OptionList &ol) |
| Declares this class' options. | |
Protected Attributes | |
| VMat | bags_index |
| Used to store the list of all bags, with the indices of the corresponding rows in the dataset. | |
Private Types | |
| typedef Splitter | inherited |
Private Member Functions | |
| void | build_ () |
| This does the actual building. | |
Definition at line 52 of file ToBagSplitter.h.
typedef Splitter PLearn::ToBagSplitter::inherited [private] |
Reimplemented from PLearn::Splitter.
Definition at line 57 of file ToBagSplitter.h.
| PLearn::ToBagSplitter::ToBagSplitter | ( | ) |
Definition at line 55 of file ToBagSplitter.cc.
:
expected_size_of_bag(10),
provide_target(false),
remove_bag(false)
{}
| string PLearn::ToBagSplitter::_classname_ | ( | ) | [static] |
Declares name and deepCopy methods.
Reimplemented from PLearn::Splitter.
Definition at line 65 of file ToBagSplitter.cc.
| OptionList & PLearn::ToBagSplitter::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::Splitter.
Definition at line 65 of file ToBagSplitter.cc.
| RemoteMethodMap & PLearn::ToBagSplitter::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::Splitter.
Definition at line 65 of file ToBagSplitter.cc.
Reimplemented from PLearn::Splitter.
Definition at line 65 of file ToBagSplitter.cc.
| Object * PLearn::ToBagSplitter::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 65 of file ToBagSplitter.cc.
| StaticInitializer ToBagSplitter::_static_initializer_ & PLearn::ToBagSplitter::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::Splitter.
Definition at line 65 of file ToBagSplitter.cc.
| void PLearn::ToBagSplitter::build | ( | ) | [virtual] |
Post-constructor.
The normal implementation should call simply inherited::build(), then this class's build_(). This method should be callable again at later times, after modifying some option fields to change the "architecture" of the object.
Reimplemented from PLearn::Object.
Definition at line 98 of file ToBagSplitter.cc.
References PLearn::Object::build(), and build_().
Referenced by setDataSet().
{
inherited::build();
build_();
}


| void PLearn::ToBagSplitter::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::Object.
Definition at line 107 of file ToBagSplitter.cc.
References bags_index, PLearn::Splitter::dataset, expected_size_of_bag, PLearn::VMat::getExample(), i, PLearn::VMat::length(), PLWARNING, provide_target, sub_splitter, PLearn::TVec< T >::subVec(), and PLearn::SumOverBagsVariable::TARGET_COLUMN_LAST.
Referenced by build().
{
if (dataset) {
// Prepare the bags index list.
int max_ninstances = 1;
// The first column in bags_store gives the number of instances in the bag,
// and the following columns give the indices of the corresponding rows in
// the original dataset.
Mat bags_store(dataset->length() / expected_size_of_bag + 1, expected_size_of_bag + 1);
int num_bag = 0;
int num_instance = 0;
int bag_signal_column = dataset->inputsize() + dataset->targetsize() - 1; // Bag signal in the last target column.
for (int i = 0; i < dataset->length(); i++) {
while (num_instance + 1 >= bags_store.width()) {
// Need to resize bags_store.
bags_store.resize(bags_store.length(), bags_store.width() * 2, 0, true);
}
if (num_instance >= max_ninstances) {
max_ninstances = num_instance + 1;
}
bags_store(num_bag, num_instance + 1) = i;
num_instance++;
if (int(dataset->get(i, bag_signal_column)) & SumOverBagsVariable::TARGET_COLUMN_LAST) {
// Last element of a bag.
bags_store(num_bag, 0) = num_instance; // Store the number of instances in this bag.
num_bag++;
num_instance = 0;
if (num_bag >= bags_store.length()) {
// Need to resize bags_store.
bags_store.resize(bags_store.length() * 2, bags_store.width(), 0, true);
}
}
}
// Resize to the minimum size needed.
bags_store.resize(num_bag, max_ninstances + 1, 0, true);
int bags_store_is = max_ninstances + 1;
int bags_store_ts = 0;
if (provide_target) {
if (dataset->targetsize() <= 1)
PLWARNING("In ToBagSplitter::build_ - 'provide_target' is true,"
" but the dataset does not seem to have any target "
"besides the bag information: no target provided to "
"the underlying splitter");
else {
bags_store_ts = dataset->targetsize() - 1;
bags_store.resize(bags_store.length(),
bags_store.width() + bags_store_ts,
0, true);
Vec input, target;
real weight;
for (int i = 0; i < bags_store.length(); i++) {
dataset->getExample(int(round(bags_store(i, 1))),
input, target, weight);
bags_store(i).subVec(bags_store_is, bags_store_ts) <<
target.subVec(0, bags_store_ts);
}
}
}
bags_index = VMat(bags_store);
bags_index->defineSizes(bags_store_is, bags_store_ts, 0);
//bags_index->savePMAT("HOME:tmp/bid.pmat");
// Provide this index to the sub_splitter.
sub_splitter->setDataSet(bags_index);
}
}


| string PLearn::ToBagSplitter::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 65 of file ToBagSplitter.cc.
| void PLearn::ToBagSplitter::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares this class' options.
Reimplemented from PLearn::Splitter.
Definition at line 70 of file ToBagSplitter.cc.
References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::Splitter::declareOptions(), expected_size_of_bag, provide_target, remove_bag, and sub_splitter.
{
declareOption(ol, "expected_size_of_bag",
&ToBagSplitter::expected_size_of_bag,
OptionBase::buildoption,
"The expected size of each bag (optional).");
declareOption(ol, "sub_splitter",
&ToBagSplitter::sub_splitter, OptionBase::buildoption,
"The underlying splitter we want to make operate on bags.");
declareOption(ol, "remove_bag",
&ToBagSplitter::remove_bag, OptionBase::buildoption,
"If true, then the bag column will be removed from the data splits.");
declareOption(ol, "provide_target",
&ToBagSplitter::provide_target, OptionBase::buildoption,
"If true, then the target (without the bag info) of a bag will be\n"
"provided to the underlying splitter. This target is obtained from\n"
"the first sample in each bag.");
// Now call the parent class' declareOptions
inherited::declareOptions(ol);
}

| static const PPath& PLearn::ToBagSplitter::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::Splitter.
Definition at line 114 of file ToBagSplitter.h.
| ToBagSplitter * PLearn::ToBagSplitter::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::Splitter.
Definition at line 65 of file ToBagSplitter.cc.
| OptionList & PLearn::ToBagSplitter::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 65 of file ToBagSplitter.cc.
| OptionMap & PLearn::ToBagSplitter::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 65 of file ToBagSplitter.cc.
| RemoteMethodMap & PLearn::ToBagSplitter::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 65 of file ToBagSplitter.cc.
Returns split number i.
Implements PLearn::Splitter.
Definition at line 176 of file ToBagSplitter.cc.
References PLearn::TVec< T >::append(), PLearn::Splitter::dataset, PLearn::get_pointer(), i, j, PLearn::TMat< T >::length(), PLearn::TVec< T >::length(), remove_bag, sub_splitter, and PLearn::TVec< T >::toMat().
{
TVec<VMat> sub_splits = sub_splitter->getSplit(k);
TVec<VMat> result;
for (int i = 0; i < sub_splits.length(); i++) {
// Get the list of corresponding indices in the original dataset.
Mat indices = sub_splits[i].toMat();
// Turn it into a TVec<int>.
TVec<int> indices_int;
for (int j = 0; j < indices.length(); j++) {
for (int q = 0; q < indices(j, 0); q++) {
int indice = int(indices(j, q + 1));
indices_int.append(indice);
}
}
VMat selected_rows = new SelectRowsVMatrix(dataset, indices_int);
if (remove_bag) {
// Remove the bag column.
int bag_column = selected_rows->inputsize() +
selected_rows->targetsize() - 1;
TVec<int> removed_col(1, bag_column);
PP<SelectColumnsVMatrix> new_sel =
new SelectColumnsVMatrix(selected_rows, removed_col, false);
new_sel->inverse_fields_selection = true;
new_sel->defineSizes(selected_rows->inputsize(),
selected_rows->targetsize() - 1,
selected_rows->weightsize(),
selected_rows->extrasize());
new_sel->build();
selected_rows = get_pointer(new_sel);
}
result.append(selected_rows);
}
return result;
}

| void PLearn::ToBagSplitter::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::Splitter.
Definition at line 215 of file ToBagSplitter.cc.
References bags_index, PLearn::deepCopyField(), PLearn::Splitter::makeDeepCopyFromShallowCopy(), and sub_splitter.
{
inherited::makeDeepCopyFromShallowCopy(copies);
deepCopyField(bags_index, copies);
deepCopyField(sub_splitter, copies);
}

| int PLearn::ToBagSplitter::nSetsPerSplit | ( | ) | const [virtual] |
Returns the number of sets per split.
Implements PLearn::Splitter.
Definition at line 227 of file ToBagSplitter.cc.
References sub_splitter.
{
// ### Return the number of sets per split
return sub_splitter->nSetsPerSplit();
}
| int PLearn::ToBagSplitter::nsplits | ( | ) | const [virtual] |
Returns the number of available different "splits".
Implements PLearn::Splitter.
Definition at line 236 of file ToBagSplitter.cc.
References sub_splitter.
{
return sub_splitter->nsplits();
}
| void PLearn::ToBagSplitter::setDataSet | ( | VMat | the_dataset | ) | [virtual] |
Overridden.
Reimplemented from PLearn::Splitter.
Definition at line 244 of file ToBagSplitter.cc.
References build(), and PLearn::Splitter::setDataSet().
{
inherited::setDataSet(the_dataset);
// Need to recompute the bags index.
build();
}

Reimplemented from PLearn::Splitter.
Definition at line 114 of file ToBagSplitter.h.
VMat PLearn::ToBagSplitter::bags_index [protected] |
Used to store the list of all bags, with the indices of the corresponding rows in the dataset.
Definition at line 69 of file ToBagSplitter.h.
Referenced by build_(), and makeDeepCopyFromShallowCopy().
Definition at line 77 of file ToBagSplitter.h.
Referenced by build_(), and declareOptions().
Definition at line 78 of file ToBagSplitter.h.
Referenced by build_(), and declareOptions().
Definition at line 79 of file ToBagSplitter.h.
Referenced by declareOptions(), and getSplit().
Definition at line 80 of file ToBagSplitter.h.
Referenced by build_(), declareOptions(), getSplit(), makeDeepCopyFromShallowCopy(), nSetsPerSplit(), and nsplits().
1.7.4