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)
| 339 | return selected_target |
| 340 | |
| 341 | def 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() |