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

Raise exception when predicting probabilities given an impossible set of observations #1129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions pomegranate/factor_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,10 @@ def predict_proba(self, X):


dims = tuple(range(1, len(current_marginals[i].shape)))
current_marginals[i] /= current_marginals[i].sum(dim=dims,
keepdims=True)
sum = current_marginals[i].sum(dim=dims, keepdims=True)
if sum == 0:
raise ImpossibleObservationsError
current_marginals[i] /= sum

loss += torch.nn.KLDivLoss(reduction="batchmean")(torch.log(
current_marginals[i] + 1e-8), prior_marginals[i])
Expand Down Expand Up @@ -574,3 +576,10 @@ def from_summaries(self):

for distribution in self.factors:
distribution.from_summaries()


class ImpossibleObservationsError(Exception):
"""Exception raised when predicting probabilities given an impossible set of
observations.
"""
pass