PLearn 0.1
|
#include <SVMClassificationTorch.h>
Public Member Functions | |
SVMClassificationTorch () | |
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 OptionList & | getOptionList () const |
virtual OptionMap & | getOptionMap () const |
virtual RemoteMethodMap & | getRemoteMethodMap () const |
virtual SVMClassificationTorch * | deepCopy (CopiesMap &copies) const |
virtual void | computeOutput (const Vec &input, Vec &output) const |
Returns the size of this learner's output, (which typically may depend on its inputsize(), targetsize() and set options). | |
virtual void | computeCostsFromOutputs (const Vec &input, const Vec &output, const Vec &target, Vec &costs) const |
Computes the costs from already computed output. | |
virtual TVec< std::string > | getTestCostNames () const |
Returns the names of the costs computed by computeCostsFromOutpus (and thus the test method). | |
virtual TVec< std::string > | getTrainCostNames () const |
Returns the names of the objective costs that the train method computes and for which it updates the VecStatsCollector train_stats. | |
virtual void | setTrainingSet (VMat training_set, bool call_forget=true) |
Overridden to forward the dataset to the kernel. | |
Static Public Member Functions | |
static string | _classname_ () |
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 | |
real | C |
real | cache_size |
Ker | kernel |
int | iter_msg |
bool | output_the_class |
bool | target_01 |
Static Public Attributes | |
static StaticInitializer | _static_initializer_ |
Static Protected Member Functions | |
static void | declareOptions (OptionList &ol) |
Declares this class' options. | |
Private Types | |
typedef TorchLearner | inherited |
Private Member Functions | |
void | build_ () |
This does the actual building. |
Definition at line 52 of file SVMClassificationTorch.h.
typedef TorchLearner PLearn::SVMClassificationTorch::inherited [private] |
Reimplemented from PLearn::TorchLearner.
Definition at line 56 of file SVMClassificationTorch.h.
PLearn::SVMClassificationTorch::SVMClassificationTorch | ( | ) |
Default constructor.
Definition at line 60 of file SVMClassificationTorch.cc.
: C(100), cache_size(50), iter_msg(1000), output_the_class(true), target_01(false) {}
string PLearn::SVMClassificationTorch::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::TorchLearner.
Definition at line 75 of file SVMClassificationTorch.cc.
OptionList & PLearn::SVMClassificationTorch::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::TorchLearner.
Definition at line 75 of file SVMClassificationTorch.cc.
RemoteMethodMap & PLearn::SVMClassificationTorch::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::TorchLearner.
Definition at line 75 of file SVMClassificationTorch.cc.
Reimplemented from PLearn::TorchLearner.
Definition at line 75 of file SVMClassificationTorch.cc.
Object * PLearn::SVMClassificationTorch::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::TorchLearner.
Definition at line 75 of file SVMClassificationTorch.cc.
StaticInitializer SVMClassificationTorch::_static_initializer_ & PLearn::SVMClassificationTorch::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::TorchLearner.
Definition at line 75 of file SVMClassificationTorch.cc.
void PLearn::SVMClassificationTorch::build | ( | ) | [virtual] |
Simply calls inherited::build() then build_().
Reimplemented from PLearn::TorchLearner.
Definition at line 129 of file SVMClassificationTorch.cc.
References PLearn::TorchLearner::build(), and build_().
{ inherited::build(); build_(); }
void PLearn::SVMClassificationTorch::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::TorchLearner.
Definition at line 138 of file SVMClassificationTorch.cc.
References PLearn::TorchLearner::build(), C, cache_size, iter_msg, kernel, PLearn::TorchLearner::machine, and PLearn::TorchLearner::trainer.
Referenced by build().
{ // Build machine. if (!machine) machine = new TSVMClassification(); PP<TSVMClassification> svm_class = (TSVMClassification*) (TMachine*) machine; svm_class->C = this->C; svm_class->cache_size = this->cache_size; svm_class->kernel = new TTorchKernelFromKernel(this->kernel); svm_class->build(); // Build trainer. if (!trainer) trainer = new TQCTrainer(); PP<TQCTrainer> qc_trainer = (TQCTrainer*) (TTrainer*) this->trainer; qc_trainer->qc_machine = (TQCMachine*) (TMachine*) this->machine; qc_trainer->iter_msg = this->iter_msg; qc_trainer->build(); // We can now build the TorchLearner. inherited::build(); }
string PLearn::SVMClassificationTorch::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::TorchLearner.
Definition at line 75 of file SVMClassificationTorch.cc.
void PLearn::SVMClassificationTorch::computeCostsFromOutputs | ( | const Vec & | input, |
const Vec & | output, | ||
const Vec & | target, | ||
Vec & | costs | ||
) | const [virtual] |
Computes the costs from already computed output.
Reimplemented from PLearn::TorchLearner.
Definition at line 162 of file SVMClassificationTorch.cc.
References PLearn::fast_exact_is_equal(), PLearn::TVec< T >::length(), PLASSERT, PLearn::TVec< T >::resize(), PLearn::sigmoid(), and target_01.
{ // No cost computed. // For safety, we check we are trying to do binary classification with -1 // and 1, or 0 and 1 when the 'target_01' option is set. PLASSERT( target.length() == 1 && ((target_01 && (target[0] == 1 || target[0] == 0)) || (!target_01 && (target[0] == 1 || target[0] == -1))) ); PLASSERT( output.length() == 1 ); costs.resize(1); real sig_output = sigmoid(output[0]); if (fast_exact_is_equal(sig_output, 0)) sig_output = REAL_EPSILON; if (fast_exact_is_equal(target[0], 1)) costs[0] = sig_output; else costs[0] = - sig_output; }
void PLearn::SVMClassificationTorch::computeOutput | ( | const Vec & | input, |
Vec & | output | ||
) | const [virtual] |
Returns the size of this learner's output, (which typically may depend on its inputsize(), targetsize() and set options).
(Re-)initializes the PLearner in its fresh state (that state may depend on the 'seed' option) And sets 'stage' back to 0 (this is the stage of a fresh learner!). The role of the train method is to bring the learner up to stage==nstages, updating the train_stats collector with training costs measured on-line in the process. Computes the output from the input.
Reimplemented from PLearn::TorchLearner.
Definition at line 185 of file SVMClassificationTorch.cc.
References PLearn::TorchLearner::computeOutput(), i, PLearn::TVec< T >::length(), output_the_class, and target_01.
{ inherited::computeOutput(input, output); if (output_the_class) for (int i = 0; i < output.length(); i++) output[i] = output[i] > 0 ? 1 : target_01 ? 0 : -1; }
void PLearn::SVMClassificationTorch::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares this class' options.
Reimplemented from PLearn::TorchLearner.
Definition at line 80 of file SVMClassificationTorch.cc.
References PLearn::OptionBase::buildoption, C, cache_size, PLearn::declareOption(), PLearn::TorchLearner::declareOptions(), iter_msg, kernel, PLearn::OptionBase::learntoption, PLearn::TorchLearner::machine, PLearn::OptionBase::nosave, output_the_class, PLearn::redeclareOption(), target_01, and PLearn::TorchLearner::trainer.
{ // ### 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 // Build options. declareOption(ol, "kernel", &SVMClassificationTorch::kernel, OptionBase::buildoption, "The kernel we use."); declareOption(ol, "C", &SVMClassificationTorch::C, OptionBase::buildoption, "Trade-off margin / error."); declareOption(ol, "output_the_class", &SVMClassificationTorch::output_the_class, OptionBase::buildoption, "If set to 1, the output will be the class, otherwise it will be a real value."); declareOption(ol, "target_01", &SVMClassificationTorch::target_01, OptionBase::buildoption, "If set to 1, the target in the training set will be assumed to be\n" "either 0 or 1 (instead of the default -1 / 1)."); declareOption(ol, "iter_msg", &SVMClassificationTorch::iter_msg, OptionBase::buildoption, "Number of iterations between each message."); declareOption(ol, "cache_size", &SVMClassificationTorch::cache_size, OptionBase::buildoption, "Cache size (in Mb)."); // Learnt options. // declareOption(ol, "myoption", &SVMClassificationTorch::myoption, OptionBase::learntoption, // "Help text describing this option"); // Now call the parent class' declareOptions. inherited::declareOptions(ol); // Redeclare some parent's options. redeclareOption(ol, "machine", &SVMClassificationTorch::machine, OptionBase::learntoption, "Constructed at build time and saved to store learnt parameters."); redeclareOption(ol, "trainer", &SVMClassificationTorch::trainer, OptionBase::nosave, "Constructed at build time (there is no need to save it)."); }
static const PPath& PLearn::SVMClassificationTorch::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::TorchLearner.
Definition at line 111 of file SVMClassificationTorch.h.
SVMClassificationTorch * PLearn::SVMClassificationTorch::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::TorchLearner.
Definition at line 75 of file SVMClassificationTorch.cc.
OptionList & PLearn::SVMClassificationTorch::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::TorchLearner.
Definition at line 75 of file SVMClassificationTorch.cc.
OptionMap & PLearn::SVMClassificationTorch::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::TorchLearner.
Definition at line 75 of file SVMClassificationTorch.cc.
RemoteMethodMap & PLearn::SVMClassificationTorch::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::TorchLearner.
Definition at line 75 of file SVMClassificationTorch.cc.
TVec< string > PLearn::SVMClassificationTorch::getTestCostNames | ( | ) | const [virtual] |
Returns the names of the costs computed by computeCostsFromOutpus (and thus the test method).
Reimplemented from PLearn::TorchLearner.
Definition at line 213 of file SVMClassificationTorch.cc.
References PLearn::TVec< T >::append(), and PLearn::TVec< T >::isEmpty().
{ static TVec<string> costs; if (costs.isEmpty()) costs.append("lift_output"); return costs; }
TVec< string > PLearn::SVMClassificationTorch::getTrainCostNames | ( | ) | const [virtual] |
Returns the names of the objective costs that the train method computes and for which it updates the VecStatsCollector train_stats.
Reimplemented from PLearn::TorchLearner.
Definition at line 224 of file SVMClassificationTorch.cc.
References PLearn::TorchLearner::getTrainCostNames().
{ return inherited::getTrainCostNames(); }
void PLearn::SVMClassificationTorch::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::TorchLearner.
Definition at line 232 of file SVMClassificationTorch.cc.
References PLearn::TorchLearner::makeDeepCopyFromShallowCopy(), and PLERROR.
{ inherited::makeDeepCopyFromShallowCopy(copies); // ### ex: // deepCopyField(trainvec, copies); // ### Remove this line when you have fully implemented this method. PLERROR("SVMClassificationTorch::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!"); }
void PLearn::SVMClassificationTorch::setTrainingSet | ( | VMat | training_set, |
bool | call_forget = true |
||
) | [virtual] |
Overridden to forward the dataset to the kernel.
Reimplemented from PLearn::TorchLearner.
Definition at line 246 of file SVMClassificationTorch.cc.
References kernel, PLASSERT, PLearn::TorchLearner::setTrainingSet(), target_01, and PLearn::tostring().
{ VMat the_train_set = training_set; if (target_01) { // Create processing program. int target_col = training_set->inputsize(); PLASSERT( target_col > 0 ); string prog = "[%0:%" + tostring(target_col - 1) + "] %" + tostring(target_col) + " 1 == 1 -1 ifelse :target"; if (training_set->weightsize() > 0) { int weight_col = training_set->inputsize() + training_set->targetsize(); prog += " [%" + tostring(weight_col) + ":%" + tostring(weight_col + training_set->weightsize() - 1) + "]"; } the_train_set = new ProcessingVMatrix(the_train_set, prog); } kernel->setDataForKernelMatrix(the_train_set); inherited::setTrainingSet(the_train_set, call_forget); }
Reimplemented from PLearn::TorchLearner.
Definition at line 111 of file SVMClassificationTorch.h.
Definition at line 70 of file SVMClassificationTorch.h.
Referenced by build_(), and declareOptions().
Definition at line 71 of file SVMClassificationTorch.h.
Referenced by build_(), and declareOptions().
Definition at line 73 of file SVMClassificationTorch.h.
Referenced by build_(), and declareOptions().
Definition at line 72 of file SVMClassificationTorch.h.
Referenced by build_(), declareOptions(), and setTrainingSet().
Definition at line 74 of file SVMClassificationTorch.h.
Referenced by computeOutput(), and declareOptions().
Definition at line 75 of file SVMClassificationTorch.h.
Referenced by computeCostsFromOutputs(), computeOutput(), declareOptions(), and setTrainingSet().