addmm

paddle.sparse. addmm ( input, x, y, beta=1.0, alpha=1.0, name=None ) [源代码]

注解

该 API 从 CUDA 11.0 开始支持。

对输入 x 与输入 y 求稀疏矩阵乘法,并将 input 加到计算结果上。

数学公式:

\[out = alpha * x * y + beta * input\]

输入、输出的格式对应关系如下:

注解

input[SparseCsrTensor] + x[SparseCsrTensor] @ y[SparseCsrTensor] -> out[SparseCsrTensor]

input[DenseTensor] + x[SparseCsrTensor] @ y[DenseTensor] -> out[DenseTensor]

input[SparseCooTensor] + x[SparseCooTensor] @ y[SparseCooTensor] -> out[SparseCooTensor]

input[DenseTensor] + x[SparseCooTensor] @ y[DenseTensor] -> out[DenseTensor]

该 API 支持反向传播,inputxy 的维度相同且>=2D,不支持自动广播。

参数

  • input (SparseTensor|DenseTensor) - 输入 Tensor,可以为 Coo 或 Csr 格式 或 DenseTensor。数据类型为 float32、float64。

  • x (SparseTensor) - 输入 Tensor,可以为 Coo 或 Csr 格式。数据类型为 float32、float64。

  • y (SparseTensor|DenseTensor) - 输入 Tensor,可以为 Coo 或 Csr 格式 或 DenseTensor。数据类型为 float32、float64。

  • beta (float, 可选) - input 的系数。默认:1.0。

  • alpha (float, 可选) - x * y 的系数。默认:1.0。

  • name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。

返回

SparseTensor|DenseTensor: 其 Tensor 类型、dtype、shape 与 input 相同。

代码示例

>>> import paddle
>>> paddle.device.set_device('gpu')

>>> # dense + csr @ dense -> dense
>>> input = paddle.rand([3, 2])
>>> crows = [0, 1, 2, 3]
>>> cols = [1, 2, 0]
>>> values = [1., 2., 3.]
>>> x = paddle.sparse.sparse_csr_tensor(crows, cols, values, [3, 3])
>>> y = paddle.rand([3, 2])
>>> out = paddle.sparse.addmm(input, x, y, 3.0, 2.0)

>>> # dense + coo @ dense -> dense
>>> input = paddle.rand([3, 2])
>>> indices = [[0, 1, 2], [1, 2, 0]]
>>> values = [1., 2., 3.]
>>> x = paddle.sparse.sparse_coo_tensor(indices, values, [3, 3])
>>> y = paddle.rand([3, 2])
>>> out = paddle.sparse.addmm(input, x, y, 3.0, 2.0)