DMP_BBO library
Public Member Functions | Friends | List of all members

LWPR (Locally Weighted Projection Regression) function approximator. More...

#include <FunctionApproximatorLWPR.hpp>

Inheritance diagram for FunctionApproximatorLWPR:
Inheritance graph
[legend]
Collaboration diagram for FunctionApproximatorLWPR:
Collaboration graph
[legend]

Public Member Functions

 FunctionApproximatorLWPR (const MetaParametersLWPR *const meta_parameters, const ModelParametersLWPR *const model_parameters=NULL)
 Initialize a function approximator with meta- and model-parameters. More...
 
 FunctionApproximatorLWPR (const ModelParametersLWPR *const model_parameters)
 Initialize a function approximator with model parameters. More...
 
FunctionApproximatorclone (void) const
 Return a pointer to a deep copy of the FunctionApproximator object. More...
 
void train (const Eigen::Ref< const Eigen::MatrixXd > &inputs, const Eigen::Ref< const Eigen::MatrixXd > &targets)
 Train the function approximator with corresponding input and target examples. More...
 
void predict (const Eigen::Ref< const Eigen::MatrixXd > &inputs, Eigen::MatrixXd &output)
 Query the function approximator to make a prediction. More...
 
std::string getName (void) const
 Get the name of this function approximator. More...
 
void set_print_training_progress (bool print_training_progress)
 Print some output to cout during training. More...
 
- Public Member Functions inherited from FunctionApproximator
 FunctionApproximator (const MetaParameters *const meta_parameters, const ModelParameters *const model_parameters=NULL)
 Initialize a function approximator with meta- and optionally model-parameters. More...
 
 FunctionApproximator (const ModelParameters *const model_parameters)
 Initialize a function approximator with model-parameters. More...
 
void train (const Eigen::Ref< const Eigen::MatrixXd > &inputs, const Eigen::Ref< const Eigen::MatrixXd > &targets, std::string save_directory, bool overwrite=false)
 Train the function approximator with corresponding input and target examples (and write results to file). More...
 
void reTrain (const Eigen::Ref< const Eigen::MatrixXd > &inputs, const Eigen::Ref< const Eigen::MatrixXd > &targets)
 Re-train the function approximator with corresponding input and target examples. More...
 
void reTrain (const Eigen::Ref< const Eigen::MatrixXd > &inputs, const Eigen::Ref< const Eigen::MatrixXd > &targets, std::string save_directory, bool overwrite=false)
 Re-train the function approximator with corresponding input and target examples (and write results to file). More...
 
virtual void predict (const Eigen::Ref< const Eigen::MatrixXd > &inputs, Eigen::MatrixXd &outputs, Eigen::MatrixXd &variances)
 Query the function approximator to make a prediction, and also to predict its variance. More...
 
virtual void predict (const Eigen::Ref< const Eigen::MatrixXd > &inputs, Eigen::MatrixXd &outputs, std::vector< Eigen::MatrixXd > &variances)
 Query the function approximator to make a prediction, and also to predict its variance. More...
 
virtual void predictVariance (const Eigen::Ref< const Eigen::MatrixXd > &inputs, Eigen::MatrixXd &variances)
 Query the function approximator to get the variance of a prediction This function is not implemented by all function approximators. More...
 
bool isTrained (void) const
 Determine whether the function approximator has already been trained with data or not. More...
 
int getExpectedInputDim (void) const
 The expected dimensionality of the input data. More...
 
int getExpectedOutputDim (void) const
 The expected dimensionality of the output data. More...
 
void getSelectableParameters (std::set< std::string > &selected_values_labels) const
 Return all the names of the parameter types that can be selected. More...
 
void setSelectedParameters (const std::set< std::string > &selected_values_labels)
 Determine which subset of parameters is represented in the vector returned by Parameterizable::getParameterVectorSelected. More...
 
void getParameterVectorSelectedMinMax (Eigen::VectorXd &min, Eigen::VectorXd &max) const
 Get the minimum and maximum of the selected parameters in one vector. More...
 
