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_while_ScatterNdUpdate.py 1.6 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. from mindspore import context, nn, Tensor, Parameter
  17. from mindspore.common import dtype as mstype
  18. from mindspore.ops import operations as P
  19. context.set_context(mode=context.GRAPH_MODE, save_graphs=False)
  20. class Net(nn.Cell):
  21. def __init__(self, data):
  22. super(Net, self).__init__()
  23. self.start = Tensor(0, dtype=mstype.int32)
  24. self.end = Tensor(2, dtype=mstype.int32)
  25. self.max_output = Parameter(data, "output_x")
  26. self.upd = P.ScatterNdUpdate()
  27. self.zero = Tensor(np.ones([1], dtype=np.int32))
  28. def construct(self, inputs):
  29. idx = self.start
  30. end = self.end
  31. while idx < end:
  32. xi = inputs[idx, :, :]
  33. self.upd(self.max_output, idx + self.zero, xi)
  34. idx = idx + 1
  35. return self.max_output + 0
  36. def test_x():
  37. x = Tensor(np.arange(10 * 2 * 3).reshape(10, 2, 3).astype(np.float32))
  38. net = Net(x)
  39. net(x)