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

probability() and log_probability() won't accept "Masked" tensors #1025

Open
jdschmitt11 opened this issue Apr 19, 2023 · 4 comments
Open

probability() and log_probability() won't accept "Masked" tensors #1025

jdschmitt11 opened this issue Apr 19, 2023 · 4 comments

Comments

@jdschmitt11
Copy link

The probability and log_probability functions won't accept masked tensors so they can't be evaluated with missing evidence. When I use the same masked tensor for predict_proba() it works fine.

IndexError Traceback (most recent call last)
Cell In[26], line 1
----> 1 model.probability(evidence_masked)

File Miniconda3\envs\bayesian-api\lib\site-packages\pomegranate\distributions_distribution.py:61, in Distribution.probability(self, X)
60 def probability(self, X):
---> 61 return torch.exp(self.log_probability(X))

File Miniconda3\envs\bayesian-api\lib\site-packages\pomegranate\bayesian_network.py:352, in BayesianNetwork.log_probability(self, X)
349 if len(parents) > 1:
350 X_ = X_.unsqueeze(-1)
--> 352 logps += distribution.log_probability(X_)
354 return logps

File Miniconda3\envs\bayesian-api\lib\site-packages\pomegranate\distributions\categorical.py:175, in Categorical.log_probability(self, X)
173 logps = torch.zeros(X.shape[0], dtype=self.probs.dtype)
174 for i in range(self.d):
--> 175 logps += self._log_probs[i][X[:, i]]
177 return logps

File Miniconda3\envs\bayesian-api\lib\site-packages\torch\masked\maskedtensor\core.py:274, in MaskedTensor.torch_function(cls, func, types, args, kwargs)
272 return NotImplemented
273 with torch._C.DisableTorchFunctionSubclass():
--> 274 ret = func(*args, **kwargs)
...
500 # We save the function ptr as the op attribute on
501 # OpOverloadPacket to access it here.
--> 502 return self._op(*args, **kwargs or {})

IndexError: The shape of the mask [1] at index 0 does not match the shape of the indexed tensor [28] at index 0

@jmschrei
Copy link
Owner

Thanks for the issue. What are you hoping the return to be? Would you like it to infer the missing states and then calculate log probabilities using those, or only calculate log probabilities for those states that have complete observations? I think the second would lead to inflated values when examples have missing values.

@jdschmitt11
Copy link
Author

jdschmitt11 commented Apr 19, 2023 via email

@jdschmitt11
Copy link
Author

jdschmitt11 commented Apr 19, 2023 via email

@jdschmitt11
Copy link
Author

I had some time to take a look at this and was able to get agreement to V 0.14 by inserting this code between lines 345 and 354 in your bayesian_network.py:

if X._masked_mask[0, i] == True:

  parents = model._parents[i] + (i,)
  X_ = X._masked_data[:, parents]

  if len(parents) > 1:
      X_ = X_.unsqueeze(-1)

  logps += distribution.log_probability(X_)

The problem with this is that is forces log_probability to only take masked tensors, similar to predict_proba() and predict().

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

No branches or pull requests

2 participants