本文簡要介紹python語言中 torch.sparse.sum
的用法。
用法:
torch.sparse.sum(input, dim=None, dtype=None)
返回給定維度
dim
中稀疏張量input
的每一行的總和。如果dim
是維度列表,則對所有維度進行歸約。當對所有sparse_dim
求和時,此方法返回密集張量而不是稀疏張量。所有求和的
dim
都被壓縮(參見torch.squeeze()
),導致輸出張量的dim
比input
的維度少。在反向期間,隻有
input
的nnz
位置處的梯度會向後傳播。請注意,input
的梯度是合並的。例子:
>>> 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 sparse tensor >>> 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])
參數:
相關用法
- Python PyTorch sum用法及代碼示例
- Python PyTorch sub用法及代碼示例
- Python PyTorch saved_tensors_hooks用法及代碼示例
- Python PyTorch sqrt用法及代碼示例
- Python PyTorch skippable用法及代碼示例
- Python PyTorch squeeze用法及代碼示例
- Python PyTorch square用法及代碼示例
- Python PyTorch save_on_cpu用法及代碼示例
- Python PyTorch scatter_object_list用法及代碼示例
- Python PyTorch skip_init用法及代碼示例
- Python PyTorch simple_space_split用法及代碼示例
- Python PyTorch sparse_csr_tensor用法及代碼示例
- Python PyTorch sentencepiece_numericalizer用法及代碼示例
- Python PyTorch symeig用法及代碼示例
- Python PyTorch sinh用法及代碼示例
- Python PyTorch sinc用法及代碼示例
- Python PyTorch std_mean用法及代碼示例
- Python PyTorch spectral_norm用法及代碼示例
- Python PyTorch slogdet用法及代碼示例
- Python PyTorch symbolic_trace用法及代碼示例
- Python PyTorch shutdown用法及代碼示例
- Python PyTorch sgn用法及代碼示例
- Python PyTorch set_flush_denormal用法及代碼示例
- Python PyTorch set_default_dtype用法及代碼示例
- Python PyTorch signbit用法及代碼示例
注:本文由純淨天空篩選整理自pytorch.org大神的英文原創作品 torch.sparse.sum。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。