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.1 kB

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