|
PLearn 0.1
|
Unidimensional cubic spline learner. More...
#include <CubicSpline.h>


Public Member Functions | |
| CubicSpline () | |
| Default constructor. | |
| virtual int | outputsize () const |
| Returns the size of this learner's output: 1 -> a single interpolated value. | |
| 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 () |
| Fit the splines to the *last* input point. | |
| 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 string | classname () const |
| virtual OptionList & | getOptionList () const |
| virtual OptionMap & | getOptionMap () const |
| virtual RemoteMethodMap & | getRemoteMethodMap () const |
| virtual CubicSpline * | deepCopy (CopiesMap &copies) const |
| virtual void | build () |
| Finish building the object; just call inherited::build followed by build_() | |
| virtual void | makeDeepCopyFromShallowCopy (CopiesMap &copies) |
| Transforms a shallow copy into a deep copy. | |
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 | m_low_slope |
| The slope to enforce at the leftmost node -- Default: NaN [None]. | |
| real | m_high_slope |
| The slope to enforce at the rightmost node -- Default: NaN [None]. | |
Static Public Attributes | |
| static StaticInitializer | _static_initializer_ |
Static Protected Member Functions | |
| static void | declareOptions (OptionList &ol) |
| Declares the class options. | |
Protected Attributes | |
| Vec | m_inputs |
| A buffer containing the last training set inputs. | |
| Vec | m_targets |
| A buffer containing the last training set targets. | |
| Vec | m_coefficients |
| The learnt coefficients. | |
Private Types | |
| typedef PLearner | inherited |
Private Member Functions | |
| void | build_ () |
| This does the actual building. | |
Unidimensional cubic spline learner.
This learner fits a unidimensional cubic spline to a given set of points. That is, inputsize() must be one and the inputs are considered to be the x values, while targetsize() must also be one and the targets are considered to be the y values. The spline is fitted to the (x,y)-pairs so formed. X values don't need to be ordered; the ordering is ensured within the train method.
Definition at line 60 of file CubicSpline.h.
typedef PLearner PLearn::CubicSpline::inherited [private] |
Reimplemented from PLearn::PLearner.
Definition at line 62 of file CubicSpline.h.
| PLearn::CubicSpline::CubicSpline | ( | ) |
Default constructor.
Definition at line 56 of file CubicSpline.cc.
: m_low_slope(MISSING_VALUE), m_high_slope(MISSING_VALUE) { // Nothing to do here }
| string PLearn::CubicSpline::_classname_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 54 of file CubicSpline.cc.
| OptionList & PLearn::CubicSpline::_getOptionList_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 54 of file CubicSpline.cc.
| RemoteMethodMap & PLearn::CubicSpline::_getRemoteMethodMap_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 54 of file CubicSpline.cc.
Reimplemented from PLearn::PLearner.
Definition at line 54 of file CubicSpline.cc.
| Object * PLearn::CubicSpline::_new_instance_for_typemap_ | ( | ) | [static] |
Reimplemented from PLearn::Object.
Definition at line 54 of file CubicSpline.cc.
| StaticInitializer CubicSpline::_static_initializer_ & PLearn::CubicSpline::_static_initialize_ | ( | ) | [static] |
Reimplemented from PLearn::PLearner.
Definition at line 54 of file CubicSpline.cc.
| void PLearn::CubicSpline::build | ( | ) | [virtual] |
Finish building the object; just call inherited::build followed by build_()
Reimplemented from PLearn::PLearner.
Definition at line 96 of file CubicSpline.cc.
References PLearn::PLearner::build(), and build_().
{
inherited::build();
build_();
}

| void PLearn::CubicSpline::build_ | ( | ) | [private] |
This does the actual building.
Reimplemented from PLearn::PLearner.
Definition at line 90 of file CubicSpline.cc.
Referenced by build().
{
// Nothing to do here
}

| string PLearn::CubicSpline::classname | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 54 of file CubicSpline.cc.
| void PLearn::CubicSpline::computeCostsFromOutputs | ( | const Vec & | input, |
| const Vec & | output, | ||
| const Vec & | target, | ||
| Vec & | costs | ||
| ) | const [virtual] |
Computes the costs from already computed output.
Implements PLearn::PLearner.
Definition at line 202 of file CubicSpline.cc.
{
// No costs...
}
Computes the output from the input.
Reimplemented from PLearn::PLearner.
Definition at line 183 of file CubicSpline.cc.
References a, b, PLearn::TVec< T >::findSorted(), PLearn::TVec< T >::length(), m_coefficients, m_inputs, m_targets, PLearn::max(), PLearn::min(), n, PLASSERT, PLearn::TVec< T >::resize(), and x.
{
PLASSERT( input.length() == 1 );
output.resize(1);
int n = m_inputs.length();
real x = input[0];
int pos = min( max(1, m_inputs.findSorted(x)), n-1 );
real h = m_inputs[pos] - m_inputs[pos-1];
PLASSERT( h > 0.0 );
real a = (m_inputs[pos] - x) / h;
real b = (x - m_inputs[pos-1]) / h;
output[0] = a*m_targets[pos-1] + b*m_targets[pos]
+ ((a*a*a - a)*m_coefficients[pos-1]
+ (b*b*b - b)*m_coefficients[pos]) * h*h/6.0;
}

| void PLearn::CubicSpline::declareOptions | ( | OptionList & | ol | ) | [static, protected] |
Declares the class options.
Reimplemented from PLearn::PLearner.
Definition at line 63 of file CubicSpline.cc.
References PLearn::OptionBase::buildoption, PLearn::declareOption(), PLearn::PLearner::declareOptions(), PLearn::OptionBase::learntoption, m_coefficients, m_high_slope, m_inputs, m_low_slope, and m_targets.
{
declareOption(ol, "low_slope", &CubicSpline::m_low_slope,
OptionBase::buildoption,
"The slope to enforce at the leftmost node -- Default: NaN [None]");
declareOption(ol, "high_slope", &CubicSpline::m_high_slope,
OptionBase::buildoption,
"The slope to enforce at the rightmost node -- Default: NaN [None]");
// Learnt options
declareOption(ol, "inputs", &CubicSpline::m_inputs,
OptionBase::learntoption,
"A buffer containing the last training set inputs");
declareOption(ol, "targets", &CubicSpline::m_targets,
OptionBase::learntoption,
"A buffer containing the last training set targets");
declareOption(ol, "coefficients", &CubicSpline::m_coefficients,
OptionBase::learntoption,
"The learnt coefficients");
// Now call the parent class' declareOptions
inherited::declareOptions(ol);
}

| static const PPath& PLearn::CubicSpline::declaringFile | ( | ) | [inline, static] |
Reimplemented from PLearn::PLearner.
Definition at line 113 of file CubicSpline.h.
:
//##### Protected Options ###############################################
| CubicSpline * PLearn::CubicSpline::deepCopy | ( | CopiesMap & | copies | ) | const [virtual] |
Reimplemented from PLearn::PLearner.
Definition at line 54 of file CubicSpline.cc.
| void PLearn::CubicSpline::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 118 of file CubicSpline.cc.
References PLearn::PLearner::forget(), m_coefficients, and PLearn::TVec< T >::resize().
{
m_coefficients->resize(0);
inherited::forget();
}

| OptionList & PLearn::CubicSpline::getOptionList | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 54 of file CubicSpline.cc.
| OptionMap & PLearn::CubicSpline::getOptionMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 54 of file CubicSpline.cc.
| RemoteMethodMap & PLearn::CubicSpline::getRemoteMethodMap | ( | ) | const [virtual] |
Reimplemented from PLearn::Object.
Definition at line 54 of file CubicSpline.cc.
| TVec< string > PLearn::CubicSpline::getTestCostNames | ( | ) | const [virtual] |
Returns the names of the costs computed by computeCostsFromOutpus (and thus the test method).
[Empty]
Implements PLearn::PLearner.
Definition at line 208 of file CubicSpline.cc.
{
// None for now
return TVec<string>();
}
| TVec< string > PLearn::CubicSpline::getTrainCostNames | ( | ) | const [virtual] |
Returns the names of the objective costs that the train method computes and for which it updates the VecStatsCollector train_stats.
[Empty]
Implements PLearn::PLearner.
Definition at line 214 of file CubicSpline.cc.
{
// None for now
return TVec<string>();
}
| void PLearn::CubicSpline::makeDeepCopyFromShallowCopy | ( | CopiesMap & | copies | ) | [virtual] |
Transforms a shallow copy into a deep copy.
Reimplemented from PLearn::PLearner.
Definition at line 103 of file CubicSpline.cc.
References PLearn::deepCopyField(), m_coefficients, m_inputs, m_targets, and PLearn::PLearner::makeDeepCopyFromShallowCopy().
{
inherited::makeDeepCopyFromShallowCopy(copies);
deepCopyField(m_inputs, copies);
deepCopyField(m_targets, copies);
deepCopyField(m_coefficients, copies);
}

| int PLearn::CubicSpline::outputsize | ( | ) | const [virtual] |
Returns the size of this learner's output: 1 -> a single interpolated value.
Implements PLearn::PLearner.
Definition at line 113 of file CubicSpline.cc.
References PLearn::PLearner::inputsize().
{
return inputsize();
}

| void PLearn::CubicSpline::train | ( | ) | [virtual] |
Fit the splines to the *last* input point.
Implements PLearn::PLearner.
Definition at line 124 of file CubicSpline.cc.
References PLearn::TVec< T >::fill(), PLearn::VMat::getColumn(), i, PLearn::PLearner::inputsize(), PLearn::is_missing(), PLearn::VMat::length(), m_coefficients, m_high_slope, m_inputs, m_low_slope, m_targets, n, PLASSERT, PLearn::TVec< T >::resize(), PLearn::TVec< T >::sortingPermutation(), PLearn::PLearner::targetsize(), PLearn::PLearner::train_set, u, and PLearn::VMat::width().
{
// This learner fits unidimensional inputs/targets
PLASSERT( inputsize() == 1 );
PLASSERT( targetsize() == 1 );
// Train set is a member of PLearner; set through setTrainingSet()
int n = train_set->length();
m_inputs = train_set.getColumn(0);
m_targets = train_set.getColumn(1);
PLASSERT( n >= 2 && train_set->width() == 2 );
// Sort the inputs and targets along the inputs values
TVec<int> indices = m_inputs.sortingPermutation();
m_inputs = m_inputs(indices);
m_targets = m_targets(indices);
Vec u(n-1, 0.0);
m_coefficients.resize(n);
m_coefficients.fill(0.0);
// Low slope hack
if ( !is_missing(m_low_slope) ) {
u[0] = (3.0/(m_inputs[1]-m_inputs[0])) *
( (m_targets[1]-m_targets[0])/
(m_inputs[1] - m_inputs[0]) - m_low_slope );
m_coefficients[0] = -0.5;
}
// Forward pass on coefficients
for (int i=1; i < (n-1); i++) {
real sig = (m_inputs[i]-m_inputs[i-1]) / (m_inputs[i+1]-m_inputs[i-1]);
real p = sig*m_coefficients[i-1]+2.0;
m_coefficients[i] = (sig-1.0)/p;
u[i] = (m_targets[i+1]-m_targets[i]) / (m_inputs[i+1]-m_inputs[i])
- (m_targets[i]-m_targets[i-1]) / (m_inputs[i]-m_inputs[i-1]);
u[i] = (6.0*u[i]/(m_inputs[i+1]-m_inputs[i-1]) - sig*u[i-1]) / p;
}
// High slope hack
real un=0, qn=0;
if ( !is_missing(m_high_slope) ) {
qn = 0.5;
un = (3.0/(m_inputs[n-1]-m_inputs[n-2])) *
(m_high_slope - (m_targets[n-1]-m_targets[n-2]) / (m_inputs[n-1]-m_inputs[n-2]));
}
// Compute the last coefficient
m_coefficients[n-1] = (un-qn*u[n-2])/(qn*m_coefficients[n-1]+1.0);
// Backsubstitution step
for (int k=n-2; k >= 0; k--)
m_coefficients[k] = m_coefficients[k]*m_coefficients[k+1]+u[k];
}

Reimplemented from PLearn::PLearner.
Definition at line 113 of file CubicSpline.h.
Vec PLearn::CubicSpline::m_coefficients [protected] |
The learnt coefficients.
Definition at line 131 of file CubicSpline.h.
Referenced by computeOutput(), declareOptions(), forget(), makeDeepCopyFromShallowCopy(), and train().
The slope to enforce at the rightmost node -- Default: NaN [None].
Definition at line 71 of file CubicSpline.h.
Referenced by declareOptions(), and train().
Vec PLearn::CubicSpline::m_inputs [protected] |
A buffer containing the last training set inputs.
Definition at line 125 of file CubicSpline.h.
Referenced by computeOutput(), declareOptions(), makeDeepCopyFromShallowCopy(), and train().
The slope to enforce at the leftmost node -- Default: NaN [None].
Definition at line 68 of file CubicSpline.h.
Referenced by declareOptions(), and train().
Vec PLearn::CubicSpline::m_targets [protected] |
A buffer containing the last training set targets.
Definition at line 128 of file CubicSpline.h.
Referenced by computeOutput(), declareOptions(), makeDeepCopyFromShallowCopy(), and train().
1.7.4