Skip to content
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

Hybrid Bayes Net sampling #1347

Merged
merged 14 commits into from
Dec 24, 2022
Merged

Hybrid Bayes Net sampling #1347

merged 14 commits into from
Dec 24, 2022

Conversation

varunagrawal
Copy link
Collaborator

@varunagrawal varunagrawal commented Dec 23, 2022

This PR adds a sample method to the HybridBayesNet, which operates as follows:

  1. Compute the discrete bayes net.
  2. Sample discrete assignment from the discrete bayes net.
  3. Select the Gaussian bayes net corresponding to the sampled assignment.
  4. Sample continuous VectorValues from the selected Gaussian bayes net.
  5. Return both samples as HybridValues.

@varunagrawal varunagrawal self-assigned this Dec 23, 2022
Copy link
Member

@dellaert dellaert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two comments:

  • given should be HybridValues
  • Model should not be needed

@@ -232,6 +235,43 @@ VectorValues HybridBayesNet::optimize(const DiscreteValues &assignment) const {
return gbn.optimize();
}

/* ************************************************************************* */
HybridValues HybridBayesNet::sample(VectorValues given, std::mt19937_64 *rng,
SharedDiagonal model) const {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given should be const&, and model should not be there I think (per my email to you).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I followed the signature in the other sample methods, so maybe update there as well?

Also it can't be const since DiscreteBayesNet does a sampleInPlace(&given) and GaussianBayesNet has the line given.insert(sampled);. Would you like me to update those to make them functional?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, now I remember! It is passed by value deliberately, to allow for updating it in-place.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add that in comment so we don’t forget again

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated GaussianBayesNet::sample to be functional. So instead of given.insert(sampled), it is now result.insert(sampled) with result initialized as VectorValues result(given);.
Perfectly backwards compatible. :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not really :-) the argument was passed by value which makes a copy, functionally equivalent to what you changed it to. Modulo possible compiler optimizations that might do something different - no idea which one is best.

gtsam/hybrid/HybridBayesNet.cpp Outdated Show resolved Hide resolved
gtsam/hybrid/HybridBayesNet.cpp Outdated Show resolved Hide resolved
gtsam/hybrid/HybridBayesNet.cpp Outdated Show resolved Hide resolved
gtsam/hybrid/HybridBayesNet.cpp Outdated Show resolved Hide resolved
gtsam/hybrid/HybridBayesNet.cpp Outdated Show resolved Hide resolved
gtsam/hybrid/HybridBayesNet.cpp Outdated Show resolved Hide resolved
gtsam/hybrid/HybridBayesNet.cpp Outdated Show resolved Hide resolved
gtsam/hybrid/HybridBayesNet.h Show resolved Hide resolved
gtsam/linear/GaussianBayesNet.cpp Outdated Show resolved Hide resolved
gtsam/hybrid/HybridBayesNet.cpp Show resolved Hide resolved
gtsam/hybrid/HybridBayesNet.cpp Outdated Show resolved Hide resolved
gtsam/hybrid/HybridBayesNet.cpp Outdated Show resolved Hide resolved
gtsam/hybrid/HybridBayesNet.cpp Outdated Show resolved Hide resolved
gtsam/hybrid/HybridBayesNet.h Outdated Show resolved Hide resolved
gtsam/linear/GaussianConditional.cpp Outdated Show resolved Hide resolved
Copy link
Member

@dellaert dellaert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I think one of your changes in GC was not strictly needed, but I think it does not make a performance difference, so let’s go with it.

@@ -64,8 +64,9 @@ namespace gtsam {
return sample(result, rng);
}

VectorValues GaussianBayesNet::sample(VectorValues result,
VectorValues GaussianBayesNet::sample(const VectorValues& given,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, for this case, the question is whether it should be value (which will automatically make the copy you inserted below) or by const&, which you modified this code to. I chose by value when implementing this so I would get the automatic copy, but maybe it does not make a difference and it is more “clever than smart”. The code that was here was correct, though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it was correct. I felt this made it clearer to read that you're given some VectorValues and you're getting back the result of sampling.
I can undo this if you'd like.

@@ -79,7 +80,7 @@ namespace gtsam {
return sample(&kRandomNumberGenerator);
}

VectorValues GaussianBayesNet::sample(VectorValues given) const {
VectorValues GaussianBayesNet::sample(const VectorValues& given) const {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This, for sure, is a positive change.

@varunagrawal varunagrawal merged commit 2abc0d9 into develop Dec 24, 2022
@varunagrawal varunagrawal deleted the hybrid/sample branch December 24, 2022 14:32
@dellaert dellaert added this to the Hybrid Inference milestone Feb 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants