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

RBFN (Radial Basis Function Network) function approximator. More...

#include <FunctionApproximatorRBFN.hpp>

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

Public Member Functions

 FunctionApproximatorRBFN (const MetaParametersRBFN *const meta_parameters, const ModelParametersRBFN *const model_parameters=NULL)
 Initialize a function approximator with meta- and model-parameters. More...
 
 FunctionApproximatorRBFN (const ModelParametersRBFN *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...
 
void preallocateMemory (int n_basis_functions)
 Preallocate memory to make certain functions real-time. More...
 
std::string getName (void) const
 Get the name of this function approximator. More...
 
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 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...
 
- 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

RBFN (Radial Basis Function Network) function approximator.

Definition at line 43 of file FunctionApproximatorRBFN.hpp.

Constructor & Destructor Documentation

FunctionApproximatorRBFN ( const MetaParametersRBFN *const  meta_parameters,
const ModelParametersRBFN *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 51 of file FunctionApproximatorRBFN.cpp.

52 :
53  FunctionApproximator(meta_parameters,model_parameters)
54 {
55  if (model_parameters!=NULL)
56  preallocateMemory(model_parameters->getNumberOfBasisFunctions());
57 }
void preallocateMemory(int n_basis_functions)
Preallocate memory to make certain functions real-time.
FunctionApproximator(void)
Default constructor.

Here is the call graph for this function:

FunctionApproximatorRBFN ( const ModelParametersRBFN *const  model_parameters)

Initialize a function approximator with model parameters.

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

Definition at line 59 of file FunctionApproximatorRBFN.cpp.

60 :
61  FunctionApproximator(model_parameters)
62 {
63  preallocateMemory(model_parameters->getNumberOfBasisFunctions());
64 }
void preallocateMemory(int n_basis_functions)
Preallocate memory to make certain functions real-time.
FunctionApproximator(void)
Default constructor.

Here is the call graph for this function:

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 74 of file FunctionApproximatorRBFN.cpp.

74  {
75  // All error checking and cloning is left to the FunctionApproximator constructor.
76  return new FunctionApproximatorRBFN(
77  dynamic_cast<const MetaParametersRBFN*>(getMetaParameters()),
78  dynamic_cast<const ModelParametersRBFN*>(getModelParameters())
79  );
80 };
FunctionApproximatorRBFN(const MetaParametersRBFN *const meta_parameters, const ModelParametersRBFN *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 82 of file FunctionApproximatorRBFN.cpp.

83 {
84  if (isTrained())
85  {
86  cerr << "WARNING: You may not call FunctionApproximatorRBFN::train more than once. Doing nothing." << endl;
87  cerr << " (if you really want to retrain, call reTrain function instead)" << endl;
88  return;
89  }
90 
91  assert(inputs.rows() == targets.rows());
92  assert(inputs.cols()==getExpectedInputDim());
93 
94  const MetaParametersRBFN* meta_parameters_rbfn =
95  dynamic_cast<const MetaParametersRBFN*>(getMetaParameters());
96 
97  // Determine the centers and widths of the basis functions, given the range of the input data
98  VectorXd min = inputs.colwise().minCoeff();
99  VectorXd max = inputs.colwise().maxCoeff();
100  MatrixXd centers, widths;
101  meta_parameters_rbfn->getCentersAndWidths(min,max,centers,widths);
102 
103  // Get the activations of the basis functions
104  bool normalized_basis_functions=false;
105  bool asymmetric_kernels=false;
106  int n_samples = inputs.rows();
107  int n_kernels = centers.rows();
108  MatrixXd activations(n_samples,n_kernels);
109  BasisFunction::Gaussian::activations(centers,widths,inputs,activations,
110  normalized_basis_functions,asymmetric_kernels);
111 
112  // Least squares, with activations as design matrix
113  bool use_offset=false;
114  double regularization = meta_parameters_rbfn->regularization();
115  VectorXd weights = leastSquares(activations,targets,use_offset,regularization);
116 
117  setModelParameters(new ModelParametersRBFN(centers,widths,weights));
118 
119  preallocateMemory(n_kernels);
120 
121 }
bool isTrained(void) const
Determine whether the function approximator has already been trained with data or not...
void preallocateMemory(int n_basis_functions)
Preallocate memory to make certain functions real-time.
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.
void activations(const std::vector< Eigen::VectorXd > &mus, const std::vector< Eigen::MatrixXd > &covars, std::vector< double > priors, const Eigen::Ref< const Eigen::MatrixXd > &inputs, Eigen::MatrixXd &kernel_activations, bool normalized_basis_functions=false)
Get the kernel activations for given centers, widths and inputs.
Eigen::MatrixXd leastSquares(const Eigen::Ref< const Eigen::MatrixXd > &inputs, const Eigen::Ref< const Eigen::MatrixXd > &targets, bool use_offset, double regularization)
(Regularized) least squares with bias

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 123 of file FunctionApproximatorRBFN.cpp.

124 {
125  if (!isTrained())
126  {
127  cerr << "WARNING: You may not call FunctionApproximatorLWPR::predict if you have not trained yet. Doing nothing." << endl;
128  return;
129  }
130 
131  const ModelParametersRBFN* model_parameters_rbfn = static_cast<const ModelParametersRBFN*>(getModelParameters());
132 
133  model_parameters_rbfn->weights(weights_prealloc_);
134 
135  int n_basis_functions = model_parameters_rbfn->getNumberOfBasisFunctions();
136 
137  bool only_one_sample = (inputs.rows()==1);
138  if (only_one_sample)
139  {
140  ENTERING_REAL_TIME_CRITICAL_CODE
141 
142  // Get the basis function activations
143  model_parameters_rbfn->kernelActivations(inputs,activations_one_prealloc_);
144 
145  // Weight the basis function activations
146  for (int b=0; b<n_basis_functions; b++)
147  activations_one_prealloc_.col(b).array() *= weights_prealloc_(b);
148 
149  // Sum over weighed basis functions
150  outputs = activations_one_prealloc_.rowwise().sum();
151 
152  EXITING_REAL_TIME_CRITICAL_CODE
153  }
154  else
155  {
156  int n_time_steps = inputs.rows();
157 
158  // The next two lines may not be real-time, as they may allocate memory.
159  // (if the size are already correct, it will be realtime)
160  activations_prealloc_.resize(n_time_steps,n_basis_functions);
161  outputs.resize(n_time_steps,getExpectedOutputDim());
162 
163  // Get the basis function activations
164  model_parameters_rbfn->kernelActivations(inputs,activations_prealloc_);
165 
166  // Weight the basis function activations
167  for (int b=0; b<n_basis_functions; b++)
168  activations_prealloc_.col(b).array() *= weights_prealloc_(b);
169 
170  // Sum over weighed basis functions
171  outputs = activations_prealloc_.rowwise().sum();
172  }
173 
174 }
bool isTrained(void) const
Determine whether the function approximator has already been trained with data or not...
int getExpectedOutputDim(void) const
The expected dimensionality of the output data.
const ModelParameters * getModelParameters(void) const
Accessor for FunctionApproximator::model_parameters_.

Here is the call graph for this function:

void preallocateMemory ( int  n_basis_functions)

Preallocate memory to make certain functions real-time.

Parameters
[in]n_basis_functionsNumber of basis functions in the RBFN.

Definition at line 66 of file FunctionApproximatorRBFN.cpp.

67 {
68  weights_prealloc_ = VectorXd(n_basis_functions);
69  activations_one_prealloc_ = MatrixXd(1,n_basis_functions);
70  activations_prealloc_ = MatrixXd(1,n_basis_functions);
71 }
std::string getName ( void  ) const
inlinevirtual

Get the name of this function approximator.

Returns
Name of this function approximator

Implements FunctionApproximator.

Definition at line 73 of file FunctionApproximatorRBFN.hpp.

73  {
74  return std::string("RBFN");
75  };

Here is the call graph for this function:

bool saveGridData ( const Eigen::VectorXd &  min,
const Eigen::VectorXd &  max,
const Eigen::VectorXi &  n_samples_per_dim,
std::string  directory,
bool  overwrite = false 
) const
virtual

Generate a grid of inputs, and output the response of the basis functions and line segments for these inputs.

This function is not pure virtual, because this might not make sense for every model parameters class.

Parameters
[in]minMinimum values for the grid (one for each dimension)
[in]maxMaximum values for the grid (one for each dimension)
[in]n_samples_per_dimNumber of samples in the grid along each dimension
[in]directoryDirectory to which to save the results to.
[in]overwriteWhether to overwrite existing files. true=do overwrite, false=don't overwrite and give a warning.
Returns
Whether saving the data was successful.

Reimplemented from FunctionApproximator.

Definition at line 176 of file FunctionApproximatorRBFN.cpp.

177 {
178  if (save_directory.empty())
179  return true;
180 
181  MatrixXd inputs_grid;
182  FunctionApproximator::generateInputsGrid(min, max, n_samples_per_dim, inputs_grid);
183 
184  const ModelParametersRBFN* model_parameters_rbfn = static_cast<const ModelParametersRBFN*>(getModelParameters());
185 
186  MatrixXd activations_grid;
187  model_parameters_rbfn->kernelActivations(inputs_grid, activations_grid);
188 
189  saveMatrix(save_directory,"n_samples_per_dim.txt",n_samples_per_dim,overwrite);
190  saveMatrix(save_directory,"inputs_grid.txt",inputs_grid,overwrite);
191  saveMatrix(save_directory,"activations_grid.txt",activations_grid,overwrite);
192 
193  // Weight the basis function activations
194  VectorXd weights = model_parameters_rbfn->weights();
195  for (int b=0; b<activations_grid.cols(); b++)
196  activations_grid.col(b).array() *= weights(b);
197  saveMatrix(save_directory,"activations_weighted_grid.txt",activations_grid,overwrite);
198 
199  // Sum over weighed basis functions
200  MatrixXd predictions_grid = activations_grid.rowwise().sum();
201  saveMatrix(save_directory,"predictions_grid.txt",predictions_grid,overwrite);
202 
203  return true;
204 
205 }
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&#39;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.
bool saveMatrix(std::string filename, Eigen::Matrix< Scalar, RowsAtCompileTime, ColsAtCompileTime > matrix, bool overwrite=false)
Save an Eigen matrix to an ASCII file.
const ModelParameters * getModelParameters(void) const
Accessor for FunctionApproximator::model_parameters_.

Here is the call graph for this function:

Friends And Related Function Documentation

friend class boost::serialization::access
friend

Give boost serialization access to private members.

Definition at line 98 of file FunctionApproximatorRBFN.hpp.


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