Skip to content

Commit

Permalink
Add kmax function.
Browse files Browse the repository at this point in the history
  • Loading branch information
srush committed Apr 7, 2020
1 parent e76b093 commit 1cfba78
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion torch_struct/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,20 @@ def argmax(self):
"""
return self._struct(MaxSemiring).marginals(self.log_potentials, self.lengths)

def topk(self, k):
def kmax(self, k):
r"""
Compute the k-max for distribution :math:`k\max p(z)`.
Returns:
kmax (*k x batch_shape*)
"""
with torch.enable_grad():
return self._struct(KMaxSemiring(k)).sum(
self.log_potentials, self.lengths, _raw=True
)

def topk(self, k):
r"""
Compute the k-argmax for distribution :math:`k\max p(z)`.
Returns:
kmax (*k x batch_shape x event_shape*)
Expand Down

0 comments on commit 1cfba78

Please sign in to comment.