|
| 1 | +import torch |
| 2 | + |
| 3 | +from torch_scatter.logsumexp import _scatter_logsumexp |
| 4 | + |
| 5 | +def scatter_log_softmax(src, index, dim=-1, dim_size=None): |
| 6 | + r""" |
| 7 | + Numerical safe log-softmax of all values from the :attr:`src` tensor into :attr:`out` at the |
| 8 | + indices specified in the :attr:`index` tensor along a given axis |
| 9 | + :attr:`dim`.If multiple indices reference the same location, their |
| 10 | + **contributions average** (`cf.` :meth:`~torch_scatter.scatter_add`). |
| 11 | +
|
| 12 | + For one-dimensional tensors, the operation computes |
| 13 | +
|
| 14 | + .. math:: |
| 15 | + \mathrm{out}_i = softmax(\mathrm{src}_i) = \mathrm{src}_i - \mathrm{logsumexp}_j ( \mathrm{src}_j) |
| 16 | +
|
| 17 | + where :math:`\mathrm{logsumexp}_j` is over :math:`j` such that |
| 18 | + :math:`\mathrm{index}_j = i`. |
| 19 | +
|
| 20 | + Compute a numerically safe log softmax operation |
| 21 | + from the :attr:`src` tensor into :attr:`out` at the indices |
| 22 | + specified in the :attr:`index` tensor along a given axis :attr:`dim`. For |
| 23 | + each value in :attr:`src`, its output index is specified by its index in |
| 24 | + :attr:`input` for dimensions outside of :attr:`dim` and by the |
| 25 | + corresponding value in :attr:`index` for dimension :attr:`dim`. |
| 26 | +
|
| 27 | + Args: |
| 28 | + src (Tensor): The source tensor. |
| 29 | + index (LongTensor): The indices of elements to scatter. |
| 30 | + dim (int, optional): The axis along which to index. |
| 31 | + (default: :obj:`-1`) |
| 32 | + dim_size (int, optional): If :attr:`out` is not given, automatically |
| 33 | + create output with size :attr:`dim_size` at dimension :attr:`dim`. |
| 34 | + If :attr:`dim_size` is not given, a minimal sized output tensor is |
| 35 | + returned. (default: :obj:`None`) |
| 36 | + fill_value (int, optional): If :attr:`out` is not given, automatically |
| 37 | + fill output tensor with :attr:`fill_value`. If set to :obj:`None`, |
| 38 | + the output tensor is filled with the smallest possible value of |
| 39 | + :obj:`src.dtype`. (default: :obj:`None`) |
| 40 | +
|
| 41 | + :rtype: :class:`Tensor` |
| 42 | + """ |
| 43 | + per_index_logsumexp, recentered_src = _scatter_logsumexp(src, index, dim=dim, dim_size=dim_size) |
| 44 | + return recentered_src - per_index_logsumexp.gather(dim, index) |
| 45 | + |
| 46 | + |
| 47 | +def scatter_softmax(src, index, dim=-1, dim_size=None): |
| 48 | + r""" |
| 49 | + Numerical safe log-softmax of all values from the :attr:`src` tensor into :attr:`out` at the |
| 50 | + indices specified in the :attr:`index` tensor along a given axis |
| 51 | + :attr:`dim`. If multiple indices reference the same location, their |
| 52 | + **contributions average** (`cf.` :meth:`~torch_scatter.scatter_add`). |
| 53 | +
|
| 54 | + For one-dimensional tensors, the operation computes |
| 55 | +
|
| 56 | + .. math:: |
| 57 | + \mathrm{out}_i = softmax(\mathrm{src}_i) = \frac{\exp(\mathrm{src}_i)}{\mathrm{logsumexp}_j ( \mathrm{src}_j)} |
| 58 | +
|
| 59 | + where :math:`\mathrm{logsumexp}_j` is over :math:`j` such that |
| 60 | + :math:`\mathrm{index}_j = i`. |
| 61 | +
|
| 62 | + Compute a numerically safe softmax operation |
| 63 | + from the :attr:`src` tensor into :attr:`out` at the indices |
| 64 | + specified in the :attr:`index` tensor along a given axis :attr:`dim`. For |
| 65 | + each value in :attr:`src`, its output index is specified by its index in |
| 66 | + :attr:`input` for dimensions outside of :attr:`dim` and by the |
| 67 | + corresponding value in :attr:`index` for dimension :attr:`dim`. |
| 68 | +
|
| 69 | + Args: |
| 70 | + src (Tensor): The source tensor. |
| 71 | + index (LongTensor): The indices of elements to scatter. |
| 72 | + dim (int, optional): The axis along which to index. |
| 73 | + (default: :obj:`-1`) |
| 74 | + dim_size (int, optional): If :attr:`out` is not given, automatically |
| 75 | + create output with size :attr:`dim_size` at dimension :attr:`dim`. |
| 76 | + If :attr:`dim_size` is not given, a minimal sized output tensor is |
| 77 | + returned. (default: :obj:`None`) |
| 78 | + fill_value (int, optional): If :attr:`out` is not given, automatically |
| 79 | + fill output tensor with :attr:`fill_value`. If set to :obj:`None`, |
| 80 | + the output tensor is filled with the smallest possible value of |
| 81 | + :obj:`src.dtype`. (default: :obj:`None`) |
| 82 | +
|
| 83 | + :rtype: :class:`Tensor` |
| 84 | + """ |
| 85 | + return scatter_log_softmax(src, index, dim, dim_size).exp() |
0 commit comments