Unverified Commit 66daff53 authored by Konstantinos Chatzilygeroudis's avatar Konstantinos Chatzilygeroudis Committed by GitHub
Browse files

Merge pull request #275 from resibots/multigp_serialize

MultiGP serialize
parents 8d30278f 4c7b9c0f
...@@ -225,7 +225,7 @@ namespace limbo { ...@@ -225,7 +225,7 @@ namespace limbo {
/// return the mean observation (only call this if the output of the GP is of dimension 1) /// return the mean observation (only call this if the output of the GP is of dimension 1)
Eigen::VectorXd mean_observation() const Eigen::VectorXd mean_observation() const
{ {
// TODO: Check if _dim_out is correct?! assert(_dim_out > 0);
return _samples.size() > 0 ? _mean_observation return _samples.size() > 0 ? _mean_observation
: Eigen::VectorXd::Zero(_dim_out); : Eigen::VectorXd::Zero(_dim_out);
} }
......
...@@ -106,6 +106,12 @@ namespace limbo { ...@@ -106,6 +106,12 @@ namespace limbo {
// compute the new observations for the GPs // compute the new observations for the GPs
std::vector<std::vector<Eigen::VectorXd>> obs(_dim_out); std::vector<std::vector<Eigen::VectorXd>> obs(_dim_out);
// compute mean observation
_mean_observation = Eigen::VectorXd::Zero(_dim_out);
for (size_t j = 0; j < _observations.size(); j++)
_mean_observation.array() += _observations[j].array();
_mean_observation.array() /= static_cast<double>(_observations.size());
for (size_t j = 0; j < observations.size(); j++) { for (size_t j = 0; j < observations.size(); j++) {
Eigen::VectorXd mean_vector = _mean_function(samples[j], *this); Eigen::VectorXd mean_vector = _mean_function(samples[j], *this);
assert(mean_vector.size() == _dim_out); assert(mean_vector.size() == _dim_out);
...@@ -154,6 +160,12 @@ namespace limbo { ...@@ -154,6 +160,12 @@ namespace limbo {
_observations.push_back(observation); _observations.push_back(observation);
// recompute mean observation
_mean_observation = Eigen::VectorXd::Zero(_dim_out);
for (size_t j = 0; j < _observations.size(); j++)
_mean_observation.array() += _observations[j].array();
_mean_observation.array() /= static_cast<double>(_observations.size());
Eigen::VectorXd mean_vector = _mean_function(sample, *this); Eigen::VectorXd mean_vector = _mean_function(sample, *this);
assert(mean_vector.size() == _dim_out); assert(mean_vector.size() == _dim_out);
...@@ -235,7 +247,7 @@ namespace limbo { ...@@ -235,7 +247,7 @@ namespace limbo {
/// return the number of samples used to compute the GP /// return the number of samples used to compute the GP
int nb_samples() const int nb_samples() const
{ {
return (_gp_models.size() > 0) ? _gp_models[0].nb_samples() : 0; return _observations.size();
} }
/// recomputes the GPs /// recomputes the GPs
...@@ -260,6 +272,14 @@ namespace limbo { ...@@ -260,6 +272,14 @@ namespace limbo {
return _gp_models[0].samples(); return _gp_models[0].samples();
} }
/// return the mean observation
Eigen::VectorXd mean_observation() const
{
assert(_dim_out > 0);
return _observations.size() > 0 ? _mean_observation
: Eigen::VectorXd::Zero(_dim_out);
}
/// return the list of GPs /// return the list of GPs
std::vector<GP_t> gp_models() const std::vector<GP_t> gp_models() const
{ {
...@@ -272,12 +292,89 @@ namespace limbo { ...@@ -272,12 +292,89 @@ namespace limbo {
return _gp_models; return _gp_models;
} }
/// save the parameters and the data for the GP to the archive (text or binary)
template <typename A>
void save(const std::string& directory)
{
A archive(directory);
save(archive);
}
/// save the parameters and the data for the GP to the archive (text or binary)
template <typename A>
void save(const A& archive)
{
Eigen::VectorXd dims(2);
dims << _dim_in, _dim_out;
archive.save(dims, "dims");
archive.save(_observations, "observations");
if (_mean_function.h_params_size() > 0) {
archive.save(_mean_function.h_params(), "mean_params");
}
for (int i = 0; i < _dim_out; i++) {
_gp_models[i].template save<A>(archive.directory() + "/gp_" + std::to_string(i));
}
}
/// load the parameters and the data for the GP from the archive (text or binary)
/// if recompute is true, we do not read the kernel matrix
/// but we recompute it given the data and the hyperparameters
template <typename A>
void load(const std::string& directory, bool recompute = true)
{
A archive(directory);
load(archive, recompute);
}
/// load the parameters and the data for the GP from the archive (text or binary)
/// if recompute is true, we do not read the kernel matrix
/// but we recompute it given the data and the hyperparameters
template <typename A>
void load(const A& archive, bool recompute = true)
{
_observations.clear();
archive.load(_observations, "observations");
Eigen::VectorXd dims;
archive.load(dims, "dims");
_dim_in = static_cast<int>(dims(0));
_dim_out = static_cast<int>(dims(1));
// recompute mean observation
_mean_observation = Eigen::VectorXd::Zero(_dim_out);
for (size_t j = 0; j < _observations.size(); j++)
_mean_observation.array() += _observations[j].array();
_mean_observation.array() /= static_cast<double>(_observations.size());
_mean_function = MeanFunction(_dim_out);
if (_mean_function.h_params_size() > 0) {
Eigen::VectorXd h_params;
archive.load(h_params, "mean_params");
assert(h_params.size() == (int)_mean_function.h_params_size());
_mean_function.set_h_params(h_params);
}
for (int i = 0; i < _dim_out; i++) {
// do not recompute the individual GPs on their own
_gp_models[i].template load<A>(archive.directory() + "/gp_" + std::to_string(i), false);
}
if (recompute)
this->recompute(true, true);
}
protected: protected:
std::vector<GP_t> _gp_models; std::vector<GP_t> _gp_models;
int _dim_in, _dim_out; int _dim_in, _dim_out;
HyperParamsOptimizer _hp_optimize; HyperParamsOptimizer _hp_optimize;
MeanFunction _mean_function; MeanFunction _mean_function;
std::vector<Eigen::VectorXd> _observations; std::vector<Eigen::VectorXd> _observations;
Eigen::VectorXd _mean_observation;
}; };
} // namespace model } // namespace model
} // namespace limbo } // namespace limbo
......
...@@ -124,6 +124,11 @@ namespace limbo { ...@@ -124,6 +124,11 @@ namespace limbo {
return _dir_name + "/" + object_name + ".bin"; return _dir_name + "/" + object_name + ".bin";
} }
const std::string& directory() const
{
return _dir_name;
}
protected: protected:
std::string _dir_name; std::string _dir_name;
......
...@@ -113,6 +113,11 @@ namespace limbo { ...@@ -113,6 +113,11 @@ namespace limbo {
return _dir_name + "/" + object_name + ".dat"; return _dir_name + "/" + object_name + ".dat";
} }
const std::string& directory() const
{
return _dir_name;
}
protected: protected:
std::string _dir_name; std::string _dir_name;
Eigen::IOFormat _fmt; Eigen::IOFormat _fmt;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment