Skip to content

Dnn2bnn#11

Merged
ranganathkrishnan merged 2 commits into
IntelLabs:mainfrom
msubedar:dnn2bnn
Dec 16, 2021
Merged

Dnn2bnn#11
ranganathkrishnan merged 2 commits into
IntelLabs:mainfrom
msubedar:dnn2bnn

Conversation

@msubedar

@msubedar msubedar commented Dec 8, 2021

Copy link
Copy Markdown

PR supports DNN to BNN auto conversion. One can create a torch.nn DNN model and call dnn_to_bnn() to convert it to BNN model. An example script examples/main_bayesian_cifar_dnn2bnn.py is provided.

@piEsposito piEsposito left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we should assign a value to kl even if it is null, so it won't break when we are integrating to already working PyTorch models and training scripts.

Also, maybe we should add unit-tests, rebase this branch on it and check if that won't break things.

What do you think @ranganathkrishnan ?


kl = self.kl_div(self.mu_kernel, sigma_weight, self.prior_weight_mu,
self.prior_weight_sigma)
if return_kl:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey, I think only computing the kl value if return_kl is set to True might break things if we are plugging bayesian layers on a deterministic model.

Maybe the best way could be if self.dnn_to_bnn_flag is set to True, we set it to null, but not computing it at all might just break everything - and we don't have unit tests yet to ensure things won't break.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

bias = (sigma_bias * eps_bias)
kl = kl + self.kl_div(self.mu_bias, sigma_bias, self.prior_bias_mu,
self.prior_bias_sigma)
if return_kl:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Idem here: Hey, I think only computing the kl value if return_kl is set to True might break things if we are plugging bayesian layers on a deterministic model.

Maybe the best way could be if self.dnn_to_bnn_flag is set to True, we set it to null, but not computing it at all might just break everything - and we don't have unit tests yet to ensure things won't break.


kl = self.kl_div(self.mu_kernel, sigma_weight, self.prior_weight_mu,
self.prior_weight_sigma)
if return_kl:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey, I think only computing the kl value if return_kl is set to True might break things if we are plugging bayesian layers on a deterministic model.

Maybe the best way could be if self.dnn_to_bnn_flag is set to True, we set it to null, but not computing it at all might just break everything - and we don't have unit tests yet to ensure things won't break.

bias = (sigma_bias * eps_bias)
kl = kl + self.kl_div(self.mu_bias, sigma_bias, self.prior_bias_mu,
self.prior_bias_sigma)
if return_kl:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey, I think only computing the kl value if return_kl is set to True might break things if we are plugging bayesian layers on a deterministic model.

Maybe the best way could be if self.dnn_to_bnn_flag is set to True, we set it to null, but not computing it at all might just break everything - and we don't have unit tests yet to ensure things won't break.


kl = self.kl_div(self.mu_kernel, sigma_weight, self.prior_weight_mu,
self.prior_weight_sigma)
if return_kl:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey, I think only computing the kl value if return_kl is set to True might break things if we are plugging bayesian layers on a deterministic model.

Maybe the best way could be if self.dnn_to_bnn_flag is set to True, we set it to null, but not computing it at all might just break everything - and we don't have unit tests yet to ensure things won't break.

weight = self.mu_kernel + (sigma_weight * eps_kernel)
kl_weight = self.kl_div(self.mu_kernel, sigma_weight,
self.prior_weight_mu, self.prior_weight_sigma)
if return_kl:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey, I think only computing the kl value if return_kl is set to True might break things if we are plugging bayesian layers on a deterministic model.

Maybe the best way could be if self.dnn_to_bnn_flag is set to True, we set it to null, but not computing it at all might just break everything - and we don't have unit tests yet to ensure things won't break.

bias = self.mu_bias + (sigma_bias * eps_bias)
kl_bias = self.kl_div(self.mu_bias, sigma_bias, self.prior_bias_mu,
self.prior_bias_sigma)
if return_kl:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey, I think only computing the kl value if return_kl is set to True might break things if we are plugging bayesian layers on a deterministic model.

Maybe the best way could be if self.dnn_to_bnn_flag is set to True, we set it to null, but not computing it at all might just break everything - and we don't have unit tests yet to ensure things won't break.

weight = self.mu_kernel + (sigma_weight * eps_kernel)
kl_weight = self.kl_div(self.mu_kernel, sigma_weight,
self.prior_weight_mu, self.prior_weight_sigma)
if return_kl:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey, I think only computing the kl value if return_kl is set to True might break things if we are plugging bayesian layers on a deterministic model.

Maybe the best way could be if self.dnn_to_bnn_flag is set to True, we set it to null, but not computing it at all might just break everything - and we don't have unit tests yet to ensure things won't break.

bias = self.mu_bias + (sigma_bias * eps_bias)
kl_bias = self.kl_div(self.mu_bias, sigma_bias, self.prior_bias_mu,
self.prior_bias_sigma)
if return_kl:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey, I think only computing the kl value if return_kl is set to True might break things if we are plugging bayesian layers on a deterministic model.

Maybe the best way could be if self.dnn_to_bnn_flag is set to True, we set it to null, but not computing it at all might just break everything - and we don't have unit tests yet to ensure things won't break.

bias = self.mu_bias + (sigma_bias * self.eps_bias.data.normal_())
kl_bias = self.kl_div(self.mu_bias, sigma_bias, self.prior_bias_mu,
self.prior_bias_sigma)
if return_kl:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hey, I think only computing the kl value if return_kl is set to True might break things if we are plugging bayesian layers on a deterministic model.

Maybe the best way could be if self.dnn_to_bnn_flag is set to True, we set it to null, but not computing it at all might just break everything - and we don't have unit tests yet to ensure things won't break.

@ranganathkrishnan ranganathkrishnan self-assigned this Dec 8, 2021
@ranganathkrishnan ranganathkrishnan self-requested a review December 8, 2021 17:49
@ranganathkrishnan

ranganathkrishnan commented Dec 16, 2021

Copy link
Copy Markdown
Contributor

I think we should assign a value to kl even if it is null, so it won't break when we are integrating to already working PyTorch models and training scripts.

Also, maybe we should add unit-tests, rebase this branch on it and check if that won't break things.

What do you think @ranganathkrishnan ?

Hi @piEsposito , I agree to include the unit-tests, but that can be done on top of this PR. @msubedar and I have validated all the existing scripts for training and evaluating the models in examples folder, so that the new feature does not break older model versions (i.e. Bayesian models defined without dnn_to_bnn()).
With these changes, kl can be computed in kl_loss() function (https://github.com/IntelLabs/bayesian-torch/pull/11/files#diff-a588ca678816135810210bacf4645009ff6ecd3f16626713fd576db4aa603b07R126). For example, see https://github.com/IntelLabs/bayesian-torch/pull/11/files#diff-ade00648d59ef89627b50d68cb88f47ffc6d4832dd4c366902285fce80fec709R157 .
Appreciate your interest in contributing to the unit-tests. Thanks!

@ranganathkrishnan ranganathkrishnan merged commit 8fe84f9 into IntelLabs:main Dec 16, 2021
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.

3 participants