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_softmax.cpp 2.9 kB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "testutil.h"
  15. static int test_softmax(const ncnn::Mat& a, int axis)
  16. {
  17. ncnn::ParamDict pd;
  18. pd.set(0, axis); // axis
  19. pd.set(1, 1); // fixbug0
  20. std::vector<ncnn::Mat> weights(0);
  21. int ret = test_layer("Softmax", pd, weights, a);
  22. if (ret != 0)
  23. {
  24. fprintf(stderr, "test_softmax failed a.dims=%d a=(%d %d %d %d) axis=%d\n", a.dims, a.w, a.h, a.d, a.c, axis);
  25. }
  26. return ret;
  27. }
  28. static int test_softmax_nd(const ncnn::Mat& m)
  29. {
  30. const int dims = m.dims;
  31. for (int i = -dims; i < dims; i++)
  32. {
  33. int ret = test_softmax(m, i);
  34. if (ret != 0)
  35. return ret;
  36. }
  37. return 0;
  38. }
  39. static int test_softmax_0()
  40. {
  41. ncnn::Mat a = RandomMat(23, 25, 27, 32);
  42. ncnn::Mat b = RandomMat(21, 22, 19, 40);
  43. ncnn::Mat c = RandomMat(24, 27, 29, 28);
  44. ncnn::Mat d = RandomMat(25, 23, 25, 31);
  45. return 0
  46. || test_softmax_nd(a)
  47. || test_softmax_nd(b)
  48. || test_softmax_nd(c)
  49. || test_softmax_nd(d);
  50. }
  51. static int test_softmax_1()
  52. {
  53. ncnn::Mat a = RandomMat(25, 27, 32);
  54. ncnn::Mat b = RandomMat(22, 19, 40);
  55. ncnn::Mat c = RandomMat(27, 29, 28);
  56. ncnn::Mat d = RandomMat(23, 25, 31);
  57. return 0
  58. || test_softmax_nd(a)
  59. || test_softmax_nd(b)
  60. || test_softmax_nd(c)
  61. || test_softmax_nd(d);
  62. }
  63. static int test_softmax_2()
  64. {
  65. ncnn::Mat a = RandomMat(125, 32);
  66. ncnn::Mat b = RandomMat(147, 40);
  67. ncnn::Mat c = RandomMat(127, 28);
  68. ncnn::Mat d = RandomMat(129, 31);
  69. return 0
  70. || test_softmax_nd(a)
  71. || test_softmax_nd(b)
  72. || test_softmax_nd(c)
  73. || test_softmax_nd(d);
  74. }
  75. static int test_softmax_3()
  76. {
  77. ncnn::Mat a = RandomMat(128);
  78. ncnn::Mat b = RandomMat(120);
  79. ncnn::Mat c = RandomMat(124);
  80. ncnn::Mat d = RandomMat(127);
  81. return 0
  82. || test_softmax_nd(a)
  83. || test_softmax_nd(b)
  84. || test_softmax_nd(c)
  85. || test_softmax_nd(d);
  86. }
  87. int main()
  88. {
  89. SRAND(7767517);
  90. return 0
  91. || test_softmax_0()
  92. || test_softmax_1()
  93. || test_softmax_2()
  94. || test_softmax_3();
  95. }