Multiply two sparse tensors element-wise. Input x and y's shape should be identical and have same sparse type(SparseCooTensor or SparseCsrTensor).If input is SparseCooTensor, x and y's sparse_dim should be identical. The equation is: .. math:: out = x * y Args:
(x: Tensor, y: Tensor, name: str | None = None)
| 355 | |
| 356 | |
| 357 | def multiply(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: |
| 358 | """ |
| 359 | Multiply two sparse tensors element-wise. Input x and y's shape should be identical and have same sparse |
| 360 | type(SparseCooTensor or SparseCsrTensor).If input is SparseCooTensor, x and y's sparse_dim should be identical. |
| 361 | The equation is: |
| 362 | |
| 363 | .. math:: |
| 364 | out = x * y |
| 365 | |
| 366 | Args: |
| 367 | x (Tensor): the input tensor, it's data type should be float32, float64, int32, int64, complex64, complex128. |
| 368 | y (Tensor): the input tensor, it's data type should be float32, float64, int32, int64, complex64, complex128. |
| 369 | name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`. |
| 370 | |
| 371 | Returns: |
| 372 | Tensor: the result tensor. |
| 373 | |
| 374 | Examples: |
| 375 | |
| 376 | .. code-block:: pycon |
| 377 | |
| 378 | >>> import paddle |
| 379 | |
| 380 | >>> paddle.device.set_device("cpu") |
| 381 | |
| 382 | >>> x = paddle.to_tensor([[0, -1, 0, 2], [0, 0, -3, 0], [4, 5, 0, 0]], 'float32') |
| 383 | >>> y = paddle.to_tensor([[0, 0, 0, -2], [0, 2, -3, 0], [2, 3, 4, 8]], 'float32') |
| 384 | >>> sparse_x = x.to_sparse_csr() |
| 385 | >>> sparse_y = y.to_sparse_csr() |
| 386 | >>> sparse_z = paddle.sparse.multiply(sparse_x, sparse_y) |
| 387 | >>> print(sparse_z.to_dense()) |
| 388 | Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True, |
| 389 | [[ 0., -0., 0., -4.], |
| 390 | [ 0., 0., 9., 0.], |
| 391 | [ 8., 15., 0., 0.]]) |
| 392 | |
| 393 | """ |
| 394 | |
| 395 | if isinstance(y, (int, float)): |
| 396 | return _C_ops.sparse_scale(x, float(y), 0.0, True) |
| 397 | else: |
| 398 | if in_dygraph_mode(): |
| 399 | return _C_ops.sparse_multiply(x, y) |
| 400 | elif in_pir_mode(): |
| 401 | return _C_ops.sparse_multiply(x, y) |
| 402 | else: |
| 403 | raise RuntimeError( |
| 404 | "We currently only support dynamic graph mode or the new IR mode." |
| 405 | ) |
| 406 | |
| 407 | |
| 408 | def divide(x: Tensor, y: Tensor, name: str | None = None) -> Tensor: |
no test coverage detected