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_expand_as.py 2.2 kB

5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. """ test expand_as"""
  16. import mindspore as ms
  17. import mindspore.nn as nn
  18. import mindspore.common.initializer as init
  19. from mindspore import Tensor
  20. from mindspore import context
  21. context.set_context(mode=context.GRAPH_MODE)
  22. def test_expand_as():
  23. class Net(nn.Cell):
  24. def __init__(self):
  25. super(Net, self).__init__()
  26. self.t1 = Tensor([1, 2, 3])
  27. self.t2 = Tensor([[1, 1, 1], [1, 1, 1]])
  28. def construct(self):
  29. return self.t1.expand_as(self.t2)
  30. net = Net()
  31. net()
  32. def test_initializer_expand_as():
  33. class Net(nn.Cell):
  34. def __init__(self):
  35. super(Net, self).__init__()
  36. self.t1 = init.initializer('one', [1, 3], ms.float32)
  37. self.t2 = init.initializer('one', [2, 3], ms.float32)
  38. def construct(self):
  39. return self.t1.expand_as(self.t2)
  40. net = Net()
  41. net()
  42. def test_expand_as_parameter():
  43. class Net(nn.Cell):
  44. def __init__(self):
  45. super(Net, self).__init__()
  46. self.t1 = Tensor([1, 2, 3])
  47. def construct(self, x):
  48. return self.t1.expand_as(x)
  49. net = Net()
  50. net(Tensor([[1, 1, 1], [1, 1, 1]]))
  51. def test_expand_tensor_as_parameter_1():
  52. class Net(nn.Cell):
  53. def __init__(self):
  54. super(Net, self).__init__()
  55. self.t2 = Tensor([[1, 1, 1], [1, 1, 1]])
  56. def construct(self, x):
  57. return x.expand_as(self.t2)
  58. net = Net()
  59. net(Tensor([1, 2, 3]))