mindspore 2.4.0__cp311-cp311-macosx_11_0_arm64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of mindspore might be problematic. Click here for more details.

Files changed (1387) hide show
  1. mindspore/.commit_id +1 -0
  2. mindspore/__init__.py +53 -0
  3. mindspore/_c_dataengine.cpython-311-darwin.so +0 -0
  4. mindspore/_c_expression.cpython-311-darwin.so +0 -0
  5. mindspore/_c_mindrecord.cpython-311-darwin.so +0 -0
  6. mindspore/_check_jit_forbidden_api.py +106 -0
  7. mindspore/_checkparam.py +1419 -0
  8. mindspore/_extends/__init__.py +23 -0
  9. mindspore/_extends/builtin_operations.py +224 -0
  10. mindspore/_extends/graph_kernel/__init__.py +17 -0
  11. mindspore/_extends/graph_kernel/model/__init__.py +19 -0
  12. mindspore/_extends/graph_kernel/model/graph_parallel.py +311 -0
  13. mindspore/_extends/graph_kernel/model/graph_split.py +1348 -0
  14. mindspore/_extends/graph_kernel/model/model.py +553 -0
  15. mindspore/_extends/graph_kernel/model/model_builder.py +216 -0
  16. mindspore/_extends/graph_kernel/parallel_estimate.py +60 -0
  17. mindspore/_extends/graph_kernel/splitter.py +140 -0
  18. mindspore/_extends/graph_kernel/utils.py +28 -0
  19. mindspore/_extends/parallel_compile/__init__.py +19 -0
  20. mindspore/_extends/parallel_compile/akg_compiler/__init__.py +19 -0
  21. mindspore/_extends/parallel_compile/akg_compiler/akg_process.py +269 -0
  22. mindspore/_extends/parallel_compile/akg_compiler/build_tbe_kernel.py +529 -0
  23. mindspore/_extends/parallel_compile/akg_compiler/compiler.py +56 -0
  24. mindspore/_extends/parallel_compile/akg_compiler/gen_custom_op_files.py +96 -0
  25. mindspore/_extends/parallel_compile/akg_compiler/get_file_path.py +36 -0
  26. mindspore/_extends/parallel_compile/akg_compiler/tbe_topi.py +556 -0
  27. mindspore/_extends/parallel_compile/akg_compiler/util.py +159 -0
  28. mindspore/_extends/parse/__init__.py +49 -0
  29. mindspore/_extends/parse/compile_config.py +299 -0
  30. mindspore/_extends/parse/namespace.py +136 -0
  31. mindspore/_extends/parse/parser.py +1448 -0
  32. mindspore/_extends/parse/resources.py +213 -0
  33. mindspore/_extends/parse/standard_method.py +4475 -0
  34. mindspore/_extends/parse/trope.py +97 -0
  35. mindspore/_extends/pijit/__init__.py +23 -0
  36. mindspore/_extends/pijit/pijit_func_white_list.py +669 -0
  37. mindspore/_extends/remote/__init__.py +19 -0
  38. mindspore/_extends/remote/kernel_build_server.py +199 -0
  39. mindspore/_extends/remote/kernel_build_server_akg.py +55 -0
  40. mindspore/_extends/remote/kernel_build_server_akg_v2.py +55 -0
  41. mindspore/_extends/remote/kernel_build_server_ascend.py +75 -0
  42. mindspore/_extends/utils.py +68 -0
  43. mindspore/_install_custom.py +43 -0
  44. mindspore/_profiler.py +30 -0
  45. mindspore/amp.py +433 -0
  46. mindspore/boost/__init__.py +42 -0
  47. mindspore/boost/adasum.py +319 -0
  48. mindspore/boost/base.py +535 -0
  49. mindspore/boost/boost.py +400 -0
  50. mindspore/boost/boost_cell_wrapper.py +790 -0
  51. mindspore/boost/dim_reduce.py +323 -0
  52. mindspore/boost/grad_accumulation.py +79 -0
  53. mindspore/boost/grad_freeze.py +382 -0
  54. mindspore/boost/group_loss_scale_manager.py +166 -0
  55. mindspore/boost/less_batch_normalization.py +174 -0
  56. mindspore/common/__init__.py +86 -0
  57. mindspore/common/_auto_dynamic.py +68 -0
  58. mindspore/common/_decorator.py +50 -0
  59. mindspore/common/_jit_fallback_utils.py +110 -0
  60. mindspore/common/_monad.py +25 -0
  61. mindspore/common/_pijit_context.py +190 -0
  62. mindspore/common/_register_for_adapter.py +74 -0
  63. mindspore/common/_register_for_recompute.py +48 -0
  64. mindspore/common/_register_for_tensor.py +46 -0
  65. mindspore/common/_stub_tensor.py +210 -0
  66. mindspore/common/_tensor_overload.py +139 -0
  67. mindspore/common/_utils.py +122 -0
  68. mindspore/common/api.py +2064 -0
  69. mindspore/common/auto_dynamic_shape.py +507 -0
  70. mindspore/common/dtype.py +422 -0
  71. mindspore/common/dump.py +130 -0
  72. mindspore/common/file_system.py +48 -0
  73. mindspore/common/generator.py +254 -0
  74. mindspore/common/hook_handle.py +143 -0
  75. mindspore/common/initializer.py +880 -0
  76. mindspore/common/jit_config.py +98 -0
  77. mindspore/common/lazy_inline.py +240 -0
  78. mindspore/common/mindir_util.py +111 -0
  79. mindspore/common/mutable.py +234 -0
  80. mindspore/common/no_inline.py +54 -0
  81. mindspore/common/np_dtype.py +25 -0
  82. mindspore/common/parameter.py +1081 -0
  83. mindspore/common/recompute.py +292 -0
  84. mindspore/common/seed.py +260 -0
  85. mindspore/common/sparse_tensor.py +1175 -0
  86. mindspore/common/symbol.py +122 -0
  87. mindspore/common/tensor.py +5039 -0
  88. mindspore/communication/__init__.py +37 -0
  89. mindspore/communication/_comm_helper.py +501 -0
  90. mindspore/communication/_hccl_management.py +297 -0
  91. mindspore/communication/comm_func.py +1395 -0
  92. mindspore/communication/management.py +673 -0
  93. mindspore/config/op_info.config +533 -0
  94. mindspore/context.py +2077 -0
  95. mindspore/dataset/__init__.py +90 -0
  96. mindspore/dataset/audio/__init__.py +61 -0
  97. mindspore/dataset/audio/transforms.py +3690 -0
  98. mindspore/dataset/audio/utils.py +386 -0
  99. mindspore/dataset/audio/validators.py +1172 -0
  100. mindspore/dataset/callback/__init__.py +20 -0
  101. mindspore/dataset/callback/ds_callback.py +368 -0
  102. mindspore/dataset/callback/validators.py +32 -0
  103. mindspore/dataset/core/__init__.py +13 -0
  104. mindspore/dataset/core/config.py +1095 -0
  105. mindspore/dataset/core/datatypes.py +101 -0
  106. mindspore/dataset/core/py_util_helpers.py +65 -0
  107. mindspore/dataset/core/validator_helpers.py +781 -0
  108. mindspore/dataset/debug/__init__.py +21 -0
  109. mindspore/dataset/debug/debug_hook.py +97 -0
  110. mindspore/dataset/debug/pre_defined_hook.py +67 -0
  111. mindspore/dataset/engine/__init__.py +124 -0
  112. mindspore/dataset/engine/cache_admin.py +47 -0
  113. mindspore/dataset/engine/cache_client.py +129 -0
  114. mindspore/dataset/engine/datasets.py +4582 -0
  115. mindspore/dataset/engine/datasets_audio.py +911 -0
  116. mindspore/dataset/engine/datasets_standard_format.py +543 -0
  117. mindspore/dataset/engine/datasets_text.py +2161 -0
  118. mindspore/dataset/engine/datasets_user_defined.py +1184 -0
  119. mindspore/dataset/engine/datasets_vision.py +4816 -0
  120. mindspore/dataset/engine/iterators.py +371 -0
  121. mindspore/dataset/engine/obs/__init__.py +23 -0
  122. mindspore/dataset/engine/obs/config_loader.py +68 -0
  123. mindspore/dataset/engine/obs/obs_mindrecord_dataset.py +508 -0
  124. mindspore/dataset/engine/obs/util.py +482 -0
  125. mindspore/dataset/engine/offload.py +596 -0
  126. mindspore/dataset/engine/queue.py +304 -0
  127. mindspore/dataset/engine/samplers.py +895 -0
  128. mindspore/dataset/engine/serializer_deserializer.py +159 -0
  129. mindspore/dataset/engine/validators.py +2895 -0
  130. mindspore/dataset/text/__init__.py +51 -0
  131. mindspore/dataset/text/transforms.py +1703 -0
  132. mindspore/dataset/text/utils.py +715 -0
  133. mindspore/dataset/text/validators.py +642 -0
  134. mindspore/dataset/transforms/__init__.py +45 -0
  135. mindspore/dataset/transforms/c_transforms.py +638 -0
  136. mindspore/dataset/transforms/py_transforms.py +393 -0
  137. mindspore/dataset/transforms/py_transforms_util.py +255 -0
  138. mindspore/dataset/transforms/transforms.py +1260 -0
  139. mindspore/dataset/transforms/validators.py +410 -0
  140. mindspore/dataset/utils/__init__.py +19 -0
  141. mindspore/dataset/utils/browse_dataset.py +190 -0
  142. mindspore/dataset/utils/line_reader.py +126 -0
  143. mindspore/dataset/vision/__init__.py +65 -0
  144. mindspore/dataset/vision/c_transforms.py +2641 -0
  145. mindspore/dataset/vision/py_transforms.py +2120 -0
  146. mindspore/dataset/vision/py_transforms_util.py +1660 -0
  147. mindspore/dataset/vision/transforms.py +7295 -0
  148. mindspore/dataset/vision/utils.py +863 -0
  149. mindspore/dataset/vision/validators.py +1483 -0
  150. mindspore/default_config.py +2 -0
  151. mindspore/experimental/__init__.py +20 -0
  152. mindspore/experimental/es/__init__.py +22 -0
  153. mindspore/experimental/es/embedding_service.py +883 -0
  154. mindspore/experimental/es/embedding_service_layer.py +581 -0
  155. mindspore/experimental/llm_boost/__init__.py +21 -0
  156. mindspore/experimental/llm_boost/atb/__init__.py +23 -0
  157. mindspore/experimental/llm_boost/atb/boost_base.py +211 -0
  158. mindspore/experimental/llm_boost/atb/llama_boost.py +115 -0
  159. mindspore/experimental/llm_boost/atb/qwen_boost.py +101 -0
  160. mindspore/experimental/llm_boost/register.py +129 -0
  161. mindspore/experimental/llm_boost/utils.py +31 -0
  162. mindspore/experimental/map_parameter.py +309 -0
  163. mindspore/experimental/optim/__init__.py +40 -0
  164. mindspore/experimental/optim/adadelta.py +161 -0
  165. mindspore/experimental/optim/adagrad.py +168 -0
  166. mindspore/experimental/optim/adam.py +193 -0
  167. mindspore/experimental/optim/adamax.py +170 -0
  168. mindspore/experimental/optim/adamw.py +290 -0
  169. mindspore/experimental/optim/asgd.py +153 -0
  170. mindspore/experimental/optim/lr_scheduler.py +1371 -0
  171. mindspore/experimental/optim/nadam.py +157 -0
  172. mindspore/experimental/optim/optimizer.py +262 -0
  173. mindspore/experimental/optim/radam.py +194 -0
  174. mindspore/experimental/optim/rmsprop.py +154 -0
  175. mindspore/experimental/optim/rprop.py +164 -0
  176. mindspore/experimental/optim/sgd.py +156 -0
  177. mindspore/hal/__init__.py +40 -0
  178. mindspore/hal/_ascend.py +57 -0
  179. mindspore/hal/_base.py +57 -0
  180. mindspore/hal/_cpu.py +56 -0
  181. mindspore/hal/_gpu.py +57 -0
  182. mindspore/hal/contiguous_tensors_handle.py +175 -0
  183. mindspore/hal/device.py +356 -0
  184. mindspore/hal/event.py +179 -0
  185. mindspore/hal/memory.py +326 -0
  186. mindspore/hal/stream.py +357 -0
  187. mindspore/include/OWNERS +7 -0
  188. mindspore/include/api/allocator.h +97 -0
  189. mindspore/include/api/callback/callback.h +93 -0
  190. mindspore/include/api/callback/ckpt_saver.h +41 -0
  191. mindspore/include/api/callback/loss_monitor.h +33 -0
  192. mindspore/include/api/callback/lr_scheduler.h +51 -0
  193. mindspore/include/api/callback/time_monitor.h +34 -0
  194. mindspore/include/api/callback/train_accuracy.h +37 -0
  195. mindspore/include/api/cell.h +90 -0
  196. mindspore/include/api/cfg.h +82 -0
  197. mindspore/include/api/context.h +602 -0
  198. mindspore/include/api/data_type.h +47 -0
  199. mindspore/include/api/delegate.h +178 -0
  200. mindspore/include/api/delegate_api.h +75 -0
  201. mindspore/include/api/dual_abi_helper.h +208 -0
  202. mindspore/include/api/format.h +28 -0
  203. mindspore/include/api/graph.h +46 -0
  204. mindspore/include/api/kernel.h +58 -0
  205. mindspore/include/api/kernel_api.h +168 -0
  206. mindspore/include/api/metrics/accuracy.h +36 -0
  207. mindspore/include/api/metrics/metrics.h +41 -0
  208. mindspore/include/api/model.h +438 -0
  209. mindspore/include/api/model_group.h +91 -0
  210. mindspore/include/api/model_parallel_runner.h +168 -0
  211. mindspore/include/api/serialization.h +185 -0
  212. mindspore/include/api/status.h +192 -0
  213. mindspore/include/api/types.h +431 -0
  214. mindspore/include/api/visible.h +41 -0
  215. mindspore/include/c_api/context_c.h +179 -0
  216. mindspore/include/c_api/data_type_c.h +52 -0
  217. mindspore/include/c_api/format_c.h +46 -0
  218. mindspore/include/c_api/model_c.h +347 -0
  219. mindspore/include/c_api/status_c.h +79 -0
  220. mindspore/include/c_api/tensor_c.h +146 -0
  221. mindspore/include/c_api/types_c.h +67 -0
  222. mindspore/include/dataset/config.h +163 -0
  223. mindspore/include/dataset/constants.h +363 -0
  224. mindspore/include/dataset/execute.h +196 -0
  225. mindspore/include/dataset/text.h +1092 -0
  226. mindspore/include/dataset/transforms.h +638 -0
  227. mindspore/include/dataset/vision.h +2129 -0
  228. mindspore/include/dataset/vision_ascend.h +206 -0
  229. mindspore/include/dataset/vision_lite.h +625 -0
  230. mindspore/lib/libavcodec.59.dylib +0 -0
  231. mindspore/lib/libavdevice.59.dylib +0 -0
  232. mindspore/lib/libavfilter.8.dylib +0 -0
  233. mindspore/lib/libavformat.59.dylib +0 -0
  234. mindspore/lib/libavutil.57.dylib +0 -0
  235. mindspore/lib/libdnnl.2.dylib +0 -0
  236. mindspore/lib/libicudata.69.dylib +0 -0
  237. mindspore/lib/libicui18n.69.dylib +0 -0
  238. mindspore/lib/libicuuc.69.dylib +0 -0
  239. mindspore/lib/libmindspore_address_sorting.15.dylib +0 -0
  240. mindspore/lib/libmindspore_backend.dylib +0 -0
  241. mindspore/lib/libmindspore_common.dylib +0 -0
  242. mindspore/lib/libmindspore_core.dylib +0 -0
  243. mindspore/lib/libmindspore_glog.0.dylib +0 -0
  244. mindspore/lib/libmindspore_gpr.15.dylib +0 -0
  245. mindspore/lib/libmindspore_grpc++.1.dylib +0 -0
  246. mindspore/lib/libmindspore_grpc.15.dylib +0 -0
  247. mindspore/lib/libmindspore_np_dtype.dylib +0 -0
  248. mindspore/lib/libmindspore_ops.dylib +0 -0
  249. mindspore/lib/libmindspore_upb.15.dylib +0 -0
  250. mindspore/lib/libnnacl.dylib +0 -0
  251. mindspore/lib/libopencv_core.4.5.dylib +0 -0
  252. mindspore/lib/libopencv_imgcodecs.4.5.dylib +0 -0
  253. mindspore/lib/libopencv_imgproc.4.5.dylib +0 -0
  254. mindspore/lib/libps_cache.dylib +0 -0
  255. mindspore/lib/libswresample.4.dylib +0 -0
  256. mindspore/lib/libswscale.6.dylib +0 -0
  257. mindspore/lib/libtinyxml2.8.dylib +0 -0
  258. mindspore/log.py +633 -0
  259. mindspore/mindrecord/__init__.py +43 -0
  260. mindspore/mindrecord/common/__init__.py +17 -0
  261. mindspore/mindrecord/common/constant.py +20 -0
  262. mindspore/mindrecord/common/enums.py +44 -0
  263. mindspore/mindrecord/common/exceptions.py +311 -0
  264. mindspore/mindrecord/config.py +809 -0
  265. mindspore/mindrecord/filereader.py +174 -0
  266. mindspore/mindrecord/filewriter.py +722 -0
  267. mindspore/mindrecord/mindpage.py +210 -0
  268. mindspore/mindrecord/shardheader.py +141 -0
  269. mindspore/mindrecord/shardindexgenerator.py +74 -0
  270. mindspore/mindrecord/shardreader.py +117 -0
  271. mindspore/mindrecord/shardsegment.py +128 -0
  272. mindspore/mindrecord/shardutils.py +185 -0
  273. mindspore/mindrecord/shardwriter.py +237 -0
  274. mindspore/mindrecord/tools/__init__.py +17 -0
  275. mindspore/mindrecord/tools/cifar10.py +140 -0
  276. mindspore/mindrecord/tools/cifar100.py +153 -0
  277. mindspore/mindrecord/tools/cifar100_to_mr.py +185 -0
  278. mindspore/mindrecord/tools/cifar10_to_mr.py +177 -0
  279. mindspore/mindrecord/tools/csv_to_mr.py +200 -0
  280. mindspore/mindrecord/tools/imagenet_to_mr.py +206 -0
  281. mindspore/mindrecord/tools/mnist_to_mr.py +259 -0
  282. mindspore/mindrecord/tools/tfrecord_to_mr.py +360 -0
  283. mindspore/mint/__init__.py +1586 -0
  284. mindspore/mint/distributed/__init__.py +31 -0
  285. mindspore/mint/distributed/distributed.py +254 -0
  286. mindspore/mint/linalg/__init__.py +22 -0
  287. mindspore/mint/nn/__init__.py +757 -0
  288. mindspore/mint/nn/functional.py +679 -0
  289. mindspore/mint/nn/layer/__init__.py +39 -0
  290. mindspore/mint/nn/layer/activation.py +133 -0
  291. mindspore/mint/nn/layer/normalization.py +477 -0
  292. mindspore/mint/nn/layer/pooling.py +110 -0
  293. mindspore/mint/optim/__init__.py +24 -0
  294. mindspore/mint/optim/adamw.py +206 -0
  295. mindspore/mint/special/__init__.py +63 -0
  296. mindspore/multiprocessing/__init__.py +73 -0
  297. mindspore/nn/__init__.py +47 -0
  298. mindspore/nn/cell.py +2787 -0
  299. mindspore/nn/dynamic_lr.py +482 -0
  300. mindspore/nn/grad/__init__.py +21 -0
  301. mindspore/nn/grad/cell_grad.py +196 -0
  302. mindspore/nn/layer/__init__.py +63 -0
  303. mindspore/nn/layer/activation.py +1822 -0
  304. mindspore/nn/layer/basic.py +1629 -0
  305. mindspore/nn/layer/channel_shuffle.py +90 -0
  306. mindspore/nn/layer/combined.py +248 -0
  307. mindspore/nn/layer/container.py +734 -0
  308. mindspore/nn/layer/conv.py +1505 -0
  309. mindspore/nn/layer/dense.py +204 -0
  310. mindspore/nn/layer/embedding.py +869 -0
  311. mindspore/nn/layer/image.py +661 -0
  312. mindspore/nn/layer/math.py +1069 -0
  313. mindspore/nn/layer/normalization.py +1273 -0
  314. mindspore/nn/layer/padding.py +880 -0
  315. mindspore/nn/layer/pooling.py +2302 -0
  316. mindspore/nn/layer/rnn_cells.py +388 -0
  317. mindspore/nn/layer/rnns.py +849 -0
  318. mindspore/nn/layer/thor_layer.py +963 -0
  319. mindspore/nn/layer/timedistributed.py +155 -0
  320. mindspore/nn/layer/transformer.py +823 -0
  321. mindspore/nn/learning_rate_schedule.py +512 -0
  322. mindspore/nn/loss/__init__.py +36 -0
  323. mindspore/nn/loss/loss.py +2924 -0
  324. mindspore/nn/metrics.py +53 -0
  325. mindspore/nn/optim/__init__.py +45 -0
  326. mindspore/nn/optim/_dist_optimizer_registry.py +111 -0
  327. mindspore/nn/optim/ada_grad.py +217 -0
  328. mindspore/nn/optim/adadelta.py +206 -0
  329. mindspore/nn/optim/adafactor.py +448 -0
  330. mindspore/nn/optim/adam.py +1297 -0
  331. mindspore/nn/optim/adamax.py +220 -0
  332. mindspore/nn/optim/adasum.py +548 -0
  333. mindspore/nn/optim/asgd.py +216 -0
  334. mindspore/nn/optim/ftrl.py +401 -0
  335. mindspore/nn/optim/lamb.py +296 -0
  336. mindspore/nn/optim/lars.py +202 -0
  337. mindspore/nn/optim/lazyadam.py +533 -0
  338. mindspore/nn/optim/momentum.py +239 -0
  339. mindspore/nn/optim/optimizer.py +1034 -0
  340. mindspore/nn/optim/proximal_ada_grad.py +242 -0
  341. mindspore/nn/optim/rmsprop.py +264 -0
  342. mindspore/nn/optim/rprop.py +251 -0
  343. mindspore/nn/optim/sgd.py +237 -0
  344. mindspore/nn/optim/tft_wrapper.py +127 -0
  345. mindspore/nn/optim/thor.py +1310 -0
  346. mindspore/nn/probability/__init__.py +22 -0
  347. mindspore/nn/probability/bijector/__init__.py +35 -0
  348. mindspore/nn/probability/bijector/bijector.py +337 -0
  349. mindspore/nn/probability/bijector/exp.py +65 -0
  350. mindspore/nn/probability/bijector/gumbel_cdf.py +144 -0
  351. mindspore/nn/probability/bijector/invert.py +126 -0
  352. mindspore/nn/probability/bijector/power_transform.py +196 -0
  353. mindspore/nn/probability/bijector/scalar_affine.py +167 -0
  354. mindspore/nn/probability/bijector/softplus.py +189 -0
  355. mindspore/nn/probability/bnn_layers/__init__.py +29 -0
  356. mindspore/nn/probability/bnn_layers/_util.py +46 -0
  357. mindspore/nn/probability/bnn_layers/bnn_cell_wrapper.py +112 -0
  358. mindspore/nn/probability/bnn_layers/conv_variational.py +267 -0
  359. mindspore/nn/probability/bnn_layers/dense_variational.py +302 -0
  360. mindspore/nn/probability/bnn_layers/layer_distribution.py +123 -0
  361. mindspore/nn/probability/distribution/__init__.py +56 -0
  362. mindspore/nn/probability/distribution/_utils/__init__.py +34 -0
  363. mindspore/nn/probability/distribution/_utils/custom_ops.py +96 -0
  364. mindspore/nn/probability/distribution/_utils/utils.py +362 -0
  365. mindspore/nn/probability/distribution/bernoulli.py +334 -0
  366. mindspore/nn/probability/distribution/beta.py +391 -0
  367. mindspore/nn/probability/distribution/categorical.py +435 -0
  368. mindspore/nn/probability/distribution/cauchy.py +383 -0
  369. mindspore/nn/probability/distribution/distribution.py +827 -0
  370. mindspore/nn/probability/distribution/exponential.py +350 -0
  371. mindspore/nn/probability/distribution/gamma.py +391 -0
  372. mindspore/nn/probability/distribution/geometric.py +335 -0
  373. mindspore/nn/probability/distribution/gumbel.py +257 -0
  374. mindspore/nn/probability/distribution/half_normal.py +133 -0
  375. mindspore/nn/probability/distribution/laplace.py +128 -0
  376. mindspore/nn/probability/distribution/log_normal.py +272 -0
  377. mindspore/nn/probability/distribution/logistic.py +379 -0
  378. mindspore/nn/probability/distribution/normal.py +336 -0
  379. mindspore/nn/probability/distribution/poisson.py +288 -0
  380. mindspore/nn/probability/distribution/student_t.py +149 -0
  381. mindspore/nn/probability/distribution/transformed_distribution.py +235 -0
  382. mindspore/nn/probability/distribution/uniform.py +375 -0
  383. mindspore/nn/reinforcement/__init__.py +24 -0
  384. mindspore/nn/reinforcement/_batch_read_write.py +142 -0
  385. mindspore/nn/reinforcement/_tensors_queue.py +152 -0
  386. mindspore/nn/reinforcement/tensor_array.py +145 -0
  387. mindspore/nn/sparse/__init__.py +23 -0
  388. mindspore/nn/sparse/sparse.py +147 -0
  389. mindspore/nn/wrap/__init__.py +49 -0
  390. mindspore/nn/wrap/cell_wrapper.py +968 -0
  391. mindspore/nn/wrap/grad_reducer.py +608 -0
  392. mindspore/nn/wrap/loss_scale.py +694 -0
  393. mindspore/numpy/__init__.py +121 -0
  394. mindspore/numpy/array_creations.py +2731 -0
  395. mindspore/numpy/array_ops.py +2629 -0
  396. mindspore/numpy/dtypes.py +185 -0
  397. mindspore/numpy/fft.py +966 -0
  398. mindspore/numpy/logic_ops.py +936 -0
  399. mindspore/numpy/math_ops.py +5911 -0
  400. mindspore/numpy/utils.py +214 -0
  401. mindspore/numpy/utils_const.py +565 -0
  402. mindspore/ops/__init__.py +56 -0
  403. mindspore/ops/_constants.py +30 -0
  404. mindspore/ops/_grad_experimental/__init__.py +31 -0
  405. mindspore/ops/_grad_experimental/grad_array_ops.py +830 -0
  406. mindspore/ops/_grad_experimental/grad_base.py +143 -0
  407. mindspore/ops/_grad_experimental/grad_comm_ops.py +714 -0
  408. mindspore/ops/_grad_experimental/grad_debug_ops.py +31 -0
  409. mindspore/ops/_grad_experimental/grad_implementations.py +203 -0
  410. mindspore/ops/_grad_experimental/grad_inner_ops.py +79 -0
  411. mindspore/ops/_grad_experimental/grad_math_ops.py +802 -0
  412. mindspore/ops/_grad_experimental/grad_nn_ops.py +231 -0
  413. mindspore/ops/_grad_experimental/grad_quant_ops.py +238 -0
  414. mindspore/ops/_grad_experimental/grad_sparse.py +342 -0
  415. mindspore/ops/_grad_experimental/grad_sparse_ops.py +399 -0
  416. mindspore/ops/_grad_experimental/taylor_rule.py +220 -0
  417. mindspore/ops/_op_impl/__init__.py +23 -0
  418. mindspore/ops/_op_impl/_custom_op/__init__.py +39 -0
  419. mindspore/ops/_op_impl/_custom_op/_basic.py +158 -0
  420. mindspore/ops/_op_impl/_custom_op/batch_matmul_impl.py +279 -0
  421. mindspore/ops/_op_impl/_custom_op/batchnorm_fold.py +156 -0
  422. mindspore/ops/_op_impl/_custom_op/batchnorm_fold2.py +109 -0
  423. mindspore/ops/_op_impl/_custom_op/batchnorm_fold2_grad.py +125 -0
  424. mindspore/ops/_op_impl/_custom_op/batchnorm_fold2_grad_reduce.py +105 -0
  425. mindspore/ops/_op_impl/_custom_op/batchnorm_fold_grad.py +124 -0
  426. mindspore/ops/_op_impl/_custom_op/cholesky_trsm_impl.py +116 -0
  427. mindspore/ops/_op_impl/_custom_op/correction_mul.py +89 -0
  428. mindspore/ops/_op_impl/_custom_op/correction_mul_grad.py +196 -0
  429. mindspore/ops/_op_impl/_custom_op/dsd_back_impl.py +366 -0
  430. mindspore/ops/_op_impl/_custom_op/dsd_impl.py +162 -0
  431. mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perchannel.py +136 -0
  432. mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perchannel_grad.py +206 -0
  433. mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perchannel_grad_reduce.py +88 -0
  434. mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perlayer.py +128 -0
  435. mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perlayer_grad.py +199 -0
  436. mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perlayer_grad_reduce.py +88 -0
  437. mindspore/ops/_op_impl/_custom_op/fake_quant_perchannel.py +156 -0
  438. mindspore/ops/_op_impl/_custom_op/fake_quant_perchannel_grad.py +184 -0
  439. mindspore/ops/_op_impl/_custom_op/fake_quant_perlayer.py +143 -0
  440. mindspore/ops/_op_impl/_custom_op/fake_quant_perlayer_grad.py +169 -0
  441. mindspore/ops/_op_impl/_custom_op/fused_abs_max1_impl.py +548 -0
  442. mindspore/ops/_op_impl/_custom_op/img2col_impl.py +881 -0
  443. mindspore/ops/_op_impl/_custom_op/matmul_cube_dense_left_impl.py +278 -0
  444. mindspore/ops/_op_impl/_custom_op/matmul_cube_dense_right_impl.py +200 -0
  445. mindspore/ops/_op_impl/_custom_op/matmul_cube_fracz_left_cast_impl.py +334 -0
  446. mindspore/ops/_op_impl/_custom_op/matmul_cube_fracz_right_mul_impl.py +255 -0
  447. mindspore/ops/_op_impl/_custom_op/matmul_cube_impl.py +222 -0
  448. mindspore/ops/_op_impl/_custom_op/matmul_dds_grad_impl.py +644 -0
  449. mindspore/ops/_op_impl/_custom_op/matmul_dds_impl.py +488 -0
  450. mindspore/ops/_op_impl/_custom_op/matrix_combine_impl.py +87 -0
  451. mindspore/ops/_op_impl/_custom_op/minmax_update_perchannel.py +129 -0
  452. mindspore/ops/_op_impl/_custom_op/minmax_update_perlayer.py +121 -0
  453. mindspore/ops/_op_impl/_custom_op/transpose02314_impl.py +352 -0
  454. mindspore/ops/_op_impl/aicpu/__init__.py +441 -0
  455. mindspore/ops/_op_impl/aicpu/abs.py +36 -0
  456. mindspore/ops/_op_impl/aicpu/acos.py +32 -0
  457. mindspore/ops/_op_impl/aicpu/acos_grad.py +33 -0
  458. mindspore/ops/_op_impl/aicpu/acosh.py +34 -0
  459. mindspore/ops/_op_impl/aicpu/acosh_grad.py +35 -0
  460. mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_2d.py +34 -0
  461. mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_2d_grad.py +34 -0
  462. mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_3d.py +39 -0
  463. mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_3d_grad.py +39 -0
  464. mindspore/ops/_op_impl/aicpu/adaptive_max_pool_2d.py +37 -0
  465. mindspore/ops/_op_impl/aicpu/adaptive_max_pool_2d_grad.py +37 -0
  466. mindspore/ops/_op_impl/aicpu/adaptive_max_pool_3d.py +42 -0
  467. mindspore/ops/_op_impl/aicpu/adaptive_max_pool_3d_grad.py +152 -0
  468. mindspore/ops/_op_impl/aicpu/add.py +43 -0
  469. mindspore/ops/_op_impl/aicpu/add_n.py +41 -0
  470. mindspore/ops/_op_impl/aicpu/add_v2.py +40 -0
  471. mindspore/ops/_op_impl/aicpu/addcdiv.py +41 -0
  472. mindspore/ops/_op_impl/aicpu/addcmul.py +47 -0
  473. mindspore/ops/_op_impl/aicpu/adjust_contrastv2.py +32 -0
  474. mindspore/ops/_op_impl/aicpu/adjust_hue.py +31 -0
  475. mindspore/ops/_op_impl/aicpu/adjust_saturation.py +32 -0
  476. mindspore/ops/_op_impl/aicpu/affine_grid.py +33 -0
  477. mindspore/ops/_op_impl/aicpu/affine_grid_grad.py +35 -0
  478. mindspore/ops/_op_impl/aicpu/angle.py +31 -0
  479. mindspore/ops/_op_impl/aicpu/arg_max.py +75 -0
  480. mindspore/ops/_op_impl/aicpu/arg_min.py +75 -0
  481. mindspore/ops/_op_impl/aicpu/argmax_with_value.py +43 -0
  482. mindspore/ops/_op_impl/aicpu/argmin_with_value.py +43 -0
  483. mindspore/ops/_op_impl/aicpu/asin.py +32 -0
  484. mindspore/ops/_op_impl/aicpu/asin_grad.py +33 -0
  485. mindspore/ops/_op_impl/aicpu/asinh.py +34 -0
  486. mindspore/ops/_op_impl/aicpu/asinh_grad.py +35 -0
  487. mindspore/ops/_op_impl/aicpu/atanh.py +34 -0
  488. mindspore/ops/_op_impl/aicpu/avgpool_grad_v1.py +37 -0
  489. mindspore/ops/_op_impl/aicpu/avgpool_v1.py +36 -0
  490. mindspore/ops/_op_impl/aicpu/bartlett_window.py +36 -0
  491. mindspore/ops/_op_impl/aicpu/batch_matmul.py +43 -0
  492. mindspore/ops/_op_impl/aicpu/batch_norm_grad_grad.py +49 -0
  493. mindspore/ops/_op_impl/aicpu/bernoulli.py +48 -0
  494. mindspore/ops/_op_impl/aicpu/bessel_i0.py +31 -0
  495. mindspore/ops/_op_impl/aicpu/betainc.py +31 -0
  496. mindspore/ops/_op_impl/aicpu/bias_add.py +44 -0
  497. mindspore/ops/_op_impl/aicpu/bias_add_grad.py +42 -0
  498. mindspore/ops/_op_impl/aicpu/bincount.py +33 -0
  499. mindspore/ops/_op_impl/aicpu/blackman_window.py +36 -0
  500. mindspore/ops/_op_impl/aicpu/broadcast_to.py +58 -0
  501. mindspore/ops/_op_impl/aicpu/bucketize.py +34 -0
  502. mindspore/ops/_op_impl/aicpu/cache_swap_table.py +102 -0
  503. mindspore/ops/_op_impl/aicpu/cast.py +225 -0
  504. mindspore/ops/_op_impl/aicpu/cauchy.py +33 -0
  505. mindspore/ops/_op_impl/aicpu/channel_shuffle.py +40 -0
  506. mindspore/ops/_op_impl/aicpu/check_numerics.py +33 -0
  507. mindspore/ops/_op_impl/aicpu/cholesky.py +32 -0
  508. mindspore/ops/_op_impl/aicpu/cholesky_inverse.py +31 -0
  509. mindspore/ops/_op_impl/aicpu/cholesky_solve.py +33 -0
  510. mindspore/ops/_op_impl/aicpu/choleskygrad.py +32 -0
  511. mindspore/ops/_op_impl/aicpu/coalesce.py +37 -0
  512. mindspore/ops/_op_impl/aicpu/col2im.py +38 -0
  513. mindspore/ops/_op_impl/aicpu/combined_non_max_suppression.py +42 -0
  514. mindspore/ops/_op_impl/aicpu/compare_and_bitpack.py +37 -0
  515. mindspore/ops/_op_impl/aicpu/complex.py +32 -0
  516. mindspore/ops/_op_impl/aicpu/complex_abs.py +31 -0
  517. mindspore/ops/_op_impl/aicpu/compute_accidental_hits.py +44 -0
  518. mindspore/ops/_op_impl/aicpu/concat.py +57 -0
  519. mindspore/ops/_op_impl/aicpu/concat_offset.py +42 -0
  520. mindspore/ops/_op_impl/aicpu/concat_offset_v1.py +31 -0
  521. mindspore/ops/_op_impl/aicpu/conj.py +42 -0
  522. mindspore/ops/_op_impl/aicpu/conjugate_transpose.py +58 -0
  523. mindspore/ops/_op_impl/aicpu/cos.py +34 -0
  524. mindspore/ops/_op_impl/aicpu/cosh.py +34 -0
  525. mindspore/ops/_op_impl/aicpu/count_nonzero.py +43 -0
  526. mindspore/ops/_op_impl/aicpu/crop_and_resize.py +69 -0
  527. mindspore/ops/_op_impl/aicpu/crop_and_resize_grad_boxes.py +68 -0
  528. mindspore/ops/_op_impl/aicpu/crop_and_resize_grad_image.py +38 -0
  529. mindspore/ops/_op_impl/aicpu/cross.py +42 -0
  530. mindspore/ops/_op_impl/aicpu/csr_sparse_matrix_to_dense.py +48 -0
  531. mindspore/ops/_op_impl/aicpu/csr_sparse_matrix_to_sparse_tensor.py +51 -0
  532. mindspore/ops/_op_impl/aicpu/ctc_greedy_decoder.py +35 -0
  533. mindspore/ops/_op_impl/aicpu/ctc_loss_v2.py +43 -0
  534. mindspore/ops/_op_impl/aicpu/ctc_loss_v2_grad.py +45 -0
  535. mindspore/ops/_op_impl/aicpu/ctcloss.py +38 -0
  536. mindspore/ops/_op_impl/aicpu/cummax.py +41 -0
  537. mindspore/ops/_op_impl/aicpu/cumprod.py +58 -0
  538. mindspore/ops/_op_impl/aicpu/cumsum.py +58 -0
  539. mindspore/ops/_op_impl/aicpu/cumulative_logsumexp.py +36 -0
  540. mindspore/ops/_op_impl/aicpu/data_format_vec_permute.py +32 -0
  541. mindspore/ops/_op_impl/aicpu/deformable_offsets.py +38 -0
  542. mindspore/ops/_op_impl/aicpu/deformable_offsets_grad.py +43 -0
  543. mindspore/ops/_op_impl/aicpu/dense_to_csr_sparse_matrix.py +49 -0
  544. mindspore/ops/_op_impl/aicpu/dense_to_dense_set_operation.py +45 -0
  545. mindspore/ops/_op_impl/aicpu/dense_to_sparse_set_operation.py +48 -0
  546. mindspore/ops/_op_impl/aicpu/depth_to_space.py +44 -0
  547. mindspore/ops/_op_impl/aicpu/diag.py +36 -0
  548. mindspore/ops/_op_impl/aicpu/diag_part.py +36 -0
  549. mindspore/ops/_op_impl/aicpu/diagonal.py +35 -0
  550. mindspore/ops/_op_impl/aicpu/digamma.py +31 -0
  551. mindspore/ops/_op_impl/aicpu/div.py +41 -0
  552. mindspore/ops/_op_impl/aicpu/div_no_nan.py +35 -0
  553. mindspore/ops/_op_impl/aicpu/dropout2d.py +42 -0
  554. mindspore/ops/_op_impl/aicpu/dropout3d.py +42 -0
  555. mindspore/ops/_op_impl/aicpu/dropout_genmask.py +41 -0
  556. mindspore/ops/_op_impl/aicpu/dropout_genmask_v3.py +32 -0
  557. mindspore/ops/_op_impl/aicpu/dynamic_stitch.py +42 -0
  558. mindspore/ops/_op_impl/aicpu/edit_distance.py +56 -0
  559. mindspore/ops/_op_impl/aicpu/eig.py +35 -0
  560. mindspore/ops/_op_impl/aicpu/embedding_lookup.py +102 -0
  561. mindspore/ops/_op_impl/aicpu/end_of_sequence.py +30 -0
  562. mindspore/ops/_op_impl/aicpu/environ_create.py +28 -0
  563. mindspore/ops/_op_impl/aicpu/environ_destroy_all.py +28 -0
  564. mindspore/ops/_op_impl/aicpu/environ_get.py +41 -0
  565. mindspore/ops/_op_impl/aicpu/environ_set.py +40 -0
  566. mindspore/ops/_op_impl/aicpu/eps.py +32 -0
  567. mindspore/ops/_op_impl/aicpu/equal.py +41 -0
  568. mindspore/ops/_op_impl/aicpu/exp.py +37 -0
  569. mindspore/ops/_op_impl/aicpu/expand.py +45 -0
  570. mindspore/ops/_op_impl/aicpu/expand_dims.py +42 -0
  571. mindspore/ops/_op_impl/aicpu/expm1.py +34 -0
  572. mindspore/ops/_op_impl/aicpu/extract_glimpse.py +35 -0
  573. mindspore/ops/_op_impl/aicpu/eye.py +44 -0
  574. mindspore/ops/_op_impl/aicpu/fft_with_size.py +47 -0
  575. mindspore/ops/_op_impl/aicpu/fill_diagonal.py +39 -0
  576. mindspore/ops/_op_impl/aicpu/fill_v2.py +58 -0
  577. mindspore/ops/_op_impl/aicpu/flatten.py +43 -0
  578. mindspore/ops/_op_impl/aicpu/floor_div.py +38 -0
  579. mindspore/ops/_op_impl/aicpu/fmax.py +36 -0
  580. mindspore/ops/_op_impl/aicpu/fmin.py +37 -0
  581. mindspore/ops/_op_impl/aicpu/fractional_avg_pool.py +41 -0
  582. mindspore/ops/_op_impl/aicpu/fractional_avg_pool_grad.py +41 -0
  583. mindspore/ops/_op_impl/aicpu/fractional_max_pool.py +41 -0
  584. mindspore/ops/_op_impl/aicpu/fractional_max_pool3d_grad_with_fixed_ksize.py +43 -0
  585. mindspore/ops/_op_impl/aicpu/fractional_max_pool3d_with_fixed_ksize.py +65 -0
  586. mindspore/ops/_op_impl/aicpu/fractional_max_pool_grad.py +42 -0
  587. mindspore/ops/_op_impl/aicpu/fractional_max_pool_grad_with_fixed_ksize.py +42 -0
  588. mindspore/ops/_op_impl/aicpu/fractional_max_pool_with_fixed_ksize.py +49 -0
  589. mindspore/ops/_op_impl/aicpu/fse_decode.py +43 -0
  590. mindspore/ops/_op_impl/aicpu/fused_sparse_adam.py +46 -0
  591. mindspore/ops/_op_impl/aicpu/fused_sparse_ftrl.py +41 -0
  592. mindspore/ops/_op_impl/aicpu/fused_sparse_lazy_adam.py +46 -0
  593. mindspore/ops/_op_impl/aicpu/fused_sparse_proximal_adagrad.py +39 -0
  594. mindspore/ops/_op_impl/aicpu/gamma.py +38 -0
  595. mindspore/ops/_op_impl/aicpu/gather.py +46 -0
  596. mindspore/ops/_op_impl/aicpu/gather_d.py +79 -0
  597. mindspore/ops/_op_impl/aicpu/gather_d_grad_v2.py +79 -0
  598. mindspore/ops/_op_impl/aicpu/gather_grad.py +54 -0
  599. mindspore/ops/_op_impl/aicpu/gather_nd.py +56 -0
  600. mindspore/ops/_op_impl/aicpu/gcd.py +32 -0
  601. mindspore/ops/_op_impl/aicpu/generate_eod_mask.py +38 -0
  602. mindspore/ops/_op_impl/aicpu/geqrf.py +32 -0
  603. mindspore/ops/_op_impl/aicpu/get_next.py +39 -0
  604. mindspore/ops/_op_impl/aicpu/glu.py +33 -0
  605. mindspore/ops/_op_impl/aicpu/glu_grad.py +34 -0
  606. mindspore/ops/_op_impl/aicpu/greater.py +41 -0
  607. mindspore/ops/_op_impl/aicpu/greater_equal.py +41 -0
  608. mindspore/ops/_op_impl/aicpu/grid_sampler_2d.py +35 -0
  609. mindspore/ops/_op_impl/aicpu/grid_sampler_2d_grad.py +38 -0
  610. mindspore/ops/_op_impl/aicpu/grid_sampler_3d.py +34 -0
  611. mindspore/ops/_op_impl/aicpu/grid_sampler_3d_grad.py +38 -0
  612. mindspore/ops/_op_impl/aicpu/hamming_window.py +57 -0
  613. mindspore/ops/_op_impl/aicpu/hard_sigmoid.py +32 -0
  614. mindspore/ops/_op_impl/aicpu/hard_sigmoid_grad.py +33 -0
  615. mindspore/ops/_op_impl/aicpu/heaviside.py +40 -0
  616. mindspore/ops/_op_impl/aicpu/histogram.py +35 -0
  617. mindspore/ops/_op_impl/aicpu/hsv_to_rgb.py +32 -0
  618. mindspore/ops/_op_impl/aicpu/hypot.py +32 -0
  619. mindspore/ops/_op_impl/aicpu/identity.py +42 -0
  620. mindspore/ops/_op_impl/aicpu/identity_n.py +41 -0
  621. mindspore/ops/_op_impl/aicpu/igamma.py +30 -0
  622. mindspore/ops/_op_impl/aicpu/igammac.py +30 -0
  623. mindspore/ops/_op_impl/aicpu/igammagrada.py +30 -0
  624. mindspore/ops/_op_impl/aicpu/im2col.py +43 -0
  625. mindspore/ops/_op_impl/aicpu/imag.py +31 -0
  626. mindspore/ops/_op_impl/aicpu/index_fill.py +54 -0
  627. mindspore/ops/_op_impl/aicpu/index_put.py +50 -0
  628. mindspore/ops/_op_impl/aicpu/init_data_set_queue.py +27 -0
  629. mindspore/ops/_op_impl/aicpu/inplace_index_add.py +39 -0
  630. mindspore/ops/_op_impl/aicpu/instance_norm_v2.py +41 -0
  631. mindspore/ops/_op_impl/aicpu/instance_norm_v2_grad.py +44 -0
  632. mindspore/ops/_op_impl/aicpu/is_finite.py +40 -0
  633. mindspore/ops/_op_impl/aicpu/is_inf.py +31 -0
  634. mindspore/ops/_op_impl/aicpu/is_nan.py +31 -0
  635. mindspore/ops/_op_impl/aicpu/kldivloss.py +34 -0
  636. mindspore/ops/_op_impl/aicpu/kldivlossgrad.py +35 -0
  637. mindspore/ops/_op_impl/aicpu/layer_norm_grad_grad.py +47 -0
  638. mindspore/ops/_op_impl/aicpu/lcm.py +32 -0
  639. mindspore/ops/_op_impl/aicpu/left_shift.py +38 -0
  640. mindspore/ops/_op_impl/aicpu/less.py +41 -0
  641. mindspore/ops/_op_impl/aicpu/less_equal.py +41 -0
  642. mindspore/ops/_op_impl/aicpu/lgamma.py +33 -0
  643. mindspore/ops/_op_impl/aicpu/linear_sum_assignment.py +57 -0
  644. mindspore/ops/_op_impl/aicpu/linspace.py +33 -0
  645. mindspore/ops/_op_impl/aicpu/list_diff.py +50 -0
  646. mindspore/ops/_op_impl/aicpu/log.py +37 -0
  647. mindspore/ops/_op_impl/aicpu/log1p.py +34 -0
  648. mindspore/ops/_op_impl/aicpu/log_matrix_determinant.py +31 -0
  649. mindspore/ops/_op_impl/aicpu/log_normal_reverse.py +33 -0
  650. mindspore/ops/_op_impl/aicpu/log_uniform_candidate_sampler.py +37 -0
  651. mindspore/ops/_op_impl/aicpu/logical_xor.py +30 -0
  652. mindspore/ops/_op_impl/aicpu/logit.py +33 -0
  653. mindspore/ops/_op_impl/aicpu/logit_grad.py +34 -0
  654. mindspore/ops/_op_impl/aicpu/logspace.py +36 -0
  655. mindspore/ops/_op_impl/aicpu/lower_bound.py +47 -0
  656. mindspore/ops/_op_impl/aicpu/lstsq.py +34 -0
  657. mindspore/ops/_op_impl/aicpu/lu.py +39 -0
  658. mindspore/ops/_op_impl/aicpu/lu_solve.py +32 -0
  659. mindspore/ops/_op_impl/aicpu/lu_unpack.py +114 -0
  660. mindspore/ops/_op_impl/aicpu/lu_unpack_grad.py +49 -0
  661. mindspore/ops/_op_impl/aicpu/masked_fill.py +42 -0
  662. mindspore/ops/_op_impl/aicpu/masked_scatter.py +40 -0
  663. mindspore/ops/_op_impl/aicpu/masked_select.py +31 -0
  664. mindspore/ops/_op_impl/aicpu/masked_select_grad.py +35 -0
  665. mindspore/ops/_op_impl/aicpu/matmul.py +39 -0
  666. mindspore/ops/_op_impl/aicpu/matrix_band_part.py +59 -0
  667. mindspore/ops/_op_impl/aicpu/matrix_determinant.py +30 -0
  668. mindspore/ops/_op_impl/aicpu/matrix_diag_part_v3.py +54 -0
  669. mindspore/ops/_op_impl/aicpu/matrix_diag_v3.py +56 -0
  670. mindspore/ops/_op_impl/aicpu/matrix_exp.py +34 -0
  671. mindspore/ops/_op_impl/aicpu/matrix_inverse.py +31 -0
  672. mindspore/ops/_op_impl/aicpu/matrix_logarithm.py +31 -0
  673. mindspore/ops/_op_impl/aicpu/matrix_power.py +37 -0
  674. mindspore/ops/_op_impl/aicpu/matrix_set_diag_v3.py +54 -0
  675. mindspore/ops/_op_impl/aicpu/matrix_solve.py +35 -0
  676. mindspore/ops/_op_impl/aicpu/matrix_solve_ls.py +36 -0
  677. mindspore/ops/_op_impl/aicpu/matrix_triangular_solve.py +36 -0
  678. mindspore/ops/_op_impl/aicpu/max_pool3d_grad_with_argmax.py +60 -0
  679. mindspore/ops/_op_impl/aicpu/max_pool3d_with_argmax.py +59 -0
  680. mindspore/ops/_op_impl/aicpu/max_unpool2d.py +57 -0
  681. mindspore/ops/_op_impl/aicpu/max_unpool2d_grad.py +58 -0
  682. mindspore/ops/_op_impl/aicpu/max_unpool3d.py +57 -0
  683. mindspore/ops/_op_impl/aicpu/max_unpool3d_grad.py +58 -0
  684. mindspore/ops/_op_impl/aicpu/maximum_grad_grad.py +40 -0
  685. mindspore/ops/_op_impl/aicpu/maxpool_grad_v1.py +46 -0
  686. mindspore/ops/_op_impl/aicpu/maxpool_v1.py +42 -0
  687. mindspore/ops/_op_impl/aicpu/median.py +39 -0
  688. mindspore/ops/_op_impl/aicpu/median_grad.py +45 -0
  689. mindspore/ops/_op_impl/aicpu/meshgrid.py +41 -0
  690. mindspore/ops/_op_impl/aicpu/minimum_grad_grad.py +40 -0
  691. mindspore/ops/_op_impl/aicpu/mirror_pad.py +50 -0
  692. mindspore/ops/_op_impl/aicpu/mirror_pad_grad.py +48 -0
  693. mindspore/ops/_op_impl/aicpu/mul.py +43 -0
  694. mindspore/ops/_op_impl/aicpu/mul_no_nan.py +42 -0
  695. mindspore/ops/_op_impl/aicpu/multi_margin_loss.py +37 -0
  696. mindspore/ops/_op_impl/aicpu/multi_margin_loss_grad.py +41 -0
  697. mindspore/ops/_op_impl/aicpu/multilabel_margin_loss_grad.py +37 -0
  698. mindspore/ops/_op_impl/aicpu/multinomial.py +47 -0
  699. mindspore/ops/_op_impl/aicpu/multinomial_with_replacement.py +35 -0
  700. mindspore/ops/_op_impl/aicpu/mvlgamma.py +32 -0
  701. mindspore/ops/_op_impl/aicpu/mvlgamma_grad.py +33 -0
  702. mindspore/ops/_op_impl/aicpu/nan_to_num.py +34 -0
  703. mindspore/ops/_op_impl/aicpu/neg.py +36 -0
  704. mindspore/ops/_op_impl/aicpu/nextafter.py +32 -0
  705. mindspore/ops/_op_impl/aicpu/nllloss.py +38 -0
  706. mindspore/ops/_op_impl/aicpu/nllloss_grad.py +39 -0
  707. mindspore/ops/_op_impl/aicpu/no_repeat_ngram.py +34 -0
  708. mindspore/ops/_op_impl/aicpu/non_deterministic_ints.py +33 -0
  709. mindspore/ops/_op_impl/aicpu/non_max_suppression.py +36 -0
  710. mindspore/ops/_op_impl/aicpu/non_max_suppression_with_overlaps.py +35 -0
  711. mindspore/ops/_op_impl/aicpu/non_zero.py +43 -0
  712. mindspore/ops/_op_impl/aicpu/not_equal.py +39 -0
  713. mindspore/ops/_op_impl/aicpu/nth_element.py +39 -0
  714. mindspore/ops/_op_impl/aicpu/nuclear_norm.py +33 -0
  715. mindspore/ops/_op_impl/aicpu/one_hot.py +116 -0
  716. mindspore/ops/_op_impl/aicpu/ones_like.py +39 -0
  717. mindspore/ops/_op_impl/aicpu/orgqr.py +34 -0
  718. mindspore/ops/_op_impl/aicpu/pad_and_shift.py +33 -0
  719. mindspore/ops/_op_impl/aicpu/pad_v3.py +61 -0
  720. mindspore/ops/_op_impl/aicpu/pad_v3_grad.py +59 -0
  721. mindspore/ops/_op_impl/aicpu/padding.py +41 -0
  722. mindspore/ops/_op_impl/aicpu/parameterized_truncated_normal.py +54 -0
  723. mindspore/ops/_op_impl/aicpu/pdist_grad.py +33 -0
  724. mindspore/ops/_op_impl/aicpu/poisson.py +37 -0
  725. mindspore/ops/_op_impl/aicpu/polar.py +32 -0
  726. mindspore/ops/_op_impl/aicpu/polygamma.py +34 -0
  727. mindspore/ops/_op_impl/aicpu/pow.py +39 -0
  728. mindspore/ops/_op_impl/aicpu/print_tensor.py +39 -0
  729. mindspore/ops/_op_impl/aicpu/priority_replay_buffer.py +113 -0
  730. mindspore/ops/_op_impl/aicpu/qr.py +36 -0
  731. mindspore/ops/_op_impl/aicpu/quant_dtype_cast.py +40 -0
  732. mindspore/ops/_op_impl/aicpu/quantile.py +35 -0
  733. mindspore/ops/_op_impl/aicpu/ragged_range.py +49 -0
  734. mindspore/ops/_op_impl/aicpu/ragged_tensor_to_sparse.py +73 -0
  735. mindspore/ops/_op_impl/aicpu/ragged_tensor_to_tensor.py +74 -0
  736. mindspore/ops/_op_impl/aicpu/random_categorical.py +68 -0
  737. mindspore/ops/_op_impl/aicpu/random_choice_with_mask.py +36 -0
  738. mindspore/ops/_op_impl/aicpu/random_gamma.py +38 -0
  739. mindspore/ops/_op_impl/aicpu/random_poisson.py +134 -0
  740. mindspore/ops/_op_impl/aicpu/random_shuffle.py +47 -0
  741. mindspore/ops/_op_impl/aicpu/randperm.py +38 -0
  742. mindspore/ops/_op_impl/aicpu/randperm_v2.py +41 -0
  743. mindspore/ops/_op_impl/aicpu/range.py +36 -0
  744. mindspore/ops/_op_impl/aicpu/range_v2.py +35 -0
  745. mindspore/ops/_op_impl/aicpu/real.py +31 -0
  746. mindspore/ops/_op_impl/aicpu/real_div.py +40 -0
  747. mindspore/ops/_op_impl/aicpu/reciprocal.py +34 -0
  748. mindspore/ops/_op_impl/aicpu/reciprocal_grad.py +35 -0
  749. mindspore/ops/_op_impl/aicpu/reduce_mean.py +57 -0
  750. mindspore/ops/_op_impl/aicpu/reduce_prod.py +57 -0
  751. mindspore/ops/_op_impl/aicpu/reduce_sum.py +57 -0
  752. mindspore/ops/_op_impl/aicpu/relu_grad_v3.py +41 -0
  753. mindspore/ops/_op_impl/aicpu/relu_v3.py +38 -0
  754. mindspore/ops/_op_impl/aicpu/reservoir_replay_buffer.py +96 -0
  755. mindspore/ops/_op_impl/aicpu/reshape.py +42 -0
  756. mindspore/ops/_op_impl/aicpu/resize_area.py +40 -0
  757. mindspore/ops/_op_impl/aicpu/resize_bicubic.py +20 -0
  758. mindspore/ops/_op_impl/aicpu/resize_bicubic_grad.py +19 -0
  759. mindspore/ops/_op_impl/aicpu/resize_bilinear.py +32 -0
  760. mindspore/ops/_op_impl/aicpu/resize_bilinear_grad.py +32 -0
  761. mindspore/ops/_op_impl/aicpu/resize_nearest_neighbor_v2.py +36 -0
  762. mindspore/ops/_op_impl/aicpu/resize_nearest_neighbor_v2_grad.py +35 -0
  763. mindspore/ops/_op_impl/aicpu/resize_v2.py +68 -0
  764. mindspore/ops/_op_impl/aicpu/resize_v2_grad.py +68 -0
  765. mindspore/ops/_op_impl/aicpu/reverse_sequence.py +55 -0
  766. mindspore/ops/_op_impl/aicpu/reversev2.py +54 -0
  767. mindspore/ops/_op_impl/aicpu/rgb_to_hsv.py +32 -0
  768. mindspore/ops/_op_impl/aicpu/right_shift.py +38 -0
  769. mindspore/ops/_op_impl/aicpu/rnnt_loss.py +35 -0
  770. mindspore/ops/_op_impl/aicpu/round.py +34 -0
  771. mindspore/ops/_op_impl/aicpu/rsqrt.py +33 -0
  772. mindspore/ops/_op_impl/aicpu/rsqrt_grad.py +36 -0
  773. mindspore/ops/_op_impl/aicpu/sample_distorted_bounding_box_v2.py +49 -0
  774. mindspore/ops/_op_impl/aicpu/scale_and_translate.py +52 -0
  775. mindspore/ops/_op_impl/aicpu/scale_and_translate_grad.py +36 -0
  776. mindspore/ops/_op_impl/aicpu/scatter.py +79 -0
  777. mindspore/ops/_op_impl/aicpu/scatter_add_with_axis.py +53 -0
  778. mindspore/ops/_op_impl/aicpu/scatter_elements.py +39 -0
  779. mindspore/ops/_op_impl/aicpu/scatter_nd.py +59 -0
  780. mindspore/ops/_op_impl/aicpu/scatter_nd_max.py +54 -0
  781. mindspore/ops/_op_impl/aicpu/scatter_nd_min.py +54 -0
  782. mindspore/ops/_op_impl/aicpu/scatter_nd_update.py +59 -0
  783. mindspore/ops/_op_impl/aicpu/search_sorted.py +44 -0
  784. mindspore/ops/_op_impl/aicpu/segment_max.py +52 -0
  785. mindspore/ops/_op_impl/aicpu/segment_mean.py +56 -0
  786. mindspore/ops/_op_impl/aicpu/segment_min.py +52 -0
  787. mindspore/ops/_op_impl/aicpu/segment_prod.py +56 -0
  788. mindspore/ops/_op_impl/aicpu/segment_sum.py +56 -0
  789. mindspore/ops/_op_impl/aicpu/select.py +45 -0
  790. mindspore/ops/_op_impl/aicpu/self_adjoint_eig.py +34 -0
  791. mindspore/ops/_op_impl/aicpu/sequence_add.py +34 -0
  792. mindspore/ops/_op_impl/aicpu/sequence_add_offset.py +34 -0
  793. mindspore/ops/_op_impl/aicpu/sequence_addn.py +38 -0
  794. mindspore/ops/_op_impl/aicpu/sequence_concat.py +40 -0
  795. mindspore/ops/_op_impl/aicpu/sequence_stack.py +40 -0
  796. mindspore/ops/_op_impl/aicpu/set_size.py +38 -0
  797. mindspore/ops/_op_impl/aicpu/sign.py +36 -0
  798. mindspore/ops/_op_impl/aicpu/sin.py +34 -0
  799. mindspore/ops/_op_impl/aicpu/sinc.py +43 -0
  800. mindspore/ops/_op_impl/aicpu/sinh.py +34 -0
  801. mindspore/ops/_op_impl/aicpu/slice.py +59 -0
  802. mindspore/ops/_op_impl/aicpu/slice_grad.py +76 -0
  803. mindspore/ops/_op_impl/aicpu/smooth_l1_loss.py +35 -0
  804. mindspore/ops/_op_impl/aicpu/smooth_l1_loss_grad.py +37 -0
  805. mindspore/ops/_op_impl/aicpu/sort.py +39 -0
  806. mindspore/ops/_op_impl/aicpu/space_to_depth.py +44 -0
  807. mindspore/ops/_op_impl/aicpu/sparse_addmm.py +87 -0
  808. mindspore/ops/_op_impl/aicpu/sparse_apply_adagrad_da.py +80 -0
  809. mindspore/ops/_op_impl/aicpu/sparse_apply_centered_rms_prop.py +105 -0
  810. mindspore/ops/_op_impl/aicpu/sparse_apply_momentum.py +80 -0
  811. mindspore/ops/_op_impl/aicpu/sparse_apply_proximal_gradient_descent.py +79 -0
  812. mindspore/ops/_op_impl/aicpu/sparse_concat.py +59 -0
  813. mindspore/ops/_op_impl/aicpu/sparse_cross.py +42 -0
  814. mindspore/ops/_op_impl/aicpu/sparse_dense_cwise_add.py +58 -0
  815. mindspore/ops/_op_impl/aicpu/sparse_dense_cwise_div.py +58 -0
  816. mindspore/ops/_op_impl/aicpu/sparse_dense_cwise_mul.py +58 -0
  817. mindspore/ops/_op_impl/aicpu/sparse_fill_empty_rows.py +63 -0
  818. mindspore/ops/_op_impl/aicpu/sparse_fill_empty_rows_grad.py +45 -0
  819. mindspore/ops/_op_impl/aicpu/sparse_matrix_mat_mul.py +56 -0
  820. mindspore/ops/_op_impl/aicpu/sparse_matrix_nnz.py +81 -0
  821. mindspore/ops/_op_impl/aicpu/sparse_matrix_transpose.py +116 -0
  822. mindspore/ops/_op_impl/aicpu/sparse_reorder.py +56 -0
  823. mindspore/ops/_op_impl/aicpu/sparse_reshape.py +34 -0
  824. mindspore/ops/_op_impl/aicpu/sparse_segment_mean_grad.py +36 -0
  825. mindspore/ops/_op_impl/aicpu/sparse_segment_mean_with_num_segments.py +44 -0
  826. mindspore/ops/_op_impl/aicpu/sparse_segment_sqrt_n.py +43 -0
  827. mindspore/ops/_op_impl/aicpu/sparse_segment_sqrt_n_grad.py +38 -0
  828. mindspore/ops/_op_impl/aicpu/sparse_segment_sqrt_n_with_num_segments.py +44 -0
  829. mindspore/ops/_op_impl/aicpu/sparse_segment_sum.py +49 -0
  830. mindspore/ops/_op_impl/aicpu/sparse_segment_sum_with_num_segments.py +68 -0
  831. mindspore/ops/_op_impl/aicpu/sparse_slice.py +63 -0
  832. mindspore/ops/_op_impl/aicpu/sparse_slice_grad.py +61 -0
  833. mindspore/ops/_op_impl/aicpu/sparse_softmax.py +33 -0
  834. mindspore/ops/_op_impl/aicpu/sparse_softmax_cross_entropy_with_logits_v2.py +35 -0
  835. mindspore/ops/_op_impl/aicpu/sparse_sparse_maximum.py +53 -0
  836. mindspore/ops/_op_impl/aicpu/sparse_sparse_minimum.py +53 -0
  837. mindspore/ops/_op_impl/aicpu/sparse_tensor_dense_add.py +84 -0
  838. mindspore/ops/_op_impl/aicpu/sparse_tensor_dense_mat_mul.py +190 -0
  839. mindspore/ops/_op_impl/aicpu/sparse_tensor_to_csr_sparse_matrix.py +51 -0
  840. mindspore/ops/_op_impl/aicpu/sparse_to_dense_v2.py +73 -0
  841. mindspore/ops/_op_impl/aicpu/split.py +45 -0
  842. mindspore/ops/_op_impl/aicpu/sqrt.py +34 -0
  843. mindspore/ops/_op_impl/aicpu/sqrt_grad.py +35 -0
  844. mindspore/ops/_op_impl/aicpu/square.py +35 -0
  845. mindspore/ops/_op_impl/aicpu/squared_difference.py +37 -0
  846. mindspore/ops/_op_impl/aicpu/squeeze.py +42 -0
  847. mindspore/ops/_op_impl/aicpu/sspaddmm.py +97 -0
  848. mindspore/ops/_op_impl/aicpu/stack.py +45 -0
  849. mindspore/ops/_op_impl/aicpu/stack_push_pop.py +87 -0
  850. mindspore/ops/_op_impl/aicpu/standard_laplace.py +34 -0
  851. mindspore/ops/_op_impl/aicpu/standard_normal.py +34 -0
  852. mindspore/ops/_op_impl/aicpu/stateless_dropout_genmask.py +37 -0
  853. mindspore/ops/_op_impl/aicpu/stft.py +70 -0
  854. mindspore/ops/_op_impl/aicpu/strided_slice.py +43 -0
  855. mindspore/ops/_op_impl/aicpu/strided_slice_grad.py +50 -0
  856. mindspore/ops/_op_impl/aicpu/sub.py +41 -0
  857. mindspore/ops/_op_impl/aicpu/sub_and_filter.py +36 -0
  858. mindspore/ops/_op_impl/aicpu/tan.py +34 -0
  859. mindspore/ops/_op_impl/aicpu/tanh.py +34 -0
  860. mindspore/ops/_op_impl/aicpu/tanh_grad.py +35 -0
  861. mindspore/ops/_op_impl/aicpu/tensor_scatter_update.py +59 -0
  862. mindspore/ops/_op_impl/aicpu/tile.py +56 -0
  863. mindspore/ops/_op_impl/aicpu/topk.py +34 -0
  864. mindspore/ops/_op_impl/aicpu/trace.py +40 -0
  865. mindspore/ops/_op_impl/aicpu/tracegrad.py +41 -0
  866. mindspore/ops/_op_impl/aicpu/trans_data.py +35 -0
  867. mindspore/ops/_op_impl/aicpu/transpose.py +58 -0
  868. mindspore/ops/_op_impl/aicpu/tridiagonal_matmul.py +42 -0
  869. mindspore/ops/_op_impl/aicpu/tridiagonal_solve.py +35 -0
  870. mindspore/ops/_op_impl/aicpu/tril.py +42 -0
  871. mindspore/ops/_op_impl/aicpu/tril_indices.py +34 -0
  872. mindspore/ops/_op_impl/aicpu/triplet_margin_loss.py +62 -0
  873. mindspore/ops/_op_impl/aicpu/triu.py +43 -0
  874. mindspore/ops/_op_impl/aicpu/triu_indices.py +34 -0
  875. mindspore/ops/_op_impl/aicpu/truncated_normal.py +39 -0
  876. mindspore/ops/_op_impl/aicpu/uniform.py +36 -0
  877. mindspore/ops/_op_impl/aicpu/uniform_candidate_sampler.py +41 -0
  878. mindspore/ops/_op_impl/aicpu/uniform_int.py +36 -0
  879. mindspore/ops/_op_impl/aicpu/uniform_real.py +33 -0
  880. mindspore/ops/_op_impl/aicpu/unique.py +31 -0
  881. mindspore/ops/_op_impl/aicpu/unique_consecutive.py +47 -0
  882. mindspore/ops/_op_impl/aicpu/unique_with_pad.py +32 -0
  883. mindspore/ops/_op_impl/aicpu/unravel_index.py +32 -0
  884. mindspore/ops/_op_impl/aicpu/unsorted_segment_prod.py +53 -0
  885. mindspore/ops/_op_impl/aicpu/unsorted_segment_sum.py +57 -0
  886. mindspore/ops/_op_impl/aicpu/unstack.py +45 -0
  887. mindspore/ops/_op_impl/aicpu/update_cache.py +44 -0
  888. mindspore/ops/_op_impl/aicpu/upper_bound.py +47 -0
  889. mindspore/ops/_op_impl/aicpu/upsample_nearest_3d.py +42 -0
  890. mindspore/ops/_op_impl/aicpu/upsample_nearest_3d_grad.py +49 -0
  891. mindspore/ops/_op_impl/aicpu/upsample_trilinear_3d.py +40 -0
  892. mindspore/ops/_op_impl/aicpu/upsample_trilinear_3d_grad.py +50 -0
  893. mindspore/ops/_op_impl/aicpu/xdivy.py +35 -0
  894. mindspore/ops/_op_impl/aicpu/xlogy.py +33 -0
  895. mindspore/ops/_op_impl/aicpu/zeros_like.py +42 -0
  896. mindspore/ops/_op_impl/aicpu/zeta.py +31 -0
  897. mindspore/ops/_op_impl/akg/__init__.py +19 -0
  898. mindspore/ops/_op_impl/akg/ascend/__init__.py +48 -0
  899. mindspore/ops/_op_impl/akg/ascend/abs.py +35 -0
  900. mindspore/ops/_op_impl/akg/ascend/add.py +42 -0
  901. mindspore/ops/_op_impl/akg/ascend/add_n.py +37 -0
  902. mindspore/ops/_op_impl/akg/ascend/batchmatmul.py +33 -0
  903. mindspore/ops/_op_impl/akg/ascend/cast.py +46 -0
  904. mindspore/ops/_op_impl/akg/ascend/equal.py +35 -0
  905. mindspore/ops/_op_impl/akg/ascend/exp.py +35 -0
  906. mindspore/ops/_op_impl/akg/ascend/expand_dims.py +33 -0
  907. mindspore/ops/_op_impl/akg/ascend/greater.py +34 -0
  908. mindspore/ops/_op_impl/akg/ascend/greater_equal.py +35 -0
  909. mindspore/ops/_op_impl/akg/ascend/less.py +31 -0
  910. mindspore/ops/_op_impl/akg/ascend/less_equal.py +35 -0
  911. mindspore/ops/_op_impl/akg/ascend/load_im2col.py +33 -0
  912. mindspore/ops/_op_impl/akg/ascend/log.py +34 -0
  913. mindspore/ops/_op_impl/akg/ascend/maximum.py +36 -0
  914. mindspore/ops/_op_impl/akg/ascend/minimum.py +39 -0
  915. mindspore/ops/_op_impl/akg/ascend/mul.py +41 -0
  916. mindspore/ops/_op_impl/akg/ascend/neg.py +37 -0
  917. mindspore/ops/_op_impl/akg/ascend/pow.py +35 -0
  918. mindspore/ops/_op_impl/akg/ascend/prod_force_se_a.py +33 -0
  919. mindspore/ops/_op_impl/akg/ascend/real_div.py +36 -0
  920. mindspore/ops/_op_impl/akg/ascend/reciprocal.py +32 -0
  921. mindspore/ops/_op_impl/akg/ascend/reduce_max.py +32 -0
  922. mindspore/ops/_op_impl/akg/ascend/reduce_min.py +32 -0
  923. mindspore/ops/_op_impl/akg/ascend/reduce_sum.py +37 -0
  924. mindspore/ops/_op_impl/akg/ascend/rsqrt.py +35 -0
  925. mindspore/ops/_op_impl/akg/ascend/select.py +37 -0
  926. mindspore/ops/_op_impl/akg/ascend/sqrt.py +35 -0
  927. mindspore/ops/_op_impl/akg/ascend/square.py +35 -0
  928. mindspore/ops/_op_impl/akg/ascend/sub.py +42 -0
  929. mindspore/ops/_op_impl/akg/cpu/__init__.py +23 -0
  930. mindspore/ops/_op_impl/akg/cpu/coo2csr.py +29 -0
  931. mindspore/ops/_op_impl/akg/cpu/csr2coo.py +29 -0
  932. mindspore/ops/_op_impl/akg/cpu/csr_gather.py +33 -0
  933. mindspore/ops/_op_impl/akg/cpu/csr_mm.py +34 -0
  934. mindspore/ops/_op_impl/akg/cpu/csr_mul.py +33 -0
  935. mindspore/ops/_op_impl/akg/cpu/csr_mv.py +33 -0
  936. mindspore/ops/_op_impl/akg/cpu/csr_reduce_sum.py +31 -0
  937. mindspore/ops/_op_impl/akg/gpu/__init__.py +24 -0
  938. mindspore/ops/_op_impl/akg/gpu/coo2csr.py +29 -0
  939. mindspore/ops/_op_impl/akg/gpu/csr2coo.py +29 -0
  940. mindspore/ops/_op_impl/akg/gpu/csr_div.py +36 -0
  941. mindspore/ops/_op_impl/akg/gpu/csr_gather.py +33 -0
  942. mindspore/ops/_op_impl/akg/gpu/csr_mm.py +37 -0
  943. mindspore/ops/_op_impl/akg/gpu/csr_mul.py +36 -0
  944. mindspore/ops/_op_impl/akg/gpu/csr_mv.py +36 -0
  945. mindspore/ops/_op_impl/akg/gpu/csr_reduce_sum.py +33 -0
  946. mindspore/ops/_op_impl/cpu/__init__.py +78 -0
  947. mindspore/ops/_op_impl/cpu/adam.py +49 -0
  948. mindspore/ops/_op_impl/cpu/adam_weight_decay.py +47 -0
  949. mindspore/ops/_op_impl/cpu/arg_max.py +30 -0
  950. mindspore/ops/_op_impl/cpu/arg_max_with_value.py +31 -0
  951. mindspore/ops/_op_impl/cpu/arg_min_with_value.py +31 -0
  952. mindspore/ops/_op_impl/cpu/buffer_append.py +28 -0
  953. mindspore/ops/_op_impl/cpu/buffer_get.py +28 -0
  954. mindspore/ops/_op_impl/cpu/buffer_sample.py +28 -0
  955. mindspore/ops/_op_impl/cpu/cast.py +171 -0
  956. mindspore/ops/_op_impl/cpu/concat_offset.py +38 -0
  957. mindspore/ops/_op_impl/cpu/conv2d.py +30 -0
  958. mindspore/ops/_op_impl/cpu/conv3d.py +30 -0
  959. mindspore/ops/_op_impl/cpu/div.py +32 -0
  960. mindspore/ops/_op_impl/cpu/dropout.py +31 -0
  961. mindspore/ops/_op_impl/cpu/dropout_grad.py +30 -0
  962. mindspore/ops/_op_impl/cpu/dynamic_shape.py +42 -0
  963. mindspore/ops/_op_impl/cpu/dynamic_stitch.py +41 -0
  964. mindspore/ops/_op_impl/cpu/equal_count.py +30 -0
  965. mindspore/ops/_op_impl/cpu/gather_d.py +49 -0
  966. mindspore/ops/_op_impl/cpu/gather_d_grad.py +38 -0
  967. mindspore/ops/_op_impl/cpu/gather_d_grad_v2.py +40 -0
  968. mindspore/ops/_op_impl/cpu/gather_v2.py +40 -0
  969. mindspore/ops/_op_impl/cpu/hsigmoid.py +33 -0
  970. mindspore/ops/_op_impl/cpu/hsigmoid_grad.py +34 -0
  971. mindspore/ops/_op_impl/cpu/hswish.py +32 -0
  972. mindspore/ops/_op_impl/cpu/hswish_grad.py +33 -0
  973. mindspore/ops/_op_impl/cpu/identity_n.py +40 -0
  974. mindspore/ops/_op_impl/cpu/is_finite.py +39 -0
  975. mindspore/ops/_op_impl/cpu/l2loss.py +30 -0
  976. mindspore/ops/_op_impl/cpu/layer_norm.py +36 -0
  977. mindspore/ops/_op_impl/cpu/layer_norm_grad.py +38 -0
  978. mindspore/ops/_op_impl/cpu/maximum.py +35 -0
  979. mindspore/ops/_op_impl/cpu/maximum_grad.py +47 -0
  980. mindspore/ops/_op_impl/cpu/minimum.py +40 -0
  981. mindspore/ops/_op_impl/cpu/minimum_grad.py +51 -0
  982. mindspore/ops/_op_impl/cpu/mirror_pad.py +36 -0
  983. mindspore/ops/_op_impl/cpu/mirror_pad_grad.py +36 -0
  984. mindspore/ops/_op_impl/cpu/mul.py +32 -0
  985. mindspore/ops/_op_impl/cpu/one_hot.py +31 -0
  986. mindspore/ops/_op_impl/cpu/pad.py +32 -0
  987. mindspore/ops/_op_impl/cpu/pow.py +32 -0
  988. mindspore/ops/_op_impl/cpu/priority_replay_buffer.py +42 -0
  989. mindspore/ops/_op_impl/cpu/pyexecute.py +29 -0
  990. mindspore/ops/_op_impl/cpu/pyfunc.py +29 -0
  991. mindspore/ops/_op_impl/cpu/range.py +34 -0
  992. mindspore/ops/_op_impl/cpu/real_div.py +33 -0
  993. mindspore/ops/_op_impl/cpu/reduce_all.py +29 -0
  994. mindspore/ops/_op_impl/cpu/reduce_any.py +29 -0
  995. mindspore/ops/_op_impl/cpu/reduce_max.py +32 -0
  996. mindspore/ops/_op_impl/cpu/reduce_mean.py +40 -0
  997. mindspore/ops/_op_impl/cpu/reduce_min.py +32 -0
  998. mindspore/ops/_op_impl/cpu/reduce_prod.py +40 -0
  999. mindspore/ops/_op_impl/cpu/reduce_std.py +31 -0
  1000. mindspore/ops/_op_impl/cpu/reduce_sum.py +41 -0
  1001. mindspore/ops/_op_impl/cpu/space_to_batch_nd.py +38 -0
  1002. mindspore/ops/_op_impl/cpu/sparse_slice.py +62 -0
  1003. mindspore/ops/_op_impl/cpu/sparse_slice_grad.py +60 -0
  1004. mindspore/ops/_op_impl/cpu/split.py +34 -0
  1005. mindspore/ops/_op_impl/cpu/sspaddmm.py +95 -0
  1006. mindspore/ops/_op_impl/cpu/stack.py +38 -0
  1007. mindspore/ops/_op_impl/cpu/sub.py +32 -0
  1008. mindspore/ops/_op_impl/cpu/tensor_copy_slices.py +41 -0
  1009. mindspore/ops/_op_impl/cpu/tile.py +37 -0
  1010. mindspore/ops/_op_impl/cpu/top_k.py +31 -0
  1011. mindspore/ops/_op_impl/cpu/transpose.py +39 -0
  1012. mindspore/ops/_primitive_cache.py +90 -0
  1013. mindspore/ops/_register_for_op.py +73 -0
  1014. mindspore/ops/_utils/__init__.py +20 -0
  1015. mindspore/ops/_utils/utils.py +147 -0
  1016. mindspore/ops/_vmap/__init__.py +25 -0
  1017. mindspore/ops/_vmap/vmap_array_ops.py +2149 -0
  1018. mindspore/ops/_vmap/vmap_base.py +533 -0
  1019. mindspore/ops/_vmap/vmap_convolution_ops.py +441 -0
  1020. mindspore/ops/_vmap/vmap_debug_ops.py +50 -0
  1021. mindspore/ops/_vmap/vmap_grad_math_ops.py +274 -0
  1022. mindspore/ops/_vmap/vmap_grad_nn_ops.py +806 -0
  1023. mindspore/ops/_vmap/vmap_image_ops.py +194 -0
  1024. mindspore/ops/_vmap/vmap_math_ops.py +993 -0
  1025. mindspore/ops/_vmap/vmap_nn_ops.py +2250 -0
  1026. mindspore/ops/_vmap/vmap_other_ops.py +105 -0
  1027. mindspore/ops/_vmap/vmap_random_ops.py +122 -0
  1028. mindspore/ops/_vmap/vmap_sparse_ops.py +89 -0
  1029. mindspore/ops/auto_generate/__init__.py +31 -0
  1030. mindspore/ops/auto_generate/cpp_create_prim_instance_helper.py +309 -0
  1031. mindspore/ops/auto_generate/gen_arg_dtype_cast.py +252 -0
  1032. mindspore/ops/auto_generate/gen_arg_handler.py +197 -0
  1033. mindspore/ops/auto_generate/gen_extend_func.py +1701 -0
  1034. mindspore/ops/auto_generate/gen_ops_def.py +8482 -0
  1035. mindspore/ops/auto_generate/gen_ops_prim.py +16704 -0
  1036. mindspore/ops/auto_generate/pyboost_inner_prim.py +549 -0
  1037. mindspore/ops/composite/__init__.py +71 -0
  1038. mindspore/ops/composite/base.py +1318 -0
  1039. mindspore/ops/composite/env_ops.py +41 -0
  1040. mindspore/ops/composite/math_ops.py +125 -0
  1041. mindspore/ops/composite/multitype_ops/__init__.py +77 -0
  1042. mindspore/ops/composite/multitype_ops/_compile_utils.py +1459 -0
  1043. mindspore/ops/composite/multitype_ops/_constexpr_utils.py +897 -0
  1044. mindspore/ops/composite/multitype_ops/add_impl.py +606 -0
  1045. mindspore/ops/composite/multitype_ops/bitwise_and_impl.py +56 -0
  1046. mindspore/ops/composite/multitype_ops/bitwise_or_impl.py +56 -0
  1047. mindspore/ops/composite/multitype_ops/bitwise_xor_impl.py +56 -0
  1048. mindspore/ops/composite/multitype_ops/div_impl.py +189 -0
  1049. mindspore/ops/composite/multitype_ops/equal_impl.py +335 -0
  1050. mindspore/ops/composite/multitype_ops/floordiv_impl.py +88 -0
  1051. mindspore/ops/composite/multitype_ops/getitem_impl.py +400 -0
  1052. mindspore/ops/composite/multitype_ops/greater_equal_impl.py +109 -0
  1053. mindspore/ops/composite/multitype_ops/greater_impl.py +110 -0
  1054. mindspore/ops/composite/multitype_ops/in_impl.py +196 -0
  1055. mindspore/ops/composite/multitype_ops/left_shift_impl.py +37 -0
  1056. mindspore/ops/composite/multitype_ops/less_equal_impl.py +111 -0
  1057. mindspore/ops/composite/multitype_ops/less_impl.py +112 -0
  1058. mindspore/ops/composite/multitype_ops/logic_not_impl.py +113 -0
  1059. mindspore/ops/composite/multitype_ops/logical_and_impl.py +60 -0
  1060. mindspore/ops/composite/multitype_ops/logical_or_impl.py +61 -0
  1061. mindspore/ops/composite/multitype_ops/mod_impl.py +86 -0
  1062. mindspore/ops/composite/multitype_ops/mul_impl.py +294 -0
  1063. mindspore/ops/composite/multitype_ops/negative_impl.py +79 -0
  1064. mindspore/ops/composite/multitype_ops/not_equal_impl.py +290 -0
  1065. mindspore/ops/composite/multitype_ops/not_in_impl.py +196 -0
  1066. mindspore/ops/composite/multitype_ops/ones_like_impl.py +96 -0
  1067. mindspore/ops/composite/multitype_ops/pow_impl.py +87 -0
  1068. mindspore/ops/composite/multitype_ops/right_shift_impl.py +37 -0
  1069. mindspore/ops/composite/multitype_ops/setitem_impl.py +884 -0
  1070. mindspore/ops/composite/multitype_ops/sub_impl.py +116 -0
  1071. mindspore/ops/composite/multitype_ops/uadd_impl.py +29 -0
  1072. mindspore/ops/composite/multitype_ops/zeros_like_impl.py +228 -0
  1073. mindspore/ops/deprecated.py +315 -0
  1074. mindspore/ops/function/__init__.py +782 -0
  1075. mindspore/ops/function/array_func.py +7226 -0
  1076. mindspore/ops/function/clip_func.py +384 -0
  1077. mindspore/ops/function/debug_func.py +181 -0
  1078. mindspore/ops/function/fft_func.py +44 -0
  1079. mindspore/ops/function/grad/__init__.py +34 -0
  1080. mindspore/ops/function/grad/grad_func.py +1425 -0
  1081. mindspore/ops/function/image_func.py +292 -0
  1082. mindspore/ops/function/linalg_func.py +416 -0
  1083. mindspore/ops/function/math_func.py +12228 -0
  1084. mindspore/ops/function/nn_func.py +8609 -0
  1085. mindspore/ops/function/other_func.py +115 -0
  1086. mindspore/ops/function/parameter_func.py +134 -0
  1087. mindspore/ops/function/random_func.py +1715 -0
  1088. mindspore/ops/function/reshard_func.py +104 -0
  1089. mindspore/ops/function/sparse_func.py +884 -0
  1090. mindspore/ops/function/sparse_unary_func.py +2422 -0
  1091. mindspore/ops/function/spectral_func.py +150 -0
  1092. mindspore/ops/function/vmap_func.py +117 -0
  1093. mindspore/ops/functional.py +464 -0
  1094. mindspore/ops/op_info_register.py +1572 -0
  1095. mindspore/ops/operations/__init__.py +722 -0
  1096. mindspore/ops/operations/_csr_ops.py +403 -0
  1097. mindspore/ops/operations/_custom_grad.py +181 -0
  1098. mindspore/ops/operations/_embedding_cache_ops.py +307 -0
  1099. mindspore/ops/operations/_grad_ops.py +2978 -0
  1100. mindspore/ops/operations/_infer_ops.py +19 -0
  1101. mindspore/ops/operations/_inner_ops.py +2544 -0
  1102. mindspore/ops/operations/_map_tensor_ops.py +112 -0
  1103. mindspore/ops/operations/_ms_kernel.py +601 -0
  1104. mindspore/ops/operations/_ocr_ops.py +379 -0
  1105. mindspore/ops/operations/_opaque_predicate_registry.py +41 -0
  1106. mindspore/ops/operations/_pyfunc_registry.py +58 -0
  1107. mindspore/ops/operations/_quant_ops.py +1844 -0
  1108. mindspore/ops/operations/_rl_inner_ops.py +1231 -0
  1109. mindspore/ops/operations/_scalar_ops.py +106 -0
  1110. mindspore/ops/operations/_sequence_ops.py +1155 -0
  1111. mindspore/ops/operations/_sparse_grad_ops.py +56 -0
  1112. mindspore/ops/operations/_tensor_array.py +359 -0
  1113. mindspore/ops/operations/_thor_ops.py +807 -0
  1114. mindspore/ops/operations/array_ops.py +6124 -0
  1115. mindspore/ops/operations/comm_ops.py +1985 -0
  1116. mindspore/ops/operations/control_ops.py +127 -0
  1117. mindspore/ops/operations/custom_ops.py +1129 -0
  1118. mindspore/ops/operations/debug_ops.py +678 -0
  1119. mindspore/ops/operations/image_ops.py +1041 -0
  1120. mindspore/ops/operations/inner_ops.py +697 -0
  1121. mindspore/ops/operations/linalg_ops.py +95 -0
  1122. mindspore/ops/operations/manually_defined/__init__.py +24 -0
  1123. mindspore/ops/operations/manually_defined/_inner.py +73 -0
  1124. mindspore/ops/operations/manually_defined/ops_def.py +2271 -0
  1125. mindspore/ops/operations/math_ops.py +5095 -0
  1126. mindspore/ops/operations/nn_ops.py +9575 -0
  1127. mindspore/ops/operations/other_ops.py +874 -0
  1128. mindspore/ops/operations/random_ops.py +1288 -0
  1129. mindspore/ops/operations/reshard_ops.py +53 -0
  1130. mindspore/ops/operations/rl_ops.py +288 -0
  1131. mindspore/ops/operations/sparse_ops.py +2753 -0
  1132. mindspore/ops/operations/spectral_ops.py +111 -0
  1133. mindspore/ops/primitive.py +1046 -0
  1134. mindspore/ops/signature.py +54 -0
  1135. mindspore/ops/vm_impl_registry.py +91 -0
  1136. mindspore/ops_generate/__init__.py +27 -0
  1137. mindspore/ops_generate/arg_dtype_cast.py +252 -0
  1138. mindspore/ops_generate/arg_handler.py +197 -0
  1139. mindspore/ops_generate/gen_aclnn_implement.py +263 -0
  1140. mindspore/ops_generate/gen_constants.py +36 -0
  1141. mindspore/ops_generate/gen_ops.py +1099 -0
  1142. mindspore/ops_generate/gen_ops_inner_prim.py +131 -0
  1143. mindspore/ops_generate/gen_pyboost_func.py +1052 -0
  1144. mindspore/ops_generate/gen_utils.py +209 -0
  1145. mindspore/ops_generate/op_proto.py +145 -0
  1146. mindspore/ops_generate/pyboost_utils.py +367 -0
  1147. mindspore/ops_generate/template.py +261 -0
  1148. mindspore/parallel/__init__.py +30 -0
  1149. mindspore/parallel/_auto_parallel_context.py +1486 -0
  1150. mindspore/parallel/_cell_wrapper.py +174 -0
  1151. mindspore/parallel/_cost_model_context.py +700 -0
  1152. mindspore/parallel/_dp_allreduce_fusion.py +159 -0
  1153. mindspore/parallel/_offload_context.py +275 -0
  1154. mindspore/parallel/_parallel_serialization.py +561 -0
  1155. mindspore/parallel/_ps_context.py +242 -0
  1156. mindspore/parallel/_recovery_context.py +110 -0
  1157. mindspore/parallel/_tensor.py +730 -0
  1158. mindspore/parallel/_transformer/__init__.py +35 -0
  1159. mindspore/parallel/_transformer/layers.py +765 -0
  1160. mindspore/parallel/_transformer/loss.py +251 -0
  1161. mindspore/parallel/_transformer/moe.py +693 -0
  1162. mindspore/parallel/_transformer/op_parallel_config.py +222 -0
  1163. mindspore/parallel/_transformer/transformer.py +3119 -0
  1164. mindspore/parallel/_utils.py +612 -0
  1165. mindspore/parallel/algo_parameter_config.py +400 -0
  1166. mindspore/parallel/checkpoint_transform.py +650 -0
  1167. mindspore/parallel/cluster/__init__.py +15 -0
  1168. mindspore/parallel/cluster/process_entity/__init__.py +18 -0
  1169. mindspore/parallel/cluster/process_entity/_api.py +352 -0
  1170. mindspore/parallel/cluster/process_entity/_utils.py +101 -0
  1171. mindspore/parallel/cluster/run.py +136 -0
  1172. mindspore/parallel/mpi/__init__.py +14 -0
  1173. mindspore/parallel/mpi/_mpi_config.py +116 -0
  1174. mindspore/parallel/parameter_broadcast.py +151 -0
  1175. mindspore/parallel/shard.py +481 -0
  1176. mindspore/parallel/transform_safetensors.py +993 -0
  1177. mindspore/profiler/__init__.py +28 -0
  1178. mindspore/profiler/common/__init__.py +14 -0
  1179. mindspore/profiler/common/constant.py +29 -0
  1180. mindspore/profiler/common/exceptions/__init__.py +14 -0
  1181. mindspore/profiler/common/exceptions/error_code.py +83 -0
  1182. mindspore/profiler/common/exceptions/exceptions.py +286 -0
  1183. mindspore/profiler/common/process_pool.py +41 -0
  1184. mindspore/profiler/common/registry.py +47 -0
  1185. mindspore/profiler/common/singleton.py +28 -0
  1186. mindspore/profiler/common/struct_type.py +118 -0
  1187. mindspore/profiler/common/util.py +472 -0
  1188. mindspore/profiler/common/validator/__init__.py +14 -0
  1189. mindspore/profiler/common/validator/validate_path.py +84 -0
  1190. mindspore/profiler/dynamic_profiler.py +694 -0
  1191. mindspore/profiler/envprofiling.py +254 -0
  1192. mindspore/profiler/parser/__init__.py +14 -0
  1193. mindspore/profiler/parser/aicpu_data_parser.py +272 -0
  1194. mindspore/profiler/parser/ascend_analysis/__init__.py +14 -0
  1195. mindspore/profiler/parser/ascend_analysis/constant.py +71 -0
  1196. mindspore/profiler/parser/ascend_analysis/file_manager.py +180 -0
  1197. mindspore/profiler/parser/ascend_analysis/function_event.py +185 -0
  1198. mindspore/profiler/parser/ascend_analysis/fwk_cann_parser.py +136 -0
  1199. mindspore/profiler/parser/ascend_analysis/fwk_file_parser.py +131 -0
  1200. mindspore/profiler/parser/ascend_analysis/msprof_timeline_parser.py +104 -0
  1201. mindspore/profiler/parser/ascend_analysis/path_manager.py +313 -0
  1202. mindspore/profiler/parser/ascend_analysis/profiler_info_parser.py +123 -0
  1203. mindspore/profiler/parser/ascend_analysis/tlv_decoder.py +86 -0
  1204. mindspore/profiler/parser/ascend_analysis/trace_event_manager.py +75 -0
  1205. mindspore/profiler/parser/ascend_cluster_generator.py +116 -0
  1206. mindspore/profiler/parser/ascend_communicate_generator.py +314 -0
  1207. mindspore/profiler/parser/ascend_flops_generator.py +116 -0
  1208. mindspore/profiler/parser/ascend_fpbp_generator.py +82 -0
  1209. mindspore/profiler/parser/ascend_hccl_generator.py +271 -0
  1210. mindspore/profiler/parser/ascend_integrate_generator.py +42 -0
  1211. mindspore/profiler/parser/ascend_memory_generator.py +185 -0
  1212. mindspore/profiler/parser/ascend_msprof_exporter.py +282 -0
  1213. mindspore/profiler/parser/ascend_msprof_generator.py +187 -0
  1214. mindspore/profiler/parser/ascend_op_generator.py +334 -0
  1215. mindspore/profiler/parser/ascend_steptrace_generator.py +94 -0
  1216. mindspore/profiler/parser/ascend_timeline_generator.py +545 -0
  1217. mindspore/profiler/parser/base_timeline_generator.py +483 -0
  1218. mindspore/profiler/parser/container.py +229 -0
  1219. mindspore/profiler/parser/cpu_gpu_timeline_generator.py +697 -0
  1220. mindspore/profiler/parser/flops_parser.py +531 -0
  1221. mindspore/profiler/parser/framework_enum.py +111 -0
  1222. mindspore/profiler/parser/framework_parser.py +464 -0
  1223. mindspore/profiler/parser/framework_struct.py +61 -0
  1224. mindspore/profiler/parser/gpu_analysis/__init__.py +14 -0
  1225. mindspore/profiler/parser/gpu_analysis/function_event.py +44 -0
  1226. mindspore/profiler/parser/gpu_analysis/fwk_file_parser.py +89 -0
  1227. mindspore/profiler/parser/gpu_analysis/profiler_info_parser.py +72 -0
  1228. mindspore/profiler/parser/hccl_parser.py +573 -0
  1229. mindspore/profiler/parser/hwts_log_parser.py +122 -0
  1230. mindspore/profiler/parser/integrator.py +526 -0
  1231. mindspore/profiler/parser/memory_usage_parser.py +277 -0
  1232. mindspore/profiler/parser/minddata_analyzer.py +800 -0
  1233. mindspore/profiler/parser/minddata_parser.py +186 -0
  1234. mindspore/profiler/parser/minddata_pipeline_parser.py +299 -0
  1235. mindspore/profiler/parser/op_intermediate_parser.py +149 -0
  1236. mindspore/profiler/parser/optime_parser.py +250 -0
  1237. mindspore/profiler/parser/profiler_info.py +213 -0
  1238. mindspore/profiler/parser/step_trace_parser.py +666 -0
  1239. mindspore/profiler/profiler.py +153 -0
  1240. mindspore/profiler/profiling.py +1922 -0
  1241. mindspore/rewrite/__init__.py +28 -0
  1242. mindspore/rewrite/api/__init__.py +17 -0
  1243. mindspore/rewrite/api/node.py +519 -0
  1244. mindspore/rewrite/api/node_type.py +53 -0
  1245. mindspore/rewrite/api/pattern_engine.py +490 -0
  1246. mindspore/rewrite/api/scoped_value.py +181 -0
  1247. mindspore/rewrite/api/symbol_tree.py +497 -0
  1248. mindspore/rewrite/ast_helpers/__init__.py +25 -0
  1249. mindspore/rewrite/ast_helpers/ast_converter.py +143 -0
  1250. mindspore/rewrite/ast_helpers/ast_finder.py +404 -0
  1251. mindspore/rewrite/ast_helpers/ast_flattener.py +268 -0
  1252. mindspore/rewrite/ast_helpers/ast_modifier.py +605 -0
  1253. mindspore/rewrite/ast_helpers/ast_replacer.py +79 -0
  1254. mindspore/rewrite/common/__init__.py +19 -0
  1255. mindspore/rewrite/common/config.py +24 -0
  1256. mindspore/rewrite/common/error_log.py +39 -0
  1257. mindspore/rewrite/common/event.py +28 -0
  1258. mindspore/rewrite/common/namer.py +271 -0
  1259. mindspore/rewrite/common/namespace.py +118 -0
  1260. mindspore/rewrite/common/observable.py +44 -0
  1261. mindspore/rewrite/common/observer.py +54 -0
  1262. mindspore/rewrite/node/__init__.py +22 -0
  1263. mindspore/rewrite/node/call_function.py +95 -0
  1264. mindspore/rewrite/node/cell_container.py +139 -0
  1265. mindspore/rewrite/node/control_flow.py +113 -0
  1266. mindspore/rewrite/node/node.py +1428 -0
  1267. mindspore/rewrite/node/node_manager.py +283 -0
  1268. mindspore/rewrite/node/node_topological_manager.py +223 -0
  1269. mindspore/rewrite/parsers/__init__.py +29 -0
  1270. mindspore/rewrite/parsers/arguments_parser.py +63 -0
  1271. mindspore/rewrite/parsers/assign_parser.py +852 -0
  1272. mindspore/rewrite/parsers/attribute_parser.py +57 -0
  1273. mindspore/rewrite/parsers/class_def_parser.py +289 -0
  1274. mindspore/rewrite/parsers/constant_parser.py +104 -0
  1275. mindspore/rewrite/parsers/container_parser.py +88 -0
  1276. mindspore/rewrite/parsers/expr_parser.py +55 -0
  1277. mindspore/rewrite/parsers/for_parser.py +61 -0
  1278. mindspore/rewrite/parsers/function_def_parser.py +84 -0
  1279. mindspore/rewrite/parsers/if_parser.py +85 -0
  1280. mindspore/rewrite/parsers/module_parser.py +117 -0
  1281. mindspore/rewrite/parsers/parser.py +43 -0
  1282. mindspore/rewrite/parsers/parser_register.py +86 -0
  1283. mindspore/rewrite/parsers/return_parser.py +37 -0
  1284. mindspore/rewrite/parsers/while_parser.py +59 -0
  1285. mindspore/rewrite/sparsify/__init__.py +0 -0
  1286. mindspore/rewrite/sparsify/sparse_transformer.py +457 -0
  1287. mindspore/rewrite/sparsify/sparsify.py +112 -0
  1288. mindspore/rewrite/sparsify/utils.py +179 -0
  1289. mindspore/rewrite/symbol_tree/__init__.py +20 -0
  1290. mindspore/rewrite/symbol_tree/symbol_tree.py +1819 -0
  1291. mindspore/rewrite/symbol_tree/symbol_tree_builder.py +76 -0
  1292. mindspore/rewrite/symbol_tree/symbol_tree_dumper.py +142 -0
  1293. mindspore/run_check/__init__.py +20 -0
  1294. mindspore/run_check/_check_version.py +507 -0
  1295. mindspore/run_check/run_check.py +66 -0
  1296. mindspore/safeguard/__init__.py +18 -0
  1297. mindspore/safeguard/rewrite_obfuscation.py +875 -0
  1298. mindspore/scipy/__init__.py +18 -0
  1299. mindspore/scipy/fft.py +264 -0
  1300. mindspore/scipy/linalg.py +919 -0
  1301. mindspore/scipy/ops.py +165 -0
  1302. mindspore/scipy/ops_grad.py +115 -0
  1303. mindspore/scipy/ops_wrapper.py +74 -0
  1304. mindspore/scipy/optimize/__init__.py +20 -0
  1305. mindspore/scipy/optimize/_bfgs.py +230 -0
  1306. mindspore/scipy/optimize/_lagrange.py +201 -0
  1307. mindspore/scipy/optimize/_lbfgs.py +146 -0
  1308. mindspore/scipy/optimize/gradient_optimization_algorithm.py +168 -0
  1309. mindspore/scipy/optimize/line_search.py +370 -0
  1310. mindspore/scipy/optimize/linear_sum_assignment.py +78 -0
  1311. mindspore/scipy/optimize/minimize.py +200 -0
  1312. mindspore/scipy/utils.py +156 -0
  1313. mindspore/scipy/utils_const.py +246 -0
  1314. mindspore/train/__init__.py +48 -0
  1315. mindspore/train/_utils.py +465 -0
  1316. mindspore/train/amp.py +935 -0
  1317. mindspore/train/anf_ir_pb2.py +1517 -0
  1318. mindspore/train/callback/__init__.py +44 -0
  1319. mindspore/train/callback/_backup_and_restore.py +117 -0
  1320. mindspore/train/callback/_callback.py +613 -0
  1321. mindspore/train/callback/_checkpoint.py +814 -0
  1322. mindspore/train/callback/_cluster_monitor.py +201 -0
  1323. mindspore/train/callback/_dataset_graph.py +150 -0
  1324. mindspore/train/callback/_early_stop.py +239 -0
  1325. mindspore/train/callback/_flops_collector.py +239 -0
  1326. mindspore/train/callback/_history.py +92 -0
  1327. mindspore/train/callback/_lambda_callback.py +80 -0
  1328. mindspore/train/callback/_landscape.py +1049 -0
  1329. mindspore/train/callback/_loss_monitor.py +107 -0
  1330. mindspore/train/callback/_lr_scheduler_callback.py +76 -0
  1331. mindspore/train/callback/_on_request_exit.py +298 -0
  1332. mindspore/train/callback/_reduce_lr_on_plateau.py +226 -0
  1333. mindspore/train/callback/_summary_collector.py +1184 -0
  1334. mindspore/train/callback/_tft_register.py +352 -0
  1335. mindspore/train/callback/_time_monitor.py +141 -0
  1336. mindspore/train/checkpoint_pb2.py +233 -0
  1337. mindspore/train/data_sink.py +219 -0
  1338. mindspore/train/dataset_helper.py +692 -0
  1339. mindspore/train/lineage_pb2.py +1260 -0
  1340. mindspore/train/loss_scale_manager.py +213 -0
  1341. mindspore/train/memory_profiling_pb2.py +298 -0
  1342. mindspore/train/metrics/__init__.py +175 -0
  1343. mindspore/train/metrics/accuracy.py +133 -0
  1344. mindspore/train/metrics/auc.py +129 -0
  1345. mindspore/train/metrics/bleu_score.py +170 -0
  1346. mindspore/train/metrics/confusion_matrix.py +700 -0
  1347. mindspore/train/metrics/cosine_similarity.py +109 -0
  1348. mindspore/train/metrics/dice.py +116 -0
  1349. mindspore/train/metrics/error.py +175 -0
  1350. mindspore/train/metrics/fbeta.py +167 -0
  1351. mindspore/train/metrics/hausdorff_distance.py +333 -0
  1352. mindspore/train/metrics/loss.py +97 -0
  1353. mindspore/train/metrics/mean_surface_distance.py +189 -0
  1354. mindspore/train/metrics/metric.py +373 -0
  1355. mindspore/train/metrics/occlusion_sensitivity.py +225 -0
  1356. mindspore/train/metrics/perplexity.py +133 -0
  1357. mindspore/train/metrics/precision.py +160 -0
  1358. mindspore/train/metrics/recall.py +159 -0
  1359. mindspore/train/metrics/roc.py +223 -0
  1360. mindspore/train/metrics/root_mean_square_surface_distance.py +191 -0
  1361. mindspore/train/metrics/topk.py +167 -0
  1362. mindspore/train/mind_ir_pb2.py +1908 -0
  1363. mindspore/train/model.py +2252 -0
  1364. mindspore/train/node_strategy_pb2.py +653 -0
  1365. mindspore/train/print_pb2.py +184 -0
  1366. mindspore/train/profiling_parallel_pb2.py +151 -0
  1367. mindspore/train/serialization.py +3325 -0
  1368. mindspore/train/summary/__init__.py +23 -0
  1369. mindspore/train/summary/_lineage_adapter.py +41 -0
  1370. mindspore/train/summary/_summary_adapter.py +496 -0
  1371. mindspore/train/summary/_writer_pool.py +207 -0
  1372. mindspore/train/summary/enums.py +56 -0
  1373. mindspore/train/summary/summary_record.py +581 -0
  1374. mindspore/train/summary/writer.py +167 -0
  1375. mindspore/train/summary_pb2.py +1165 -0
  1376. mindspore/train/train_thor/__init__.py +20 -0
  1377. mindspore/train/train_thor/convert_utils.py +268 -0
  1378. mindspore/train/train_thor/dataset_helper.py +192 -0
  1379. mindspore/train/train_thor/model_thor.py +257 -0
  1380. mindspore/utils/__init__.py +21 -0
  1381. mindspore/utils/utils.py +60 -0
  1382. mindspore/version.py +1 -0
  1383. mindspore-2.4.0.dist-info/METADATA +352 -0
  1384. mindspore-2.4.0.dist-info/RECORD +1387 -0
  1385. mindspore-2.4.0.dist-info/WHEEL +5 -0
  1386. mindspore-2.4.0.dist-info/entry_points.txt +3 -0
  1387. mindspore-2.4.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,2924 @@
