PLearn 0.1
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Static Protected Member Functions | Protected Attributes | Private Types | Private Member Functions
PLearn::TorchLearner Class Reference

#include <TorchLearner.h>

Inheritance diagram for PLearn::TorchLearner:
Inheritance graph
[legend]
Collaboration diagram for PLearn::TorchLearner:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 TorchLearner ()
 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 OptionListgetOptionList () const
virtual OptionMapgetOptionMap () const
virtual RemoteMethodMapgetRemoteMethodMap () const
virtual TorchLearnerdeepCopy (CopiesMap &copies) const
virtual int outputsize () const
 Returns the size of this learner's output, (which typically may depend on its inputsize(), targetsize() and set options).
virtual void forget ()
 (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!).
virtual void train ()
 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.
virtual void computeOutput (const Vec &input, Vec &output) const
 Computes the output from the input.
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 convert to a Torch dataset.
virtual ~TorchLearner ()
 Free memory.

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 PPathdeclaringFile ()

Public Attributes

PP< TMachine > machine
PP< TTrainer > trainer

Static Public Attributes

static StaticInitializer _static_initializer_

Static Protected Member Functions

static void declareOptions (OptionList &ol)
 Declares this class' options.

Protected Attributes

int outputsize_
Torch::Allocator * allocator
 Allocator for Torch memory management.
Torch::Sequence * inputs
 A sequence to provide inputs to the Torch machine.
PP< TTorchDataSetFromVMat > torch_train_set
 The Torch version of the training set.

Private Types

typedef PLearner inherited

Private Member Functions

void build_ ()
 This does the actual building.

Detailed Description

Definition at line 59 of file TorchLearner.h.


Member Typedef Documentation

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 63 of file TorchLearner.h.


Constructor & Destructor Documentation

PLearn::TorchLearner::TorchLearner ( )

Default constructor.

Definition at line 55 of file TorchLearner.cc.

References allocator, and inputs.

    : outputsize_(-1)
{
    allocator = new Torch::Allocator;
    inputs = 0;
}
PLearn::TorchLearner::~TorchLearner ( ) [virtual]

Free memory.

Definition at line 243 of file TorchLearner.cc.

References allocator.

                            {
    delete allocator;
}

Member Function Documentation

string PLearn::TorchLearner::_classname_ ( ) [static]

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 65 of file TorchLearner.cc.

OptionList & PLearn::TorchLearner::_getOptionList_ ( ) [static]

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 65 of file TorchLearner.cc.

RemoteMethodMap & PLearn::TorchLearner::_getRemoteMethodMap_ ( ) [static]

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 65 of file TorchLearner.cc.

bool PLearn::TorchLearner::_isa_ ( const Object o) [static]

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 65 of file TorchLearner.cc.

Object * PLearn::TorchLearner::_new_instance_for_typemap_ ( ) [static]

Reimplemented from PLearn::Object.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 65 of file TorchLearner.cc.

StaticInitializer TorchLearner::_static_initializer_ & PLearn::TorchLearner::_static_initialize_ ( ) [static]

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 65 of file TorchLearner.cc.

void PLearn::TorchLearner::build ( ) [virtual]

Simply calls inherited::build() then build_().

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 106 of file TorchLearner.cc.

References PLearn::PLearner::build(), and build_().

Referenced by PLearn::SVMClassificationTorch::build(), and PLearn::SVMClassificationTorch::build_().

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::TorchLearner::build_ ( ) [private]

This does the actual building.

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 115 of file TorchLearner.cc.

References allocator, inputs, PLearn::PLearner::inputsize_, machine, and outputsize_.

Referenced by build().

{
    // ### This method should do the real building of the object,
    // ### according to set 'options', in *any* situation. 
    // ### Typical situations include:
    // ###  - Initial building of an object from a few user-specified options
    // ###  - Building of a "reloaded" object: i.e. from the complete set of all serialised options.
    // ###  - Updating or "re-building" of an object after a few "tuning" options have been modified.
    // ### You should assume that the parent class' build_() has already been called.
    if (machine && machine->machine->outputs)
        outputsize_ = machine->machine->outputs->frame_size;
    // Initialize the inputs sequence.
    if (inputsize_ >= 0 && (!inputs || inputs->frame_size != inputsize_)) {
        allocator->free(inputs); // Free old input sequence.
        inputs = new(allocator) Torch::Sequence(1, inputsize_);
    }
}

