You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sparse.py 4.4 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. # Copyright 2020-2021 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Sparse related tools."""
  16. from mindspore.ops import operations as P
  17. from ..cell import Cell
  18. class SparseToDense(Cell):
  19. """
  20. Convert a sparse tensor into dense.
  21. Not yet supported by any backend at the moment.
  22. Args:
  23. sparse_tensor (SparseTensor): the sparse tensor to convert.
  24. Returns:
  25. Tensor, the tensor converted.
  26. Supported Platforms:
  27. ``CPU``
  28. Examples:
  29. >>> import mindspore as ms
  30. >>> from mindspore import Tensor, SparseTensor
  31. >>> import mindspore.nn as nn
  32. >>> indices = Tensor([[0, 1], [1, 2]])
  33. >>> values = Tensor([1, 2], dtype=ms.int32)
  34. >>> dense_shape = (3, 4)
  35. >>> sparse_tensor = SparseTensor(indices, values, dense_shape)
  36. >>> sparse_to_dense = nn.SparseToDense()
  37. >>> result = sparse_to_dense(sparse_tensor)
  38. >>> print(result)
  39. [[0 1 0 0]
  40. [0 0 2 0]
  41. [0 0 0 0]]
  42. """
  43. def __init__(self):
  44. super(SparseToDense, self).__init__()
  45. self.sparse_to_dense = P.SparseToDense()
  46. def construct(self, sparse_tensor):
  47. return self.sparse_to_dense(sparse_tensor.indices,
  48. sparse_tensor.values,
  49. sparse_tensor.dense_shape)
  50. class SparseTensorDenseMatmul(Cell):
  51. """
  52. Multiply SparseTensor(of rank 2) "A" by dense tensor.
  53. The shape of sparse tensor is :math:`(N, C)`, and the shape of dense tensor is :math:`(C, M)`, then the shape of
  54. output tensor is :math:`(N, M)`.The output data type is the same as "values".
  55. Args:
  56. - *adjoint_st** (Bool) - If true, SparseTensor is transposed before multiplication. Default: False.
  57. - *adjoint_dt** (Bool) - If true, DenseTensor is transposed before multiplication. Default: False.
  58. Inputs:
  59. - **indices** (Tensor) - The indices of sparse representation, support int32/int64.
  60. - **values** (Tensor) - Values corresponding to each row of indices.
  61. - **dense_shape** (tuple) - An int tuple which specifies the shape of dense tensor. The dense_shape is :
  62. math:`(N, C)`. If `adjoint_st` is True, its shape must be :math:`(N, C)` after transpose.
  63. - **dense** (Tensor) - Dense Matrix. The shape of the tensor is :math:`(C, M)`. If
  64. `adjoint_dt` is True, its shape must be :math:`(C, M)` after transpose.
  65. Returns:
  66. Tensor, the shape of tensor is :math:`(N, M)`.The output data type is the same as "values".
  67. Examples:
  68. >>> class NetSparseDenseMatmul(nn.Cell):
  69. ... def __init__(self):
  70. ... super(NetSparseDenseMatmul, self).__init__()
  71. ... self.matmul = nn.SparseTensorDenseMatmul()
  72. ...
  73. ... def construct(self, indices, values, dens_shape, dt):
  74. ... return self.matmul(indices, values, dens_shape, dt)
  75. ...
  76. >>> indices = Tensor([[0, 1], [1, 2]], dtype=ms.int32)
  77. >>> values = Tensor([1, 2], dtype=ms.float32)
  78. >>> dense_shape = (3, 4)
  79. >>> dsMatrix = Tensor([[1, 1], [2, 2], [3, 3], [4, 4]], dtype=ms.float32)
  80. >>> test_SparseDenseMatmul = NetSparseDenseMatmul()
  81. >>> out = test_SparseDenseMatmul(indices, values, dens_shape, dsMatrix)
  82. """
  83. def __init__(self, adjoint_st=False, adjoint_dt=False):
  84. """Initialize SparseTensorDenseMatmul"""
  85. super(SparseTensorDenseMatmul, self).__init__()
  86. self.adjst = adjoint_st
  87. self.adjdt = adjoint_dt
  88. self.matmul = P.SparseTensorDenseMatmul(adjoint_st=self.adjst, adjoint_dt=self.adjdt)
  89. def construct(self, indices, values, dense_shape, dense):
  90. return self.matmul(indices, values, dense_shape, dense)