torch.Tensor.to_sparse_csr¶
- Tensor.to_sparse_csr(dense_dim=None) Tensor ¶
将张量转换为压缩行存储格式 (CSR)。除了步幅张量外,仅适用于 2D 张量。如果
self
是步幅张量,则可以指定密集维度数,并将创建一个混合 CSR 张量,其中包含 dense_dim 个密集维度和 self.dim() - 2 - dense_dim 个批处理维度。- 参数
dense_dim (int, 可选) – 结果 CSR 张量的密集维度数。仅当
self
是步幅张量时才应使用此参数,并且必须是介于 0 和self
张量维度减 2 之间的值。
示例
>>> dense = torch.randn(5, 5) >>> sparse = dense.to_sparse_csr() >>> sparse._nnz() 25 >>> dense = torch.zeros(3, 3, 1, 1) >>> dense[0, 0] = dense[1, 2] = dense[2, 1] = 1 >>> dense.to_sparse_csr(dense_dim=2) tensor(crow_indices=tensor([0, 1, 2, 3]), col_indices=tensor([0, 2, 1]), values=tensor([[[1.]], [[1.]], [[1.]]]), size=(3, 3, 1, 1), nnz=3, layout=torch.sparse_csr)