Here is the caller graph for this function:

string PLearn::TorchLearner::classname ( ) const [virtual]

Reimplemented from PLearn::Object.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 65 of file TorchLearner.cc.

void PLearn::TorchLearner::computeCostsFromOutputs ( const Vec input,
const Vec output,
const Vec target,
Vec costs 
) const [virtual]

Computes the costs from already computed output.

Implements PLearn::PLearner.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 136 of file TorchLearner.cc.

{
    // No cost computed for now.
}                                
void PLearn::TorchLearner::computeOutput ( const Vec input,
Vec output 
) const [virtual]

Computes the output from the input.

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 145 of file TorchLearner.cc.

References PLearn::TVec< T >::data(), inputs, machine, outputsize_, PLASSERT, and PLearn::TVec< T >::resize().

Referenced by PLearn::SVMClassificationTorch::computeOutput().

{
    PLASSERT( outputsize_ >= 0);
    output.resize(outputsize_);
    inputs->copyFrom(input.data());
    machine->forward(inputs);
    machine->machine->outputs->copyTo(output.data());
}    

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::TorchLearner::declareOptions ( OptionList ol) [static, protected]

Declares this class' options.

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 70 of file TorchLearner.cc.

References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::PLearner::declareOptions(), PLearn::OptionBase::learntoption, machine, PLearn::OptionBase::nosave, PLearn::PLearner::nstages, outputsize_, PLearn::redeclareOption(), PLearn::PLearner::seed_, and trainer.

Referenced by PLearn::SVMClassificationTorch::declareOptions().

{
    // ### 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, "machine", &TorchLearner::machine, OptionBase::buildoption,
                  "The Torch learning machine.");

    declareOption(ol, "trainer", &TorchLearner::trainer, OptionBase::buildoption,
                  "The Torch trainer, responsible for training the machine.");

    // Learnt options.

    declareOption(ol, "outputsize", &TorchLearner::outputsize_, OptionBase::learntoption,
                  "Saves the output size of this learner for faster access.");

    // Now call the parent class' declareOptions.
    inherited::declareOptions(ol);

    // Hide unused parent's options.

    redeclareOption(ol, "seed", &TorchLearner::seed_, OptionBase::nosave,
                    "Torch learners in general will not use the PLearn seed.");

    redeclareOption(ol, "nstages", &TorchLearner::nstages, OptionBase::nosave,
                    "A Torch learner is usually only trained on one stage.");

}

Here is the call graph for this function:

Here is the caller graph for this function:

static const PPath& PLearn::TorchLearner::declaringFile ( ) [inline, static]

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 127 of file TorchLearner.h.

TorchLearner * PLearn::TorchLearner::deepCopy ( CopiesMap copies) const [virtual]

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 65 of file TorchLearner.cc.

void PLearn::TorchLearner::forget ( ) [virtual]

(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!).

Reimplemented from PLearn::PLearner.

Definition at line 157 of file TorchLearner.cc.

References machine, outputsize_, and PLearn::PLearner::stage.

{
    stage = 0;
    outputsize_ = -1;
    if (machine)
        machine->reset();
}
OptionList & PLearn::TorchLearner::getOptionList ( ) const [virtual]

Reimplemented from PLearn::Object.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 65 of file TorchLearner.cc.

OptionMap & PLearn::TorchLearner::getOptionMap ( ) const [virtual]

Reimplemented from PLearn::Object.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 65 of file TorchLearner.cc.

RemoteMethodMap & PLearn::TorchLearner::getRemoteMethodMap ( ) const [virtual]

Reimplemented from PLearn::Object.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 65 of file TorchLearner.cc.

TVec< string > PLearn::TorchLearner::getTestCostNames ( ) const [virtual]

Returns the names of the costs computed by computeCostsFromOutpus (and thus the test method).

Implements PLearn::PLearner.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 168 of file TorchLearner.cc.

{
    // No cost computed for now.
    return TVec<string>();
}
TVec< string > PLearn::TorchLearner::getTrainCostNames ( ) const [virtual]

Returns the names of the objective costs that the train method computes and for which it updates the VecStatsCollector train_stats.

