python - Torch sum a tensor along an axis - Stack Overflow
stackoverflow.com › questions › 44790670Jun 27, 2017 · So, in your example, you could use: outputs.sum(1) or torch.sum(outputs,1), or, equivalently, outputs.sum(-1) or torch.sum(outputs,-1). All of these would give the same result, an output tensor of size torch.Size([10]), with each entry being the sum over the all rows in a given column of the tensor outputs. To illustrate with a 3-dimensional ...
torch.sum — PyTorch 1.10.1 documentation
pytorch.org › docs › stabletorch.sum(input, dim, keepdim=False, *, dtype=None) → Tensor. Returns the sum of each row of the input tensor in the given dimension dim. If dim is a list of dimensions, reduce over all of them. If keepdim is True, the output tensor is of the same size as input except in the dimension (s) dim where it is of size 1.