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.

softmax.cpp 11 kB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2017 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 "softmax.h"
  15. #include <float.h>
  16. #include <math.h>
  17. #include <algorithm>
  18. namespace ncnn {
  19. DEFINE_LAYER_CREATOR(Softmax)
  20. Softmax::Softmax()
  21. {
  22. one_blob_only = true;
  23. support_inplace = true;
  24. }
  25. int Softmax::load_param(const ParamDict& pd)
  26. {
  27. axis = pd.get(0, 0);
  28. return 0;
  29. }
  30. int Softmax::forward_inplace(Mat& bottom_top_blob, const Option& opt) const
  31. {
  32. // value = exp( value - global max value )
  33. // sum all value
  34. // value = value / sum
  35. int dims = bottom_top_blob.dims;
  36. size_t elemsize = bottom_top_blob.elemsize;
  37. if (dims == 1) // axis == 0
  38. {
  39. int w = bottom_top_blob.w;
  40. float* ptr = bottom_top_blob;
  41. float max = -FLT_MAX;
  42. for (int i=0; i<w; i++)
  43. {
  44. max = std::max(max, ptr[i]);
  45. }
  46. for (int i=0; i<w; i++)
  47. {
  48. ptr[i] = exp(ptr[i] - max);
  49. }
  50. float sum = 0.f;
  51. for (int i=0; i<w; i++)
  52. {
  53. sum += ptr[i];
  54. }
  55. for (int i=0; i<w; i++)
  56. {
  57. ptr[i] /= sum;
  58. }
  59. return 0;
  60. }
  61. if (dims == 2 && axis == 0)
  62. {
  63. int w = bottom_top_blob.w;
  64. int h = bottom_top_blob.h;
  65. Mat max;
  66. max.create(w, elemsize, opt.workspace_allocator);
  67. if (max.empty())
  68. return -100;
  69. max.fill(-FLT_MAX);
  70. for (int i=0; i<h; i++)
  71. {
  72. const float* ptr = bottom_top_blob.row(i);
  73. for (int j=0; j<w; j++)
  74. {
  75. max[j] = std::max(max[j], ptr[j]);
  76. }
  77. }
  78. for (int i=0; i<h; i++)
  79. {
  80. float* ptr = bottom_top_blob.row(i);
  81. for (int j=0; j<w; j++)
  82. {
  83. ptr[j] = exp(ptr[j] - max[j]);
  84. }
  85. }
  86. Mat sum;
  87. sum.create(w, elemsize, opt.workspace_allocator);
  88. if (sum.empty())
  89. return -100;
  90. sum.fill(0.f);
  91. for (int i=0; i<h; i++)
  92. {
  93. const float* ptr = bottom_top_blob.row(i);
  94. for (int j=0; j<w; j++)
  95. {
  96. sum[j] += ptr[j];
  97. }
  98. }
  99. for (int i=0; i<h; i++)
  100. {
  101. float* ptr = bottom_top_blob.row(i);
  102. for (int j=0; j<w; j++)
  103. {
  104. ptr[j] /= sum[j];
  105. }
  106. }
  107. return 0;
  108. }
  109. if (dims == 2 && axis == 1)
  110. {
  111. int w = bottom_top_blob.w;
  112. int h = bottom_top_blob.h;
  113. Mat max;
  114. max.create(h, elemsize, opt.workspace_allocator);
  115. if (max.empty())
  116. return -100;
  117. for (int i=0; i<h; i++)
  118. {
  119. const float* ptr = bottom_top_blob.row(i);
  120. float m = -FLT_MAX;
  121. for (int j=0; j<w; j++)
  122. {
  123. m = std::max(m, ptr[j]);
  124. }
  125. max[i] = m;
  126. }
  127. for (int i=0; i<h; i++)
  128. {
  129. float* ptr = bottom_top_blob.row(i);
  130. float m = max[i];
  131. for (int j=0; j<w; j++)
  132. {
  133. ptr[j] = exp(ptr[j] - m);
  134. }
  135. }
  136. Mat sum;
  137. sum.create(h, elemsize, opt.workspace_allocator);
  138. if (sum.empty())
  139. return -100;
  140. for (int i=0; i<h; i++)
  141. {
  142. const float* ptr = bottom_top_blob.row(i);
  143. float s = 0.f;
  144. for (int j=0; j<w; j++)
  145. {
  146. s += ptr[j];
  147. }
  148. sum[i] = s;
  149. }
  150. for (int i=0; i<h; i++)
  151. {
  152. float* ptr = bottom_top_blob.row(i);
  153. float s = sum[i];
  154. for (int j=0; j<w; j++)
  155. {
  156. ptr[j] /= s;
  157. }
  158. }
  159. return 0;
  160. }
  161. if (dims == 3 && axis == 0)
  162. {
  163. int w = bottom_top_blob.w;
  164. int h = bottom_top_blob.h;
  165. int channels = bottom_top_blob.c;
  166. int size = w * h;
  167. Mat max;
  168. max.create(w, h, elemsize, opt.workspace_allocator);
  169. if (max.empty())
  170. return -100;
  171. max.fill(-FLT_MAX);
  172. for (int q=0; q<channels; q++)
  173. {
  174. const float* ptr = bottom_top_blob.channel(q);
  175. for (int i=0; i<size; i++)
  176. {
  177. max[i] = std::max(max[i], ptr[i]);
  178. }
  179. }
  180. #pragma omp parallel for num_threads(opt.num_threads)
  181. for (int q=0; q<channels; q++)
  182. {
  183. float* ptr = bottom_top_blob.channel(q);
  184. for (int i=0; i<size; i++)
  185. {
  186. ptr[i] = exp(ptr[i] - max[i]);
  187. }
  188. }
  189. Mat sum;
  190. sum.create(w, h, elemsize, opt.workspace_allocator);
  191. if (sum.empty())
  192. return -100;
  193. sum.fill(0.f);
  194. for (int q=0; q<channels; q++)
  195. {
  196. const float* ptr = bottom_top_blob.channel(q);
  197. for (int i=0; i<size; i++)
  198. {
  199. sum[i] += ptr[i];
  200. }
  201. }
  202. #pragma omp parallel for num_threads(opt.num_threads)
  203. for (int q=0; q<channels; q++)
  204. {
  205. float* ptr = bottom_top_blob.channel(q);
  206. for (int i=0; i<size; i++)
  207. {
  208. ptr[i] /= sum[i];
  209. }
  210. }
  211. return 0;
  212. }
  213. if (dims == 3 && axis == 1)
  214. {
  215. int w = bottom_top_blob.w;
  216. int h = bottom_top_blob.h;
  217. int channels = bottom_top_blob.c;
  218. Mat max;
  219. max.create(h, channels, elemsize, opt.workspace_allocator);
  220. if (max.empty())
  221. return -100;
  222. max.fill(-FLT_MAX);
  223. #pragma omp parallel for num_threads(opt.num_threads)
  224. for (int q=0; q<channels; q++)
  225. {
  226. const float* ptr = bottom_top_blob.channel(q);
  227. float* maxptr = max.row(q);
  228. for (int i=0; i<h; i++)
  229. {
  230. float max = -FLT_MAX;
  231. for (int j=0; j<w; j++)
  232. {
  233. max = std::max(max, ptr[j]);
  234. }
  235. maxptr[i] = max;
  236. ptr += w;
  237. }
  238. }
  239. #pragma omp parallel for num_threads(opt.num_threads)
  240. for (int q=0; q<channels; q++)
  241. {
  242. float* ptr = bottom_top_blob.channel(q);
  243. float* maxptr = max.row(q);
  244. for (int i=0; i<h; i++)
  245. {
  246. float max = maxptr[i];
  247. for (int j=0; j<w; j++)
  248. {
  249. ptr[j] = exp(ptr[j] - max);
  250. }
  251. ptr += w;
  252. }
  253. }
  254. Mat sum;
  255. sum.create(h, channels, elemsize, opt.workspace_allocator);
  256. if (sum.empty())
  257. return -100;
  258. sum.fill(0.f);
  259. #pragma omp parallel for num_threads(opt.num_threads)
  260. for (int q=0; q<channels; q++)
  261. {
  262. const float* ptr = bottom_top_blob.channel(q);
  263. float* sumptr = sum.row(q);
  264. for (int i=0; i<h; i++)
  265. {
  266. float sum = 0.f;
  267. for (int j=0; j<w; j++)
  268. {
  269. sum += ptr[j];
  270. }
  271. sumptr[i] = sum;
  272. ptr += w;
  273. }
  274. }
  275. #pragma omp parallel for num_threads(opt.num_threads)
  276. for (int q=0; q<channels; q++)
  277. {
  278. float* ptr = bottom_top_blob.channel(q);
  279. float* sumptr = sum.row(q);
  280. for (int i=0; i<h; i++)
  281. {
  282. float sum = sumptr[i];
  283. for (int j=0; j<w; j++)
  284. {
  285. ptr[j] /= sum;
  286. }
  287. ptr += w;
  288. }
  289. }
  290. return 0;
  291. }
  292. if (dims == 3 && axis == 2)
  293. {
  294. int w = bottom_top_blob.w;
  295. int h = bottom_top_blob.h;
  296. int channels = bottom_top_blob.c;
  297. Mat max;
  298. max.create(w, channels, elemsize, opt.workspace_allocator);
  299. if (max.empty())
  300. return -100;
  301. max.fill(-FLT_MAX);
  302. #pragma omp parallel for num_threads(opt.num_threads)
  303. for (int q=0; q<channels; q++)
  304. {
  305. const float* ptr = bottom_top_blob.channel(q);
  306. float* maxptr = max.row(q);
  307. for (int i=0; i<h; i++)
  308. {
  309. for (int j=0; j<w; j++)
  310. {
  311. maxptr[j] = std::max(maxptr[j], ptr[j]);
  312. }
  313. ptr += w;
  314. }
  315. }
  316. #pragma omp parallel for num_threads(opt.num_threads)
  317. for (int q=0; q<channels; q++)
  318. {
  319. float* ptr = bottom_top_blob.channel(q);
  320. float* maxptr = max.row(q);
  321. for (int i=0; i<h; i++)
  322. {
  323. for (int j=0; j<w; j++)
  324. {
  325. ptr[j] = exp(ptr[j] - maxptr[j]);
  326. }
  327. ptr += w;
  328. }
  329. }
  330. Mat sum;
  331. sum.create(w, channels, elemsize, opt.workspace_allocator);
  332. if (sum.empty())
  333. return -100;
  334. sum.fill(0.f);
  335. #pragma omp parallel for num_threads(opt.num_threads)
  336. for (int q=0; q<channels; q++)
  337. {
  338. const float* ptr = bottom_top_blob.channel(q);
  339. float* sumptr = sum.row(q);
  340. for (int i=0; i<h; i++)
  341. {
  342. for (int j=0; j<w; j++)
  343. {
  344. sumptr[j] += ptr[j];
  345. }
  346. ptr += w;
  347. }
  348. }
  349. #pragma omp parallel for num_threads(opt.num_threads)
  350. for (int q=0; q<channels; q++)
  351. {
  352. float* ptr = bottom_top_blob.channel(q);
  353. float* sumptr = sum.row(q);
  354. for (int i=0; i<h; i++)
  355. {
  356. for (int j=0; j<w; j++)
  357. {
  358. ptr[j] /= sumptr[j];
  359. }
  360. ptr += w;
  361. }
  362. }
  363. return 0;
  364. }
  365. return 0;
  366. }
  367. } // namespace ncnn