Implements PLearn::PLearner.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 177 of file TorchLearner.cc.

Referenced by PLearn::SVMClassificationTorch::getTrainCostNames().

{
    // No cost computed for now.
    return TVec<string>();
}

Here is the caller graph for this function:

void PLearn::TorchLearner::makeDeepCopyFromShallowCopy ( CopiesMap copies) [virtual]

Transforms a shallow copy into a deep copy.

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 186 of file TorchLearner.cc.

References PLearn::PLearner::makeDeepCopyFromShallowCopy(), and PLERROR.

Referenced by PLearn::SVMClassificationTorch::makeDeepCopyFromShallowCopy().

{
    inherited::makeDeepCopyFromShallowCopy(copies);

    // ### ex:
    // deepCopyField(trainvec, copies);

    // ### Remove this line when you have fully implemented this method.
    PLERROR("TorchLearner::makeDeepCopyFromShallowCopy not fully (correctly) implemented yet!");
}

Here is the call graph for this function:

Here is the caller graph for this function:

int PLearn::TorchLearner::outputsize ( ) const [virtual]

Returns the size of this learner's output, (which typically may depend on its inputsize(), targetsize() and set options).

Implements PLearn::PLearner.

Definition at line 200 of file TorchLearner.cc.

References machine, outputsize_, and PLASSERT.

{
    // Compute and return the size of this learner's output, (which typically
    // may depend on its inputsize(), targetsize() and set options).
    PLASSERT( machine );
    PLASSERT( outputsize_ >= 0 || machine->machine->outputs );
    if (outputsize_ >=0)
        return outputsize_;
    return machine->machine->outputs->frame_size;
}
void PLearn::TorchLearner::setTrainingSet ( VMat  training_set,
bool  call_forget = true 
) [virtual]

Overridden to convert to a Torch dataset.

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 214 of file TorchLearner.cc.

References allocator, inputs, PLearn::PLearner::setTrainingSet(), and torch_train_set.

Referenced by PLearn::SVMClassificationTorch::setTrainingSet().

                                                                     {
    inherited::setTrainingSet(training_set, call_forget);
    torch_train_set = new TTorchDataSetFromVMat(training_set);
    allocator->free(inputs); // Free old input sequence.
    inputs = new(allocator) Torch::Sequence(1, training_set->inputsize());
}

Here is the call graph for this function:

Here is the caller graph for this function:

void PLearn::TorchLearner::train ( ) [virtual]

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.

Implements PLearn::PLearner.

Definition at line 224 of file TorchLearner.cc.

References machine, PLearn::PLearner::nstages, outputsize_, PLERROR, PLWARNING, PLearn::PLearner::stage, torch_train_set, and trainer.

{
    if (stage >= nstages) {
        PLWARNING("In TorchLearner::train - Learner has already been trained, skipping training");
        return;
    }
    if (!trainer || !machine)
        PLERROR("In TorchLearner::train - You must set both the 'trainer' and 'machine' options "
                "before calling train()");
    trainer->train((TTorchDataSetFromVMat*) torch_train_set);
    if (machine->machine->outputs)
        // Update outputsize_
        outputsize_ = machine->machine->outputs->frame_size;
    stage = 1;
}

Member Data Documentation

Reimplemented from PLearn::PLearner.

Reimplemented in PLearn::SVMClassificationTorch.

Definition at line 127 of file TorchLearner.h.

Torch::Allocator* PLearn::TorchLearner::allocator [protected]

Allocator for Torch memory management.

Definition at line 76 of file TorchLearner.h.

Referenced by build_(), setTrainingSet(), TorchLearner(), and ~TorchLearner().

Torch::Sequence* PLearn::TorchLearner::inputs [protected]

A sequence to provide inputs to the Torch machine.

Definition at line 79 of file TorchLearner.h.

Referenced by build_(), computeOutput(), setTrainingSet(), and TorchLearner().

Definition at line 71 of file TorchLearner.h.

Referenced by build_(), computeOutput(), declareOptions(), forget(), outputsize(), and train().

PP<TTorchDataSetFromVMat> PLearn::TorchLearner::torch_train_set [protected]

The Torch version of the training set.

Definition at line 82 of file TorchLearner.h.

Referenced by setTrainingSet(), and train().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines