00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00043
#include "SelectInputSubsetLearner.h"
00044
#include <plearn/vmat/SelectColumnsVMatrix.h>
00045
#include <plearn/math/random.h>
00046
00047
namespace PLearn {
00048
using namespace std;
00049
00050 SelectInputSubsetLearner::SelectInputSubsetLearner() : random_fraction(0)
00051
00052 {
00053 }
00054
00055
PLEARN_IMPLEMENT_OBJECT(
SelectInputSubsetLearner,
"PLearner which selects a subset of the inputs for an embedded learner.",
00056
"This learner class contains an embedded learner for which it selects a subset of the inputs.\n"
00057
"The subset can be either selected explicitly or chosen randomly (the user chooses what fraction\n"
00058
"of the original inputs will be selected).");
00059
00060 void SelectInputSubsetLearner::declareOptions(
OptionList& ol)
00061 {
00062
00063
00064
00065
00066
00067
00068
declareOption(ol,
"selected_inputs", &SelectInputSubsetLearner::selected_inputs, OptionBase::buildoption,
00069
"List of selected inputs. If this option is set then random_fraction should not be set (or set to 0).\n");
00070
00071
declareOption(ol,
"random_fraction", &SelectInputSubsetLearner::random_fraction, OptionBase::buildoption,
00072
"Fraction of the original inputs that is randomly selected.\n"
00073
"If 0 then the selected_inputs option should be set.\n"
00074
"If selected_inputs is provided (length>0) then this option is ignored.\n");
00075
00076
00077 inherited::declareOptions(ol);
00078 }
00079
00080 void SelectInputSubsetLearner::build_()
00081 {
00082
if (
random_fraction>0 && learner_ && inputsize_>0 &&
selected_inputs.
length()==0)
00083 {
00084
int n_selected =
int(rint(
random_fraction*inputsize_));
00085
selected_inputs.
resize(inputsize_);
00086
for (
int i=0;i<n_selected;i++)
00087
selected_inputs[i]=i;
00088
shuffleElements(
selected_inputs);
00089
selected_inputs.
resize(n_selected);
00090 }
00091
learner_inputs.
resize(
selected_inputs.
length());
00092 }
00093
00094
00095 void SelectInputSubsetLearner::build()
00096 {
00097 inherited::build();
00098
build_();
00099 }
00100
00101
00102 void SelectInputSubsetLearner::makeDeepCopyFromShallowCopy(map<const void*, void*>& copies)
00103 {
00104 inherited::makeDeepCopyFromShallowCopy(copies);
00105
00106
00107
00108
00109
00110
deepCopyField(
selected_inputs, copies);
00111
deepCopyField(
all_indices, copies);
00112
deepCopyField(
learner_inputs, copies);
00113 }
00114
00115 int SelectInputSubsetLearner::inputsize()
const
00116
{
return inputsize_; }
00117
00118
00119 void SelectInputSubsetLearner::computeOutput(
const Vec& input,
Vec& output)
const
00120
{
00121
for (
int i=0;i<
learner_inputs.
length();i++)
00122
learner_inputs[i] = input[
selected_inputs[i]];
00123 learner_->computeOutput(
learner_inputs,output);
00124 }
00125
00126 void SelectInputSubsetLearner::computeCostsFromOutputs(
const Vec& input,
const Vec& output,
00127
const Vec& target,
Vec& costs)
const
00128
{
00129
00130
for (
int i=0;i<
learner_inputs.
length();i++)
00131
learner_inputs[i] = input[
selected_inputs[i]];
00132 learner_->computeCostsFromOutputs(
learner_inputs,output,target,costs);
00133 }
00134
00135 void SelectInputSubsetLearner::computeOutputAndCosts(
const Vec& input,
const Vec& target,
00136
Vec& output,
Vec& costs)
const
00137
{
00138
for (
int i=0;i<
learner_inputs.
length();i++)
00139
learner_inputs[i] = input[
selected_inputs[i]];
00140 learner_->computeOutputAndCosts(
learner_inputs, target, output, costs);
00141 }
00142
00143 void SelectInputSubsetLearner::setTrainingSet(
VMat training_set,
bool call_forget)
00144 {
00145 inherited::setTrainingSet(training_set,call_forget);
00146
int n_other_columns = training_set->
width()-
inputsize();
00147
all_indices.
resize(
selected_inputs.
length()+n_other_columns);
00148
for (
int i=0;i<
selected_inputs.
length();i++)
00149
all_indices[i]=
selected_inputs[i];
00150
for (
int j=0;j<n_other_columns;j++)
00151
all_indices[selected_inputs.length()+j]=
inputsize()+j;
00152
VMat vm =
new SelectColumnsVMatrix(training_set,
all_indices);
00153 vm->defineSizes(selected_inputs.length(),training_set->targetsize(),training_set->weightsize());
00154 learner_->setTrainingSet(vm,call_forget);
00155 }
00156
00157 }