torch. Sum () sums up one dimension of the input tensor data, which are divided into two forms:
1.torch.sum(input, dtype=None)
2.torch.sum(input, list: dim, bool: keepdim=False, dtype=None) → Tensor
input:输入一个tensor
dim:要求和的维度,可以是一个列表
keepdim:求和之后这个dim的元素个数为1,所以要被去掉,如果要保留这个维度,则应当keepdim=True
#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.
example:
a = torch.ones((2, 3))
print(a):
tensor([[1, 1, 1],
[1, 1, 1]])
a1 = torch.sum(a)
a2 = torch.sum(a, dim=0)
a3 = torch.sum(a, dim=1)
print(a)
print(a1)
print(a2)
output:
tensor(6.)
tensor([2., 2., 2.])
tensor([3., 3.])
if you add keepdim=True, the dim dimension is kept from being squeezed
a1 = torch.sum(a, dim=(0, 1), keepdim=True)
a2 = torch.sum(a, dim=(0, ), keepdim=True)
a3 = torch.sum(a, dim=(1, ), keepdim=True)
output:
tensor([[6.]])
tensor([[2., 2., 2.]])
tensor([[3., 3.]])
</ div>
Read More:
- tf.one_ How to use hot ()
- RuntimeError: NCCL error in: /pytorch/torch/lib/c10d/ProcessGroupNCCL.cpp:784 torch
- RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the
- Tensorflow: How to use expand_Dim() to add dimensions
- To solve the problem of increasing video memory when training network (torch)
- PyTorch – AttributeError: ‘bool‘ object has no attribute ‘sum‘
- The sum of the two numbers of leetcode
- How to use the menu of elementui unique opened sidebar
- How to use C + + function pointer array
- How to use matlab to solve equation
- torch.nn.BCELoss are unsafe to autocast [How to Solve]
- How to Solve failed Precondition Error: attempting to use uninitialized value beta1_power
- How to use matlab xlswrite
- How to use Latex to Write Matrices
- How to Use paho.mqtt.cpp
- How to Use Truffle to Deploy contracts on moonbeam
- Build your own resnet18 network and load torch vision’s own weight
- How to Use Subfigure in Latex
- How to Use Apt get Command Under Mac OSX
- How to Use the Reverse() Function