def scatter_std(src, index, dim=-1, out=None, dim_size=None, unbiased=True): r""" | .. image:: https://raw.githubusercontent.com/rusty1s/pytsd/ master/docs/source/_figures/std.svg?sanitize=true :align: center :width: 400px | Computes the standard-deviation from all values from the :attr:`src` tensor into :attr:`out` at the indices specified in the :attr:`index` tensor along a given axis :attr:`dim` (`cf.` :meth:`~tsd.scatter_add`). For one-dimensional tensors, the operation computes .. math:: \mathrm{out}_i = \sqrt{\frac{\sum_j {\left( x_j - \overline{x}_i \right)}^2}{N_i - 1}} where :math:`\sum_j` is over :math:`j` such that :math:`\mathrm{index}_j = i`. :math:`N_i` and :math:`\overline{x}_i` indicate the number of indices referencing :math:`i` and their mean value, respectively. 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`) unbiased (bool, optional): If set to :obj:`False`, then the standard- deviation will be calculated via the biased estimator. (default: :obj:`True`) :rtype: :class:`Tensor` """ src, out, index, dim = gen(src, index, dim, out, dim_size, fill_value=0) tmp = None if out is None else out.clone().fill_(0) tmp = scatter_add(src, index, dim, tmp, dim_size) count = None if out is None else out.clone().fill_(0) count = scatter_add(torch.ones_like(src), index, dim, count, dim_size) mean = tmp / count.clamp(min=1) var = (src - mean.gather(dim, index)) var = var * var out = scatter_add(var, index, dim, out, dim_size) out = out / (count - 1 if unbiased else count).clamp(min=1) out = torch.sqrt(out) return out
def scatter_div(src, index, dim=-1, out=None, dim_size=None, fill_value=1): r""" | .. image:: https://raw.githubusercontent.com/rusty1s/pytsd/ master/docs/source/_figures/div.svg?sanitize=true :align: center :width: 400px | Divides all values from the :attr:`src` tensor into :attr:`out` at the indices specified in the :attr:`index` tensor along a given axis :attr:`dim`.If multiple indices reference the same location, their **contributions divide** (`cf.` :meth:`~tsd.scatter_add`). For one-dimensional tensors, the operation computes .. math:: \mathrm{out}_i = \mathrm{out}_i \cdot \prod_j \frac{1}{\mathrm{src}_j} where :math:`\prod_j` is over :math:`j` such that :math:`\mathrm{index}_j = i`. 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 fill output tensor with :attr:`fill_value`. (default: :obj:`1`) :rtype: :class:`Tensor` .. testsetup:: import torch .. testcode:: from tsd import scatter_div src = torch.Tensor([[2, 1, 1, 4, 2], [1, 2, 1, 2, 4]]).float() index = torch.tensor([[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]]) out = src.new_ones((2, 6)) out = scatter_div(src, index, out=out) print(out) .. testoutput:: tensor([[1.0000, 1.0000, 0.2500, 0.5000, 0.5000, 1.0000], [0.5000, 0.2500, 0.5000, 1.0000, 1.0000, 1.0000]]) """ src, out, index, dim = gen(src, index, dim, out, dim_size, fill_value) if src.size(dim) == 0: # pragma: no cover return out return ScatterDiv.apply(out, src, index, dim)
def scatter_min(src, index, dim=-1, out=None, dim_size=None, fill_value=None): r""" | .. image:: https://raw.githubusercontent.com/rusty1s/pytsd/ master/docs/source/_figures/min.svg?sanitize=true :align: center :width: 400px | Minimizes all values from the :attr:`src` tensor into :attr:`out` at the indices specified in the :attr:`index` tensor along a given axis :attr:`dim`.If multiple indices reference the same location, their **contributions minimize** (`cf.` :meth:`~tsd.scatter_add`). The second return tensor contains index location in :attr:`src` of each minimum value (known as argmin). For one-dimensional tensors, the operation computes .. math:: \mathrm{out}_i = \min(\mathrm{out}_i, \min_j(\mathrm{src}_j)) where :math:`\min_j` is over :math:`j` such that :math:`\mathrm{index}_j = i`. 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 fill output tensor with :attr:`fill_value`. (default: :obj:`None`) fill_value (int, optional): If :attr:`out` is not given, automatically fill output tensor with :attr:`fill_value`. If set to :obj:`None`, the output tensor is filled with the greatest possible value of :obj:`src.dtype`. (default: :obj:`None`) :rtype: (:class:`Tensor`, :class:`LongTensor`) .. testsetup:: import torch .. testcode:: from tsd import scatter_min src = torch.Tensor([[-2, 0, -1, -4, -3], [0, -2, -1, -3, -4]]) index = torch.tensor([[ 4, 5, 4, 2, 3], [0, 0, 2, 2, 1]]) out = src.new_zeros((2, 6)) out, argmin = scatter_min(src, index, out=out) print(out) print(argmin) .. testoutput:: tensor([[ 0., 0., -4., -3., -2., 0.], [-2., -4., -3., 0., 0., 0.]]) tensor([[-1, -1, 3, 4, 0, 1], [ 1, 4, 3, -1, -1, -1]]) """ if fill_value is None: op = torch.finfo if torch.is_floating_point(src) else torch.iinfo fill_value = op(src.dtype).max src, out, index, dim = gen(src, index, dim, out, dim_size, fill_value) if src.size(dim) == 0: # pragma: no cover return out, index.new_full(out.size(), -1) return ScatterMin.apply(out, src, index, dim)
def scatter_add(src, index, dim=-1, out=None, dim_size=None, fill_value=0): r""" | .. image:: https://raw.githubusercontent.com/rusty1s/pytorch_scatter/ master/docs/source/_figures/add.svg?sanitize=true :align: center :width: 400px | Sums all values from the :attr:`src` tensor into :attr:`out` at the indices specified in the :attr:`index` tensor along a given axis :attr:`dim`. For each value in :attr:`src`, its output index is specified by its index in :attr:`input` for dimensions outside of :attr:`dim` and by the corresponding value in :attr:`index` for dimension :attr:`dim`. If multiple indices reference the same location, their **contributions add**. Formally, if :attr:`src` and :attr:`index` are n-dimensional tensors with size :math:`(x_0, ..., x_{i-1}, x_i, x_{i+1}, ..., x_{n-1})` and :attr:`dim` = `i`, then :attr:`out` must be an n-dimensional tensor with size :math:`(x_0, ..., x_{i-1}, y, x_{i+1}, ..., x_{n-1})`. Moreover, the values of :attr:`index` must be between `0` and `out.size(dim) - 1`. Both :attr:`src` and :attr:`index` are broadcasted in case their dimensions do not match. For one-dimensional tensors, the operation computes .. math:: \mathrm{out}_i = \mathrm{out}_i + \sum_j \mathrm{src}_j where :math:`\sum_j` is over :math:`j` such that :math:`\mathrm{index}_j = i`. 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 fill output tensor with :attr:`fill_value`. (default: :obj:`0`) :rtype: :class:`Tensor` .. testsetup:: import torch .. testcode:: from torch_scatter import scatter_add src = torch.Tensor([[2, 0, 1, 4, 3], [0, 2, 1, 3, 4]]) index = torch.tensor([[4, 5, 4, 2, 3], [0, 0, 2, 2, 1]]) out = src.new_zeros((2, 6)) out = scatter_add(src, index, out=out) print(out) .. testoutput:: tensor([[0., 0., 4., 3., 3., 0.], [2., 4., 4., 0., 0., 0.]]) """ src, out, index, dim = gen(src, index, dim, out, dim_size, fill_value) return out.scatter_add_(dim, index, src)