1
+ # Copyright 2020-2022 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
+ """loss"""
16
+ from __future__ import absolute_import, division
17
+ import math
18
+
19
+ import mindspore
20
+ import mindspore.common.dtype as mstype
21
+ import mindspore.ops as ops
22
+ from mindspore import log
23
+ from mindspore.common.tensor import Tensor
24
+ from mindspore.common.parameter import Parameter
25
+ from mindspore.ops import operations as P
26
+ from mindspore.ops.operations import _inner_ops as inner
27
+ from mindspore.ops.operations.nn_ops import MultiMarginLoss as MultiMarginLossOp
28
+ from mindspore.ops.operations.nn_ops import MultilabelMarginLoss as MultilabelMarginLossOp
29
+ from mindspore.ops import functional as F
30
+ from mindspore import nn
31
+ from mindspore.ops.primitive import constexpr, _primexpr
32
+ from mindspore.nn.cell import Cell
33
+ from mindspore.nn.layer.activation import get_activation
34
+ from mindspore import _checkparam as validator
35
+ from mindspore import context
36
+ from mindspore.ops.auto_generate import l1_loss_ext_op
37
+
38
+
39
+ class LossBase(Cell):
40
+ """
41
+ Base class for other losses.
42
+
43
+ Other losses derived from this should implement their own `construct` and use method `self.get_loss`
44
+ to apply reduction to loss values.
45
+
46
+ Args:
47
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
48
+ ``'sum'`` . Default: ``'mean'`` .
49
+
50
+ - ``'none'``: no reduction will be applied.
51
+ - ``'mean'``: compute and return the (weighted) mean of elements in the output.
52
+ - ``'sum'``: the output elements will be summed.
53
+
54
+ Raises:
55
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
56
+
57
+ Supported Platforms:
58
+ ``Ascend`` ``GPU`` ``CPU``
59
+
60
+ Examples:
61
+ >>> import mindspore
62
+ >>> from mindspore import ops, Tensor, nn
63
+ >>> import numpy as np
64
+ >>>
65
+ >>> class Net(nn.LossBase):
66
+ ... def __init__(self, reduction='mean'):
67
+ ... super(Net, self).__init__(reduction)
68
+ ... self.abs = ops.Abs()
69
+ ...
70
+ ... def construct(self, logits, labels):
71
+ ... x = self.abs(logits - labels)
72
+ ... output = self.get_loss(x)
73
+ ... axis = self.get_axis(x)
74
+ ... return output, axis
75
+ >>> net = Net()
76
+ >>> # Case 1: logits.shape = labels.shape = (3,)
77
+ >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
78
+ >>> labels = Tensor(np.array([1, 2, 2]), mindspore.float32)
79
+ >>> output, axis = net(logits, labels)
80
+ >>> print(output)
81
+ 0.33333334
82
+ >>> print(axis)
83
+ (0,)
84
+ >>> # Case 2: logits.shape = labels.shape = (3, 3)
85
+ >>> logits = Tensor(np.array([[1, 2, 3],[1, 2, 3],[1, 2, 3]]), mindspore.float32)
86
+ >>> labels = Tensor(np.array([[1, 2, 2],[1, 2, 3],[1, 2, 3]]), mindspore.float32)
87
+ >>> output, axis = net(logits, labels)
88
+ >>> print(output)
89
+ 0.11111111
90
+ >>> print(axis)
91
+ (0, 1)
92
+ """
93
+
94
+ def __init__(self, reduction='mean'):
95
+ """Initialize Loss."""
96
+ super(LossBase, self).__init__()
97
+
98
+ if reduction not in ('mean', 'sum', 'none'):
99
+ raise ValueError(f"For '{self.cls_name}', the 'reduction' must be in ['mean', 'sum', 'none'], "
100
+ f"but got {reduction}.")
101
+
102
+ self.average = True
103
+ self.reduce = True
104
+ if reduction == 'sum':
105
+ self.average = False
106
+ if reduction == 'none':
107
+ self.reduce = False
108
+
109
+ self.reduce_mean = P.ReduceMean()
110
+ self.reduce_sum = P.ReduceSum()
111
+ self.mul = P.Mul()
112
+ self.cast = P.Cast()
113
+
114
+ def get_axis(self, x):
115
+ """
116
+ Get a range of axis for input.
117
+
118
+ Args:
119
+ x (Tensor): Tensor of any shape.
120
+ """
121
+ shape = F.shape(x)
122
+ length = F.tuple_len(shape)
123
+ perm = F.make_range(0, length)
124
+ return perm
125
+
126
+ def get_loss(self, x, weights=1.0):
127
+ """
128
+ Computes the weighted loss.
129
+
130
+ Args:
131
+ x (Tensor): Tensor of shape :math:`(N, *)` where :math:`*` means, any number of
132
+ additional dimensions.
133
+ weights (Union[float, Tensor]): Optional `Tensor` whose rank is either 0, or the same rank as inputs,
134
+ and must be broadcastable to inputs (i.e., all dimensions must be either `1`,
135
+ or the same as the corresponding inputs dimension). Default: ``1.0`` .
136
+
137
+ Returns:
138
+ Return the weighted loss.
139
+ """
140
+ input_dtype = x.dtype
141
+ x = self.cast(x, mstype.float32)
142
+ weights = self.cast(weights, mstype.float32)
143
+ x = self.mul(weights, x)
144
+ if self.reduce and self.average:
145
+ x = self.reduce_mean(x, self.get_axis(x))
146
+ if self.reduce and not self.average:
147
+ x = self.reduce_sum(x, self.get_axis(x))
148
+ x = self.cast(x, input_dtype)
149
+ return x
150
+
151
+ def construct(self, logits, labels):
152
+ raise NotImplementedError
153
+
154
+
155
+ class _Loss(LossBase):
156
+ """
157
+ Base class for other losses.
158
+ """
159
+
160
+ def __init__(self, reduction='mean'):
161
+ """Initialize _Loss."""
162
+ log.warning("'_Loss' is deprecated from version 1.3 and "
163
+ "will be removed in a future version, use 'LossBase' instead.")
164
+ super(_Loss, self).__init__(reduction)
165
+
166
+ def construct(self, logits, labels):
167
+ raise NotImplementedError
168
+
169
+
170
+ @constexpr(check=False)
171
+ def _check_is_tensor(param_name, input_data, cls_name):
172
+ """Internal function, used to check whether the input data is Tensor."""
173
+ if input_data is not None and not isinstance(F.typeof(input_data), mstype.TensorType):
174
+ raise TypeError(f"For '{cls_name}', the '{param_name}' must be '{mstype.TensorType}', "
175
+ f"but got '{F.typeof(input_data)}'")
176
+
177
+
178
+ class L1Loss(LossBase):
179
+ r"""
180
+ L1Loss is used to calculate the mean absolute error between the predicted value and the target value.
181
+
182
+ Assuming that the :math:`x` and :math:`y` are 1-D Tensor, length :math:`N`, then calculate the loss of :math:`x` and
183
+ :math:`y` without dimensionality reduction (the reduction parameter is set to "none"). The formula is as follows:
184
+
185
+ .. math::
186
+ \ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad \text{with } l_n = \left| x_n - y_n \right|,
187
+
188
+ where :math:`N` is the batch size. If `reduction` is not ``'none'``, then:
189
+
190
+ .. math::
191
+ \ell(x, y) =
192
+ \begin{cases}
193
+ \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\
194
+ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.}
195
+ \end{cases}
196
+
197
+ Args:
198
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
199
+ ``'sum'`` . Default: ``'mean'`` .
200
+
201
+ - ``'none'``: no reduction will be applied.
202
+ - ``'mean'``: compute and return the mean of elements in the output.
203
+ - ``'sum'``: the output elements will be summed.
204
+
205
+ Inputs:
206
+ - **logits** (Tensor) - Predicted value, Tensor of any dimension.
207
+ - **labels** (Tensor) - Target value, same shape as the `logits` in common cases.
208
+ However, it supports the shape of `logits` is different from the shape of `labels`
209
+ and they should be broadcasted to each other.
210
+
211
+ Outputs:
212
+ Tensor, data type is float.
213
+
214
+ Raises:
215
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
216
+ ValueError: If `logits` and `labels` have different shapes and cannot be broadcasted to each other.
217
+
218
+ Supported Platforms:
219
+ ``Ascend`` ``GPU`` ``CPU``
220
+
221
+ Examples:
222
+ >>> import mindspore
223
+ >>> from mindspore import Tensor, nn
224
+ >>> import numpy as np
225
+ >>> # Case 1: logits.shape = labels.shape = (3,)
226
+ >>> loss = nn.L1Loss()
227
+ >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
228
+ >>> labels = Tensor(np.array([1, 2, 2]), mindspore.float32)
229
+ >>> output = loss(logits, labels)
230
+ >>> print(output)
231
+ 0.33333334
232
+ >>> # Case 2: logits.shape = (3,), labels.shape = (2, 3)
233
+ >>> loss = nn.L1Loss(reduction='none')
234
+ >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
235
+ >>> labels = Tensor(np.array([[1, 1, 1], [1, 2, 2]]), mindspore.float32)
236
+ >>> output = loss(logits, labels)
237
+ >>> print(output)
238
+ [[0. 1. 2.]
239
+ [0. 0. 1.]]
240
+ """
241
+
242
+ def __init__(self, reduction='mean'):
243
+ """Initialize L1Loss."""
244
+ super(L1Loss, self).__init__(reduction)
245
+ self.reduction = reduction
246
+
247
+ def construct(self, logits, labels):
248
+ return F.l1_loss(logits, labels, self.reduction)
249
+
250
+
251
+ class L1LossExt(LossBase):
252
+ r"""
253
+ L1Loss is used to calculate the mean absolute error between the predicted value and the target value.
254
+
255
+ Assuming that the :math:`x` and :math:`y` are 1-D Tensor, length :math:`N`, then calculate the loss of :math:`x` and
256
+ :math:`y` without dimensionality reduction (the reduction parameter is set to ``'none'`` ). The formula is as
257
+ follows:
258
+
259
+ .. math::
260
+ \ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad \text{with } l_n = \left| x_n - y_n \right|,
261
+
262
+ where :math:`N` is the batch size. If `reduction` is not ``'none'`` , then:
263
+
264
+ .. math::
265
+ \ell(x, y) =
266
+ \begin{cases}
267
+ \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\
268
+ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.}
269
+ \end{cases}
270
+
271
+ Args:
272
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
273
+ ``'sum'`` . Default: ``'mean'`` .
274
+
275
+ - ``'none'``: no reduction will be applied.
276
+ - ``'mean'``: compute and return the mean of elements in the output.
277
+ - ``'sum'``: the output elements will be summed.
278
+
279
+ Inputs:
280
+ - **logits** (Tensor) - Predicted value, Tensor of any dimension.
281
+ - **labels** (Tensor) - Target value, same shape as the `logits` in common cases.
282
+ However, it supports the shape of `logits` is different from the shape of `labels`
283
+ and they should be broadcasted to each other.
284
+
285
+ Outputs:
286
+ Tensor, data type is float.
287
+
288
+ Raises:
289
+ ValueError: If `reduction` is not one of ``'none'`` , ``'mean'`` or ``'sum'`` .
290
+ ValueError: If `logits` and `labels` have different shapes and cannot be broadcasted to each other.
291
+
292
+ Supported Platforms:
293
+ ``Ascend``
294
+
295
+ Examples:
296
+ >>> import mindspore
297
+ >>> from mindspore import Tensor, nn
298
+ >>> import numpy as np
299
+ >>> # Case 1: logits.shape = labels.shape = (3,)
300
+ >>> loss = nn.L1LossExt()
301
+ >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
302
+ >>> labels = Tensor(np.array([1, 2, 2]), mindspore.float32)
303
+ >>> output = loss(logits, labels)
304
+ >>> print(output)
305
+ 0.33333334
306
+ >>> # Case 2: logits.shape = (3,), labels.shape = (2, 3)
307
+ >>> loss = nn.L1LossExt(reduction='none')
308
+ >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
309
+ >>> labels = Tensor(np.array([[1, 1, 1], [1, 2, 2]]), mindspore.float32)
310
+ >>> output = loss(logits, labels)
311
+ >>> print(output)
312
+ [[0. 1. 2.]
313
+ [0. 0. 1.]]
314
+ """
315
+
316
+ def __init__(self, reduction='mean'):
317
+ """Initialize L1LossExt."""
318
+ super(L1LossExt, self).__init__(reduction)
319
+ self.reduction = reduction
320
+
321
+ def construct(self, logits, labels):
322
+ return l1_loss_ext_op(logits, labels, self.reduction)
323
+
324
+
325
+ class MSELoss(LossBase):
326
+ r"""
327
+ Calculates the mean squared error between the predicted value and the label value.
328
+
329
+ For simplicity, let :math:`x` and :math:`y` be 1-dimensional Tensor with length :math:`N`,
330
+ the unreduced loss (i.e. with argument reduction set to 'none') of :math:`x` and :math:`y` is given as:
331
+
332
+ .. math::
333
+ \ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad \text{with} \quad l_n = (x_n - y_n)^2.
334
+
335
+ where :math:`N` is the batch size. If `reduction` is not ``'none'``, then:
336
+
337
+ .. math::
338
+ \ell(x, y) =
339
+ \begin{cases}
340
+ \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\
341
+ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.}
342
+ \end{cases}
343
+
344
+ Args:
345
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
346
+ ``'sum'`` . Default: ``'mean'`` .
347
+
348
+ - ``'none'``: no reduction will be applied.
349
+ - ``'mean'``: compute and return the mean of elements in the output.
350
+ - ``'sum'``: the output elements will be summed.
351
+
352
+ Inputs:
353
+ - **logits** (Tensor) - The predicted value of the input. Tensor of any dimension.
354
+ - **labels** (Tensor) - The input label. Tensor of any dimension, same shape as the `logits` in common cases.
355
+ However, it supports the shape of `logits` is different from the shape of `labels`
356
+ and they should be broadcasted to each other.
357
+
358
+ Outputs:
359
+ Tensor, loss of type float, the shape is zero if `reduction` is ``'mean'`` or ``'sum'`` ,
360
+ while the shape of output is the broadcasted shape if `reduction` is 'none'.
361
+
362
+ Raises:
363
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'`` or ``'sum'``.
364
+ ValueError: If `logits` and `labels` have different shapes and cannot be broadcasted.
365
+ TypeError: if `logits` and `labels` have different data types.
366
+
367
+ Supported Platforms:
368
+ ``Ascend`` ``GPU`` ``CPU``
369
+
370
+ Examples:
371
+ >>> import mindspore
372
+ >>> from mindspore import Tensor, nn
373
+ >>> import numpy as np
374
+ >>> # Case 1: logits.shape = labels.shape = (3,)
375
+ >>> loss = nn.MSELoss()
376
+ >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
377
+ >>> labels = Tensor(np.array([1, 1, 1]), mindspore.float32)
378
+ >>> output = loss(logits, labels)
379
+ >>> print(output)
380
+ 1.6666667
381
+ >>> # Case 2: logits.shape = (3,), labels.shape = (2, 3)
382
+ >>> loss = nn.MSELoss(reduction='none')
383
+ >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
384
+ >>> labels = Tensor(np.array([[1, 1, 1], [1, 2, 2]]), mindspore.float32)
385
+ >>> output = loss(logits, labels)
386
+ >>> print(output)
387
+ [[0. 1. 4.]
388
+ [0. 0. 1.]]
389
+ """
390
+
391
+ def construct(self, logits, labels):
392
+ _check_is_tensor('logits', logits, self.cls_name)
393
+ _check_is_tensor('labels', labels, self.cls_name)
394
+ x = F.square(logits - labels)
395
+ return self.get_loss(x)
396
+
397
+
398
+ @constexpr
399
+ def _check_rmseloss_dtype(param_dtype, not_supported_dtype, cls_name):
400
+ """Check RMSELoss not supported data type"""
401
+ if param_dtype in not_supported_dtype:
402
+ raise TypeError(f"For '{cls_name}', the parameters data type must not be in {not_supported_dtype}, "
403
+ f"but got mindspore.{str(param_dtype).lower()}.")
404
+
405
+
406
+ class RMSELoss(LossBase):
407
+ r"""
408
+ RMSELoss creates a criterion to measure the root mean square error between :math:`x` and :math:`y`
409
+ element-wise, where :math:`x` is the input and :math:`y` is the labels.
410
+
411
+ For simplicity, let :math:`x` and :math:`y` be 1-dimensional Tensor with length :math:`N`,
412
+ the loss of :math:`x` and :math:`y` is given as:
413
+
414
+ .. math::
415
+ loss = \sqrt{\frac{1}{N}\sum_{i=1}^{N}{(x_i-y_i)^2}}
416
+
417
+ Inputs:
418
+ - **logits** (Tensor) - Tensor of shape :math:`(N, *)` where :math:`*` means, any number of
419
+ additional dimensions.
420
+ - **labels** (Tensor) - Tensor of shape :math:`(N, *)`, same shape as the `logits` in common cases.
421
+ However, it supports the shape of `logits` is different from the shape of `labels`
422
+ and they should be broadcasted to each other.
423
+
424
+ Outputs:
425
+ Tensor, weighted loss float tensor and its shape is :math:`()`.
426
+
427
+ Supported Platforms:
428
+ ``Ascend`` ``GPU`` ``CPU``
429
+
430
+ Examples:
431
+ >>> import mindspore
432
+ >>> from mindspore import Tensor, nn
433
+ >>> import numpy as np
434
+ >>> # Case 1: logits.shape = labels.shape = (3,)
435
+ >>> loss = nn.RMSELoss()
436
+ >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
437
+ >>> labels = Tensor(np.array([1, 2, 2]), mindspore.float32)
438
+ >>> output = loss(logits, labels)
439
+ >>> print(output)
440
+ 0.57735026
441
+ >>> # Case 2: logits.shape = (3,), labels.shape = (2, 3)
442
+ >>> loss = nn.RMSELoss()
443
+ >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
444
+ >>> labels = Tensor(np.array([[1, 1, 1], [1, 2, 2]]), mindspore.float32)
445
+ >>> output = loss(logits, labels)
446
+ >>> print(output)
447
+ 1.0
448
+ """
449
+
450
+ def __init__(self):
451
+ """Initialize RMSELoss."""
452
+ super(RMSELoss, self).__init__()
453
+ self.dtype = P.DType()
454
+ self.MSELoss = MSELoss()
455
+
456
+ def construct(self, logits, label):
457
+ logits_dtype = self.dtype(logits)
458
+ label_dtype = self.dtype(label)
459
+ not_supported_dtype = [mstype.uint8, mstype.uint16, mstype.uint32, mstype.uint64]
460
+ _check_rmseloss_dtype(logits_dtype, not_supported_dtype, 'RMSELoss')
461
+ _check_rmseloss_dtype(label_dtype, not_supported_dtype, "RMSELoss")
462
+
463
+ rmse_loss = F.sqrt(self.MSELoss(logits, label))
464
+
465
+ return rmse_loss
466
+
467
+
468
+ class MAELoss(LossBase):
469
+ r"""
470
+ MAELoss creates a criterion to measure the average absolute error between :math:`x` and :math:`y`
471
+ element-wise, where :math:`x` is the input and :math:`y` is the labels.
472
+
473
+ For simplicity, let :math:`x` and :math:`y` be 1-dimensional Tensor with length :math:`N`,
474
+ the unreduced loss (i.e. with argument reduction set to 'none') of :math:`x` and :math:`y` is given as:
475
+
476
+ .. math::
477
+ \ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad \text{with } l_n = \left| x_n - y_n \right|
478
+
479
+ where :math:`N` is the batch size. If `reduction` is not ``'none'``, then:
480
+
481
+ .. math::
482
+ \ell(x, y) =
483
+ \begin{cases}
484
+ \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\
485
+ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.}
486
+ \end{cases}
487
+
488
+ Args:
489
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
490
+ ``'sum'`` . Default: ``'mean'`` .
491
+
492
+ - ``'none'``: no reduction will be applied.
493
+ - ``'mean'``: compute and return the mean of elements in the output.
494
+ - ``'sum'``: the output elements will be summed.
495
+
496
+ Inputs:
497
+ - **logits** (Tensor) - Tensor of shape :math:`(M, *)` where :math:`*` means, any number of
498
+ additional dimensions.
499
+ - **labels** (Tensor) - Tensor of shape :math:`(N, *)`, same shape as the `logits` in common cases.
500
+ However, it supports the shape of `logits` is different from the shape of `labels`
501
+ and they should be broadcasted to each other.
502
+
503
+ Outputs:
504
+ Tensor, weighted loss float tensor, the shape is zero if `reduction` is ``'mean'`` or ``'sum'`` .,
505
+ while the shape of output is the broadcasted shape if `reduction` is 'none'.
506
+
507
+ Raises:
508
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
509
+
510
+ Supported Platforms:
511
+ ``Ascend`` ``GPU`` ``CPU``
512
+
513
+ Examples:
514
+ >>> import mindspore
515
+ >>> from mindspore import Tensor, nn
516
+ >>> import numpy as np
517
+ >>> # Case 1: logits.shape = labels.shape = (3,)
518
+ >>> loss = nn.MAELoss()
519
+ >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
520
+ >>> labels = Tensor(np.array([1, 2, 2]), mindspore.float32)
521
+ >>> output = loss(logits, labels)
522
+ >>> print(output)
523
+ 0.33333334
524
+ >>> # Case 2: logits.shape = (3,), labels.shape = (2, 3)
525
+ >>> loss = nn.MAELoss(reduction='none')
526
+ >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
527
+ >>> labels = Tensor(np.array([[1, 1, 1], [1, 2, 2]]), mindspore.float32)
528
+ >>> output = loss(logits, labels)
529
+ >>> print(output)
530
+ [[0. 1. 2.]
531
+ [0. 0. 1.]]
532
+ """
533
+
534
+ def __init__(self, reduction='mean'):
535
+ """Initialize MAELoss."""
536
+ super(MAELoss, self).__init__(reduction)
537
+ self.abs = P.Abs()
538
+
539
+ def construct(self, logits, label):
540
+ _check_is_tensor('logits', logits, self.cls_name)
541
+ _check_is_tensor('labels', label, self.cls_name)
542
+ x = self.abs(logits - label)
543
+ return self.get_loss(x)
544
+
545
+
546
+ class MarginRankingLoss(LossBase):
547
+ r"""
548
+ MarginRankingLoss creates a criterion that measures the loss.
549
+
550
+ Given two tensors :math:`input1`, :math:`input2` and a Tensor label :math:`target` with values 1 or -1,
551
+ the operation is as follows:
552
+
553
+ .. math::
554
+ \text{loss}(input1, input2, target) = \max(0, -target * (input1 - input2) + \text{margin})
555
+
556
+ Args:
557
+ margin (float, optional): Specify the adjustment factor of the operation. Default: ``0.0`` .
558
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
559
+ ``'sum'`` . Default: ``'mean'`` .
560
+
561
+ - ``'none'``: no reduction will be applied.
562
+ - ``'mean'``: compute and return the mean of elements in the output.
563
+ - ``'sum'``: the output elements will be summed.
564
+
565
+ Inputs:
566
+ - **input1** (Tensor) - Tensor of shape :math:`(N, *)` where :math:`*` means, any number
567
+ of additional dimensions.
568
+ - **input2** (Tensor) - Tensor of shape :math:`(N, *)`, same shape and dtype as `input1`.
569
+ - **target** (Tensor) - Contains value 1 or -1. Suppose the shape of `input1` is
570
+ :math:`(x_1, x_2, x_3, ..., x_R)`, then the shape of `target` must be :math:`(x_1, x_2, x_3, ..., x_R)`.
571
+
572
+ Outputs:
573
+ Tensor or Scalar. if `reduction` is ``'none'``, its shape is the same as `input1`.
574
+ Otherwise, a scalar value will be returned.
575
+
576
+ Raises:
577
+ TypeError: If `margin` is not a float.
578
+ TypeError: If `input1`, `input2` or `target` is not a Tensor.
579
+ TypeError: If the types of `input1` and `input2` are inconsistent.
580
+ TypeError: If the types of `input1` and `target` are inconsistent.
581
+ ValueError: If the shape of `input1` and `input2` are inconsistent.
582
+ ValueError: If the shape of `input1` and `target` are inconsistent.
583
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'`` , ``'sum'``.
584
+
585
+ Supported Platforms:
586
+ ``Ascend`` ``GPU`` ``CPU``
587
+
588
+ Examples:
589
+ >>> import mindspore as ms
590
+ >>> from mindspore import Tensor, nn, ops
591
+ >>> import numpy as np
592
+ >>> loss1 = nn.MarginRankingLoss(reduction='none')
593
+ >>> loss2 = nn.MarginRankingLoss(reduction='mean')
594
+ >>> loss3 = nn.MarginRankingLoss(reduction='sum')
595
+ >>> sign = ops.Sign()
596
+ >>> input1 = Tensor(np.array([0.3864, -2.4093, -1.4076]), ms.float32)
597
+ >>> input2 = Tensor(np.array([-0.6012, -1.6681, 1.2928]), ms.float32)
598
+ >>> target = sign(Tensor(np.array([-2, -2, 3]), ms.float32))
599
+ >>> output1 = loss1(input1, input2, target)
600
+ >>> print(output1)
601
+ [0.98759997 0. 2.7003999 ]
602
+ >>> output2 = loss2(input1, input2, target)
603
+ >>> print(output2)
604
+ 1.2293333
605
+ >>> output3 = loss3(input1, input2, target)
606
+ >>> print(output3)
607
+ 3.6879997
608
+ """
609
+
610
+ def __init__(self, margin=0.0, reduction='mean'):
611
+ """Initialize MarginRankingLoss."""
612
+ super(MarginRankingLoss, self).__init__(reduction)
613
+ self.reduction = reduction
614
+ self.margin = margin
615
+
616
+ def construct(self, input1, input2, target):
617
+ x = ops.margin_ranking_loss(input1, input2, target, self.margin, self.reduction)
618
+ return x
619
+
620
+
621
+ class SmoothL1Loss(LossBase):
622
+ r"""
623
+ SmoothL1 loss function, if the absolute error element-wise between the predicted value and the target value
624
+ is less than the set threshold `beta`, the square term is used, otherwise the absolute error term is used.
625
+
626
+ Given two input :math:`x,\ y`, the SmoothL1Loss can be described as follows:
627
+
628
+ .. math::
629
+ L_{i} =
630
+ \begin{cases}
631
+ \frac{0.5 (x_i - y_i)^{2}}{\beta}, & \text{if } |x_i - y_i| < {\beta} \\
632
+ |x_i - y_i| - 0.5 {\beta}, & \text{otherwise.}
633
+ \end{cases}
634
+
635
+ Where :math:`{\beta}` represents the threshold `beta`.
636
+
637
+ If `reduction` is not `none`, then:
638
+
639
+ .. math::
640
+ L =
641
+ \begin{cases}
642
+ \operatorname{mean}(L_{i}), & \text{if reduction} = \text{'mean';}\\
643
+ \operatorname{sum}(L_{i}), & \text{if reduction} = \text{'sum'.}
644
+ \end{cases}
645
+
646
+ .. note::
647
+ - SmoothL1Loss can be regarded as modified version of :class:`mindspore.nn.L1Loss`
648
+ or a combination of :class:`mindspore.nn.L1Loss` and :class:`mindspore.ops.L2Loss`.
649
+ - :class:`mindspore.nn.L1Loss` computes the element-wise absolute difference between two input tensor
650
+ while :class:`mindspore.ops.L2Loss` computes the
651
+ - squared difference between two input tensors. :class:`mindspore.ops.L2Loss`
652
+ often leads to faster convergence but it is less
653
+ robust to outliers, and the loss function has better robustness.
654
+
655
+ Args:
656
+ beta (float): The loss function calculates the threshold of the transformation between L1Loss and L2Loss.
657
+ Default: ``1.0`` .
658
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
659
+ ``'sum'`` . Default: ``'none'`` .
660
+
661
+ - ``'none'``: no reduction will be applied.
662
+ - ``'mean'``: compute and return the mean of elements in the output.
663
+ - ``'sum'``: the output elements will be summed.
664
+
665
+ Inputs:
666
+ - **logits** (Tensor) - Predictive value. Tensor of any dimension. Data type must be one of float16 or
667
+ float32.
668
+ - **labels** (Tensor) - Ground truth data, same shape and dtype as the `logits`.
669
+
670
+ Outputs:
671
+ Tensor, if `reduction` is ``'none'``, then output is a tensor with the same shape as `logits`.
672
+ Otherwise the shape of output tensor is :math:`()`.
673
+
674
+ Raises:
675
+ TypeError: If `beta` is not a float.
676
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
677
+ TypeError: If `logits` or `labels` are not Tensor.
678
+ TypeError: If dtype of `logits` or `labels` is neither float16 not float32.
679
+ TypeError: If dtype of `logits` is not the same as `labels`.
680
+ ValueError: If `beta` is less than or equal to 0.
681
+ ValueError: If shape of `logits` is not the same as `labels`.
682
+
683
+ Supported Platforms:
684
+ ``Ascend`` ``GPU`` ``CPU``
685
+
686
+ Examples:
687
+ >>> import mindspore
688
+ >>> from mindspore import Tensor, nn
689
+ >>> import numpy as np
690
+ >>> loss = nn.SmoothL1Loss()
691
+ >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
692
+ >>> labels = Tensor(np.array([1, 2, 2]), mindspore.float32)
693
+ >>> output = loss(logits, labels)
694
+ >>> print(output)
695
+ [0. 0. 0.5]
696
+ """
697
+
698
+ def __init__(self, beta=1.0, reduction='none'):
699
+ """Initialize SmoothL1Loss."""
700
+ super(SmoothL1Loss, self).__init__(reduction)
701
+ self.beta = beta
702
+ self.reduction = reduction
703
+ self.smooth_l1_loss = P.SmoothL1Loss(self.beta, self.reduction)
704
+
705
+ def construct(self, logits, labels):
706
+ return self.smooth_l1_loss(logits, labels)
707
+
708
+
709
+ class SoftMarginLoss(LossBase):
710
+ r"""
711
+ A loss class for two-class classification problems.
712
+
713
+ SoftMarginLoss creates a criterion that optimizes a two-class classification
714
+ logistic loss between input tensor :math:`x` and labels tensor :math:`y`
715
+ (containing 1 or -1).
716
+
717
+ .. math::
718
+ \text{loss}(x, y) = \sum_i \frac{\log(1 + \exp(-y[i]*x[i]))}{x.nelement()}
719
+
720
+ :math:`x.nelement()` represents the number of element of `x` .
721
+
722
+ Args:
723
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
724
+ ``'sum'`` . Default: ``'mean'`` .
725
+
726
+ - ``'none'``: no reduction will be applied.
727
+ - ``'mean'``: compute and return the mean of elements in the output.
728
+ - ``'sum'``: the output elements will be summed.
729
+
730
+ Inputs:
731
+ - **logits** (Tensor) - Predict data. Data type must be float16 or float32.
732
+ - **labels** (Tensor) - Ground truth data, with the same type and shape as `logits`.
733
+
734
+ Outputs:
735
+ Tensor or Scalar, if `reduction` is ``"none"``, its shape is the same as `logits`.
736
+ Otherwise, a scalar value will be returned.
737
+
738
+ Raises:
739
+ TypeError: If `logits` or `labels` is not a Tensor.
740
+ TypeError: If dtype of `logits` or `labels` is neither float16 nor float32.
741
+ ValueError: If shape of `logits` is not the same as `labels`.
742
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
743
+
744
+ Supported Platforms:
745
+ ``Ascend`` ``GPU``
746
+
747
+ Examples:
748
+ >>> import mindspore
749
+ >>> from mindspore import Tensor, nn
750
+ >>> import numpy as np
751
+ >>> loss = nn.SoftMarginLoss()
752
+ >>> logits = Tensor(np.array([[0.3, 0.7], [0.5, 0.5]]), mindspore.float32)
753
+ >>> labels = Tensor(np.array([[-1, 1], [1, -1]]), mindspore.float32)
754
+ >>> output = loss(logits, labels)
755
+ >>> print(output)
756
+ 0.6764238
757
+ """
758
+
759
+ def __init__(self, reduction='mean'):
760
+ super(SoftMarginLoss, self).__init__()
761
+ self.soft_margin_loss = P.SoftMarginLoss(reduction)
762
+
763
+ def construct(self, logits, labels):
764
+ return self.soft_margin_loss(logits, labels)
765
+
766
+
767
+ class SoftmaxCrossEntropyWithLogits(LossBase):
768
+ r"""
769
+ Computes softmax cross entropy between logits and labels.
770
+
771
+ Measures the distribution error between the probabilities of the input (computed with softmax function) and the
772
+ labels where the classes are mutually exclusive (only one class is positive) using cross entropy loss.
773
+
774
+ Typical input into this function is unnormalized scores denoted as :math:`x` whose shape is :math:`(N, C)` ,
775
+ and the corresponding targets.
776
+
777
+ Typically, the input to this function is the fractional value of each category and the corresponding target value,
778
+ and the input format is :math:`(N, C)` .
779
+
780
+ For each instance :math:`x_i`, :math:`i` ranges from 0 to N-1, the loss is given as:
781
+
782
+ .. math::
783
+ \ell(x_i, c) = - \log\left(\frac{\exp(x_i[c])}{\sum_j \exp(x_i[j])}\right)
784
+ = -x_i[c] + \log\left(\sum_j \exp(x_i[j])\right)
785
+
786
+ where :math:`x_i` is a 1D score Tensor, :math:`c` is the index of 1 in one-hot.
787
+
788
+ Note:
789
+ While the labels classes are mutually exclusive, i.e., only one class is positive in the labels, the predicted
790
+ probabilities does not need to be exclusive. It is only required that the predicted probability distribution
791
+ of entry is a valid one.
792
+
793
+ Args:
794
+ sparse (bool, optional): Specifies whether labels use sparse format or not. Default: ``False`` .
795
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
796
+ ``'sum'`` . Default: ``'none'`` .
797
+
798
+ - ``'none'``: no reduction will be applied.
799
+ - ``'mean'``: compute and return the mean of elements in the output.
800
+ - ``'sum'``: the output elements will be summed.
801
+
802
+ Inputs:
803
+ - **logits** (Tensor) - Tensor of shape :math:`(N, C)` . Data type must be float16 or float32.
804
+ - **labels** (Tensor) - Tensor of shape :math:`(N, )` . If `sparse` is True, The type of
805
+ `labels` is int32 or int64. Otherwise, the type of `labels` is the same as the type of `logits`.
806
+
807
+ Outputs:
808
+ Tensor, a tensor of the same shape and type as logits with the component-wise logistic losses.
809
+
810
+ Raises:
811
+ TypeError: If `sparse` is not a bool.
812
+ TypeError: If `sparse` is True and dtype of `labels` is neither int32 nor int64.
813
+ TypeError: If `sparse` is False and dtype of `labels` is neither float16 not float32.
814
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
815
+
816
+ Supported Platforms:
817
+ ``Ascend`` ``GPU`` ``CPU``
818
+
819
+ Examples:
820
+ >>> import mindspore
821
+ >>> from mindspore import Tensor, nn
822
+ >>> import numpy as np
823
+ >>> # case 1: sparse=True
824
+ >>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
825
+ >>> logits = Tensor(np.array([[3, 5, 6, 9, 12, 33, 42, 12, 32, 72]]), mindspore.float32)
826
+ >>> labels_np = np.array([1]).astype(np.int32)
827
+ >>> labels = Tensor(labels_np)
828
+ >>> output = loss(logits, labels)
829
+ >>> print(output)
830
+ [67.]
831
+ >>> # case 2: sparse=False
832
+ >>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=False)
833
+ >>> logits = Tensor(np.array([[3, 5, 6, 9, 12, 33, 42, 12, 32, 72]]), mindspore.float32)
834
+ >>> labels_np = np.array([[0, 0, 0, 0, 0, 0, 1, 0, 0, 0]]).astype(np.float32)
835
+ >>> labels = Tensor(labels_np)
836
+ >>> output = loss(logits, labels)
837
+ >>> print(output)
838
+ [30.]
839
+ """
840
+
841
+ def __init__(self,
842
+ sparse=False,
843
+ reduction='none'):
844
+ """Initialize SoftmaxCrossEntropyWithLogits."""
845
+ super(SoftmaxCrossEntropyWithLogits, self).__init__(reduction)
846
+ self.sparse = validator.check_bool(sparse, "sparse", self.cls_name)
847
+ self.reduction = reduction
848
+ self.softmax_cross_entropy = P.SoftmaxCrossEntropyWithLogits()
849
+ self.one_hot = P.OneHot()
850
+ self.on_value = Tensor(1.0, mstype.float32)
851
+ self.off_value = Tensor(0., mstype.float32)
852
+ self.is_cpugpu = context.get_context('device_target') in ["CPU", "GPU"]
853
+ self.sparse_softmax_cross_entropy = P.SparseSoftmaxCrossEntropyWithLogits()
854
+
855
+ def construct(self, logits, labels):
856
+ _check_is_tensor('logits', logits, self.cls_name)
857
+ _check_is_tensor('labels', labels, self.cls_name)
858
+ if self.sparse:
859
+ if self.reduction == 'mean':
860
+ x = self.sparse_softmax_cross_entropy(logits, labels)
861
+ return x
862
+ labels = self.one_hot(labels, F.shape(logits)[-1], self.on_value, self.off_value)
863
+ x = self.softmax_cross_entropy(logits, labels)[0]
864
+ return self.get_loss(x)
865
+
866
+
867
+ @_primexpr
868
+ def _check_dice_shape(logits_shape, label_shape, prim_name=None):
869
+ """Internal function, check whether the shape of logits and labels meets the requirements."""
870
+ validator.check('logits_shape', logits_shape, 'label_shape', label_shape, prim_name=prim_name)
871
+
872
+
873
+ @constexpr
874
+ def _check_label_dtype(labels_dtype, cls_name):
875
+ """Internal function, used to check whether the data type of labels meets the requirements."""
876
+ validator.check_type_name("labels", labels_dtype, [mstype.int32, mstype.int64], cls_name)
877
+
878
+
879
+ class DiceLoss(LossBase):
880
+ r"""
881
+ The Dice coefficient is a set similarity loss, which is used to calculate the similarity between two samples. The
882
+ value of the Dice coefficient is 1 when the segmentation result is the best and is 0 when the segmentation result
883
+ is the worst. The Dice coefficient indicates the ratio of the area between two objects to the total area.
884
+ The function is shown as follows:
885
+
886
+ .. math::
887
+ dice = 1 - \frac{2 * |pred \bigcap true|}{|pred| + |true| + smooth}
888
+
889
+ :math:`pred` represent `logits`, :math:`true` represent `labels` .
890
+
891
+ Args:
892
+ smooth (float): A term added to the denominator to improve numerical stability. Should be greater than 0.
893
+ Default: ``1e-5`` .
894
+
895
+ Inputs:
896
+ - **logits** (Tensor) - Input predicted value. The data type must be float16 or float32.
897
+ - **labels** (Tensor) - Input target value. Same shape as the `logits`.
898
+ The data type must be float16 or float32.
899
+
900
+ Outputs:
901
+ Tensor, a tensor of shape with the per-example sampled Dice losses.
902
+
903
+ Raises:
904
+ ValueError: If the dimension of `logits` is different from `labels`.
905
+ TypeError: If the type of `logits` or `labels` is not a tensor.
906
+
907
+ Supported Platforms:
908
+ ``Ascend`` ``GPU`` ``CPU``
909
+
910
+ Examples:
911
+ >>> import mindspore
912
+ >>> from mindspore import Tensor, nn
913
+ >>> import numpy as np
914
+ >>> loss = nn.DiceLoss(smooth=1e-5)
915
+ >>> logits = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mindspore.float32)
916
+ >>> labels = Tensor(np.array([[0, 1], [1, 0], [0, 1]]), mindspore.float32)
917
+ >>> output = loss(logits, labels)
918
+ >>> print(output)
919
+ 0.38596618
920
+ """
921
+
922
+ def __init__(self, smooth=1e-5):
923
+ """Initialize DiceLoss."""
924
+ super(DiceLoss, self).__init__()
925
+ self.smooth = validator.check_positive_float(smooth, "smooth")
926
+ self.reshape = P.Reshape()
927
+
928
+ def construct(self, logits, label):
929
+ _check_is_tensor('logits', logits, self.cls_name)
930
+ _check_is_tensor('labels', label, self.cls_name)
931
+ _check_dice_shape(logits.shape, label.shape, self.cls_name)
932
+ if logits.dtype == mstype.uint8:
933
+ raise TypeError(f"For '{self.cls_name}', the dtype of 'logits' can not be uint8.")
934
+ if label.dtype == mstype.uint8:
935
+ raise TypeError(f"For '{self.cls_name}', the dtype of 'labels' can not be uint8.")
936
+ intersection = self.reduce_sum(self.mul(logits.view(-1), label.view(-1)))
937
+ unionset = self.reduce_sum(self.mul(logits.view(-1), logits.view(-1))) + \
938
+ self.reduce_sum(self.mul(label.view(-1), label.view(-1)))
939
+
940
+ single_dice_coeff = (2 * intersection) / (unionset + self.smooth)
941
+ dice_loss = 1 - single_dice_coeff
942
+
943
+ return dice_loss
944
+
945
+
946
+ @_primexpr
947
+ def _check_shape(logits_shape, label_shape, prim_name=None):
948
+ """Internal function, used to check whether the shape of logits and labels meets the requirements."""
949
+ validator.check('logits_shape', logits_shape, 'label_shape', label_shape, prim_name=prim_name)
950
+
951
+
952
+ @_primexpr
953
+ def _check_ndim_multi(logits_dim, label_dim, prim_name=None):
954
+ """Internal function, used to check whether the dimension of logits and label meets the requirements."""
955
+ msg_prefix = f'For \'{prim_name}\', the' if prim_name else "The"
956
+ if logits_dim < 2:
957
+ raise ValueError(f"{msg_prefix} 'logits' dimension must be greater than 1, but got {logits_dim}.")
958
+ if label_dim < 2:
959
+ raise ValueError(f"{msg_prefix} 'labels' dimension must be greater than 1, but got {label_dim}.")
960
+
961
+
962
+ @_primexpr
963
+ def _check_weights(weight_shape, label_shape, prim_name=None):
964
+ """Internal function, used to check whether the reduced shape meets the requirements."""
965
+ msg_prefix = f'For \'{prim_name}\', the' if prim_name else "The"
966
+ if weight_shape != label_shape:
967
+ raise ValueError(f"{msg_prefix} weight_shape[0] must be equal to label_shape[1], "
968
+ f"but got weight_shape[0]: {weight_shape} and label_shape[1]: {label_shape}.")
969
+
970
+
971
+ class MultiClassDiceLoss(LossBase):
972
+ r"""
973
+ When there are multiple classifications, label is transformed into multiple binary classifications by one hot.
974
+ For each channel section in the channel, it can be regarded as a binary classification problem, so it can be
975
+ obtained through the binary :class:`mindspore.nn.DiceLoss` losses of each category,
976
+ and then the average value of the binary losses.
977
+
978
+ Args:
979
+ weights (Union[Tensor, None]): Tensor of shape :math:`(num\_classes, dim)`. The weight shape[0] should be
980
+ equal to labels shape[1].
981
+ Default: ``None`` .
982
+ ignore_indiex (Union[int, None]): Class index to ignore.
983
+ Default: ``None`` .
984
+ activation (Union[str, Cell]): Activate function applied to the output of the fully connected layer, eg. 'ReLU'.
985
+ Default: ``'softmax'`` . Choose from: [ ``'softmax'`` , ``'logsoftmax'`` , ``'relu'`` , ``'relu6'`` ,
986
+ ``'tanh'`` , ``'Sigmoid'`` ]
987
+
988
+ Inputs:
989
+ - **logits** (Tensor) - Tensor of shape :math:`(N, C, *)` where :math:`*` means, any number of additional
990
+ dimensions. The logits dimension should be greater than 1. The data type must be float16 or float32.
991
+ - **labels** (Tensor) - Tensor of shape :math:`(N, C, *)`, same shape as the `logits`.
992
+ The labels dimension should be greater than 1. The data type must be float16 or float32.
993
+
994
+ Outputs:
995
+ Tensor, a tensor of shape with the per-example sampled MultiClass Dice Losses.
996
+
997
+ Raises:
998
+ ValueError: If the shape of `logits` is different from `labels`.
999
+ TypeError: If the type of `logits` or `labels` is not a tensor.
1000
+ ValueError: If the dimension of `logits` or `labels` is less than 2.
1001
+ ValueError: If the weights.shape[0] is not equal to labels.shape[1].
1002
+ ValueError: If `weights` is a tensor, but its dimension is not 2.
1003
+
1004
+ Supported Platforms:
1005
+ ``Ascend`` ``GPU`` ``CPU``
1006
+
1007
+ Examples:
1008
+ >>> import mindspore
1009
+ >>> from mindspore import Tensor, nn
1010
+ >>> import numpy as np
1011
+ >>> loss = nn.MultiClassDiceLoss(weights=None, ignore_indiex=None, activation="softmax")
1012
+ >>> logits = Tensor(np.array([[0.2, 0.5, 0.7], [0.3, 0.1, 0.5], [0.9, 0.6, 0.3]]), mindspore.float32)
1013
+ >>> labels = Tensor(np.array([[0, 1, 0], [1, 0, 0], [0, 0, 1]]), mindspore.float32)
1014
+ >>> output = loss(logits, labels)
1015
+ >>> print(output)
1016
+ 0.54958105
1017
+ """
1018
+
1019
+ def __init__(self, weights=None, ignore_indiex=None, activation="softmax"):
1020
+ """Initialize MultiClassDiceLoss."""
1021
+ super(MultiClassDiceLoss, self).__init__()
1022
+ activation_list = ['softmax', 'logsoftmax', 'relu', 'relu6', 'tanh', 'sigmoid']
1023
+
1024
+ self.binarydiceloss = DiceLoss(smooth=1e-5)
1025
+ self.weights = weights if weights is None else validator.check_value_type("weights", weights, [Tensor])
1026
+ if isinstance(self.weights, Tensor) and self.weights.ndim != 2:
1027
+ raise ValueError(f"For '{self.cls_name}', the dimension of 'weights' must be 2, "
1028
+ f"but got {self.weights.ndim}.")
1029
+ self.ignore_indiex = ignore_indiex if ignore_indiex is None else validator.check_value_type("ignore_indiex",
1030
+ ignore_indiex,
1031
+ [int])
1032
+ if isinstance(activation, str) and activation not in activation_list:
1033
+ raise ValueError(f"For '{self.cls_name}', the 'activation' must be in {activation_list}, "
1034
+ f"but got {activation}.")
1035
+
1036
+ self.activation = get_activation(activation) if isinstance(activation, str) else activation
1037
+ if self.activation is not None and not isinstance(self.activation, Cell):
1038
+ raise TypeError(f"For '{self.cls_name}', the 'activation' must be str or Cell, "
1039
+ f"but got {type(self.activation)}.")
1040
+ self.reshape = P.Reshape()
1041
+
1042
+ def construct(self, logits, label):
1043
+ _check_is_tensor('logits', logits, self.cls_name)
1044
+ _check_is_tensor('labels', label, self.cls_name)
1045
+ _check_shape(logits.shape, label.shape, self.cls_name)
1046
+ _check_ndim_multi(logits.ndim, label.ndim, self.cls_name)
1047
+ total_loss = 0
1048
+
1049
+ if self.activation is not None:
1050
+ logits = self.activation(logits)
1051
+
1052
+ for i in range(label.shape[1]):
1053
+ if i != self.ignore_indiex:
1054
+ dice_loss = self.binarydiceloss(logits[:, i], label[:, i])
1055
+ if self.weights is not None:
1056
+ _check_weights(self.weights.shape[0], label.shape[1], self.cls_name)
1057
+ dice_loss *= self.weights[i]
1058
+ total_loss += dice_loss
1059
+
1060
+ return total_loss / label.shape[1]
1061
+
1062
+
1063
+ class SampledSoftmaxLoss(LossBase):
1064
+ r"""
1065
+ Computes the sampled softmax training loss. This operator can accelerate the training of the softmax classifier
1066
+ over a large number of classes. It is generally an underestimate of the full softmax loss.
1067
+
1068
+ Args:
1069
+ num_sampled (int): The number of classes to randomly sample per batch.
1070
+ num_classes (int): The number of possible classes.
1071
+ num_true (int): The number of labels classes per training example. Default: ``1`` .
1072
+ sampled_values (Union[list, tuple]): List or tuple of (`sampled_candidates`, `true_expected_count`,
1073
+ `sampled_expected_count`) returned by a `*CandidateSampler` function.
1074
+ Default to None, `UniformCandidateSampler` is applied. Default: ``None`` .
1075
+ remove_accidental_hits (bool): Whether to remove "accidental hits"
1076
+ where a sampled class equals to one of the labels classes. Default: ``True`` .
1077
+ seed (int): Random seed for candidate sampling. Default: ``0``.
1078
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
1079
+ ``'sum'`` . Default: ``'none'`` .
1080
+
1081
+ - ``'none'``: no reduction will be applied.
1082
+ - ``'mean'``: compute and return the mean of elements in the output.
1083
+ - ``'sum'``: the output elements will be summed.
1084
+
1085
+ Inputs:
1086
+ - **weights** (Tensor) - The weights of input. Tensor of shape :math:`(C, dim)`.
1087
+ - **bias** (Tensor) - Tensor of shape :math:`(C,)`. The class biases.
1088
+ - **labels** (Tensor) - Tensor of shape :math:`(N, num\_true)`, type `int64, int32`. The labels classes.
1089
+ - **logits** (Tensor) - Tensor of shape :math:`(N, dim)`. The forward activations of the input network.
1090
+
1091
+ Outputs:
1092
+ Tensor or Scalar, if `reduction` is ``'none'``, then output is a tensor with shape :math:`(N,)`.
1093
+ Otherwise, the output is a scalar.
1094
+
1095
+ Raises:
1096
+ TypeError: If `sampled_values` is not a list or tuple.
1097
+ TypeError: If dtype of `labels` is neither int32 nor int64.
1098
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
1099
+ ValueError: If `num_sampled` or `num_true` is greater than `num_classes`.
1100
+ ValueError: If length of `sampled_values` is not equal to 3.
1101
+
1102
+ Supported Platforms:
1103
+ ``GPU``
1104
+
1105
+ Examples:
1106
+ >>> import mindspore
1107
+ >>> from mindspore import Tensor, nn
1108
+ >>> import numpy as np
1109
+ >>> mindspore.set_seed(1)
1110
+ >>> loss = nn.SampledSoftmaxLoss(num_sampled=4, num_classes=7, num_true=1)
1111
+ >>> weights = Tensor(np.random.randint(0, 9, [7, 10]), mindspore.float32)
1112
+ >>> biases = Tensor(np.random.randint(0, 9, [7]), mindspore.float32)
1113
+ >>> labels = Tensor([0, 1, 2])
1114
+ >>> logits = Tensor(np.random.randint(0, 9, [3, 10]), mindspore.float32)
1115
+ >>> output = loss(weights, biases, labels, logits)
1116
+ >>> print(output)
1117
+ [4.6051701e+01 1.4000047e+01 6.1989022e-06]
1118
+ """
1119
+
1120
+ def __init__(self, num_sampled, num_classes, num_true=1,
1121
+ sampled_values=None, remove_accidental_hits=True, seed=0,
1122
+ reduction='none'):
1123
+ """Initialize SampledSoftmaxLoss."""
1124
+ super(SampledSoftmaxLoss, self).__init__(reduction)
1125
+
1126
+ if num_true < 1:
1127
+ raise ValueError(f"For '{self.cls_name}', the 'num_true' must be greater than or equal to 1, "
1128
+ f"but got {num_true}.")
1129
+ if seed < 0:
1130
+ raise ValueError(f"For '{self.cls_name}', the 'seed' must be greater than or equal to 0, but got {seed}.")
1131
+ if num_sampled > num_classes:
1132
+ raise ValueError(f"For '{self.cls_name}', the 'num_sampled' must be smaller than or "
1133
+ f"equal to 'num_classes', but got 'num_sampled': {num_sampled} "
1134
+ f"and 'num_classes': {num_classes}.")
1135
+ if num_true > num_classes:
1136
+ raise ValueError(f"For '{self.cls_name}', the 'num_true' must be smaller than or equal to 'num_classes', "
1137
+ f"but got 'num_true': {num_true} amd 'num_classes': {num_classes}.")
1138
+ if sampled_values is not None:
1139
+ if not isinstance(sampled_values, (list, tuple)):
1140
+ raise TypeError(f"For '{self.cls_name}', the type of 'sampled_values' must be a list or tuple, "
1141
+ f"but got {type(sampled_values).__name__}.")
1142
+ if len(sampled_values) != 3:
1143
+ raise ValueError(f"For '{self.cls_name}', the length of 'sampled_values' must be equal to 3,"
1144
+ f"but got {len(sampled_values)}.")
1145
+
1146
+ self.num_sampled = num_sampled
1147
+ self.num_classes = num_classes
1148
+ self.num_true = num_true
1149
+ self.sampled_values = sampled_values
1150
+ self.remove_accidental_hits = remove_accidental_hits
1151
+ self.seed = seed
1152
+ self.sampler = P.UniformCandidateSampler(
1153
+ num_true,
1154
+ num_sampled,
1155
+ True,
1156
+ num_classes,
1157
+ seed,
1158
+ remove_accidental_hits)
1159
+ self.cast = P.Cast()
1160
+ self.reshape = P.Reshape()
1161
+ self.shape = P.Shape()
1162
+ self.exp = P.Exp()
1163
+ self.log = P.Log()
1164
+ self.slice_op = P.Slice()
1165
+ self.matmul = P.MatMul(False, True)
1166
+ self.gather_v2 = P.Gather()
1167
+ self.reduce_max_true = P.ReduceMax(True)
1168
+ self.reduce_sum = P.ReduceSum()
1169
+ self.reduce_sum_true = P.ReduceSum(True)
1170
+ self.concat_dim0 = P.Concat(0)
1171
+ self.concat_dim1 = P.Concat(1)
1172
+ self.ones_like = P.OnesLike()
1173
+ self.zeros_like = P.ZerosLike()
1174
+ self.mul = P.Mul()
1175
+ self.expand_dims = P.ExpandDims()
1176
+ self.dtype = P.DType()
1177
+
1178
+ def construct(self, weights, biases, labels, logits):
1179
+ _check_is_tensor('weights', weights, self.cls_name)
1180
+ _check_is_tensor('biases', biases, self.cls_name)
1181
+ _check_is_tensor('labels', labels, self.cls_name)
1182
+ _check_is_tensor('logits', logits, self.cls_name)
1183
+ _check_label_dtype(self.dtype(labels), self.cls_name)
1184
+
1185
+ logits, labels = self._compute_sampled_logits(
1186
+ weights=weights,
1187
+ biases=biases,
1188
+ labels=labels,
1189
+ logits=logits,
1190
+ num_true=self.num_true,
1191
+ sampled_values=self.sampled_values,
1192
+ subtract_log_q=True)
1193
+
1194
+ x = self._softmax_cross_entropy(logits, labels)
1195
+ return x
1196
+
1197
+ def _softmax_cross_entropy(self, logits, targets):
1198
+ stable_exp_logits = self.exp(logits - self.reduce_max_true(logits, 1))
1199
+ pred = stable_exp_logits / self.reduce_sum_true(stable_exp_logits, 1)
1200
+ return -1 * self.reduce_sum(targets * self.log(pred + 1.0e-20), 1)
1201
+
1202
+ def _compute_sampled_logits(self, weights,
1203
+ biases,
1204
+ labels,
1205
+ logits,
1206
+ num_true=1,
1207
+ sampled_values=None,
1208
+ subtract_log_q=True):
1209
+ """Helper function for SampledSoftmaxLoss functions.
1210
+
1211
+ Computes sampled output training logits and labels suitable
1212
+
1213
+ Note: In the case where num_true > 1, we assign to each labels class
1214
+ with the labels probability (1/num_true) so that the labels probabilities
1215
+ sum to 1 per-example.
1216
+
1217
+ Args:
1218
+ weights (Tensor): Tensor of shape `[num_classes, dim]`.
1219
+ biases (Tensor): Tensor of shape `[num_classes]`.
1220
+ labels (Tensor): Tensor of shape `[batch_size, num_true]`. The labels classes.
1221
+ logits (Tensor): Tensor of shape `[batch_size, dim]`. The forward
1222
+ activations of the input network.
1223
+ num_true (int): The number of labels classes per training example.
1224
+ sampled_values: A tuple of (`sampled_candidates`, `true_expected_count`,
1225
+ `sampled_expected_count`) returned by a `UniformCandidateSampler` function.
1226
+ subtract_log_q: A `bool`. whether to subtract the log expected count of
1227
+ the labels in the sample to get the logits of the true labels. Default: ``True`` .
1228
+ Returns:
1229
+ out_logits: `Tensor` object with shape
1230
+ `[batch_size, num_true + num_sampled]`
1231
+ out_labels: A tensor object with the same shape as `out_logits`.
1232
+ """
1233
+
1234
+ if not labels.dtype == mstype.int32:
1235
+ labels = self.cast(labels, mstype.int32)
1236
+ labels = self.reshape(labels, (-1, num_true))
1237
+ labels_flat = self.reshape(labels, (-1,))
1238
+
1239
+ # Sample the negative labels.
1240
+ # sampled shape: [num_sampled] tensor
1241
+ # true_expected_count shape is [batch_size, 1] tensor
1242
+ # sampled_expected_count shape is [num_sampled] tensor
1243
+ if sampled_values is None:
1244
+ sampled_values = self.sampler(labels)
1245
+
1246
+ (sampled, true_expected_count, sampled_expected_count) = sampled_values
1247
+
1248
+ if not sampled.dtype == mstype.int32:
1249
+ sampled = self.cast(sampled, mstype.int32)
1250
+ all_ids = self.concat_dim0((labels_flat, sampled))
1251
+ all_w = self.gather_v2(weights, all_ids, 0)
1252
+
1253
+ n_true = self.shape(labels_flat)[0]
1254
+ n_sampled = self.shape(sampled)[0]
1255
+ n_dim = self.shape(all_w)[1]
1256
+
1257
+ true_w = self.slice_op(all_w, [0, 0], [n_true, n_dim])
1258
+ sampled_w = self.slice_op(all_w, [n_true, 0], [n_sampled, n_dim])
1259
+ sampled_logits = self.matmul(logits, sampled_w)
1260
+
1261
+ all_b = self.gather_v2(biases, all_ids, 0)
1262
+ true_b = self.slice_op(all_b, [0], [n_true])
1263
+ sampled_b = self.slice_op(all_b, [n_true], [n_sampled])
1264
+
1265
+ new_true_w_shape = (-1, num_true, n_dim)
1266
+ row_wise_dots = self.mul(self.expand_dims(logits, 1),
1267
+ self.reshape(true_w, new_true_w_shape))
1268
+
1269
+ # We want the row-wise dot plus biases which yields a
1270
+ # [batch_size, num_true] tensor of true_logits.
1271
+ dots_as_matrix = self.reshape(row_wise_dots, (-1, n_dim))
1272
+ true_logits = self.reshape(self.reduce_sum(dots_as_matrix, 1), (-1, num_true))
1273
+ true_b = self.reshape(true_b, (-1, num_true))
1274
+ true_logits += true_b
1275
+ sampled_logits += sampled_b
1276
+
1277
+ if subtract_log_q:
1278
+ # Subtract log of Q(l), prior probability that l appears in sampled.
1279
+ true_logits -= self.log(true_expected_count)
1280
+ sampled_logits -= self.log(sampled_expected_count)
1281
+
1282
+ # Construct output logits and labels. The true labels/logits start at col 0.
1283
+ out_logits = self.concat_dim1((true_logits, sampled_logits))
1284
+
1285
+ # true_logits is a float tensor, ones_like(true_logits) is a float
1286
+ # tensor of ones. We then divide by num_true to ensure the per-example
1287
+ # labels sum to 1.0, i.e. form a proper probability distribution.
1288
+ out_labels = self.concat_dim1((
1289
+ self.ones_like(true_logits) / num_true,
1290
+ self.zeros_like(sampled_logits)
1291
+ ))
1292
+ return out_logits, out_labels
1293
+
1294
+
1295
+ class TripletMarginWithDistanceLoss(LossBase):
1296
+ r"""
1297
+ TripletMarginWithDistanceLoss operation.
1298
+
1299
+ Creates a criterion that measures the triplet loss given an input
1300
+ tensors :math:`x1`, :math:`x2`, :math:`x3` and a margin with a value greater than :math:`0`.
1301
+ This is used for measuring a relative similarity between samples. A triplet
1302
+ is composed by `a`, `p` and `n` (i.e., `anchor`, `positive examples` and `negative
1303
+ examples` respectively). The shapes of all input tensors should be
1304
+ :math:`(N, D)`.
1305
+
1306
+ The distance swap is described in detail in the paper `Learning shallow
1307
+ convolutional feature descriptors with triplet losses` by
1308
+ V. Balntas, E. Riba et al.
1309
+
1310
+ The loss function for each sample in the mini-batch is:
1311
+
1312
+ .. math::
1313
+ L(a, p, n) = \max \{d(a_i, p_i) - d(a_i, n_i) + {\rm margin}, 0\}
1314
+
1315
+ where
1316
+
1317
+ .. math::
1318
+ d(x_i, y_i) = \left\lVert {\bf x}_i - {\bf y}_i \right\rVert_p
1319
+
1320
+ Args:
1321
+ distance_function (callable): The distance function needed to calculate the margin loss of a triplet.
1322
+ if no distance metric is specified, the pairwise distance will be used. Default: ``None`` .
1323
+ swap (bool): The distance swap is described in detail in the paper
1324
+ `Learning shallow convolutional feature descriptors with triplet losses` by
1325
+ V. Balntas, E. Riba et al. Default: ``False`` .
1326
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
1327
+ ``'sum'`` . Default: ``'mean'`` .
1328
+
1329
+ - ``'none'``: no reduction will be applied.
1330
+ - ``'mean'``: compute and return the mean of elements in the output.
1331
+ - ``'sum'``: the output elements will be summed.
1332
+
1333
+ margin (float): Make a margin between the positive pair and the negative pair. Default: ``1.0`` .
1334
+
1335
+ Inputs:
1336
+ - **x** (Tensor) - A sample randomly selected from the training set. Data type must be BasicType.
1337
+ The shape should be :math:`(N, D)`.
1338
+ - **positive** (Tensor) - A sample belonging to the same category as x,
1339
+ with the same type and shape as `x`.
1340
+ - **negative** (Tensor) - A sample belonging to the different class from x,
1341
+ with the same type and shape as `x`.
1342
+
1343
+ Outputs:
1344
+ Union[Tensor, Scalar], if `reduction` is ``'none'``, its shape is :math:`(N)`.
1345
+ Otherwise, a scalar value will be returned.
1346
+
1347
+ Raises:
1348
+ TypeError: If `x` or `positive` or `negative` is not a Tensor.
1349
+ TypeError: If `swap` is not a bool.
1350
+ ValueError: If dimensions of input `x`, `positive` and `negative` are less than or equal to 1 at the same time.
1351
+ ValueError: If length of shape of `margin` is not 0.
1352
+ ValueError: If shape of `x`, `positive` and `negative` cannot broadcast.
1353
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
1354
+
1355
+ Supported Platforms:
1356
+ ``Ascend`` ``GPU`` ``CPU``
1357
+
1358
+ Examples:
1359
+ >>> import mindspore
1360
+ >>> from mindspore import Tensor, nn
1361
+ >>> x = Tensor([[0.3, 0.7], [0.5, 0.5]])
1362
+ >>> positive = Tensor([[0.4, 0.6], [0.4, 0.6]])
1363
+ >>> negative = Tensor([[0.2, 0.9], [0.3, 0.7]])
1364
+ >>> loss = nn.TripletMarginWithDistanceLoss()
1365
+ >>> out = loss(x, positive, negative)
1366
+ >>> print(out.asnumpy())
1367
+ 0.8881968
1368
+ """
1369
+
1370
+ def __init__(self, distance_function=None, swap=False, reduction="mean", margin=1.0):
1371
+ """Initialize TripletMarginWithDistanceLoss."""
1372
+ super(TripletMarginWithDistanceLoss, self).__init__(reduction=reduction)
1373
+ validator.check_is_float(margin, "margin", self.cls_name)
1374
+ validator.check_bool(swap, "swap", self.cls_name)
1375
+ if distance_function is None:
1376
+ def pairwise_distance(x, y):
1377
+ d = (x - y).abs()
1378
+ if d.ndim == 0:
1379
+ raise ValueError(
1380
+ "For 'pairwise_distance' in 'TripletMarginWithDistanceLoss', "
1381
+ "'ndim' of the input must be positive, "
1382
+ f"but got {d.ndim}"
1383
+ )
1384
+ return P.LpNorm(axis=1, p=2)(d)
1385
+
1386
+ self.distance_function = pairwise_distance
1387
+ else:
1388
+ self.distance_function = distance_function
1389
+ self.swap = swap
1390
+ self.reduction = reduction
1391
+ self.margin = margin
1392
+ self.minimum = P.Minimum()
1393
+ self.maximum = P.Maximum()
1394
+
1395
+ def construct(self, x, positive, negative):
1396
+ _check_is_tensor("x", x, self.cls_name)
1397
+ _check_is_tensor("positive", positive, self.cls_name)
1398
+ _check_is_tensor("negative", negative, self.cls_name)
1399
+ d1 = self.distance_function(x, positive)
1400
+ d2 = self.distance_function(x, negative)
1401
+ if self.swap:
1402
+ d2 = self.minimum(d2, self.distance_function(positive, negative))
1403
+ loss = self.maximum(d1 - d2 + self.margin, 0)
1404
+ return self.get_loss(loss)
1405
+
1406
+
1407
+ class PoissonNLLLoss(LossBase):
1408
+ r"""
1409
+ Poisson negative log likelihood loss.
1410
+
1411
+ The loss is:
1412
+
1413
+ .. math::
1414
+ \mathcal{L}_{D} = \sum_{i = 0}^{|D|}\left( x_{i} - y_{i}\ln x_{i} + \ln{y_{i}!} \right)
1415
+
1416
+ where :math:`\mathcal{L}_{D}` is the loss, :math:`y_{i}` is the `target`,
1417
+ :math:`x_{i}` is the `input`.
1418
+
1419
+ If `log_input` is True, use :math:`e^{x_{i}} - y_{i} x_{i}` instead of :math:`x_{i} - y_{i}\ln x_{i}`.
1420
+ When calculating logarithms, the lower bound of `input` is set to `eps` to avoid numerical errors.
1421
+
1422
+ If `full` is False, the last term :math:`\ln{y_{i}!}` will be omitted,
1423
+ otherwise the last term will be approximated using Stirling formula:
1424
+
1425
+ .. math::
1426
+ n! \approx \sqrt{2\pi n}\left( \frac{n}{e} \right)^{n}
1427
+
1428
+ Note:
1429
+ Calculating the logarithm of a negative number or the exponent of a large positive number under Ascend
1430
+ will have a different range of return values and results different from those under GPU and CPU.
1431
+
1432
+ Args:
1433
+ log_input (bool, optional): Whether use log input. Default: ``True`` .
1434
+ full (bool, optional): Whether include the Stirling approximation term in the loss calculation.
1435
+ Default: ``False`` .
1436
+ eps (float, optional): Lower bound of `input` when calculating logarithms. Default: ``1e-08`` .
1437
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
1438
+ ``'sum'`` . Default: ``'mean'`` .
1439
+
1440
+ - ``'none'``: no reduction will be applied.
1441
+ - ``'mean'``: compute and return the mean of elements in the output.
1442
+ - ``'sum'``: the output elements will be summed.
1443
+
1444
+ Inputs:
1445
+ - **input** (Tensor) - The input Tensor. The shape can be any number of dimensions.
1446
+ - **target** (Tensor) - The label Tensor which has the same shape as `input`.
1447
+
1448
+ Outputs:
1449
+ Tensor or Scalar, if `reduction` is ``'none'``, then output is a tensor and has the same shape as `input`.
1450
+ Otherwise it is a scalar.
1451
+
1452
+ Raises:
1453
+ TypeError: If `reduction` is not a str.
1454
+ TypeError: If neither `input` nor `target` is a tensor.
1455
+ TypeError: If dtype of `input` or `target` is not currently supported.
1456
+
1457
+ Supported Platforms:
1458
+ ``Ascend`` ``GPU`` ``CPU``
1459
+
1460
+ Examples:
1461
+ >>> import mindspore as ms
1462
+ >>> import mindspore.nn as nn
1463
+ >>> x = ms.Tensor([[0.3, 0.7], [0.5, 0.5]])
1464
+ >>> target = ms.Tensor([[1.0, 2.0], [3.0, 4.0]])
1465
+ >>> loss = nn.PoissonNLLLoss()
1466
+ >>> output = loss(x, target)
1467
+ >>> print(output.asnumpy())
1468
+ 0.3652635
1469
+ """
1470
+
1471
+ def __init__(self, log_input=True, full=False, eps=1e-08, reduction="mean"):
1472
+ """Initialize PoissonNLLLoss."""
1473
+ super(PoissonNLLLoss, self).__init__(reduction=reduction)
1474
+ self.log_input = log_input
1475
+ self.full = full
1476
+ self.eps = eps
1477
+ self.maximum = P.Maximum()
1478
+ self.cast = P.Cast()
1479
+
1480
+ def construct(self, input, target):
1481
+ _check_is_tensor('input', input, self.cls_name)
1482
+ _check_is_tensor('target', target, self.cls_name)
1483
+ target = self.cast(target, input.dtype)
1484
+ if self.log_input:
1485
+ loss = input.exp() - target * input
1486
+ else:
1487
+ loss = input - target * ((input + self.eps).log())
1488
+ if self.full:
1489
+ target = self.maximum(target, self.eps)
1490
+ stirling_term = (target > 1) * ((target + 0.5) * target.log() - target + get_half_ln_2_pi())
1491
+ loss += F.masked_fill(stirling_term, target <= 1, F.cast(0, stirling_term.dtype))
1492
+ out = self.get_loss(loss)
1493
+ return out
1494
+
1495
+
1496
+ @constexpr
1497
+ def get_half_ln_2_pi():
1498
+ return 0.5 * math.log(2 * math.pi)
1499
+
1500
+
1501
+ class MultiLabelSoftMarginLoss(LossBase):
1502
+ r"""
1503
+ Calculates the MultiLabelSoftMarginLoss.
1504
+ The multi-label soft margin loss is a commonly used loss function in multi-label classification tasks
1505
+ where an input sample can belong to multiple classes.
1506
+ Given an input :math:`x` and binary labels :math:`y` of size :math:`(N,C)`, where :math:`N` denotes
1507
+ the number of samples and :math:`C` denotes the number of classes.
1508
+
1509
+ .. math::
1510
+ \mathcal{loss\left( x , y \right)} = - \frac{1}{N}\frac{1}{C}\sum_{i = 1}^{N}
1511
+ \sum_{j = 1}^{C}\left(y_{ij}\log\frac{1}{1 + e^{- x_{ij}}} + \left( 1 - y_{ij}
1512
+ \right)\log\frac{e^{-x_{ij}}}{1 + e^{-x_{ij}}} \right)
1513
+
1514
+ where :math:`x_{ij}` represents the predicted score of sample :math:`i` for class :math:`j`. :math:`y_{ij}`
1515
+ represents the binary label of sample :math:`i` for class :math:`j`, where sample :math:`i` belongs to
1516
+ class :math:`j` if :math:`y_{ij}=1` , and sample :math:`i` does not belong to class :math:`j` if :math:`y_{ij}=0`.
1517
+ For a multi-label classification task, each sample may have multiple labels with a value of 1 in the binary
1518
+ label :math:`y`. `weight` will multiply to the loss of each class if given.
1519
+
1520
+ Args:
1521
+ weight (Union[Tensor, int, float]): The manual rescaling weight given to each class. Default: ``None`` .
1522
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
1523
+ ``'sum'`` . Default: ``'mean'`` .
1524
+
1525
+ - ``'none'``: no reduction will be applied.
1526
+ - ``'mean'``: compute and return the weighted mean of elements in the output.
1527
+ - ``'sum'``: the output elements will be summed.
1528
+
1529
+ Inputs:
1530
+ - **x** (Tensor) - A tensor of shape :math:`(N, C)`, where N is batch size and C is number
1531
+ of classes.
1532
+ - **target** (Tensor) - The label target Tensor which has the same shape as `x`.
1533
+
1534
+ Outputs:
1535
+ Tensor, the data type is the same as x, if the reduction is ``'none'``, its shape is (N), otherwise it is zero.
1536
+
1537
+ Raises:
1538
+ ValueError: If the rank of `x` or `target` is not 2.
1539
+
1540
+ Supported Platforms:
1541
+ ``Ascend`` ``GPU`` ``CPU``
1542
+
1543
+ Examples:
1544
+ >>> import mindspore as ms
1545
+ >>> import mindspore.nn as nn
1546
+ >>> x = ms.Tensor([[0.3, 0.6, 0.6], [0.9, 0.4, 0.2]])
1547
+ >>> target = ms.Tensor([[0.0, 0.0, 1.0], [0.0, 0.0, 1.0]])
1548
+ >>> loss = nn.MultiLabelSoftMarginLoss(reduction='mean')
1549
+ >>> out = loss(x, target)
1550
+ >>> print(out.asnumpy())
1551
+ 0.84693956
1552
+ """
1553
+
1554
+ def __init__(self, weight=None, reduction="mean"):
1555
+ """Initialize MultiLabelSoftMarginLoss."""
1556
+ super(MultiLabelSoftMarginLoss, self).__init__(reduction)
1557
+ self.weight = weight
1558
+ self.reduction = reduction
1559
+
1560
+ def construct(self, x, target):
1561
+ return F.multilabel_soft_margin_loss(x, target, self.weight, self.reduction)
1562
+
1563
+
1564
+ class MultiMarginLoss(LossBase):
1565
+ r"""
1566
+ Creates a criterion that optimizes a multi-class classification hinge
1567
+ loss (margin-based loss) between input :math:`x` (a 2D mini-batch `Tensor`) and
1568
+ output :math:`y` (which is a 1D tensor of target class indices,
1569
+ :math:`0 \leq y \leq \text{x.size}(1)-1`):
1570
+
1571
+ For each mini-batch sample, the loss in terms of the 1D input :math:`x` and scalar
1572
+ output :math:`y` is:
1573
+
1574
+ .. math::
1575
+ \text{loss}(x, y) = \frac{\sum_i \max(0, w[y] * (\text{margin} - x[y] + x[i]))^p}{\text{x.size}(0)}
1576
+
1577
+ where :math:`x \in \left\{0, \; \cdots , \; \text{x.size}(0) - 1\right\}`
1578
+ and :math:`i \neq y`.
1579
+
1580
+ Args:
1581
+ p (int, optional): The norm degree for pairwise distance. Should be 1 or 2. Default: ``1`` .
1582
+ margin (float, optional): A parameter to change pairwise distance. Default: 1.0.
1583
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
1584
+ ``'sum'`` . Default: ``'mean'`` .
1585
+
1586
+ - ``'none'``: no reduction will be applied.
1587
+ - ``'mean'``: compute and return the weighted mean of elements in the output.
1588
+ - ``'sum'``: the output elements will be summed.
1589
+
1590
+ weight (Tensor, optional): The rescaling weight to each class with shape :math:`(C,)`. Data type only
1591
+ support float32, float16 or float64. Default: ``None`` , all classes are weighted equally.
1592
+
1593
+ Inputs:
1594
+ - **x** (Tensor) - Input x, with shape :math:`(N, C)`. Data type only supports float32, float16 or float64.
1595
+ x is :math:`x` in the above formula.
1596
+ - **target** (Tensor) - Ground truth labels, with shape :math:`(N,)`. Data type only supports int64. The
1597
+ value of target should be non-negative, less than C. `target` is :math:`y` in the above formula.
1598
+
1599
+ Outputs:
1600
+ Tensor. When `reduction` is ``'none'``, the shape is :math:`(N,)`.
1601
+ Otherwise, it is a scalar. Has the same data type with `x`.
1602
+
1603
+ Raises:
1604
+ TypeError: If dtype of `p` or `target` is not int.
1605
+ TypeError: If dtype of `margin` is not float.
1606
+ TypeError: If dtype of `reduction` is not str.
1607
+ TypeError: If dtype of `x` is not float16, float or float64.
1608
+ TypeError: If dtype of `weight` and `x` is not the same.
1609
+ ValueError: If `p` is not 1 or 2.
1610
+ ValueError: If `reduction` is not one of { ``'none'`` , ``'sum'`` , ``'mean'`` }.
1611
+ ValueError: If shape[0] of `x` is not equal to shape[0] of `target`.
1612
+ ValueError: If shape[1] of `x` is not equal to shape[0] of `weight`.
1613
+ ValueError: IF rank of `weight` is not 1.
1614
+ ValueError: If rank of `x` is not 2 or rank of 'target' is not 1.
1615
+
1616
+ Supported Platforms:
1617
+ ``Ascend`` ``GPU`` ``CPU``
1618
+
1619
+ Examples:
1620
+ >>> import mindspore as ms
1621
+ >>> import mindspore.nn as nn
1622
+ >>> import numpy as np
1623
+ >>> x = ms.Tensor(np.ones(shape=[3, 3]), ms.float32)
1624
+ >>> target = ms.Tensor(np.array([1, 2, 1]), ms.int64)
1625
+ >>> loss = nn.MultiMarginLoss()
1626
+ >>> output = loss(x, target)
1627
+ >>> print(output)
1628
+ 0.6666667
1629
+ """
1630
+
1631
+ def __init__(self, p=1, margin=1.0, reduction='mean', weight=None):
1632
+ """Initialize MultiMarginLoss."""
1633
+ super(MultiMarginLoss, self).__init__()
1634
+ self.multi_margin_loss = MultiMarginLossOp(p=p, margin=margin, reduction=reduction)
1635
+ self.weight = weight
1636
+
1637
+ def construct(self, x, target, weight=None):
1638
+ _check_is_tensor('x', x, self.cls_name)
1639
+ _check_is_tensor('target', target, self.cls_name)
1640
+ if self.weight is not None:
1641
+ weight = self.weight
1642
+ weight_one = weight is None
1643
+ if not weight_one:
1644
+ _check_is_tensor('weight', weight, self.cls_name)
1645
+ else:
1646
+ weight = F.fill(x.dtype, x.astype('float32')[0].shape, 1)
1647
+ loss = self.multi_margin_loss(x, target, weight)
1648
+ return loss
1649
+
1650
+
1651
+ class BCELoss(LossBase):
1652
+ r"""
1653
+ BCELoss creates a criterion to measure the binary cross entropy between the true labels and predicted labels.
1654
+
1655
+ Set the predicted labels as :math:`x`, true labels as :math:`y`, the output loss as :math:`\ell(x, y)`.
1656
+ The formula is as follow:
1657
+
1658
+ .. math::
1659
+ L = \{l_1,\dots,l_n,\dots,l_N\}^\top, \quad
1660
+ l_n = - w_n \left[ y_n \cdot \log x_n + (1 - y_n) \cdot \log (1 - x_n) \right]
1661
+
1662
+ where N is the batch size. Then,
1663
+
1664
+ .. math::
1665
+ \ell(x, y) = \begin{cases}
1666
+ L, & \text{if reduction} = \text{'none';}\\
1667
+ \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\
1668
+ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.}
1669
+ \end{cases}
1670
+
1671
+ Note:
1672
+ Note that the predicted labels should always be the output of sigmoid. Because it is a two-class
1673
+ classification, the true labels should be numbers between 0 and 1.
1674
+ And if input is either 0 or 1, one of the log terms would be mathematically undefined in the above loss
1675
+ equation.
1676
+
1677
+ Args:
1678
+ weight (Tensor, optional): A rescaling weight applied to the loss of each batch element.
1679
+ And it must have the same shape and data type as `inputs`. Default: ``None`` .
1680
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
1681
+ ``'sum'`` . Default: ``'mean'`` .
1682
+
1683
+ - ``'none'``: no reduction will be applied.
1684
+ - ``'mean'``: compute and return the weighted mean of elements in the output.
1685
+ - ``'sum'``: the output elements will be summed.
1686
+
1687
+ Inputs:
1688
+ - **logits** (Tensor) - The input tensor with shape :math:`(N, *)` where :math:`*` means, any number
1689
+ of additional dimensions. The data type must be float16 or float32.
1690
+ - **labels** (Tensor) - The label tensor with shape :math:`(N, *)` where :math:`*` means, any number
1691
+ of additional dimensions. The same shape and data type as `logits`.
1692
+
1693
+ Outputs:
1694
+ Tensor, has the same dtype as `logits`. if `reduction` is ``'none'``, then it has the same shape as `logits`.
1695
+ Otherwise, it is a scalar Tensor.
1696
+
1697
+ Raises:
1698
+ TypeError: If dtype of `logits`, `labels` or `weight` (if given) is neither float16 not float32.
1699
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
1700
+ ValueError: If shape of `logits` is not the same as `labels` or `weight` (if given).
1701
+
1702
+ Supported Platforms:
1703
+ ``Ascend`` ``GPU`` ``CPU``
1704
+
1705
+ Examples:
1706
+ >>> import mindspore as ms
1707
+ >>> import mindspore.nn as nn
1708
+ >>> import numpy as np
1709
+ >>> weight = ms.Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 3.3, 2.2]]), ms.float32)
1710
+ >>> loss = nn.BCELoss(weight=weight, reduction='mean')
1711
+ >>> logits = ms.Tensor(np.array([[0.1, 0.2, 0.3], [0.5, 0.7, 0.9]]), ms.float32)
1712
+ >>> labels = ms.Tensor(np.array([[0, 1, 0], [0, 0, 1]]), ms.float32)
1713
+ >>> output = loss(logits, labels)
1714
+ >>> print(output)
1715
+ 1.8952923
1716
+ """
1717
+
1718
+ def __init__(self, weight=None, reduction='mean'):
1719
+ """Initialize BCELoss."""
1720
+ super(BCELoss, self).__init__(reduction)
1721
+ self.binary_cross_entropy = P.BinaryCrossEntropy(reduction=reduction)
1722
+ self.weight_one = weight is None
1723
+ if not self.weight_one:
1724
+ self.weight = weight
1725
+ else:
1726
+ self.ones = P.OnesLike()
1727
+
1728
+ def construct(self, logits, labels):
1729
+ _check_is_tensor('logits', logits, self.cls_name)
1730
+ _check_is_tensor('labels', labels, self.cls_name)
1731
+ if self.weight_one:
1732
+ weight = self.ones(logits)
1733
+ else:
1734
+ weight = self.weight
1735
+ loss = self.binary_cross_entropy(logits, labels, weight)
1736
+ return loss
1737
+
1738
+
1739
+ class CosineEmbeddingLoss(LossBase):
1740
+ r"""
1741
+ CosineEmbeddingLoss creates a criterion to measure the similarity between two tensors using cosine distance.
1742
+
1743
+ Given two tensors :math:`x1`, :math:`x2`, and a Tensor label :math:`y` with values 1 or -1:
1744
+
1745
+ .. math::
1746
+ loss(x_1, x_2, y) = \begin{cases}
1747
+ 1-cos(x_1, x_2), & \text{if } y = 1\\
1748
+ \max(0, cos(x_1, x_2)-margin), & \text{if } y = -1\\
1749
+ \end{cases}
1750
+
1751
+ Args:
1752
+ margin (float): Should be in [-1.0, 1.0]. Default: ``0.0`` .
1753
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
1754
+ ``'sum'`` . Default: ``'mean'`` .
1755
+
1756
+ - ``'none'``: no reduction will be applied.
1757
+ - ``'mean'``: compute and return the mean of elements in the output.
1758
+ - ``'sum'``: the output elements will be summed.
1759
+
1760
+ Inputs:
1761
+ - **logits_x1** (Tensor) - Tensor of shape :math:`(N, *)` where :math:`*` means, any number
1762
+ of additional dimensions.
1763
+ - **logits_x2** (Tensor) - Tensor of shape :math:`(N, *)`, same shape and dtype as `logits_x1`.
1764
+ - **labels** (Tensor) - Contains value 1 or -1. Suppose the shape of `logits_x1` is
1765
+ :math:`(x_1, x_2, x_3, ..., x_R)`, then the shape of `labels` must be :math:`(x_1, x_3, x_4, ..., x_R)`.
1766
+
1767
+ Outputs:
1768
+ Tensor or Scalar, if `reduction` is ``"none"``, its shape is the same as `labels`.
1769
+ Otherwise, a scalar value will be returned.
1770
+
1771
+ Raises:
1772
+ TypeError: If `margin` is not a float.
1773
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
1774
+ ValueError: If `margin` is not in range [-1.0, 1.0].
1775
+
1776
+ Supported Platforms:
1777
+ ``Ascend`` ``GPU`` ``CPU``
1778
+
1779
+ Examples:
1780
+ >>> import mindspore as ms
1781
+ >>> import mindspore.nn as nn
1782
+ >>> import numpy as np
1783
+ >>> logits_x1 = ms.Tensor(np.array([[0.3, 0.8], [0.4, 0.3]]), ms.float32)
1784
+ >>> logits_x2 = ms.Tensor(np.array([[0.4, 1.2], [-0.4, -0.9]]), ms.float32)
1785
+ >>> labels = ms.Tensor(np.array([1, -1]), ms.int32)
1786
+ >>> cosine_embedding_loss = nn.CosineEmbeddingLoss()
1787
+ >>> output = cosine_embedding_loss(logits_x1, logits_x2, labels)
1788
+ >>> print(output)
1789
+ 0.0003425479
1790
+ """
1791
+
1792
+ def __init__(self, margin=0.0, reduction="mean"):
1793
+ """Initialize CosineEmbeddingLoss."""
1794
+ super(CosineEmbeddingLoss, self).__init__(reduction)
1795
+ self.reduce_sum = P.ReduceSum()
1796
+ self.maximum = P.Maximum()
1797
+ validator.check_value_type("margin", margin, [float], self.cls_name)
1798
+ self.margin = validator.check_float_range(margin, -1.0, 1.0, validator.INC_BOTH, "margin", self.cls_name)
1799
+
1800
+ def construct(self, logits_x1, logits_x2, labels):
1801
+ _check_is_tensor('logits_x1', logits_x1, self.cls_name)
1802
+ _check_is_tensor('logits_x2', logits_x2, self.cls_name)
1803
+ _check_is_tensor('labels', labels, self.cls_name)
1804
+ inner.same_type_shape_(logits_x1, logits_x2)
1805
+ # if labels > 0, 1-cosine(logits_x1, logits_x2)
1806
+ # else, max(0, cosine(logits_x1, logits_x2)-margin)
1807
+ prod_sum = self.reduce_sum(logits_x1 * logits_x2, (1,))
1808
+ square1 = self.reduce_sum(F.square(logits_x1), (1,))
1809
+ square2 = self.reduce_sum(F.square(logits_x2), (1,))
1810
+ denom = F.sqrt(square1) * F.sqrt(square2)
1811
+ cosine = prod_sum / denom
1812
+
1813
+ pos_value = 1.0 - cosine
1814
+ neg_value = self.maximum(cosine - self.margin, 0.0)
1815
+ zeros = F.zeros_like(cosine)
1816
+ pos_part = F.select(labels == 1, pos_value, zeros)
1817
+ neg_part = F.select(labels == -1, neg_value, zeros)
1818
+ output_unreduced = pos_part + neg_part
1819
+
1820
+ return self.get_loss(output_unreduced)
1821
+
1822
+
1823
+ class MultilabelMarginLoss(LossBase):
1824
+ r"""
1825
+ Creates a loss criterion that minimizes the hinge loss for multi-class
1826
+ classification tasks.
1827
+ It takes a 2D mini-batch Tensor :math:`x` as input and a 2D
1828
+ Tensor :math:`y` containing target class indices as output.
1829
+
1830
+ Each sample in the mini-batch, the loss is computed as follows:
1831
+
1832
+ .. math::
1833
+ \text{loss}(x, y) = \sum_{ij}\frac{\max(0, 1 - (x[y[j]] - x[i]))}{\text{x.size}(0)}
1834
+
1835
+ where :math:`x \in \left\{0, \; \cdots , \; \text{x.size}(0) - 1\right\}`, \
1836
+ :math:`y \in \left\{0, \; \cdots , \; \text{y.size}(0) - 1\right\}`, \
1837
+ :math:`0 \leq y[j] \leq \text{x.size}(0)-1`, \
1838
+ and for all :math:`i` and :math:`j`, :math:`i` does not equal to :math:`y[j]`.
1839
+
1840
+ Furthermore, both :math:`y` and :math:`x` should have identical sizes.
1841
+
1842
+ Note:
1843
+ For this operator, only a contiguous sequence of non-negative targets that starts at
1844
+ the beginning is taken into consideration, which means that different samples can have different
1845
+ number of target classes.
1846
+
1847
+ Args:
1848
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
1849
+ ``'sum'`` . Default: ``'mean'`` .
1850
+
1851
+ - ``'none'``: no reduction will be applied.
1852
+ - ``'mean'``: compute and return the mean of elements in the output.
1853
+ - ``'sum'``: the output elements will be summed.
1854
+
1855
+ Inputs:
1856
+ - **x** (Tensor) - Predict data. Tensor of shape :math:`(C)` or :math:`(N, C)`, where :math:`N`
1857
+ is the batch size and :math:`C` is the number of classes. Data type must be float16 or float32.
1858
+ - **target** (Tensor) - Ground truth data, with the same shape as `x`, data type must be int32 and
1859
+ label targets padded by -1.
1860
+
1861
+ Outputs:
1862
+ - **y** (Union[Tensor, Scalar]) - The loss of MultilabelMarginLoss. If `reduction` is ``"none"``, its shape
1863
+ is :math:`(N)`. Otherwise, a scalar value will be returned.
1864
+
1865
+ Raises:
1866
+ TypeError: If `x` or `target` is not a Tensor.
1867
+ TypeError: If dtype of `x` is neither float16 nor float32.
1868
+ TypeError: If dtype of `target` is not int32.
1869
+ ValueError: If length of shape of `x` is neither 1 nor 2.
1870
+ ValueError: If shape of `x` is not the same as `target`.
1871
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
1872
+
1873
+ Supported Platforms:
1874
+ ``Ascend`` ``GPU``
1875
+
1876
+ Examples:
1877
+ >>> import mindspore as ms
1878
+ >>> import mindspore.nn as nn
1879
+ >>> import numpy as np
1880
+ >>> loss = nn.MultilabelMarginLoss()
1881
+ >>> x = ms.Tensor(np.array([[0.1, 0.2, 0.4, 0.8], [0.2, 0.3, 0.5, 0.7]]), ms.float32)
1882
+ >>> target = ms.Tensor(np.array([[1, 2, 0, 3], [2, 3, -1, 1]]), ms.int32)
1883
+ >>> output = loss(x, target)
1884
+ >>> print(output)
1885
+ 0.325
1886
+ """
1887
+
1888
+ def __init__(self, reduction='mean'):
1889
+ super(MultilabelMarginLoss, self).__init__()
1890
+ self.multilabel_margin_loss = MultilabelMarginLossOp(reduction=reduction)
1891
+
1892
+ def construct(self, x, target):
1893
+ loss, _ = self.multilabel_margin_loss(x, target)
1894
+ return loss
1895
+
1896
+
1897
+ class BCEWithLogitsLoss(LossBase):
1898
+ r"""
1899
+ Adds sigmoid activation function to input `input` as logits, and uses the given logits to compute binary cross
1900
+ entropy between the `input` and the `target`.
1901
+
1902
+ Sets input `input` as :math:`X`, input `target` as :math:`Y`, output as :math:`L`. Then,
1903
+
1904
+ .. math::
1905
+ p_{ij} = sigmoid(X_{ij}) = \frac{1}{1 + e^{-X_{ij}}}
1906
+
1907
+ .. math::
1908
+ L_{ij} = -[Y_{ij} \cdot \log(p_{ij}) + (1 - Y_{ij}) \cdot \log(1 - p_{ij})]
1909
+
1910
+ Then,
1911
+
1912
+ .. math::
1913
+ \ell(x, y) = \begin{cases}
1914
+ L, & \text{if reduction} = \text{'none';}\\
1915
+ \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\
1916
+ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.}
1917
+ \end{cases}
1918
+
1919
+ Args:
1920
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
1921
+ ``'sum'`` . Default: ``'mean'`` .
1922
+
1923
+ - ``'none'``: no reduction will be applied.
1924
+ - ``'mean'``: compute and return the weighted mean of elements in the output.
1925
+ - ``'sum'``: the output elements will be summed.
1926
+
1927
+ weight (Tensor, optional): A rescaling weight applied to the loss of each batch element.
1928
+ If not None, it can be broadcast to a tensor with shape of `input`,
1929
+ data type must be float16, float32 or bfloat16(only Atlas A2 series products are supported).
1930
+ Default: ``None`` .
1931
+ pos_weight (Tensor, optional): A weight of positive examples. Must be a vector with length equal to the
1932
+ number of classes. If not None, it must be broadcast to a tensor with shape of `input`, data type
1933
+ must be float16, float32 or bfloat16(only Atlas A2 series products are supported). Default: ``None`` .
1934
+
1935
+ Inputs:
1936
+ - **input** (Tensor) - Input `input` with shape :math:`(N, *)` where :math:`*` means, any number
1937
+ of additional dimensions. The data type must be float16, float32 or bfloat16(only Atlas A2 series products
1938
+ are supported).
1939
+ - **target** (Tensor) - Ground truth label with shape :math:`(N, *)` where :math:`*` means, any number
1940
+ of additional dimensions. The same shape and data type as `input`.
1941
+
1942
+ Outputs:
1943
+ Tensor or Scalar, if `reduction` is ``'none'``, its shape is the same as `input`.
1944
+ Otherwise, a scalar value will be returned.
1945
+
1946
+ Raises:
1947
+ TypeError: If input `input` or `target` is not Tensor.
1948
+ TypeError: If data type of `input` or `target` is not float16, float32 or bfloat16.
1949
+ TypeError: If `weight` or `pos_weight` is a parameter.
1950
+ TypeError: If data type of `weight` or `pos_weight` is not float16 , float32 or bfloat16.
1951
+ TypeError: If data type of `reduction` is not string.
1952
+ ValueError: If `weight` or `pos_weight` can not be broadcast to a tensor with shape of `input`.
1953
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
1954
+
1955
+ Supported Platforms:
1956
+ ``Ascend`` ``GPU`` ``CPU``
1957
+
1958
+ Examples:
1959
+ >>> import mindspore as ms
1960
+ >>> import mindspore.nn as nn
1961
+ >>> import numpy as np
1962
+ >>> input = ms.Tensor(np.array([[-0.8, 1.2, 0.7], [-0.1, -0.4, 0.7]]).astype(np.float32))
1963
+ >>> target = ms.Tensor(np.array([[0.3, 0.8, 1.2], [-0.6, 0.1, 2.2]]).astype(np.float32))
1964
+ >>> loss = nn.BCEWithLogitsLoss()
1965
+ >>> output = loss(input, target)
1966
+ >>> print(output)
1967
+ 0.3463612
1968
+ """
1969
+
1970
+ def __init__(self, reduction='mean', weight=None, pos_weight=None):
1971
+ """Initialize BCEWithLogitsLoss."""
1972
+ super(BCEWithLogitsLoss, self).__init__()
1973
+ self.reduction = reduction
1974
+ if isinstance(weight, Parameter):
1975
+ raise TypeError(f"For '{self.cls_name}', the 'weight' can not be a Parameter.")
1976
+ if isinstance(pos_weight, Parameter):
1977
+ raise TypeError(f"For '{self.cls_name}', the 'pos_weight' can not be a Parameter.")
1978
+ self.weight = weight
1979
+ self.pos_weight = pos_weight
1980
+
1981
+ def construct(self, input, target):
1982
+ _check_is_tensor('input', input, self.cls_name)
1983
+ _check_is_tensor('target', target, self.cls_name)
1984
+ loss = ops.binary_cross_entropy_with_logits(input, target, self.weight, self.pos_weight, self.reduction)
1985
+ return loss
1986
+
1987
+
1988
+ @_primexpr
1989
+ def _check_ndim(logits_nidm, labels_ndim, prime_name=None):
1990
+ '''Internal function, used to check whether the dimension of logits and labels meets the requirements.'''
1991
+ msg_prefix = f'For \'{prime_name}\', the' if prime_name else "The"
1992
+ if logits_nidm < 2 or logits_nidm > 4:
1993
+ raise ValueError(f"{msg_prefix} dimensions of 'logits' must be in [2, 4], but got "
1994
+ f"dimension of 'logits' {logits_nidm}.")
1995
+ if labels_ndim < 2 or labels_ndim > 4:
1996
+ raise ValueError(f"{msg_prefix} dimensions of 'labels' must be in [2, 4], but got "
1997
+ f"dimension of 'labels' {labels_ndim}.")
1998
+ if logits_nidm != labels_ndim:
1999
+ raise ValueError(f"{msg_prefix} dimensions of 'logits' and 'labels' must be equal, but got "
2000
+ f"dimension of 'logits' {logits_nidm} and dimension of 'labels' {labels_ndim}.")
2001
+
2002
+
2003
+ @_primexpr
2004
+ def _check_channel_and_shape(logits, labels, prime_name=None):
2005
+ """Internal function, used to check whether the channels or shape of logits and labels meets the requirements."""
2006
+ msg_prefix = f'For \'{prime_name}\', the' if prime_name else "The"
2007
+ if logits == 1:
2008
+ raise ValueError(f"{msg_prefix} 'logits'.shape[1] cannot be one, but got {logits}.")
2009
+ if labels not in (1, logits):
2010
+ raise ValueError(f"{msg_prefix} 'labels'.shape[1] must be one or equal to 'logits'.shape[1]: {logits}, "
2011
+ f"but got {labels}.")
2012
+
2013
+
2014
+ @constexpr
2015
+ def _check_input_dtype(labels_dtype, cls_name):
2016
+ """Internal function, used to check whether the data type of labels meets the requirements."""
2017
+ validator.check_type_name("labels", labels_dtype,
2018
+ [mstype.int32, mstype.int64, mstype.float16, mstype.float32], cls_name)
2019
+
2020
+
2021
+ class FocalLoss(LossBase):
2022
+ r"""
2023
+ It is a loss function to solve the imbalance of categories and the difference of
2024
+ classification difficulty.
2025
+ The loss function proposed by Kaiming team in their paper
2026
+ `Focal Loss for Dense Object Detection <https://arxiv.org/pdf/1708.02002.pdf>`_ improves the
2027
+ effect of image object detection.
2028
+ The function is shown as follows:
2029
+
2030
+ .. math::
2031
+ FL(p_t) = -(1-p_t)^\gamma \log(p_t)
2032
+
2033
+ Args:
2034
+ gamma (float): Gamma is used to adjust the steepness of weight curve in focal loss. Default: ``2.0`` .
2035
+ weight (Union[Tensor, None]): A rescaling weight applied to the loss of each batch element. The dimension of
2036
+ weight should be 1. If None, no weight is applied. Default: ``None`` .
2037
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
2038
+ ``'sum'`` . Default: ``'mean'`` .
2039
+
2040
+ - ``'none'``: no reduction will be applied.
2041
+ - ``'mean'``: compute and return the weighted mean of elements in the output.
2042
+ - ``'sum'``: the output elements will be summed.
2043
+
2044
+ Inputs:
2045
+ - **logits** (Tensor) - Tensor of shape should be :math:`(N, C)` or :math:`(N, C, H)` or :math:`(N, C, H, W)`.
2046
+ Where :math:`C` is the number of classes. Its value is greater than 1. If the shape is :math:`(N, C, H, W)`
2047
+ or :math:`(N, C, H)`, the :math:`H` or product of :math:`H` and :math:`W` should be the same as labels.
2048
+ - **labels** (Tensor) - Tensor of shape should be :math:`(N, C)` or :math:`(N, C, H)` or :math:`(N, C, H, W)`.
2049
+ The value of :math:`C` is 1 or it needs to be the same as predict's :math:`C`. If :math:`C` is not 1,
2050
+ the shape of target should be the same as that of predict, where :math:`C` is the number of classes.
2051
+ If the shape is :math:`(N, C, H, W)` or :math:`(N, C, H)`, the :math:`H` or product of :math:`H`
2052
+ and :math:`W` should be the same as logits. The value of `labels` is should be in the
2053
+ range [-:math:`C`, :math:`C`). Where :math:`C` is the number of classes in logits.
2054
+
2055
+ Outputs:
2056
+ Tensor or Scalar, if `reduction` is ``"none"``, its shape is the same as `logits`.
2057
+ Otherwise, a scalar value will be returned.
2058
+
2059
+ Raises:
2060
+ TypeError: If the data type of `gamma` is not a float.
2061
+ TypeError: If `weight` is not a Tensor.
2062
+ ValueError: If `labels` dim is different from `logits`.
2063
+ ValueError: If `labels` channel is not 1 and `labels` shape is different from `logits`.
2064
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
2065
+
2066
+ Supported Platforms:
2067
+ ``Ascend``
2068
+
2069
+ Examples:
2070
+ >>> import mindspore as ms
2071
+ >>> import mindspore.nn as nn
2072
+ >>> logits = ms.Tensor([[0.8, 1.4], [0.5, 0.9], [1.2, 0.9]], ms.float32)
2073
+ >>> labels = ms.Tensor([[1], [1], [0]], ms.int32)
2074
+ >>> focalloss = nn.FocalLoss(weight=ms.Tensor([1, 2]), gamma=2.0, reduction='mean')
2075
+ >>> output = focalloss(logits, labels)
2076
+ >>> print(output)
2077
+ 0.12516622
2078
+ """
2079
+
2080
+ def __init__(self, weight=None, gamma=2.0, reduction='mean'):
2081
+ """Initialize FocalLoss."""
2082
+ super(FocalLoss, self).__init__(reduction=reduction)
2083
+
2084
+ self.gamma = validator.check_value_type("gamma", gamma, [float])
2085
+ if weight is not None and not isinstance(weight, Tensor):
2086
+ raise TypeError(f"For '{self.cls_name}', the type of 'weight' must be a Tensor, "
2087
+ f"but got {type(weight).__name__}.")
2088
+ if isinstance(weight, Tensor) and weight.ndim != 1:
2089
+ raise ValueError(f"For '{self.cls_name}', the dimension of 'weight' must be 1, but got {weight.ndim}.")
2090
+ self.weight = weight
2091
+ self.expand_dims = P.ExpandDims()
2092
+ self.gather_d = P.GatherD()
2093
+ self.squeeze = P.Squeeze(axis=1)
2094
+ self.tile = P.Tile()
2095
+ self.cast = P.Cast()
2096
+ self.dtype = P.DType()
2097
+ self.logsoftmax = nn.LogSoftmax(1)
2098
+
2099
+ def construct(self, logits, labels):
2100
+ _check_is_tensor('logits', logits, self.cls_name)
2101
+ _check_is_tensor('labels', labels, self.cls_name)
2102
+ labelss = labels
2103
+ _check_ndim(logits.ndim, labelss.ndim, self.cls_name)
2104
+ _check_channel_and_shape(logits.shape[1], labelss.shape[1], self.cls_name)
2105
+ _check_input_dtype(self.dtype(labelss), self.cls_name)
2106
+
2107
+ if logits.ndim > 2:
2108
+ logits = logits.view(logits.shape[0], logits.shape[1], -1)
2109
+ labelss = labelss.view(labelss.shape[0], labelss.shape[1], -1)
2110
+ else:
2111
+ logits = self.expand_dims(logits, 2)
2112
+ labelss = self.expand_dims(labelss, 2)
2113
+
2114
+ log_probability = self.logsoftmax(logits)
2115
+
2116
+ if labels.shape[1] == 1:
2117
+ log_probability = self.gather_d(log_probability, 1, self.cast(labelss, mindspore.int32))
2118
+ log_probability = self.squeeze(log_probability)
2119
+
2120
+ probability = F.exp(log_probability)
2121
+
2122
+ if self.weight is not None:
2123
+ convert_weight = self.weight[None, :, None]
2124
+ convert_weight = self.tile(convert_weight, (labelss.shape[0], 1, labelss.shape[2]))
2125
+ if labels.shape[1] == 1:
2126
+ convert_weight = self.gather_d(convert_weight, 1, self.cast(labelss, mindspore.int32))
2127
+ convert_weight = self.squeeze(convert_weight)
2128
+ log_probability = log_probability * convert_weight
2129
+
2130
+ weight = F.pows(-1 * probability + 1.0, self.gamma)
2131
+ if labels.shape[1] == 1:
2132
+ loss = (-1 * weight * log_probability).mean(axis=1)
2133
+ else:
2134
+ loss = (-1 * weight * labelss * log_probability).mean(axis=-1)
2135
+
2136
+ return self.get_loss(loss)
2137
+
2138
+
2139
+ class HuberLoss(LossBase):
2140
+ r"""
2141
+ HuberLoss calculate the error between the predicted value and the target value.
2142
+ It has the advantages of both L1Loss and MSELoss.
2143
+
2144
+ Assuming that the :math:`x` and :math:`y` are 1-D Tensor, length :math:`N`, then calculate the loss of :math:`x` and
2145
+ :math:`y` without dimensionality reduction (the reduction parameter is set to "none"). The formula is as follows:
2146
+
2147
+ .. math::
2148
+ \ell(x, y) = L = \{l_1,\dots,l_N\}^\top
2149
+
2150
+ with
2151
+
2152
+ .. math::
2153
+ l_n = \begin{cases}
2154
+ 0.5 * (x_n - y_n)^2, & \text{if } |x_n - y_n| < delta; \\
2155
+ delta * (|x_n - y_n| - 0.5 * delta), & \text{otherwise. }
2156
+ \end{cases}
2157
+
2158
+ where :math:`N` is the batch size. If `reduction` is not ``"none"``, then:
2159
+
2160
+ .. math::
2161
+ \ell(x, y) =
2162
+ \begin{cases}
2163
+ \operatorname{mean}(L), & \text{if reduction} = \text{"mean";}\\
2164
+ \operatorname{sum}(L), & \text{if reduction} = \text{"sum".}
2165
+ \end{cases}
2166
+
2167
+ Args:
2168
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
2169
+ ``'sum'`` . Default: ``'mean'`` .
2170
+
2171
+ - ``'none'``: no reduction will be applied.
2172
+ - ``'mean'``: compute and return the mean of elements in the output.
2173
+ - ``'sum'``: the output elements will be summed.
2174
+
2175
+ delta (Union[int, float]): The threshold to change between two type of loss.
2176
+ The value must be positive. Default: ``1.0`` .
2177
+
2178
+ Inputs:
2179
+ - **logits** (Tensor) - Predicted value, Tensor of any dimension. The data type must be float16 or float32.
2180
+ - **labels** (Tensor) - Target value, same dtype and shape as the `logits` in common cases.
2181
+ However, it supports the shape of `logits` is different from the shape of `labels`
2182
+ and they should be broadcasted to each other.
2183
+
2184
+ Outputs:
2185
+ Tensor or Scalar, if `reduction` is ``"none"``, return a Tensor with same shape and dtype as `logits`.
2186
+ Otherwise, a scalar value will be returned.
2187
+
2188
+ Raises:
2189
+ TypeError: If data type of `logits` or `labels` is neither float16 nor float32.
2190
+ TypeError: If data type of `logits` or `labels` are not the same.
2191
+ TypeError: If dtype of `delta` is neither float nor int.
2192
+ ValueError: If `delta` is less than or equal to 0.
2193
+ ValueError: If `reduction` is not one of ``"none"``, ``"mean"``, ``"sum"``.
2194
+ ValueError: If `logits` and `labels` have different shapes and cannot be broadcasted to each other.
2195
+
2196
+ Supported Platforms:
2197
+ ``Ascend`` ``GPU`` ``CPU``
2198
+
2199
+ Examples:
2200
+ >>> import mindspore as ms
2201
+ >>> import mindspore.nn as nn
2202
+ >>> import numpy as np
2203
+ >>> # Case 1: logits.shape = labels.shape = (3,)
2204
+ >>> loss = nn.HuberLoss()
2205
+ >>> logits = ms.Tensor(np.array([1, 2, 3]), ms.float32)
2206
+ >>> labels = ms.Tensor(np.array([1, 2, 2]), ms.float32)
2207
+ >>> output = loss(logits, labels)
2208
+ >>> print(output)
2209
+ 0.16666667
2210
+ >>> # Case 2: logits.shape = (3,), labels.shape = (2, 3)
2211
+ >>> loss = nn.HuberLoss(reduction="none")
2212
+ >>> logits = ms.Tensor(np.array([1, 2, 3]), ms.float32)
2213
+ >>> labels = ms.Tensor(np.array([[1, 1, 1], [1, 2, 2]]), ms.float32)
2214
+ >>> output = loss(logits, labels)
2215
+ >>> print(output)
2216
+ [[0. 0.5 1.5]
2217
+ [0. 0. 0.5]]
2218
+ """
2219
+
2220
+ def __init__(self, reduction="mean", delta=1.0):
2221
+ """Initialize HuberLoss."""
2222
+ super(HuberLoss, self).__init__(reduction=reduction)
2223
+ self.reduction = reduction
2224
+ self.delta = delta
2225
+
2226
+ def construct(self, logits, labels):
2227
+ return F.huber_loss(logits, labels, self.reduction, self.delta)
2228
+
2229
+
2230
+ class TripletMarginLoss(LossBase):
2231
+ r"""
2232
+ TripletMarginLoss operation.
2233
+
2234
+ Triple loss is used to measure the relative similarity between samples,
2235
+ which is measured by a triplet and a :math:`margin` with a value greater than :math:`0` .
2236
+ The triplet is composed by :math:`a`, :math:`p`, :math:`n` in the following formula.
2237
+
2238
+ The shapes of all input tensors should be :math:`(N, *)` , where :math:`N` is batch size
2239
+ and :math:`*` means any number of additional dimensions.
2240
+
2241
+ The distance swap is described in detail in the paper
2242
+ `Learning local feature descriptors with triplets and shallow convolutional neural
2243
+ networks <http://158.109.8.37/files/BRP2016.pdf>`_
2244
+ by V. Balntas, E. Riba et al.
2245
+
2246
+ The loss function for each sample in the mini-batch is:
2247
+
2248
+ .. math::
2249
+ L(a, p, n) = \max \{d(a_i, p_i) - d(a_i, n_i) + {\rm margin}, 0\}
2250
+
2251
+ where
2252
+
2253
+ .. math::
2254
+ d(x_i, y_i) = \left\lVert {\bf x}_i - {\bf y}_i \right\rVert_p
2255
+
2256
+ Args:
2257
+ p (int, optional): The degree of norm for pairwise distance. Default: ``2`` .
2258
+ eps (float, optional): Add small value to avoid division by zero. Default: ``1e-06`` .
2259
+ swap (bool, optional): The distance swap change the negative distance to the distance between positive
2260
+ sample and negative sample. Default: ``False`` .
2261
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
2262
+ ``'sum'`` . Default: ``'mean'`` .
2263
+
2264
+ - ``'none'``: no reduction will be applied.
2265
+ - ``'mean'``: compute and return the mean of elements in the output.
2266
+ - ``'sum'``: the output elements will be summed.
2267
+
2268
+ margin (Union[Tensor, float]): Make a margin between the positive pair and the negative pair.
2269
+ Default: ``1.0`` .
2270
+
2271
+ Inputs:
2272
+ - **x** (Tensor) - A sample randomly selected from the training set. Data type must be BasicType.
2273
+ :math:`a` in the above formula.
2274
+ - **positive** (Tensor) - A sample belonging to the same category as `x`, with the same type and
2275
+ shape as `x`. :math:`p` in the above formula.
2276
+ - **negative** (Tensor) - A sample belonging to the different class from `x`, with the same type and shape
2277
+ as `x`. :math:`n` in the above formula.
2278
+ - **margin** (Union[Tensor, float]) - Make a margin between the positive pair and the negative pair.
2279
+ Default: ``1.0`` .
2280
+
2281
+ Outputs:
2282
+ Tensor. If `reduction` is ``"none"``, its shape is :math:`(N)`. Otherwise, a scalar value will be returned.
2283
+
2284
+ Raises:
2285
+ TypeError: If `x` or `positive` or 'negative' is not a Tensor.
2286
+ TypeError: If dtype of `x`, `positive` and `negative` is not the same.
2287
+ TypeError: If `p` is not an int.
2288
+ TypeError: If `eps` is not a float.
2289
+ TypeError: If `swap` is not a bool.
2290
+ ValueError: If dimensions of input `x`, `positive` and `negative` are less than or equal to 1 at the same time.
2291
+ ValueError: If the dimension of input `x` or `positive` or `negative` is bigger than or equal to 8.
2292
+ ValueError: If length of shape of `margin` is not 0.
2293
+ ValueError: If shape of `x`, `positive` and `negative` cannot broadcast.
2294
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
2295
+
2296
+ Supported Platforms:
2297
+ ``GPU``
2298
+
2299
+ Examples:
2300
+ >>> import mindspore as ms
2301
+ >>> import mindspore.nn as nn
2302
+ >>> import numpy as np
2303
+ >>> loss = nn.TripletMarginLoss()
2304
+ >>> x = ms.Tensor(np.array([[0.3, 0.7], [0.5, 0.5]]), ms.float32)
2305
+ >>> positive = ms.Tensor(np.array([[0.4, 0.6], [0.4, 0.6]]), ms.float32)
2306
+ >>> negative = ms.Tensor(np.array([[0.2, 0.9], [0.3, 0.7]]), ms.float32)
2307
+ >>> output = loss(x, positive, negative)
2308
+ >>> print(output)
2309
+ 0.8881968
2310
+ """
2311
+
2312
+ def __init__(self, p=2, swap=False, eps=1e-06, reduction="mean", margin=1.):
2313
+ super(TripletMarginLoss, self).__init__()
2314
+ self.p = p
2315
+ self.swap = swap
2316
+ self.eps = eps
2317
+ self.reduction = reduction
2318
+ self.margin = margin
2319
+
2320
+ def construct(self, x, positive, negative, margin=1.):
2321
+ if self.margin != 1.0:
2322
+ margin = self.margin
2323
+ return F.triplet_margin_loss(x, positive, negative, margin=margin, p=self.p,
2324
+ eps=self.eps, swap=self.swap, reduction=self.reduction)
2325
+
2326
+
2327
+ class NLLLoss(LossBase):
2328
+ r"""
2329
+ Gets the negative log likelihood loss between logits and labels.
2330
+
2331
+ The nll loss with :math:`reduction = none` can be described as:
2332
+
2333
+ .. math::
2334
+ \ell(x, t)=L=\left\{l_{1}, \ldots, l_{N}\right\}^{\top},
2335
+ \quad l_{n}=-w_{t_{n}} x_{n, t_{n}},
2336
+ \quad w_{c}=\text { weight }[c] \cdot \mathbb{1}\{c \not= \text{ignore_index}\}
2337
+
2338
+ where :math:`x` is the logits, :math:`t` is the labels, :math:`w` is the weight,
2339
+ :math:`N` is the batch size, :math:`c` belonging to :math:`[0, C-1]` is class index,
2340
+ where :math:`C` is the number of classes.
2341
+
2342
+ If :math:`reduction \neq none` (default ``'mean'`` ), then
2343
+
2344
+ .. math::
2345
+
2346
+ \ell(x, t)=\left\{\begin{array}{ll}
2347
+ \sum_{n=1}^{N} \frac{1}{\sum_{n=1}^{N} w_{t n}} l_{n}, & \text { if reduction }=\text { 'mean', } \\
2348
+ \sum_{n=1}^{N} l_{n}, & \text { if reduction }=\text { 'sum' }
2349
+ \end{array}\right.
2350
+
2351
+ Args:
2352
+ weight (Tensor): The rescaling weight to each class. If the value is not None, the shape is :math:`(C,)`.
2353
+ The data type only supports float32 or float16. Default: ``None`` .
2354
+ ignore_index (int): Specifies a target value that is ignored (typically for padding value)
2355
+ and does not contribute to the gradient. Default: ``-100`` .
2356
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
2357
+ ``'sum'`` . Default: ``'mean'`` .
2358
+
2359
+ - ``'none'``: no reduction will be applied.
2360
+ - ``'mean'``: compute and return the weighted mean of elements in the output.
2361
+ - ``'sum'``: the output elements will be summed.
2362
+
2363
+ Inputs:
2364
+ - **logits** (Tensor) - Tensor of shape :math:`(N, C)`
2365
+ or :math:`(N, C, d_1, d_2, ..., d_K)` for :math:`K`-dimensional data, where `C = number of classes`.
2366
+ Data type must be float16 or float32. `inputs` needs to be logarithmic probability.
2367
+ - **labels** (Tensor) -:math:`(N)` or :math:`(N, d_1, d_2, ..., d_K)` for :math:`K`-dimensional data.
2368
+ Data type must be int32.
2369
+
2370
+ Returns:
2371
+ Tensor, the computed negative log likelihood loss value.
2372
+
2373
+ Raises:
2374
+ TypeError: If `weight` is not a Tensor.
2375
+ TypeError: If `ignore_index` is not an int.
2376
+ TypeError: If the data type of `weight` is not float16 or float32.
2377
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
2378
+ TypeError: If `logits` is not a Tensor.
2379
+ TypeError: If `labels` is not a Tensor.
2380
+
2381
+ Supported Platforms:
2382
+ ``Ascend`` ``GPU`` ``CPU``
2383
+
2384
+ Examples:
2385
+ >>> import mindspore as ms
2386
+ >>> import mindspore.nn as nn
2387
+ >>> import numpy as np
2388
+ >>> logits = ms.Tensor(np.random.randn(3, 5), ms.float32)
2389
+ >>> labels = ms.Tensor(np.array([1, 0, 4]), ms.int32)
2390
+ >>> loss = nn.NLLLoss()
2391
+ >>> output = loss(logits, labels)
2392
+ """
2393
+
2394
+ def __init__(self, weight=None, ignore_index=-100, reduction='mean'):
2395
+ super().__init__(reduction)
2396
+ validator.check_value_type('ignore_index', ignore_index, int, self.cls_name)
2397
+ if weight is not None:
2398
+ validator.check_value_type("weight", weight, [Tensor], self.cls_name)
2399
+ validator.check_type_name('weight', weight.dtype, [mstype.float16, mstype.float32], self.cls_name)
2400
+
2401
+ self.weight = weight
2402
+ self.ignore_index = ignore_index
2403
+ self.reduction = reduction
2404
+
2405
+ def construct(self, logits, labels):
2406
+ return F.nll_loss(logits, labels, self.weight, self.ignore_index, self.reduction)
2407
+
2408
+
2409
+ @constexpr
2410
+ def _check_cross_entropy_inputs(logits_shape, label_shape,
2411
+ logits_rank, label_rank,
2412
+ logits_dtype, label_dtype,
2413
+ prim_name=None):
2414
+ """Internal function, used to check whether the shape of logits and labels meets the requirements."""
2415
+ validator.check_type_name('logits', logits_dtype, [mstype.float16, mstype.float32], prim_name)
2416
+
2417
+ msg_prefix = f'For \'{prim_name}\', the' if prim_name else "The"
2418
+ if logits_rank == label_rank:
2419
+ validator.check_type_name('labels', label_dtype, [mstype.float16, mstype.float32], prim_name)
2420
+ if logits_shape != label_shape:
2421
+ raise ValueError(f"{msg_prefix} shape of 'logits' should be (N, C, d_0, d_1, ...), "
2422
+ f"and the shape of 'labels' should be (N, C, d_0, d_1, ...), "
2423
+ f"but get 'logits' shape: {logits_shape} and 'labels' shape: {label_shape}.")
2424
+ elif label_rank == logits_rank - 1:
2425
+ validator.check_type_name('labels', label_dtype, [mstype.int32], prim_name)
2426
+ if logits_rank != 1:
2427
+ logits_shape_new = (logits_shape[0], *logits_shape[2:])
2428
+ if logits_shape_new != label_shape:
2429
+ raise ValueError(f"{msg_prefix} shape of 'logits' should be (N, C, d_0, d_1, ...), "
2430
+ f"and the shape of 'labels' should be (N, d_0, d_1, ...), "
2431
+ f"but get 'logits' shape: {logits_shape} and 'labels' shape: {label_shape}.")
2432
+ else:
2433
+ raise ValueError(f"{msg_prefix} rank of 'logits' and 'labels' should be:\n"
2434
+ f"1. 'logits.ndim == labels.ndim' for probabilities, \n"
2435
+ f"2. 'logits.ndim - 1 == labels.ndim' for class indices, \n"
2436
+ f"but get 'logits' rank: {logits_rank} and 'labels' rank: {label_rank}.")
2437
+
2438
+
2439
+ class CrossEntropyLoss(LossBase):
2440
+ r"""
2441
+ The cross entropy loss between input and target.
2442
+
2443
+ The CrossEntropyLoss support two kind of targets:
2444
+
2445
+ - Class indices (int) in the range :math:`[0, C)` where :math:`C` is the number of classes,
2446
+ when reduction is ``none``, the loss can be described as:
2447
+
2448
+ .. math::
2449
+
2450
+ \ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad
2451
+ l_n = - w_{y_n} \log \frac{\exp(x_{n,y_n})}{\sum_{c=1}^C \exp(x_{n,c})}
2452
+ \cdot \mathbb{1}\{y_n \not= \text{ignore_index}\}
2453
+
2454
+ where :math:`x` is the inputs, :math:`t` is the target, :math:`w` is the weight,
2455
+ N is the batch size, :math:`c` belonging to [0, C-1] is class index, where :math:`C` is the number of classes.
2456
+
2457
+ If reduction is not ``'none'`` (default 'mean'), then
2458
+
2459
+ .. math::
2460
+
2461
+ \ell(x, y) = \begin{cases}
2462
+ \sum_{n=1}^N \frac{1}{\sum_{n=1}^N w_{y_n} \cdot \mathbb{1}\{y_n \not= \text{ignore_index}\}} l_n, &
2463
+ \text{if reduction} = \text{'mean',}\\
2464
+ \sum_{n=1}^N l_n, &
2465
+ \text{if reduction} = \text{'sum'.}
2466
+ \end{cases}
2467
+
2468
+ - Probabilities (float) for each class, useful when labels beyond a single class per minibatch item
2469
+ are required, the loss with reduction=none can be described as:
2470
+
2471
+ .. math::
2472
+
2473
+ \ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad
2474
+ l_n = - \sum_{c=1}^C w_c \log \frac{\exp(x_{n,c})}{\sum_{i=1}^C \exp(x_{n,i})} y_{n,c}
2475
+
2476
+ where :math:`x` is the inputs, :math:`t` is the target, :math:`w` is the weight,
2477
+ N is the batch size, :math:`c` belonging to [0, C-1] is class index, where :math:`C` is the number of classes.
2478
+
2479
+ If reduction is not ``'none'`` (default 'mean'), then
2480
+
2481
+ .. math::
2482
+
2483
+ \ell(x, y) = \begin{cases}
2484
+ \frac{\sum_{n=1}^N l_n}{N}, &
2485
+ \text{if reduction} = \text{'mean',}\\
2486
+ \sum_{n=1}^N l_n, &
2487
+ \text{if reduction} = \text{'sum'.}
2488
+ \end{cases}
2489
+
2490
+ Args:
2491
+ weight (Tensor): The rescaling weight to each class. If the value is not None, the shape is :math:`(C,)`.
2492
+ The data type only supports float32 or float16. Default: ``None`` .
2493
+ ignore_index (int): Specifies a target value that is ignored (typically for padding value)
2494
+ and does not contribute to the gradient. Default: ``-100`` .
2495
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
2496
+ ``'sum'`` . Default: ``'mean'`` .
2497
+
2498
+ - ``'none'``: no reduction will be applied.
2499
+ - ``'mean'``: compute and return the weighted mean of elements in the output.
2500
+ - ``'sum'``: the output elements will be summed.
2501
+
2502
+ label_smoothing (float): Label smoothing values, a regularization tool used to prevent the model
2503
+ from overfitting when calculating Loss. The value range is [0.0, 1.0]. Default value: ``0.0`` .
2504
+
2505
+ Inputs:
2506
+ - **logits** (Tensor) - Tensor of shape :math:`(C,)` :math:`(N, C)` or :math:`(N, C, d_1, d_2, ..., d_K)`,
2507
+ where `C = number of classes`. Data type must be float16 or float32.
2508
+ - **labels** (Tensor) - For class indices, tensor of shape :math:`()`, :math:`(N)` or
2509
+ :math:`(N, d_1, d_2, ..., d_K)` , data type must be int32.
2510
+ For probabilities, tensor of shape :math:`(C,)` :math:`(N, C)` or :math:`(N, C, d_1, d_2, ..., d_K)` ,
2511
+ data type must be float16 or float32.
2512
+
2513
+ Returns:
2514
+ Tensor, the computed cross entropy loss value.
2515
+
2516
+ Raises:
2517
+ TypeError: If `weight` is not a Tensor.
2518
+ TypeError: If `ignore_index` is not an int.
2519
+ TypeError: If the data type of `weight` is not float16 or float32.
2520
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
2521
+ TypeError: If `label_smoothing` is not a float.
2522
+ TypeError: If `logits` is not a Tensor.
2523
+ TypeError: If `labels` is not a Tensor.
2524
+
2525
+ Supported Platforms:
2526
+ ``Ascend`` ``GPU`` ``CPU``
2527
+
2528
+ Examples:
2529
+ >>> import mindspore as ms
2530
+ >>> import mindspore.nn as nn
2531
+ >>> import numpy as np
2532
+ >>> # Case 1: Indices labels
2533
+ >>> inputs = ms.Tensor(np.random.randn(3, 5), ms.float32)
2534
+ >>> target = ms.Tensor(np.array([1, 0, 4]), ms.int32)
2535
+ >>> loss = nn.CrossEntropyLoss()
2536
+ >>> output = loss(inputs, target)
2537
+ >>> # Case 2: Probability labels
2538
+ >>> inputs = ms.Tensor(np.random.randn(3, 5), ms.float32)
2539
+ >>> target = ms.Tensor(np.random.randn(3, 5), ms.float32)
2540
+ >>> loss = nn.CrossEntropyLoss()
2541
+ >>> output = loss(inputs, target)
2542
+ """
2543
+
2544
+ def __init__(self, weight=None, ignore_index=-100, reduction='mean',
2545
+ label_smoothing=0.0):
2546
+ super().__init__(reduction)
2547
+ validator.check_value_type('ignore_index', ignore_index, int, self.cls_name)
2548
+ validator.check_value_type('label_smoothing', label_smoothing, float, self.cls_name)
2549
+ validator.check_float_range(label_smoothing, 0.0, 1.0, validator.INC_BOTH, 'label_smoothing', self.cls_name)
2550
+
2551
+ if weight is not None:
2552
+ validator.check_value_type("weight", weight, [Tensor], self.cls_name)
2553
+ validator.check_type_name('weight', weight.dtype, [mstype.float16, mstype.float32], self.cls_name)
2554
+
2555
+ self.weight = weight
2556
+ self.ignore_index = ignore_index
2557
+ self.reduction = reduction
2558
+ self.label_smoothing = label_smoothing
2559
+
2560
+ def construct(self, logits, labels):
2561
+ _check_is_tensor('logits', logits, self.cls_name)
2562
+ _check_is_tensor('labels', labels, self.cls_name)
2563
+ _check_cross_entropy_inputs(logits.shape, labels.shape,
2564
+ logits.ndim, labels.ndim,
2565
+ logits.dtype, labels.dtype,
2566
+ self.cls_name)
2567
+
2568
+ return F.cross_entropy(logits, labels, self.weight, self.ignore_index, self.reduction, self.label_smoothing)
2569
+
2570
+
2571
+ class KLDivLoss(LossBase):
2572
+ r"""
2573
+ Computes the Kullback-Leibler divergence between the `logits` and the `labels`.
2574
+
2575
+ For tensors of the same shape :math:`x` and :math:`target`,
2576
+ the updating formulas of KLDivLoss algorithm are as follows,
2577
+
2578
+ .. math::
2579
+ L(x, target) = target \cdot (\log target - x)
2580
+
2581
+ Then,
2582
+
2583
+ .. math::
2584
+ \ell(x, target) = \begin{cases}
2585
+ L(x, target), & \text{if reduction} = \text{'none';}\\
2586
+ \operatorname{mean}(L(x, target)), & \text{if reduction} = \text{'mean';}\\
2587
+ \operatorname{sum}(L(x, target)) / x.\operatorname{shape}[0], & \text{if reduction} = \text{'batchmean';}\\
2588
+ \operatorname{sum}(L(x, target)), & \text{if reduction} = \text{'sum'.}
2589
+ \end{cases}
2590
+
2591
+ where :math:`x` represents `logits`,
2592
+ :math:`target` represents `labels`, and
2593
+ :math:`\ell(x, target)` represents `output`.
2594
+
2595
+ Note:
2596
+ - Currently it does not support float64 input on `Ascend`.
2597
+ - The output aligns with the mathematical definition of Kullback-Leibler divergence
2598
+ only when `reduction` is set to 'batchmean'.
2599
+
2600
+ Args:
2601
+ reduction (str): Specifies the reduction to be applied to the output. Default: ``'mean'`` .
2602
+
2603
+ - On Ascend, the value of `reduction` must be one of ``'batchmean'`` , ``'none'`` or ``'sum'`` .
2604
+ - On GPU, the value of `reduction` must be one of ``'mean'`` , ``'none'`` or ``'sum'`` .
2605
+ - On CPU, the value of `reduction` must be one of ``'mean'`` , ``'batchmean'`` , ``'none'`` or ``'sum'`` .
2606
+
2607
+ Inputs:
2608
+ - **logits** (Tensor) - The input Tensor. The data type must be float16, float32 or float64.
2609
+ - **labels** (Tensor) - The label Tensor which has the same shape and data type as `logits`.
2610
+
2611
+ Outputs:
2612
+ Tensor or Scalar, if `reduction` is ``'none'``, then output is a tensor and has the same shape as `logits`.
2613
+ Otherwise, it is a scalar.
2614
+
2615
+ Raises:
2616
+ TypeError: If `reduction` is not a str.
2617
+ TypeError: If neither `logits` nor `labels` is a Tensor.
2618
+ TypeError: If dtype of `logits` or `labels` is not currently supported.
2619
+ ValueError: If shape of `logits` is not the same as `labels`.
2620
+ RuntimeError: If `logits` or `labels` is a scalar when `reduction` is 'batchmean'.
2621
+
2622
+ Supported Platforms:
2623
+ ``Ascend`` ``GPU`` ``CPU``
2624
+
2625
+ Examples:
2626
+ >>> import mindspore as ms
2627
+ >>> import mindspore.nn as nn
2628
+ >>> import numpy as np
2629
+ >>> logits = ms.Tensor(np.array([0.2, 0.7, 0.1]), ms.float32)
2630
+ >>> labels = ms.Tensor(np.array([0., 1., 0.]), ms.float32)
2631
+ >>> loss = nn.KLDivLoss(reduction='mean')
2632
+ >>> output = loss(logits, labels)
2633
+ >>> print(output)
2634
+ -0.23333333
2635
+ """
2636
+
2637
+ def __init__(self, reduction='mean'):
2638
+ super().__init__()
2639
+ self.reduction = reduction
2640
+
2641
+ def construct(self, logits, labels):
2642
+ _check_is_tensor('logits', logits, self.cls_name)
2643
+ _check_is_tensor('labels', labels, self.cls_name)
2644
+ return F.kl_div(logits, labels, self.reduction)
2645
+
2646
+
2647
+ @_primexpr
2648
+ def _check_ctcloss_targets_shape(targets):
2649
+ """Internal function, used to check whether the shape of CTC targets meets the requirements."""
2650
+ if targets.ndim > 2:
2651
+ raise ValueError(f"For CTCLoss, when the shape of log_probs is (T, C), the dimension of targets should"
2652
+ f"be 1 or 2, but got {targets.ndim}.")
2653
+ if targets.ndim == 2 and targets.shape[0] != 1:
2654
+ raise ValueError(f"For CTCLoss, the first dimension of 2-D targets should be 1,"
2655
+ f"but got {targets.shape[0]}.")
2656
+
2657
+
2658
+ class CTCLoss(LossBase):
2659
+ """
2660
+ Calculates the CTC (Connectionist Temporal Classification) loss. It's mainly used to calculate the loss between
2661
+ the continuous, unsegemented time series and the target series.
2662
+
2663
+ For the CTC algorithm, refer to `Connectionist Temporal Classification: Labeling Unsegmented Sequence Data with
2664
+ Recurrent Neural Networks <http://www.cs.toronto.edu/~graves/icml_2006.pdf>`_ .
2665
+
2666
+ Args:
2667
+ blank (int, optional): The blank label. Default: ``0`` .
2668
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
2669
+ ``'sum'`` . Default: ``'mean'`` .
2670
+
2671
+ - ``'none'``: no reduction will be applied.
2672
+ - ``'mean'``: compute and return the mean of elements in the output.
2673
+ - ``'sum'``: the output elements will be summed.
2674
+
2675
+ zero_infinity (bool, optional): If loss is infinite, this parameter determines whether to set that loss
2676
+ and its correlated gradient to zero. Default: ``False`` .
2677
+
2678
+ Inputs:
2679
+ - **log_probs** (Tensor) - A tensor of shape :math:`(T, N, C)` or :math:`(T, C)`, where T is length of input,
2680
+ N is size of the batch and C is the number of classes. T, N and C are positive integers.
2681
+ - **targets** (Tensor) - A tensor of shape :math:`(N, S)` or (sum( `target_lengths` )),
2682
+ where S is max target length, means the target sequences.
2683
+ - **input_lengths** (Union[tuple, Tensor]) - A tuple or Tensor of shape :math:`(N)`.
2684
+ It means the lengths of the input.
2685
+ - **target_lengths** (Union[tuple, Tensor]) - A tuple or Tensor of shape :math:`(N)`.
2686
+ It means the lengths of the target.
2687
+
2688
+ Outputs:
2689
+ - **neg_log_likelihood** (Tensor) - A loss value which is differentiable with respect to each input node.
2690
+
2691
+ Raises:
2692
+ TypeError: If `log_probs` or `targets` is not a Tensor.
2693
+ TypeError: If `zero_infinity` is not a bool, `reduction` is not string.
2694
+ TypeError: If the dtype of `log_probs` is not float or double.
2695
+ TypeError: If the dtype of `targets`, `input_lengths` or `target_lengths` is not int32 or int64.
2696
+ ValueError: If `reduction` is not ``"none"``, ``"mean"`` or ``"sum"``.
2697
+ ValueError: If the value of `blank` is not in range [0, C). C is number of classes of `log_probs` .
2698
+ ValueError: If the shape of `log_probs` is :math:`(T, C)`, the dimension of `targets` is not 1 or 2.
2699
+ ValueError: If the shape of `log_probs` is :math:`(T, C)`, the first dimension of 2-D `target` is not 1.
2700
+ RuntimeError: If any value of `input_lengths` is larger than T. T is length of `log_probs` .
2701
+ RuntimeError: If any target_lengths[i] is not in range [0, input_length[i]].
2702
+
2703
+ Supported Platforms:
2704
+ ``Ascend`` ``GPU`` ``CPU``
2705
+
2706
+ Examples:
2707
+ >>> import mindspore as ms
2708
+ >>> import mindspore.nn as nn
2709
+ >>> import numpy as np
2710
+ >>> T = 5 # Input sequence length
2711
+ >>> C = 2 # Number of classes
2712
+ >>> N = 2 # Batch size
2713
+ >>> S = 3 # Target sequence length of longest target in batch (padding length)
2714
+ >>> S_min = 2 # Minimum target length, for demonstration purposes
2715
+ >>> arr = np.arange(T*N*C).reshape((T, N, C))
2716
+ >>> ms_input = ms.Tensor(arr, dtype=ms.float32)
2717
+ >>> input_lengths = np.full(shape=(N), fill_value=T)
2718
+ >>> input_lengths = ms.Tensor(input_lengths, dtype=ms.int32)
2719
+ >>> target_lengths = np.full(shape=(N), fill_value=S_min)
2720
+ >>> target_lengths = ms.Tensor(target_lengths, dtype=ms.int32)
2721
+ >>> target = np.random.randint(1, C, size=(N, S))
2722
+ >>> target = ms.Tensor(target, dtype=ms.int32)
2723
+ >>> ctc_loss = nn.CTCLoss(blank=0, reduction='none', zero_infinity=False)
2724
+ >>> loss = ctc_loss(ms_input, target, input_lengths, target_lengths)
2725
+ >>> print(loss)
2726
+ [-45.79497 -55.794968]
2727
+ >>> arr = np.arange(T*C).reshape((T, C))
2728
+ >>> ms_input = ms.Tensor(arr, dtype=ms.float32)
2729
+ >>> input_lengths = ms.Tensor([T], dtype=ms.int32)
2730
+ >>> target_lengths = ms.Tensor([S_min], dtype=ms.int32)
2731
+ >>> target = np.random.randint(1, C, size=(S_min,))
2732
+ >>> target = ms.Tensor(target, dtype=ms.int32)
2733
+ >>> ctc_loss = nn.CTCLoss(blank=0, reduction='none', zero_infinity=False)
2734
+ >>> loss = ctc_loss(ms_input, target, input_lengths, target_lengths)
2735
+ >>> print(loss)
2736
+ -25.794968
2737
+ """
2738
+
2739
+ def __init__(self, blank=0, reduction='mean', zero_infinity=False):
2740
+ super().__init__()
2741
+ self.blank = blank
2742
+ self.reduction = reduction
2743
+ self.zero_infinity = zero_infinity
2744
+
2745
+ def construct(self, log_probs, targets, input_lengths, target_lengths):
2746
+ _check_is_tensor('log_probs', log_probs, self.cls_name)
2747
+ _check_is_tensor('targets', targets, self.cls_name)
2748
+ if log_probs.ndim == 2:
2749
+ _check_ctcloss_targets_shape(targets)
2750
+ if targets.ndim == 1:
2751
+ targets = targets.expand_dims(0)
2752
+ log_probs = log_probs.expand_dims(-2)
2753
+ neg_log_hood, _ = F.ctc_loss(log_probs, targets, input_lengths, target_lengths, self.blank, self.reduction,
2754
+ self.zero_infinity)
2755
+ return neg_log_hood.squeeze(axis=0)
2756
+ neg_log_hood, _ = F.ctc_loss(log_probs, targets, input_lengths, target_lengths, self.blank, self.reduction,
2757
+ self.zero_infinity)
2758
+ return neg_log_hood
2759
+
2760
+
2761
+ class GaussianNLLLoss(LossBase):
2762
+ r"""
2763
+ Gaussian negative log likelihood loss.
2764
+
2765
+ The target values are considered to be samples from a Gaussian distribution, where the expectation and variance are
2766
+ predicted by a neural network. For `labels` modeled on a Gaussian distribution, `logits` to record expectations,
2767
+ and the variance `var` (elements are all positive), the calculated loss is:
2768
+
2769
+ .. math::
2770
+ \text{loss} = \frac{1}{2}\left(\log\left(\text{max}\left(\text{var},
2771
+ \ \text{eps}\right)\right) + \frac{\left(\text{logits} - \text{labels}\right)^2}
2772
+ {\text{max}\left(\text{var}, \ \text{eps}\right)}\right) + \text{const.}
2773
+
2774
+ where :math:`eps` is used for stability of :math:`log`. When :math:`full=True`, a constant will be added to
2775
+ the loss. If the shape of :math:`var` and :math:`logits` are not the same (due to a homoscedastic assumption),
2776
+ their shapes must allow correct broadcasting.
2777
+
2778
+ Keyword Args:
2779
+ full (bool, optional): Whether include the constant term in the loss calculation. When :math:`full=True`,
2780
+ the constant term `const.` will be :math:`0.5 * log(2\pi)`. Default: ``False`` .
2781
+ eps (float, optional): Used to improve the stability of log function. Default: ``1e-6`` .
2782
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
2783
+ ``'sum'`` . Default: ``'mean'`` .
2784
+
2785
+ - ``'none'``: no reduction will be applied.
2786
+ - ``'mean'``: compute and return the mean of elements in the output.
2787
+ - ``'sum'``: the output elements will be summed.
2788
+
2789
+ Inputs:
2790
+ - **logits** (Tensor) - Tensor of shape :math:`(N, *)` or :math:`(*)` where :math:`*` means any number of
2791
+ additional dimensions.
2792
+ - **labels** (Tensor) - Tensor of shape :math:`(N, *)` or :math:`(*)`, same shape as the logits, or same shape
2793
+ as the logits but with one dimension equal to 1 (to allow for broadcasting).
2794
+ - **var** - Tensor of shape :math:`(N, *)` or :math:`(*)`, same shape as logits, or same shape as the logits
2795
+ but with one dimension equal to 1, or same shape as the logits but with one fewer dimension
2796
+ (to allow for broadcasting).
2797
+
2798
+ Returns:
2799
+ Tensor or Tensor scalar, the computed loss depending on :math:`reduction`.
2800
+
2801
+ Raises:
2802
+ TypeError: If `logits` is not a Tensor.
2803
+ TypeError: If `labels` is not a Tensor.
2804
+ TypeError: If `full` is not a bool.
2805
+ TypeError: If `eps` is not a float.
2806
+ ValueError: If `eps` is not a float within (0, inf).
2807
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
2808
+
2809
+ Supported Platforms:
2810
+ ``Ascend`` ``GPU`` ``CPU``
2811
+
2812
+ Examples:
2813
+ >>> import mindspore as ms
2814
+ >>> import mindspore.nn as nn
2815
+ >>> import numpy as np
2816
+ >>> arr1 = np.arange(8).reshape((4, 2))
2817
+ >>> arr2 = np.array([2, 3, 1, 4, 6, 4, 4, 9]).reshape((4, 2))
2818
+ >>> logits = ms.Tensor(arr1, ms.float32)
2819
+ >>> labels = ms.Tensor(arr2, ms.float32)
2820
+ >>> loss = nn.GaussianNLLLoss(reduction='mean')
2821
+ >>> var = ms.Tensor(np.ones((4, 1)), ms.float32)
2822
+ >>> output = loss(logits, labels, var)
2823
+ >>> print(output)
2824
+ 1.4374993
2825
+
2826
+ Reference:
2827
+ Nix, D. A. and Weigend, A. S., "Estimating the mean and variance of the
2828
+ target probability distribution", Proceedings of 1994 IEEE International
2829
+ Conference on Neural Networks (ICNN'94), Orlando, FL, USA, 1994, pp. 55-60
2830
+ vol.1, doi: 10.1109/ICNN.1994.374138.
2831
+ """
2832
+
2833
+ def __init__(self, *, full=False, eps=1e-6, reduction='mean'):
2834
+ super(GaussianNLLLoss, self).__init__()
2835
+ validator.check_float_range(eps, 0, float('inf'), validator.INC_NEITHER, "eps", self.cls_name)
2836
+ validator.check_value_type('full', full, [bool], self.cls_name)
2837
+ validator.check_string(reduction, ['none', 'mean', 'sum'], 'reduction', 'gaussian_nll_loss')
2838
+ self.full = full
2839
+ self.eps = eps
2840
+ self.reduction = reduction
2841
+
2842
+ def construct(self, logits, labels, var):
2843
+ _check_is_tensor('logits', logits, self.cls_name)
2844
+ _check_is_tensor('labels', labels, self.cls_name)
2845
+ _check_is_tensor('var', var, self.cls_name)
2846
+ return ops.gaussian_nll_loss(logits, labels, var, self.full, self.eps, self.reduction)
2847
+
2848
+
2849
+ class HingeEmbeddingLoss(LossBase):
2850
+ r"""
2851
+ Calculate the Hinge Embedding Loss value based on the input 'logits' and' labels' (only including 1 or -1).
2852
+ Usually used to measure the similarity between two inputs.
2853
+
2854
+ The loss function for :math:`n`-th sample in the mini-batch is
2855
+
2856
+ .. math::
2857
+ l_n = \begin{cases}
2858
+ x_n, & \text{if}\; y_n = 1,\\
2859
+ \max \{0, \Delta - x_n\}, & \text{if}\; y_n = -1,
2860
+ \end{cases}
2861
+
2862
+ and the total loss functions is
2863
+
2864
+ .. math::
2865
+ \ell(x, y) = \begin{cases}
2866
+ \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\
2867
+ \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.}
2868
+ \end{cases}
2869
+
2870
+ where :math:`L = \{l_1,\dots,l_N\}^\top`.
2871
+
2872
+ Args:
2873
+ margin (float, int): Threshold defined by Hinge Embedding Loss :math:`margin`.
2874
+ Represented as :math:`\Delta` in the formula. Default: ``1.0`` .
2875
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
2876
+ ``'sum'`` . Default: ``'mean'`` .
2877
+
2878
+ - ``'none'``: no reduction will be applied.
2879
+ - ``'mean'``: compute and return the mean of elements in the output.
2880
+ - ``'sum'``: the output elements will be summed.
2881
+
2882
+ Inputs:
2883
+ - **logits** (Tensor) - The predicted value, expressed as :math:`x` in the equation.
2884
+ Tensor of shape :math:`(*)` where :math:`*` means any number of dimensions.
2885
+ - **labels** (Tensor) - Label value, represented as :math:`y` in the equation.
2886
+ Same shape as the logits, contains -1 or 1.
2887
+
2888
+ Returns:
2889
+ Tensor or Tensor scalar, the computed loss depending on :math:`reduction`.
2890
+
2891
+ Raises:
2892
+ TypeError: If `logits` is not a Tensor.
2893
+ TypeError: If `labels` is not a Tensor.
2894
+ TypeError: If `margin` is not a float or int.
2895
+ ValueError: If `labels` does not have the same shape as `logits` or they could not broadcast to each other.
2896
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
2897
+
2898
+ Supported Platforms:
2899
+ ``Ascend`` ``GPU`` ``CPU``
2900
+
2901
+ Examples:
2902
+ >>> import mindspore as ms
2903
+ >>> import mindspore.nn as nn
2904
+ >>> import numpy as np
2905
+ >>> arr1 = np.array([0.9, -1.2, 2, 0.8, 3.9, 2, 1, 0, -1]).reshape((3, 3))
2906
+ >>> arr2 = np.array([1, 1, -1, 1, -1, 1, -1, 1, 1]).reshape((3, 3))
2907
+ >>> logits = ms.Tensor(arr1, ms.float32)
2908
+ >>> labels = ms.Tensor(arr2, ms.float32)
2909
+ >>> loss = nn.HingeEmbeddingLoss(reduction='mean')
2910
+ >>> output = loss(logits, labels)
2911
+ >>> print(output)
2912
+ 0.16666667
2913
+ """
2914
+
2915
+ def __init__(self, margin=1.0, reduction='mean'):
2916
+ super(HingeEmbeddingLoss, self).__init__()
2917
+ validator.check_value_type('margin', margin, [float, int], self.cls_name)
2918
+ validator.check_string(reduction, ['none', 'sum', 'mean'], 'reduction', self.cls_name)
2919
+ self.margin = margin
2920
+ self.reduction = reduction
2921
+
2922
+ def construct(self, logits, labels):
2923
+ loss = ops.hinge_embedding_loss(logits, labels, self.margin, self.reduction)
2924
+ return loss