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_tril.py 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. """
  16. test nn.Tril()
  17. """
  18. import numpy as np
  19. import mindspore.nn as nn
  20. from mindspore import Tensor
  21. from mindspore import context
  22. context.set_context(mode=context.GRAPH_MODE)
  23. def test_tril():
  24. class Net(nn.Cell):
  25. def __init__(self):
  26. super(Net, self).__init__()
  27. self.value = Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  28. def construct(self):
  29. tril = nn.Tril()
  30. return tril(self.value, 0)
  31. net = Net()
  32. out = net()
  33. assert np.sum(out.asnumpy()) == 34
  34. def test_tril_1():
  35. class Net(nn.Cell):
  36. def __init__(self):
  37. super(Net, self).__init__()
  38. self.value = Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  39. def construct(self):
  40. tril = nn.Tril()
  41. return tril(self.value, 1)
  42. net = Net()
  43. out = net()
  44. assert np.sum(out.asnumpy()) == 42
  45. def test_tril_2():
  46. class Net(nn.Cell):
  47. def __init__(self):
  48. super(Net, self).__init__()
  49. self.value = Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
  50. def construct(self):
  51. tril = nn.Tril()
  52. return tril(self.value, -1)
  53. net = Net()
  54. out = net()
  55. assert np.sum(out.asnumpy()) == 19
  56. def test_tril_parameter():
  57. class Net(nn.Cell):
  58. def __init__(self):
  59. super(Net, self).__init__()
  60. def construct(self, x):
  61. tril = nn.Tril()
  62. return tril(x, 0)
  63. net = Net()
  64. net(Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
  65. def test_tril_parameter_1():
  66. class Net(nn.Cell):
  67. def __init__(self):
  68. super(Net, self).__init__()
  69. def construct(self, x):
  70. tril = nn.Tril()
  71. return tril(x, 1)
  72. net = Net()
  73. net(Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))
  74. def test_tril_parameter_2():
  75. class Net(nn.Cell):
  76. def __init__(self):
  77. super(Net, self).__init__()
  78. def construct(self, x):
  79. tril = nn.Tril()
  80. return tril(x, -1)
  81. net = Net()
  82. net(Tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]))