The simplest and best solution is to use torch.sum()
.
To sum all elements of a tensor:
torch.sum(outputs) # gives back a scalar
To sum over all rows (i.e. for each column):
torch.sum(outputs, dim=0) # size = [1, ncol]
To sum over all columns (i.e. for each row):
torch.sum(outputs, dim=1) # size = [nrow, 1]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…