-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nearest Neighbor Model #158
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright (C) 2019 Swift Navigation Inc. | ||
* Contact: Swift Navigation <[email protected]> | ||
* | ||
* This source is subject to the license found in the file 'LICENSE' which must | ||
* be distributed together with this source. All other rights reserved. | ||
* | ||
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, | ||
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. | ||
*/ | ||
|
||
#ifndef ALBATROSS_NEAREST_NEIGHBOR_MODEL_H | ||
#define ALBATROSS_NEAREST_NEIGHBOR_MODEL_H | ||
|
||
#include "Core" | ||
|
||
#include <albatross/src/models/nearest_neighbor.hpp> | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright (C) 2019 Swift Navigation Inc. | ||
* Contact: Swift Navigation <[email protected]> | ||
* | ||
* This source is subject to the license found in the file 'LICENSE' which must | ||
* be distributed together with this source. All other rights reserved. | ||
* | ||
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, | ||
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. | ||
*/ | ||
|
||
#ifndef ALBATROSS_SERIALIZE_NEAREST_NEIGHBOR_H | ||
#define ALBATROSS_SERIALIZE_NEAREST_NEIGHBOR_H | ||
|
||
#include "Core" | ||
|
||
#include "../src/cereal/nearest_neighbor.hpp" | ||
|
||
#endif |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (C) 2019 Swift Navigation Inc. | ||
* Contact: Swift Navigation <[email protected]> | ||
* | ||
* This source is subject to the license found in the file 'LICENSE' which must | ||
* be distributed together with this source. All other rights reserved. | ||
* | ||
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, | ||
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. | ||
*/ | ||
|
||
#ifndef ALBATROSS_CEREAL_NEAREST_NEIGHBOR_HPP_ | ||
#define ALBATROSS_CEREAL_NEAREST_NEIGHBOR_HPP_ | ||
|
||
namespace albatross { | ||
|
||
template <typename DistanceMetric> class NearestNeighborModel; | ||
|
||
template <typename FeatureType> struct NearestNeighborFit; | ||
|
||
} // namespace albatross | ||
|
||
namespace cereal { | ||
|
||
template <typename Archive, typename FeatureType> | ||
inline void | ||
save(Archive &archive, | ||
const albatross::Fit<albatross::NearestNeighborFit<FeatureType>> &fit, | ||
const std::uint32_t) { | ||
archive(cereal::make_nvp("training_features", fit.training_data.features)); | ||
archive(cereal::make_nvp("training_targets", fit.training_data.targets)); | ||
} | ||
|
||
template <typename Archive, typename FeatureType> | ||
inline void | ||
load(Archive &archive, | ||
albatross::Fit<albatross::NearestNeighborFit<FeatureType>> &fit, | ||
const std::uint32_t) { | ||
std::vector<FeatureType> features; | ||
archive(cereal::make_nvp("training_features", features)); | ||
albatross::MarginalDistribution targets; | ||
archive(cereal::make_nvp("training_targets", targets)); | ||
fit.training_data = RegressionDataset<FeatureType>(features, targets); | ||
} | ||
|
||
} // namespace cereal | ||
|
||
#endif /* ALBATROSS_CEREAL_NEAREST_NEIGHBOR_HPP_ */ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright (C) 2019 Swift Navigation Inc. | ||
* Contact: Swift Navigation <[email protected]> | ||
* | ||
* This source is subject to the license found in the file 'LICENSE' which must | ||
* be distributed together with this source. All other rights reserved. | ||
* | ||
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, | ||
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. | ||
*/ | ||
|
||
#ifndef ALBATROSS_SRC_MODELS_NEAREST_NEIGHBOR_MODEL_HPP_ | ||
#define ALBATROSS_SRC_MODELS_NEAREST_NEIGHBOR_MODEL_HPP_ | ||
|
||
namespace albatross { | ||
|
||
template <typename DistanceMetric> class NearestNeighborModel; | ||
|
||
template <typename FeatureType> struct NearestNeighborFit; | ||
|
||
template <typename FeatureType> struct Fit<NearestNeighborFit<FeatureType>> { | ||
|
||
Fit() : training_data(){}; | ||
|
||
Fit(const RegressionDataset<FeatureType> &dataset) : training_data(dataset){}; | ||
|
||
bool operator==(const Fit<NearestNeighborFit<FeatureType>> &other) const { | ||
return training_data == other.training_data; | ||
} | ||
|
||
RegressionDataset<FeatureType> training_data; | ||
}; | ||
|
||
template <typename DistanceMetric> | ||
class NearestNeighborModel | ||
: public ModelBase<NearestNeighborModel<DistanceMetric>> { | ||
|
||
public: | ||
NearestNeighborModel() : distance_metric(){}; | ||
|
||
std::string get_name() const { return "nearest_neighbor_model"; }; | ||
|
||
template <typename FeatureType> | ||
Fit<NearestNeighborFit<FeatureType>> | ||
_fit_impl(const std::vector<FeatureType> &features, | ||
const MarginalDistribution &targets) const { | ||
return Fit<NearestNeighborFit<FeatureType>>( | ||
RegressionDataset<FeatureType>(features, targets)); | ||
} | ||
|
||
template <typename FeatureType> | ||
auto fit_from_prediction(const std::vector<FeatureType> &features, | ||
const JointDistribution &prediction) const { | ||
const NearestNeighborModel<DistanceMetric> m(*this); | ||
MarginalDistribution marginal_pred( | ||
prediction.mean, prediction.covariance.diagonal().asDiagonal()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah exactly, I need to zero the non-diagonal elements since the |
||
Fit<NearestNeighborFit<FeatureType>> fit = { | ||
RegressionDataset<FeatureType>(features, marginal_pred)}; | ||
FitModel<NearestNeighborModel, Fit<NearestNeighborFit<FeatureType>>> | ||
fit_model(m, fit); | ||
return fit_model; | ||
} | ||
|
||
template <typename FeatureType> | ||
MarginalDistribution | ||
_predict_impl(const std::vector<FeatureType> &features, | ||
const Fit<NearestNeighborFit<FeatureType>> &fit, | ||
PredictTypeIdentity<MarginalDistribution> &&) const { | ||
const Eigen::Index n = static_cast<Eigen::Index>(features.size()); | ||
Eigen::VectorXd mean = Eigen::VectorXd::Zero(n); | ||
mean.fill(NAN); | ||
Eigen::VectorXd variance = Eigen::VectorXd::Zero(n); | ||
variance.fill(NAN); | ||
|
||
for (std::size_t i = 0; i < features.size(); ++i) { | ||
const auto min_index = | ||
index_with_min_distance(features[i], fit.training_data.features); | ||
mean[i] = fit.training_data.targets.mean[min_index]; | ||
variance[i] = fit.training_data.targets.get_diagonal(min_index); | ||
} | ||
|
||
if (fit.training_data.targets.has_covariance()) { | ||
return MarginalDistribution(mean, variance.asDiagonal()); | ||
} else { | ||
return MarginalDistribution(mean); | ||
} | ||
} | ||
|
||
private: | ||
template <typename FeatureType> | ||
std::size_t | ||
index_with_min_distance(const FeatureType &ref, | ||
const std::vector<FeatureType> &features) const { | ||
assert(features.size() > 0); | ||
|
||
std::size_t min_index = 0; | ||
double min_distance = distance_metric(ref, features[0]); | ||
|
||
for (std::size_t i = 1; i < features.size(); ++i) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we turn There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like that pattern better too ... but so far There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An alternative is to initialize the |
||
const double dist = distance_metric(ref, features[i]); | ||
if (dist < min_distance) { | ||
min_index = i; | ||
min_distance = dist; | ||
} | ||
} | ||
return min_index; | ||
} | ||
|
||
DistanceMetric distance_metric; | ||
}; | ||
|
||
} // namespace albatross | ||
|
||
#endif // ALBATROSS_SRC_MODELS_NEAREST_NEIGHBOR_MODEL_HPP_ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to do this or just add
1e6
as variances where we currently have none?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this method we're taking a pair of
MarginalDistribution
and concatenating them, so if the both don't have a defined covariance then we want to preserve that in the concatenation. Somewhere on my list of want to dos is to remove the optional behavior for convariances in favor of a third distribution type, something like:or something along those lines, but that's out of scope here.