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.

test_sparse_tensor.py 3.4 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright 2020 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. """
  16. @File : test_sparse_tensor.py
  17. @Author:
  18. @Date : 2020-07-16
  19. @Desc : test mindspore sparse_tensor's operation
  20. """
  21. import numpy as np
  22. import pytest
  23. import mindspore as ms
  24. import mindspore.nn as nn
  25. from mindspore.ops import composite as C
  26. from mindspore import Tensor, SparseTensor, context
  27. context.set_context(mode=context.GRAPH_MODE, enable_sparse=True)
  28. grad_op = C.GradOperation('get_all', get_all=True)
  29. class MakeSparseTensor(nn.Cell):
  30. def __init__(self, dense_shape):
  31. super(MakeSparseTensor, self).__init__()
  32. self.dense_shape = dense_shape
  33. def construct(self, indices, values):
  34. ret = (SparseTensor(indices, values, self.dense_shape),)
  35. return ret[0]
  36. def test_sparse_tensor_make_sparse_tensor():
  37. indices = Tensor([[0, 1], [1, 2]])
  38. values = Tensor([1, 2], dtype=ms.float32)
  39. MakeSparseTensor((3, 4))(indices, values)
  40. def test_sparse_tensor_attr():
  41. class SparseTensorGetAttr(nn.Cell):
  42. def __init__(self):
  43. super(SparseTensorGetAttr, self).__init__()
  44. self.dense_shape = (3, 4)
  45. def construct(self, indices, values):
  46. x = SparseTensor(indices, values, self.dense_shape)
  47. return x.values(), x.indices(), x.dense_shape()
  48. indices = Tensor([[0, 1], [1, 2]])
  49. values = Tensor([1, 2], dtype=ms.float32)
  50. SparseTensorGetAttr()(indices, values)
  51. grad_op(SparseTensorGetAttr())(indices, values)
  52. def test_sparse_tensor_indices_dim_greater_than_dense_shape_dim():
  53. indices = Tensor(np.array([[0, 0, 0], [0, 0, 1]], dtype=np.int32))
  54. values = Tensor(np.array([100, 200], dtype=np.float32))
  55. dense_shape = (2, 2)
  56. with pytest.raises(TypeError):
  57. MakeSparseTensor(dense_shape)(indices, values)
  58. def test_sparse_tensor_indices_dim_less_than_dense_shape_dim():
  59. indices = Tensor(np.array([[0, 0], [0, 1]], dtype=np.int32))
  60. values = Tensor(np.array([100, 200], dtype=np.float32))
  61. dense_shape = (2, 2, 2)
  62. with pytest.raises(TypeError):
  63. MakeSparseTensor(dense_shape)(indices, values)
  64. def test_sparse_tensor_to_tensor():
  65. class SparseToDenseCell(nn.Cell):
  66. def __init__(self, dense_shape):
  67. super(SparseToDenseCell, self).__init__()
  68. self.dense_shape = dense_shape
  69. self.sparse_to_dense = nn.SparseToDense()
  70. def construct(self, indices, values):
  71. sparse = SparseTensor(indices, values, self.dense_shape)
  72. return self.sparse_to_dense(sparse)
  73. indices = Tensor([[0, 1], [1, 2]])
  74. values = Tensor([1, 2], dtype=ms.float32)
  75. dense_shape = (3, 4)
  76. SparseToDenseCell(dense_shape)(indices, values)
  77. grad_op(SparseToDenseCell(dense_shape))(indices, values)