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