torch.Tensor.to_sparse_csr¶
- Tensor.to_sparse_csr(dense_dim=None) Tensor ¶
将张量转换为压缩行存储 (CSR) 格式。除了步进张量 (strided tensors) 外,仅适用于二维张量。如果
self
是步进张量,则可以指定稠密维度 (dense_dim) 的数量,并将创建一个混合 CSR 张量,其中包含 dense_dim 个稠密维度和 self.dim() - 2 - dense_dim 个批处理维度。- 参数
dense_dim (int, 可选) – 结果 CSR 张量的稠密维度数量。此参数仅在
self
是步进张量时使用,其值必须介于 0 和self
张量的维度减二之间。
示例
>>> 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)