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.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. class MakeSparseTensor(nn.Cell):
  29. def __init__(self, dense_shape):
  30. super(MakeSparseTensor, self).__init__()
  31. self.dense_shape = dense_shape
  32. def construct(self, indices, values):
  33. ret = (SparseTensor(indices, values, self.dense_shape),)
  34. return ret[0]
  35. def test_sparse_tensor_make_sparse_tensor():
  36. indices = Tensor([[0, 1], [1, 2]])
  37. values = Tensor([1, 2], dtype=ms.float32)
  38. MakeSparseTensor((3, 4))(indices, values)
  39. def test_sparse_tensor_attr():
  40. grad_op = C.GradOperation('get_all', get_all=True)
  41. class GradWrap(nn.Cell):
  42. def __init__(self, network):
  43. super(GradWrap, self).__init__()
  44. self.network = network
  45. def construct(self, input1, input2):
  46. gout = grad_op(self.network)(input1, input2)
  47. return gout
  48. class SparseTensorGetAttr(nn.Cell):
  49. def __init__(self):
  50. super(SparseTensorGetAttr, self).__init__()
  51. self.dense_shape = (3, 4)
  52. def construct(self, indices, values):
  53. x = SparseTensor(indices, values, self.dense_shape)
  54. return x.values(), x.indices(), x.dense_shape()
  55. indices = Tensor([[0, 1], [1, 2]])
  56. values = Tensor([1, 2], dtype=ms.float32)
  57. SparseTensorGetAttr()(indices, values)
  58. grad_op(SparseTensorGetAttr())(indices, values)
  59. def test_sparse_tensor_indices_dim_greater_than_dense_shape_dim():
  60. indices = Tensor(np.array([[0, 0, 0], [0, 0, 1]], dtype=np.int32))
  61. values = Tensor(np.array([100, 200], dtype=np.float32))
  62. dense_shape = (2, 2)
  63. with pytest.raises(TypeError):
  64. MakeSparseTensor(dense_shape)(indices, values)
  65. def test_sparse_tensor_indices_dim_less_than_dense_shape_dim():
  66. indices = Tensor(np.array([[0, 0], [0, 1]], dtype=np.int32))
  67. values = Tensor(np.array([100, 200], dtype=np.float32))
  68. dense_shape = (2, 2, 2)
  69. with pytest.raises(TypeError):
  70. MakeSparseTensor(dense_shape)(indices, values)