queezed (see [ ` torch.squeeze()  `
](generated/torch.squeeze.html#torch.squeeze "torch.squeeze") ), resulting an
output tensor having ` dim  ` fewer dimensions than ` input  ` .

During backward, only gradients at ` nnz  ` locations of ` input  ` will
propagate back. Note that the gradients of ` input  ` is coalesced.

Parameters

    

  * **input** ( [ _Tensor_ ](tensors.html#torch.Tensor "torch.Tensor") ) – the input SparseTensor 

  * **dim** ( [ _int_ ](https://docs.python.org/3/library/functions.html#int "\(in Python v3.8\)") _or_ _tuple of python:ints_ ) – a dimension or a list of dimensions to reduce. Default: reduce over all dims. 

  * **dtype** ( ` torch.dtype  ` , optional) – the desired data type of returned Tensor. Default: dtype of ` input  ` . 

Example:

    
    
    >>> nnz = 3
    >>> dims = [5, 5, 2, 3]
    >>> I = torch.cat([torch.randint(0, dims[0], size=(nnz,)),
                       torch.randint(0, dims[1], size=(nnz,))], 0).reshape(2, nnz)
    >>> V = torch.randn(nnz, dims[2], dims[3])
    >>> size = torch.Size(dims)
    >>> S = torch.sparse_coo_tensor(I, V, size)
    >>> S
    tensor(indices=tensor([[2, 0, 3],
                           [2, 4, 1]]),
           values=tensor([[[-0.6438, -1.6467,  1.4004],
                           [ 0.3411,  0.0918, -0.2312]],
    
                          [[ 0.5348,  0.0634, -2.0494],
                           [-0.7125, -1.0646,  2.1844]],
    
                          [[ 0.1276,  0.1874, -0.6334],
                           [-1.9682, -0.5340,  0.7483]]]),
           size=(5, 5, 2, 3), nnz=3, layout=torch.sparse_coo)
    
    # when sum over only part of sparse_dims, return a SparseTensor
    >>> torch.sparse.sum(S, [1, 3])
    tensor(indices=tensor([[0, 2, 3]]),
           values=tensor([[-1.4512,  0.4073],
                          [-0.8901,  0.2017],
                          [-0.3183, -1.7539]]),
           size=(5, 2), nnz=3, layout=torch.sparse_coo)
    
    # when sum over all sparse dim, return a dense Tensor
    # with summed dims squeezed
    >>> torch.sparse.sum(S, [0, 1, 3])
    tensor([-2.6596, -1.1450])
    

[ Next ![](_static/images/chevron-right-orange.svg) ](storage.html
"torch.Storage") [ ![](_static/images/chevron-right-orange.svg) Previous
](random.html "torch.random")

* * *

© Copyright 2019, Torch Contributors.

Built with [ Sphinx ](http://sphinx-doc.org/) using a [ theme
](https://github.com/rtfd/sphinx_rtd_theme) provided by [ Read the Docs
](https://readthedocs.org) .

  * torch.sparse 
    * Functions 

![](https://www.googleadservices.com/pagead/conversion/795629140/?label=txkmCPmdtosBENSssfsC&guid=ON&script=0)

##  Docs

Access comprehensive developer documentation for PyTorch

[ View Docs ](https://pytorch.org/docs/stable/index.html)

##  Tutorials

Get in-depth tutorials for beginners and advanced developers

[ View Tutorials ](https://pytorch.org/tutorials)

##  Resources

Find development resources and get your questions answered

[ View Resources ](https://pytorch.org/resources)

[ ](https://pytorch.org/)

  * [ PyTorch ](https://pytorch.org/)
  * [ Get Started ](https://pytorch.org/get-started)
  * [ Features ](https://pytorch.org/features)
  * [ Ecosystem ](https://pytorch.org/ecosystem)
  * [ Blog ](https://pytorch.org/blog/)
  * [ Contributing ](https://github.com/pytorch/pytorch/blob/master/CONTRIBUTING.md)

  * [ Resources ](https://pytorch.org/resources)
  * [ Tutorials ](https://pytorch.org/tutorials)
  * [ Docs ](https://pytorch.org/docs/stable/index.html)
  * [ Discuss ](https://discuss.pytorch.org)
  * [ Github Issues ](https://github.com/pytorch/pytorch/issues)
  * [ Brand Guidelines ](https://pytorch.org/assets/brand-guidelines/PyTorch-Brand-Guidelines.pdf)

  * Stay Connected 
  * Email Address 

[ ](https://www.facebook.com/pytorch) [ ](https://twitter.com/pytorch) [
](https://www.youtube.com/pytorch)

To analyze traffic and optimize your experience, we serve cookies on this
site. By clicking or navigating, yo