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_scatter_nd.py 1.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. import numpy as np
  16. import pytest
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.ops import operations as P
  21. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  22. class Net(nn.Cell):
  23. def __init__(self, _shape):
  24. super(Net, self).__init__()
  25. self.shape = _shape
  26. self.scatternd = P.ScatterNd()
  27. def construct(self, indices, update):
  28. return self.scatternd(indices, update, self.shape)
  29. def scatternd_net(indices, update, _shape, expect):
  30. scatternd = Net(_shape)
  31. output = scatternd(Tensor(indices), Tensor(update))
  32. error = np.ones(shape=output.asnumpy().shape) * 1.0e-6
  33. diff = output.asnumpy() - expect
  34. assert np.all(diff < error)
  35. assert np.all(-diff < error)
  36. @pytest.mark.level0
  37. @pytest.mark.platform_x86_gpu_traning
  38. @pytest.mark.env_onecard
  39. def test_scatternd():
  40. arr_indices = np.array([[0, 1], [1, 1]]).astype(np.int32)
  41. arr_update = np.array([3.2, 1.1]).astype(np.float32)
  42. shape = (2, 2)
  43. expect = np.array([[0., 3.2],
  44. [0., 1.1]])
  45. scatternd_net(arr_indices, arr_update, shape, expect)