int getParameterVectorSelectedSize (void) const
 Get the size of the vector of selected parameters, as returned by getParameterVectorSelected(. More...
 
void setParameterVectorSelected (const Eigen::VectorXd &values, bool normalized=false)
 Set all the values of the selected parameters with one vector. More...
 
void getParameterVectorSelected (Eigen::VectorXd &values, bool normalized=false) const
 Get the values of the selected parameters in one vector. More...
 
void getParameterVectorMask (const std::set< std::string > selected_values_labels, Eigen::VectorXi &selected_mask) const
 Get a mask for selecting parameters. More...
 
int getParameterVectorAllSize (void) const
 Get the size of the parameter values vector when it contains all available parameter values. More...
 
void getParameterVectorAll (Eigen::VectorXd &values) const
 Return a vector that returns all available parameter values. More...
 
void setParameterVectorAll (const Eigen::VectorXd &values)
 Set all available parameter values with one vector. More...
 
UnifiedModelgetUnifiedModel (void) const
 Return a representation of this function approximator's model as a unified model. More...
 
std::string toString (void) const
 Returns a string representation of the object. More...
 
const MetaParametersgetMetaParameters (void) const
 Accessor for FunctionApproximator::meta_parameters_. More...
 
const ModelParametersgetModelParameters (void) const
 Accessor for FunctionApproximator::model_parameters_. More...
 
void setParameterVectorModifierPrivate (std::string modifier, bool new_value)
 Turn certain modifiers on or off, see Parameterizable::setParameterVectorModifier(). More...
 
virtual bool saveGridData (const Eigen::VectorXd &min, const Eigen::VectorXd &max, const Eigen::VectorXi &n_samples_per_dim, std::string directory, bool overwrite=false) const
 Generate a grid of inputs, and output the response of the basis functions and line segments for these inputs. More...
 
- Public Member Functions inherited from Parameterizable
virtual ~Parameterizable (void)
 Destructor.
 
virtual void getParameterVectorSelectedNormalized (Eigen::VectorXd &values) const
 Get the normalized values of the selected parameters in one vector. More...
 
void getParameterVectorSelectedMinMax (Eigen::VectorXd &min, Eigen::VectorXd &max) const
 Get the minimum and maximum of the selected parameters in one vector. More...
 
void getParameterVectorAllMinMax (Eigen::VectorXd &min, Eigen::VectorXd &max) const
 Get the minimum and maximum values of the current parameter vector. More...
 
void getParameterVectorSelectedRanges (Eigen::VectorXd &ranges) const
 Get the ranges of the selected parameters, i.e. More...
 
virtual void setParameterVectorSelectedNormalized (const Eigen::VectorXd &values)
 Set all the values of the selected parameters with one vector of normalized values. More...
 
void setSelectedParametersOne (std::string selected)
 Set the parameters that are currently selected. More...
 
void setParameterVectorModifier (std::string modifier, bool new_value)
 Turn certain modifiers on or off. More...
 
void setVectorLengthsPerDimension (const Eigen::VectorXi &lengths_per_dimension)
 The vector (VectorXd) with parameter values can be split into different parts (as vector<VectorXd>; this function specifices the length of each sub-vector. More...
 
Eigen::VectorXi getVectorLengthsPerDimension (void) const
 Get the specified length of each vector in each dimension. More...
 
void getParameterVectorSelected (std::vector< Eigen::VectorXd > &values, bool normalized=false) const
 Get the values of the selected parameters in one vector. More...
 
void setParameterVectorSelected (const std::vector< Eigen::VectorXd > &values, bool normalized=false)
 Set all the values of the selected parameters with a vector of vectors. More...
 

Friends

class boost::serialization::access
 Give boost serialization access to private members. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from FunctionApproximator
static void generateInputsGrid (const Eigen::VectorXd &min, const Eigen::VectorXd &max, const Eigen::VectorXi &n_samples_per_dim, Eigen::MatrixXd &inputs_grid)
 Generate a input samples that lie on a grid (much like Matlab's meshgrid) For instance, if min = [2 6], and max = [3 8], and n_samples_per_dim = [3 5] then this function first makes linearly spaces samples along each dimension, e.g. More...
 
- Protected Member Functions inherited from FunctionApproximator
void setModelParameters (ModelParameters *model_parameters)
 Accessor for FunctionApproximator::model_parameters_. More...
 
 FunctionApproximator (void)
 Default constructor. More...
 

Detailed Description

LWPR (Locally Weighted Projection Regression) function approximator.

Definition at line 44 of file FunctionApproximatorLWPR.hpp.

Constructor & Destructor Documentation

FunctionApproximatorLWPR ( const MetaParametersLWPR *const  meta_parameters,
const ModelParametersLWPR *const  model_parameters = NULL 
)

Initialize a function approximator with meta- and model-parameters.

Parameters
[in]meta_parametersThe training algorithm meta-parameters
[in]model_parametersThe parameters of the trained model. If this parameter is not passed, the function approximator is initialized as untrained. In this case, you must call FunctionApproximator::train() before being able to call FunctionApproximator::predict(). Either meta_parameters XOR model-parameters can passed as NULL, but not both.

Definition at line 48 of file FunctionApproximatorLWPR.cpp.

49 :
50  FunctionApproximator(meta_parameters,model_parameters),
51  print_training_progress_(false)
52 {
53 }
FunctionApproximator(void)
Default constructor.
FunctionApproximatorLWPR ( const ModelParametersLWPR *const  model_parameters)

Initialize a function approximator with model parameters.

Parameters
[in]model_parametersThe parameters of the (previously) trained model.

Definition at line 55 of file FunctionApproximatorLWPR.cpp.

56 :
57  FunctionApproximator(model_parameters),
58  print_training_progress_(false)
59 {
60 }
FunctionApproximator(void)
Default constructor.

Member Function Documentation

FunctionApproximator * clone ( void  ) const
virtual

Return a pointer to a deep copy of the FunctionApproximator object.

Returns
Pointer to a deep copy

Implements FunctionApproximator.

Definition at line 62 of file FunctionApproximatorLWPR.cpp.

62  {
63  // All error checking and cloning is left to the FunctionApproximator constructor.
65  dynamic_cast<const MetaParametersLWPR*>(getMetaParameters()),
66  dynamic_cast<const ModelParametersLWPR*>(getModelParameters())
67  );
68  fa_lwpr->set_print_training_progress(print_training_progress_);
69  return fa_lwpr;
70 };
FunctionApproximatorLWPR(const MetaParametersLWPR *const meta_parameters, const ModelParametersLWPR *const model_parameters=NULL)
Initialize a function approximator with meta- and model-parameters.
const MetaParameters * getMetaParameters(void) const
Accessor for FunctionApproximator::meta_parameters_.
const ModelParameters * getModelParameters(void) const
Accessor for FunctionApproximator::model_parameters_.

Here is the call graph for this function:

void train ( const Eigen::Ref< const Eigen::MatrixXd > &  inputs,
const Eigen::Ref< const Eigen::MatrixXd > &  targets 
)
virtual

Train the function approximator with corresponding input and target examples.

Parameters
[in]inputsInput values of the training examples
[in]targetsTarget values of the training examples

Implements FunctionApproximator.

Definition at line 72 of file FunctionApproximatorLWPR.cpp.

73 {
74  if (isTrained())
75  {
76  cerr << "WARNING: You may not call FunctionApproximatorLWPR::train more than once. Doing nothing." << endl;
77  cerr << " (if you really want to retrain, call reTrain function instead)" << endl;
78  return;
79  }
80 
81  assert(inputs.rows() == targets.rows()); // Must have same number of examples
82  assert(inputs.cols()==getExpectedInputDim());
83 
84  const MetaParametersLWPR* meta_parameters_lwpr = dynamic_cast<const MetaParametersLWPR*>(getMetaParameters());
85 
86  int n_in =inputs.cols();
87  int n_out=targets.cols();
88 
89  LWPR_Object* lwpr_object = new LWPR_Object(n_in, n_out);
90  lwpr_object->setInitD( meta_parameters_lwpr->init_D_[0]); // todo Fix this
91  lwpr_object->wGen( meta_parameters_lwpr->w_gen_);
92  lwpr_object->wPrune( meta_parameters_lwpr->w_prune_);
93  lwpr_object->updateD( meta_parameters_lwpr->update_D_);
94  lwpr_object->setInitAlpha(meta_parameters_lwpr->init_alpha_);
95  lwpr_object->penalty( meta_parameters_lwpr->penalty_);
96  lwpr_object->diagOnly( meta_parameters_lwpr->diag_only_);
97  lwpr_object->useMeta( meta_parameters_lwpr->use_meta_);
98  lwpr_object->metaRate( meta_parameters_lwpr->meta_rate_);
99  lwpr_object->kernel( meta_parameters_lwpr->kernel_name_.c_str());
100 
101 
102  vector<double> input_vector(n_in);
103  vector<double> target_vector(n_out);
104  int n_input_samples = inputs.rows();
105 
106  //http://stackoverflow.com/questions/15858569/randomly-permute-rows-columns-of-a-matrix-with-eigen
107  PermutationMatrix<Dynamic,Dynamic> permute(n_input_samples);
108  permute.setIdentity();
109  VectorXi shuffled_indices = VectorXi::LinSpaced(n_input_samples,0,n_input_samples-1);
110  MatrixXd outputs;
111  for (int iterations=0; iterations<50; iterations++)
112  {
113  random_shuffle(permute.indices().data(), permute.indices().data()+permute.indices().size());
114  shuffled_indices = (shuffled_indices.transpose()*permute).transpose();
115 
116  for (int ii=0; ii<n_input_samples; ii++)
117  {
118  // Eigen::VectorXd -> std::vector
119  Map<VectorXd>(input_vector.data(),n_in) = inputs.row(shuffled_indices[ii]);
120  Map<VectorXd>(target_vector.data(),n_out) = targets.row(shuffled_indices[ii]);
121  // update model
122  lwpr_object->update(input_vector, target_vector);
123  }
124 
125  if (print_training_progress_)
126  {
127  if (iterations%5==0)
128  {
129  FunctionApproximatorLWPR* fa_tmp = new FunctionApproximatorLWPR(new ModelParametersLWPR(new LWPR_Object(*lwpr_object)));
130  fa_tmp->predict(inputs, outputs);
131  delete fa_tmp;
132  MatrixXd abs_error = (targets.array()-outputs.array()).abs();
133  VectorXd mean_abs_error_per_output_dim = abs_error.colwise().mean();
134  cout << "Iteration " << iterations << " MEA=" << mean_abs_error_per_output_dim << endl;
135  }
136  }
137 
138  }
139  setModelParameters(new ModelParametersLWPR(lwpr_object));
140 
141 }
bool isTrained(void) const
Determine whether the function approximator has already been trained with data or not...
FunctionApproximatorLWPR(const MetaParametersLWPR *const meta_parameters, const ModelParametersLWPR *const model_parameters=NULL)
Initialize a function approximator with meta- and model-parameters.
const MetaParameters * getMetaParameters(void) const
Accessor for FunctionApproximator::meta_parameters_.
void setModelParameters(ModelParameters *model_parameters)
Accessor for FunctionApproximator::model_parameters_.
int getExpectedInputDim(void) const
The expected dimensionality of the input data.

Here is the call graph for this function:

void predict ( const Eigen::Ref< const Eigen::MatrixXd > &  inputs,
Eigen::MatrixXd &  outputs 
)
virtual

Query the function approximator to make a prediction.

Parameters
[in]inputsInput values of the query
[out]outputsPredicted output values
Remarks
This method should be const. But third party functions which is called in this function have not always been implemented as const (Examples: LWPRObject::predict or RRRFF::predict ). Therefore, this function cannot be const.

Implements FunctionApproximator.

Definition at line 144 of file FunctionApproximatorLWPR.cpp.

145 {
146  if (!isTrained())
147  {
148  cerr << "WARNING: You may not call FunctionApproximatorLWPR::predict if you have not trained yet. Doing nothing." << endl;
149  return;
150  }
151 
152  const ModelParametersLWPR* model_parameters_lwpr = static_cast<const ModelParametersLWPR*>(getModelParameters());
153 
154  int n_in = model_parameters_lwpr->lwpr_object_->model.nIn;
155  assert(inputs.cols()==n_in);
156 
157  int n_input_samples = inputs.rows();
158  int n_out = model_parameters_lwpr->lwpr_object_->model.nOut;
159 
160  outputs.resize(n_input_samples,n_out);
161 
162  // Allocate memory for the temporary vectors for LWPR_Object::predict
163  vector<double> input_vector(n_in);
164  vector<double> output_vector(n_out);
165 
166  // Do prediction for each sample
167  for (int ii=0; ii<n_input_samples; ii++)
168  {
169  // LWPR_Object::predict uses std::vector, so do some conversions here.
170  Map<VectorXd>(input_vector.data(),n_in) = inputs.row(ii); // Eigen::VectorXd -> std::vector
171  output_vector = model_parameters_lwpr->lwpr_object_->predict(input_vector);
172  outputs.row(ii) = Map<VectorXd>(&output_vector[0], n_out); // std::vector -> Eigen::VectorXd
173  }
174 
175 }
bool isTrained(void) const
Determine whether the function approximator has already been trained with data or not...
const ModelParameters * getModelParameters(void) const
Accessor for FunctionApproximator::model_parameters_.

Here is the call graph for this function:

std::string getName ( void  ) const
inlinevirtual

Get the name of this function approximator.

Returns
Name of this function approximator

Implements FunctionApproximator.

Definition at line 68 of file FunctionApproximatorLWPR.hpp.

68  {
69  return std::string("LWPR");
70  };
void set_print_training_progress ( bool  print_training_progress)
inline

Print some output to cout during training.

This can be useful to see if the error is decreasing consistently.

Parameters
[in]print_training_progresstrue: print to cout, false: do nothing.

Definition at line 77 of file FunctionApproximatorLWPR.hpp.

78  {
79  print_training_progress_ = print_training_progress;
80  }

Friends And Related Function Documentation

friend class boost::serialization::access
friend

Give boost serialization access to private members.

Definition at line 91 of file FunctionApproximatorLWPR.hpp.


The documentation for this class was generated from the following files: