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_parameter_init.py 1.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # Copyright 2019 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. import numpy as np
  15. from mindspore import context
  16. import mindspore.nn as nn
  17. from mindspore.ops import operations as P
  18. from mindspore import Tensor, Parameter
  19. import mindspore as ms
  20. class NetWithLoss(nn.Cell):
  21. def __init__(self, network):
  22. super(NetWithLoss, self).__init__()
  23. self.loss = P.SoftmaxCrossEntropyWithLogits()
  24. self.network = network
  25. def construct(self, x, b):
  26. predict = self.network(x)
  27. return self.loss(predict, b)[0]
  28. def test_parameter_init():
  29. class Net(nn.Cell):
  30. def __init__(self, strategy1, weight):
  31. super().__init__()
  32. self.weight = Parameter(weight, "w1")
  33. self.matmul = P.MatMul(transpose_a=False, transpose_b=True).set_strategy(strategy1)
  34. def construct(self, x):
  35. out = self.matmul(x, self.weight)
  36. return out
  37. context.set_auto_parallel_context(device_num=2, global_rank=0)
  38. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  39. strategy1 = ((1, 1), (2, 1))
  40. context.set_context(mode=context.GRAPH_MODE)
  41. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  42. weight = Tensor(np.ones([64, 32]), dtype=ms.float32)
  43. net = Net(strategy1, weight)
  44. net(x,)
  45. if __name__ == '__main__':
  46. test_parameter_init()