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

Efficient burn in for sampling #298

Closed
wants to merge 1 commit into from

Conversation

vgnecula
Copy link
Contributor

Modified the sampling.hpp to solve: Inefficient handling of burn in phase in sampling functions #180.

Handling burn in phase without storing the points required for this phase, for each of the sampling methods presented.

Cleared my branch non-related developments in order to emphasize just the sampling.hpp code.


for (unsigned int i = 0; i < rnum; ++i) {
walk.apply(P, p, walk_len, rng);
randPoints.push_back(p);
Copy link
Member

Choose a reason for hiding this comment

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

Could you please reserve memory equal to rnum points before the push_backs? (i.e., randPoints.reserve(rnum);)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not able to do that directly using randPoints.reserve(rnum); as randPoints is an instance of an std::list and not std::vector. Thus it does not support this type of command. Am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about finding a workaround for reserving space from the first time, however as I figured out randPoints was a list, I decided that it is not the case anymore, as I can't get continuous storage anyway.

Copy link
Contributor

@vfisikop vfisikop Mar 27, 2024

Choose a reason for hiding this comment

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

randPoints is templated (i.e. it can be either a std::vector or a std::list or any container that supports push_back), thus you can create a specialized version of this function that reserves memory only for the case of a vector but I propose not to do anything in this PR. We should think if we need to want to keep it as it is or restrict randPoints to std::list or std::vector.
@TolisChal do we need random access in samples? If not should we restrict to std::list here?


for (unsigned int i = 0; i < rnum; ++i) {
walk.apply(P, p, walk_len, rng);
randPoints.push_back(p);
Copy link
Member

Choose a reason for hiding this comment

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

same as before


for (unsigned int i = 0; i < rnum; ++i) {
walk.apply(P, p, a, walk_len, rng);
randPoints.push_back(p);
Copy link
Member

Choose a reason for hiding this comment

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

same as before


for (unsigned int i = 0; i < rnum; ++i) {
walk.apply(P, p, a, walk_len, rng);
randPoints.push_back(p);
Copy link
Member

Choose a reason for hiding this comment

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

same as before

@TolisChal
Copy link
Member

TolisChal commented Mar 26, 2024

@vgnecula thanks for this PR!

A general comment is, when you want to change a particular part of the code, just change this one without affecting anything else. Then, it's easier to review the code and be sure about the changes.

Copy link
Contributor

@vfisikop vfisikop left a comment

Choose a reason for hiding this comment

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

Thanks. I suggest as @TolisChal also suggested to focus on the issue of the burn in phase and leave style changes for future PR.

}
}

randPoints.clear();
Copy link
Contributor

Choose a reason for hiding this comment

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

clear is not needed here.

typename RandomNumberGenerator,
typename Point

template <
Copy link
Contributor

Choose a reason for hiding this comment

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

I would prefer to change style at a separate PR.



randPoints.clear();
Copy link
Contributor

Choose a reason for hiding this comment

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

is this needed?

Copy link
Contributor

Choose a reason for hiding this comment

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

similar in all walks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Initially, I did not have this function. However, I thought that it might be a good practice to ensure that everything is cleared before performing the actual sampling. Besides, this operation should be done in constant time (correct me if I am wrong), especially in the case in which we expect randPoints to be empty. Consequently, I thought that there would be no problem in adding it just for safety.


randPoints.clear();

for (unsigned int i = 0; i < rnum; ++i) {
Copy link
Contributor

Choose a reason for hiding this comment

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

there is a mistake here, it was rnum/2 before


for (unsigned int i = 0; i < rnum; ++i) {
walk.apply(P, p1, p2, walk_len, rng);
randPoints.push_back(p1);
Copy link
Contributor

Choose a reason for hiding this comment

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

should we also push p2?


Point p = starting_point;
Point p = starting_point;
Copy link
Contributor

Choose a reason for hiding this comment

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

please do not add trailing whitespaces (there are more cases like this in this PR)

include/sampling/sampling.hpp Show resolved Hide resolved
include/sampling/sampling.hpp Show resolved Hide resolved
const NT &a,
Point &starting_point,
unsigned int const& nburns
) {
Copy link
Contributor

Choose a reason for hiding this comment

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

looks like a new style of code, I suggest using the style of most of the functions in volesti

@vfisikop
Copy link
Contributor

vfisikop commented Mar 26, 2024

Also it seems that you are deleting (by mistake) some crucial parts of the code in sampling.hpp.

@vfisikop
Copy link
Contributor

Side comment: there is no need to open a new PR every time you want to change something in your branch, just update your branch!

@vgnecula
Copy link
Contributor Author

vgnecula commented Apr 8, 2024

@TolisChal @vfisikop just commited a new version, which adresses all your comments (find it as - minor probem with CRHMC solved - the last version of my new modifications). On my end it passes all tests. However, it takes way too much time to process crhmc_test_polytope_sampling. Besides, as you will probably see, I modified some functions, as I did not like some details that I implemented before.

Copy link
Contributor

@vfisikop vfisikop left a comment

Choose a reason for hiding this comment

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

I reviewed part of it, but it is difficult to continue. Could you please separate your commits such that there is one commit with the changes to burn in feasibility and another one with the style changes?

RandomNumberGenerator &rng,
const unsigned int &walk_len,
const unsigned int &rnum,
Point &starting_point,
Copy link
Contributor

Choose a reason for hiding this comment

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

please avoid using trailing whitespaces.

Copy link
Contributor

Choose a reason for hiding this comment

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

there are similar cases below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for taking the time to review my new Commits!

I just wanted to be sure I understand what you mean by style changes.

So, I should have a first commit that for the lines where I did not change anything regarding the algorithm logic - the lines remain the same, while the second commit adds my proposed style changes.

I saw that there are some small differences in the ways some things are written and I tried to bring them all to the same writing style (that is why I made those modifications). Sorry for the white spaces, I missed some of them.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, exactly, we want one commit to fix the burn in phase that does not change any coding style (i.e. indentation etc) and another commit that contains all coding styles. Also please rebase (see git rebase documentation) your branch to only have those two commits and not the current ones (currently 4).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vfisikop should I push the "style-changes" commit? Or should I wait until the current one gets reviewed? Thank you!

Copy link
Contributor

Choose a reason for hiding this comment

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

The PR should consist of two commits, one with the fixes and one with the style changes. So those could be reviewed independently.

Copy link
Contributor

Choose a reason for hiding this comment

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

Having a look at this code again I see there are some problems with this interface, e.g. there is an issue of what walk should the user use for a particular distribution. You can have a look at this draft PR #300 for an idea of changing this to a new interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just started to look into it more. Could you please elaborate a bit more on what you think I should do? I want to be sure I fully understood what you meant.

randPoints, push_back_policy, rng);
randPoints.clear();
for (unsigned int i = 0; i < nburns; ++i) {
walk.apply(P, current_point, dummy_point, walk_len, rng);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there is not reason to keep only one point, they are both on the boundary, so I propose to keep both. @TolisChal what do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, we could do that, however keeping only one would be closer to a uniform sample (just intuitively).
A general comment from my side is that we need to delete this method in the future and implement Shake and Bake.


// Initialize random walk parameters
unsigned int dim = starting_point.dimension();
walk_params params(F, dim);
Copy link
Contributor

Choose a reason for hiding this comment

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

how you pass the parameters to the walk?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@vfisikop Thank you for pointing this out! So previously I was only relying on the assumption that the walk object will auto set the params. However after looking into how the langevin_walk is implemented, I saw that this is not the case. Thus, now I am setting the params using the set_params method. Let me know if this makes sense.

@vgnecula vgnecula force-pushed the burnIn_handling branch 6 times, most recently from e19ce96 to 46d4224 Compare April 20, 2024 05:26
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.

None yet

3 participants