torch.sparse_coo_tensor¶
- torch.sparse_coo_tensor(indices, values, size=None, *, dtype=None, device=None, pin_memory=False, requires_grad=False, check_invariants=None, is_coalesced=None) Tensor ¶
構造一個 COO(rdinate) 格式的稀疏張量,在給定的
indices
指定位置上賦予特定的值。注意
當
is_coalesced
未指定或為None
時,此函數會返回一個 未合併(uncoalesced)的張量。注意
如果未指定
device
參數,則給定的values
和 indices 張量的裝置 (device) 必須匹配。 但是,如果指定了該參數,則輸入張量將被轉換為給定的裝置,進而確定構造的稀疏張量的裝置。- 參數
indices (array_like) – 張量的初始資料。可以是列表、元組、NumPy
ndarray
、純量和其他類型。在內部會轉換為torch.LongTensor
。indices 是矩陣中非零值的坐標,因此應該是二維的,其中第一維是張量維度的數量,第二維是非零值的數量。values (array_like) – 張量的初始值。可以是列表、元組、NumPy
ndarray
、純量和其他類型。size (list, tuple, or
torch.Size
, optional) – 稀疏張量的大小。如果未提供,則大小將被推斷為足以容納所有非零元素的最小大小。
- 關鍵字參數
dtype (
torch.dtype
, optional) – 返回張量的期望資料類型。預設值:如果為 None,則從values
推斷資料類型。device (
torch.device
, optional) – 返回張量的期望裝置。預設值:如果為 None,則使用預設張量類型的當前裝置(請參閱torch.set_default_device()
)。對於 CPU 張量類型,device
將為 CPU,對於 CUDA 張量類型,則為當前的 CUDA 裝置。pin_memory (bool, optional) – 如果設定,返回的張量將在鎖頁記憶體 (pinned memory) 中分配。僅適用於 CPU 張量。預設值:
False
。requires_grad (bool, optional) – 如果 autograd 應該記錄返回張量的操作。預設值:
False
。check_invariants (bool, optional) – 是否檢查稀疏張量不變性。預設值:由
torch.sparse.check_sparse_tensor_invariants.is_enabled()
返回,初始值為 False。is_coalesced (bool, optional) – 當為``True``時,呼叫者有責任提供對應於合併張量的張量索引。如果
check_invariants
標誌為 False,如果未滿足前提條件,則不會引發錯誤,並且這將導致無聲地產生不正確的結果。要強制合併,請在結果張量上使用coalesce()
。預設值:None:除了簡單的情況(例如 nnz < 2)外,生成的張量的 is_coalesced 設定為False`
。
範例
>>> i = torch.tensor([[0, 1, 1], ... [2, 0, 2]]) >>> v = torch.tensor([3, 4, 5], dtype=torch.float32) >>> torch.sparse_coo_tensor(i, v, [2, 4]) tensor(indices=tensor([[0, 1, 1], [2, 0, 2]]), values=tensor([3., 4., 5.]), size=(2, 4), nnz=3, layout=torch.sparse_coo) >>> torch.sparse_coo_tensor(i, v) # Shape inference tensor(indices=tensor([[0, 1, 1], [2, 0, 2]]), values=tensor([3., 4., 5.]), size=(2, 3), nnz=3, layout=torch.sparse_coo) >>> torch.sparse_coo_tensor(i, v, [2, 4], ... dtype=torch.float64, ... device=torch.device('cuda:0')) tensor(indices=tensor([[0, 1, 1], [2, 0, 2]]), values=tensor([3., 4., 5.]), device='cuda:0', size=(2, 4), nnz=3, dtype=torch.float64, layout=torch.sparse_coo) # Create an empty sparse tensor with the following invariants: # 1. sparse_dim + dense_dim = len(SparseTensor.shape) # 2. SparseTensor._indices().shape = (sparse_dim, nnz) # 3. SparseTensor._values().shape = (nnz, SparseTensor.shape[sparse_dim:]) # # For instance, to create an empty sparse tensor with nnz = 0, dense_dim = 0 and # sparse_dim = 1 (hence indices is a 2D tensor of shape = (1, 0)) >>> S = torch.sparse_coo_tensor(torch.empty([1, 0]), [], [1]) tensor(indices=tensor([], size=(1, 0)), values=tensor([], size=(0,)), size=(1,), nnz=0, layout=torch.sparse_coo) # and to create an empty sparse tensor with nnz = 0, dense_dim = 1 and # sparse_dim = 1 >>> S = torch.sparse_coo_tensor(torch.empty([1, 0]), torch.empty([0, 2]), [1, 2]) tensor(indices=tensor([], size=(1, 0)), values=tensor([], size=(0, 2)), size=(1, 2), nnz=0, layout=torch.sparse_coo)