logsumexp.py 2.29 KB
Newer Older
1
2
3
4
5
import torch

from . import scatter_add, scatter_max


6
def scatter_logsumexp(src, index, dim=-1, out=None, dim_size=None,
7
8
9
10
11
12
13
                      fill_value=None, eps=1e-12):
    r"""Fills :attr:`out` with the log of summed exponentials of all values
    from the :attr:`src` tensor at the indices specified in the :attr:`index`
    tensor along a given axis :attr:`dim`.
    If multiple indices reference the same location, their
    **exponential contributions add**
    (`cf.` :meth:`~torch_scatter.scatter_add`).
14
15
16
17

    For one-dimensional tensors, the operation computes

    .. math::
18
19
        \mathrm{out}_i = \log \, \left( \exp(\mathrm{out}_i) + \sum_j
        \exp(\mathrm{src}_j) \right)
20

21
22
    where :math:`\sum_j` is over :math:`j` such that
    :math:`\mathrm{index}_j = i`.
23
24
25
26
27
28
29
30
31
32
33
34

    Args:
        src (Tensor): The source tensor.
        index (LongTensor): The indices of elements to scatter.
        dim (int, optional): The axis along which to index.
            (default: :obj:`-1`)
        out (Tensor, optional): The destination tensor. (default: :obj:`None`)
        dim_size (int, optional): If :attr:`out` is not given, automatically
            create output with size :attr:`dim_size` at dimension :attr:`dim`.
            If :attr:`dim_size` is not given, a minimal sized output tensor is
            returned. (default: :obj:`None`)
        fill_value (int, optional): If :attr:`out` is not given, automatically
35
36
37
            fill output tensor with :attr:`fill_value`. (default: :obj:`None`)
        eps (float, optional): Small value to ensure numerical stability.
            (default: :obj:`1e-12`)
38
39
40

    :rtype: :class:`Tensor`
    """
41
    if not torch.is_floating_point(src):
42
        raise ValueError('`scatter_logsumexp` can only be computed over '
43
                         'tensors with floating point data types.')
44

45
46
    max_value_per_index, _ = scatter_max(src, index, dim, out, dim_size,
                                         fill_value)
47
48
    max_per_src_element = max_value_per_index.gather(dim, index)
    recentered_scores = src - max_per_src_element
49
50
51
52
    out = (out - max_per_src_element).exp() if out is not None else None

    sum_per_index = scatter_add(recentered_scores.exp(), index, dim, out,
                                dim_size, fill_value=0)
53

54
    return torch.log(sum_per_index + eps) + max_value_per_index