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_view.py 3.5 kB

4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 view"""
  16. import pytest
  17. import mindspore as ms
  18. import mindspore.nn as nn
  19. import mindspore.common.initializer as init
  20. from mindspore import Tensor
  21. from mindspore import context
  22. context.set_context(mode=context.GRAPH_MODE)
  23. def test_view():
  24. class Net(nn.Cell):
  25. def __init__(self):
  26. super(Net, self).__init__()
  27. self.value = Tensor([[1, 2, 3], [4, 5, 6]])
  28. def construct(self):
  29. return self.value.view(-1)
  30. net = Net()
  31. net()
  32. def test_view_initializer():
  33. class Net(nn.Cell):
  34. def __init__(self):
  35. super(Net, self).__init__()
  36. self.value = init.initializer('normal', [2, 3], ms.float32)
  37. def construct(self):
  38. return self.value.view(-1)
  39. net = Net()
  40. net()
  41. def test_view_1():
  42. class Net(nn.Cell):
  43. def __init__(self):
  44. super(Net, self).__init__()
  45. self.value = Tensor([[1, 2, 3], [4, 5, 6]])
  46. def construct(self):
  47. return self.value.view((3, 2))
  48. net = Net()
  49. net()
  50. def test_view_2():
  51. class Net(nn.Cell):
  52. def __init__(self):
  53. super(Net, self).__init__()
  54. self.value = Tensor([[1, 2, 3], [4, 5, 6]])
  55. def construct(self):
  56. return self.value.view(3, 2)
  57. net = Net()
  58. net()
  59. def test_view_parameter():
  60. class Net(nn.Cell):
  61. def __init__(self):
  62. super(Net, self).__init__()
  63. def construct(self, x):
  64. return x.view(-1)
  65. net = Net()
  66. net(Tensor([[1, 2, 3], [4, 5, 6]]))
  67. def test_view_parameter_1():
  68. class Net(nn.Cell):
  69. def __init__(self):
  70. super(Net, self).__init__()
  71. def construct(self, x):
  72. return x.view((3, 2))
  73. net = Net()
  74. net(Tensor([[1, 2, 3], [4, 5, 6]]))
  75. def test_view_parameter_2():
  76. class Net(nn.Cell):
  77. def __init__(self):
  78. super(Net, self).__init__()
  79. def construct(self, x):
  80. return x.view(3, 2)
  81. net = Net()
  82. net(Tensor([[1, 2, 3], [4, 5, 6]]))
  83. def test_view_shape_error():
  84. class Net(nn.Cell):
  85. def __init__(self):
  86. super(Net, self).__init__()
  87. self.value = Tensor([[1, 2, 3], [4, 5, 6]])
  88. def construct(self):
  89. return self.value.view()
  90. net = Net()
  91. with pytest.raises(ValueError) as ex:
  92. net()
  93. assert "The shape variable should not be empty" in str(ex.value)
  94. def test_view_shape_error_1():
  95. class Net(nn.Cell):
  96. def __init__(self):
  97. super(Net, self).__init__()
  98. self.value = Tensor([[1, 2, 3], [4, 5, 6]])
  99. def construct(self):
  100. return self.value.view((2, 3), (4, 5))
  101. net = Net()
  102. with pytest.raises(ValueError) as ex:
  103. net()
  104. assert "Only one tuple is needed" in str(ex.value)