MCPcopy Create free account
hub / github.com/awslabs/gap-text2sql / logsumexp

Function logsumexp

relogic/logickit/utils/utils.py:341–363  ·  view source on GitHub ↗

A numerically stable computation of logsumexp. This is mathematically equivalent to `tensor.exp().sum(dim, keep=keepdim).log()`. This function is typically used for summing log probabilities. Parameters ---------- tensor : torch.FloatTensor, required. A tensor of arbitrary size.

(tensor: torch.Tensor,
              dim: int = -1,
              keepdim: bool = False)

Source from the content-addressed store, hash-verified

339 return selected_target
340
341def logsumexp(tensor: torch.Tensor,
342 dim: int = -1,
343 keepdim: bool = False) -> torch.Tensor:
344 """
345 A numerically stable computation of logsumexp. This is mathematically equivalent to
346 `tensor.exp().sum(dim, keep=keepdim).log()`. This function is typically used for summing log
347 probabilities.
348
349 Parameters
350 ----------
351 tensor : torch.FloatTensor, required.
352 A tensor of arbitrary size.
353 dim : int, optional (default = -1)
354 The dimension of the tensor to apply the logsumexp to.
355 keepdim: bool, optional (default = False)
356 Whether to retain a dimension of size one at the dimension we reduce over.
357 """
358 max_score, _ = tensor.max(dim, keepdim=keepdim)
359 if keepdim:
360 stable_vec = tensor - max_score
361 else:
362 stable_vec = tensor - max_score.unsqueeze(dim)
363 return max_score + (stable_vec.exp().sum(dim, keepdim=keepdim)).log()

Callers

nothing calls this directly

Calls 1

logMethod · 0.80

Tested by

no test coverage detected