dgl.sparse.matmul
- dgl.sparse.matmul(A: Tensor | SparseMatrix, B: Tensor | SparseMatrix) Tensor | SparseMatrix [source]
将两个密集/稀疏矩阵相乘,等价于
A @ B
。此函数不支持
A
是torch.Tensor
而B
是SparseMatrix
的情况。如果两个矩阵都是 torch.Tensor,它会调用
torch.matmul()
。结果是一个密集矩阵。如果两个矩阵都是稀疏的,它会调用
dgl.sparse.spspmm()
。结果是一个稀疏矩阵。如果
A
是稀疏的而B
是密集的,它会调用dgl.sparse.spmm()
。结果是一个密集矩阵。该运算符支持批处理的稀疏-密集矩阵乘法。在这种情况下,稀疏矩阵
A
的形状应为(L, M)
,其中非零值具有批处理维度K
。密集矩阵B
的形状应为(M, N, K)
。输出是一个形状为(L, N, K)
的密集矩阵。稀疏-稀疏矩阵乘法不支持批处理计算。
- 参数:
A (torch.Tensor 或 SparseMatrix) – 第一个矩阵。
B (torch.Tensor 或 SparseMatrix) – 第二个矩阵。
- 返回:
结果矩阵
- 返回类型:
torch.Tensor 或 SparseMatrix
示例
将对角矩阵与密集矩阵相乘。
>>> val = torch.randn(3) >>> A = dglsp.diag(val) >>> B = torch.randn(3, 2) >>> result = dglsp.matmul(A, B) >>> type(result) <class 'torch.Tensor'> >>> result.shape torch.Size([3, 2])
将稀疏矩阵与密集矩阵相乘。
>>> indices = torch.tensor([[0, 1, 1], [1, 0, 1]]) >>> val = torch.randn(indices.shape[1]) >>> A = dglsp.spmatrix(indices, val) >>> X = torch.randn(2, 3) >>> result = dglsp.matmul(A, X) >>> type(result) <class 'torch.Tensor'> >>> result.shape torch.Size([2, 3])
将稀疏矩阵与稀疏矩阵相乘。
>>> indices1 = torch.tensor([[0, 1, 1], [1, 0, 1]]) >>> val1 = torch.ones(indices1.shape[1]) >>> A = dglsp.spmatrix(indices1, val1) >>> indices2 = torch.tensor([[0, 1, 1], [0, 2, 1]]) >>> val2 = torch.ones(indices2.shape[1]) >>> B = dglsp.spmatrix(indices2, val2) >>> result = dglsp.matmul(A, B) >>> type(result) <class 'dgl.sparse.sparse_matrix.SparseMatrix'> >>> result.shape (2, 3)