mindspore 2.0.0rc1__cp38-none-any.whl → 2.2.0__cp38-none-any.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 (870) hide show
  1. mindspore/.commit_id +1 -1
  2. mindspore/Third_Party_Open_Source_Software_Notice +2 -2
  3. mindspore/__init__.py +5 -2
  4. mindspore/_akg/akg/build_module.py +5 -6
  5. mindspore/_akg/akg/composite/build_module.py +49 -16
  6. mindspore/_akg/akg/composite/split_stitch.py +10 -11
  7. mindspore/_akg/akg/config/repository.json +195 -0
  8. mindspore/_akg/akg/global_configs.py +5 -1
  9. mindspore/_akg/akg/ms/info_version_adapt.py +67 -1
  10. mindspore/_akg/akg/tvm/api.py +4 -3
  11. mindspore/_akg/akg/tvm/autotvm/__init__.py +1 -2
  12. mindspore/_akg/akg/tvm/autotvm/graph_tuner/base_graph_tuner.py +1 -5
  13. mindspore/_akg/akg/tvm/autotvm/measure/__init__.py +1 -1
  14. mindspore/_akg/akg/tvm/autotvm/measure/measure.py +1 -10
  15. mindspore/_akg/akg/tvm/autotvm/measure/measure_methods.py +1 -372
  16. mindspore/_akg/akg/tvm/build_module.py +16 -1
  17. mindspore/_akg/akg/tvm/contrib/graph_runtime.py +0 -53
  18. mindspore/_akg/akg/tvm/hybrid/parser.py +7 -6
  19. mindspore/_akg/akg/tvm/ir_builder.py +1 -1
  20. mindspore/_akg/akg/tvm/module.py +1 -2
  21. mindspore/_akg/akg/tvm/stmt.py +2 -2
  22. mindspore/_akg/akg/utils/composite_op_helper.py +9 -10
  23. mindspore/_akg/akg/utils/kernel_exec.py +58 -260
  24. mindspore/_akg/akg/utils/op_dsl.py +17 -1
  25. mindspore/_akg/akg/utils/result_analysis.py +4 -24
  26. mindspore/_akg/akg/utils/tbe_codegen_utils.py +198 -0
  27. mindspore/_c_dataengine.cpython-38-aarch64-linux-gnu.so +0 -0
  28. mindspore/_c_expression.cpython-38-aarch64-linux-gnu.so +0 -0
  29. mindspore/_c_mindrecord.cpython-38-aarch64-linux-gnu.so +0 -0
  30. mindspore/_check_jit_forbidden_api.py +5 -1
  31. mindspore/_checkparam.py +79 -62
  32. mindspore/_extends/graph_kernel/__init__.py +0 -1
  33. mindspore/_extends/graph_kernel/model/graph_split.py +2 -0
  34. mindspore/_extends/graph_kernel/model/model_builder.py +9 -50
  35. mindspore/_extends/graph_kernel/splitter.py +1 -9
  36. mindspore/_extends/parallel_compile/akg_compiler/akg_process.py +128 -21
  37. mindspore/_extends/parallel_compile/akg_compiler/build_tbe_kernel.py +2 -2
  38. mindspore/_extends/parallel_compile/akg_compiler/tbe_topi.py +4 -2
  39. mindspore/_extends/parallel_compile/tbe_compiler/tbe_adapter.py +18 -13
  40. mindspore/_extends/parallel_compile/tbe_compiler/tbe_helper.py +13 -9
  41. mindspore/_extends/parallel_compile/tbe_compiler/tbe_job.py +1 -1
  42. mindspore/_extends/parallel_compile/tbe_compiler/tbe_job_manager.py +1 -1
  43. mindspore/_extends/parse/__init__.py +19 -17
  44. mindspore/_extends/parse/namespace.py +7 -36
  45. mindspore/_extends/parse/parser.py +375 -189
  46. mindspore/_extends/parse/resources.py +36 -41
  47. mindspore/_extends/parse/standard_method.py +350 -245
  48. mindspore/_extends/parse/trope.py +2 -12
  49. mindspore/_extends/remote/kernel_build_server.py +24 -7
  50. mindspore/_extends/remote/kernel_build_server_akg_v2.py +55 -0
  51. mindspore/_install_custom.py +43 -0
  52. mindspore/_mindspore_offline_debug.cpython-38-aarch64-linux-gnu.so +0 -0
  53. mindspore/amp.py +85 -19
  54. mindspore/bin/cache_admin +0 -0
  55. mindspore/bin/cache_server +0 -0
  56. mindspore/boost/base.py +2 -2
  57. mindspore/boost/boost.py +27 -32
  58. mindspore/boost/boost_cell_wrapper.py +37 -13
  59. mindspore/boost/grad_accumulation.py +1 -1
  60. mindspore/boost/grad_freeze.py +34 -6
  61. mindspore/boost/group_loss_scale_manager.py +15 -14
  62. mindspore/boost/less_batch_normalization.py +28 -3
  63. mindspore/common/__init__.py +15 -11
  64. mindspore/common/_auto_dynamic.py +68 -0
  65. mindspore/common/_jit_fallback_utils.py +111 -0
  66. mindspore/common/_register_for_adapter.py +17 -5
  67. mindspore/common/_register_for_tensor.py +2 -2
  68. mindspore/common/_stub_tensor.py +18 -15
  69. mindspore/common/_utils.py +31 -7
  70. mindspore/common/api.py +269 -101
  71. mindspore/common/auto_dynamic_shape.py +498 -0
  72. mindspore/common/dtype.py +61 -21
  73. mindspore/common/dump.py +9 -7
  74. mindspore/common/initializer.py +106 -76
  75. mindspore/common/jit_config.py +35 -14
  76. mindspore/common/lazy_inline.py +187 -0
  77. mindspore/common/mindir_util.py +101 -0
  78. mindspore/common/mutable.py +10 -13
  79. mindspore/common/parameter.py +246 -55
  80. mindspore/common/seed.py +13 -7
  81. mindspore/common/sparse_tensor.py +29 -33
  82. mindspore/common/tensor.py +907 -251
  83. mindspore/communication/__init__.py +7 -4
  84. mindspore/communication/_comm_helper.py +84 -4
  85. mindspore/communication/management.py +160 -88
  86. mindspore/config/op_info.config +99 -75
  87. mindspore/config/super_bar_config.json +36 -4
  88. mindspore/context.py +526 -219
  89. mindspore/dataset/__init__.py +9 -46
  90. mindspore/dataset/audio/__init__.py +4 -19
  91. mindspore/dataset/audio/transforms.py +545 -233
  92. mindspore/dataset/audio/utils.py +21 -18
  93. mindspore/dataset/callback/ds_callback.py +42 -13
  94. mindspore/dataset/core/config.py +158 -100
  95. mindspore/dataset/core/validator_helpers.py +1 -63
  96. mindspore/dataset/debug/debug_hook.py +45 -13
  97. mindspore/dataset/debug/pre_defined_hook.py +5 -5
  98. mindspore/dataset/engine/__init__.py +0 -5
  99. mindspore/dataset/engine/cache_client.py +38 -15
  100. mindspore/dataset/engine/datasets.py +615 -278
  101. mindspore/dataset/engine/datasets_audio.py +154 -283
  102. mindspore/dataset/engine/datasets_standard_format.py +104 -116
  103. mindspore/dataset/engine/datasets_text.py +443 -326
  104. mindspore/dataset/engine/datasets_user_defined.py +251 -164
  105. mindspore/dataset/engine/datasets_vision.py +839 -1443
  106. mindspore/dataset/engine/iterators.py +11 -4
  107. mindspore/dataset/engine/obs/obs_mindrecord_dataset.py +7 -3
  108. mindspore/dataset/engine/obs/util.py +3 -0
  109. mindspore/dataset/engine/offload.py +6 -6
  110. mindspore/dataset/engine/queue.py +15 -14
  111. mindspore/dataset/engine/samplers.py +39 -23
  112. mindspore/dataset/engine/serializer_deserializer.py +22 -6
  113. mindspore/dataset/engine/validators.py +21 -331
  114. mindspore/dataset/text/__init__.py +5 -33
  115. mindspore/dataset/text/transforms.py +334 -165
  116. mindspore/dataset/text/utils.py +215 -145
  117. mindspore/dataset/transforms/__init__.py +1 -1
  118. mindspore/dataset/transforms/c_transforms.py +3 -2
  119. mindspore/dataset/transforms/py_transforms_util.py +40 -12
  120. mindspore/dataset/transforms/transforms.py +174 -71
  121. mindspore/dataset/utils/browse_dataset.py +25 -17
  122. mindspore/dataset/utils/line_reader.py +24 -21
  123. mindspore/dataset/vision/__init__.py +5 -26
  124. mindspore/dataset/vision/c_transforms.py +177 -165
  125. mindspore/dataset/vision/py_transforms.py +114 -119
  126. mindspore/dataset/vision/py_transforms_util.py +54 -51
  127. mindspore/dataset/vision/transforms.py +1127 -381
  128. mindspore/dataset/vision/utils.py +54 -38
  129. mindspore/dataset/vision/validators.py +12 -2
  130. mindspore/experimental/map_parameter.py +38 -4
  131. mindspore/{dataset/datapreprocess → experimental/optim}/__init__.py +14 -4
  132. mindspore/experimental/optim/adam.py +192 -0
  133. mindspore/experimental/optim/adamw.py +181 -0
  134. mindspore/experimental/optim/lr_scheduler.py +1427 -0
  135. mindspore/experimental/optim/optimizer.py +252 -0
  136. mindspore/experimental/optim/sgd.py +147 -0
  137. mindspore/gen_ops.py +273 -0
  138. mindspore/include/OWNERS +1 -2
  139. mindspore/include/api/context.h +21 -1
  140. mindspore/include/api/data_type.h +2 -1
  141. mindspore/include/api/graph.h +0 -15
  142. mindspore/include/api/kernel.h +2 -0
  143. mindspore/include/api/kernel_api.h +37 -12
  144. mindspore/include/api/model.h +29 -42
  145. mindspore/include/api/model_group.h +14 -3
  146. mindspore/include/api/model_parallel_runner.h +18 -2
  147. mindspore/include/api/serialization.h +26 -0
  148. mindspore/include/api/status.h +1 -0
  149. mindspore/include/api/types.h +38 -4
  150. mindspore/include/c_api/ms/abstract.h +67 -0
  151. mindspore/include/c_api/ms/attribute.h +197 -0
  152. mindspore/include/c_api/ms/base/handle_types.h +43 -0
  153. mindspore/include/c_api/ms/base/macros.h +32 -0
  154. mindspore/include/c_api/ms/base/status.h +33 -0
  155. mindspore/include/c_api/ms/base/types.h +282 -0
  156. mindspore/include/c_api/ms/context.h +102 -0
  157. mindspore/include/c_api/ms/graph.h +160 -0
  158. mindspore/include/c_api/ms/node.h +606 -0
  159. mindspore/include/c_api/ms/tensor.h +161 -0
  160. mindspore/include/c_api/ms/value.h +84 -0
  161. mindspore/include/c_api/status_c.h +3 -0
  162. mindspore/include/dataset/constants.h +6 -12
  163. mindspore/include/dataset/execute.h +23 -13
  164. mindspore/include/dataset/text.h +26 -26
  165. mindspore/include/dataset/transforms.h +25 -31
  166. mindspore/include/dataset/vision.h +60 -60
  167. mindspore/include/dataset/vision_ascend.h +5 -6
  168. mindspore/include/dataset/vision_lite.h +17 -17
  169. mindspore/include/mindapi/base/format.h +0 -1
  170. mindspore/include/mindapi/base/type_id.h +2 -1
  171. mindspore/include/mindapi/base/types.h +5 -1
  172. mindspore/lib/libdnnl.so.2 +0 -0
  173. mindspore/lib/libjemalloc.so.2 +0 -0
  174. mindspore/lib/libmindspore.so +0 -0
  175. mindspore/lib/libmindspore_backend.so +0 -0
  176. mindspore/lib/libmindspore_common.so +0 -0
  177. mindspore/lib/libmindspore_core.so +0 -0
  178. mindspore/lib/libmindspore_glog.so.0 +0 -0
  179. mindspore/lib/libmindspore_gpr.so.15 +0 -0
  180. mindspore/lib/libmindspore_grpc++.so.1 +0 -0
  181. mindspore/lib/libmindspore_grpc.so.15 +0 -0
  182. mindspore/lib/libmindspore_shared_lib.so +0 -0
  183. mindspore/lib/libmpi_adapter.so +0 -0
  184. mindspore/lib/libnnacl.so +0 -0
  185. mindspore/lib/libopencv_core.so.4.5 +0 -0
  186. mindspore/lib/libopencv_imgcodecs.so.4.5 +0 -0
  187. mindspore/lib/libopencv_imgproc.so.4.5 +0 -0
  188. mindspore/lib/libps_cache.so +0 -0
  189. mindspore/lib/plugin/ascend/custom_aicpu_ops/op_impl/cpu/aicpu_kernel/impl/libcust_aicpu_kernels.so +0 -0
  190. mindspore/lib/plugin/ascend/custom_aicpu_ops/op_impl/cpu/aicpu_kernel/impl/libcust_cpu_kernels.so +0 -0
  191. mindspore/lib/plugin/ascend/custom_aicpu_ops/op_impl/cpu/config/cust_aicpu_kernel.json +9000 -0
  192. mindspore/lib/plugin/ascend/custom_aicpu_ops/op_proto/libcust_op_proto.so +0 -0
  193. mindspore/lib/plugin/ascend/libakg.so +0 -0
  194. mindspore/lib/plugin/ascend/libascend_collective.so +0 -0
  195. mindspore/lib/plugin/ascend/libdvpp_utils.so +0 -0
  196. mindspore/lib/plugin/ascend/libhccl_plugin.so +0 -0
  197. mindspore/lib/plugin/ascend/libmindspore_aicpu_kernels.so +0 -0
  198. mindspore/lib/plugin/ascend/libmindspore_cpu_kernels.so +0 -0
  199. mindspore/lib/plugin/cpu/libakg.so +0 -0
  200. mindspore/lib/plugin/libmindspore_ascend.so.1 +0 -0
  201. mindspore/lib/plugin/libmindspore_ascend.so.2 +0 -0
  202. mindspore/log.py +9 -6
  203. mindspore/mindrecord/filereader.py +33 -4
  204. mindspore/mindrecord/filewriter.py +70 -35
  205. mindspore/mindrecord/mindpage.py +40 -34
  206. mindspore/mindrecord/shardreader.py +1 -1
  207. mindspore/mindrecord/shardsegment.py +1 -1
  208. mindspore/mindrecord/tools/cifar100_to_mr.py +25 -18
  209. mindspore/mindrecord/tools/cifar10_to_mr.py +25 -18
  210. mindspore/mindrecord/tools/csv_to_mr.py +29 -13
  211. mindspore/mindrecord/tools/imagenet_to_mr.py +24 -10
  212. mindspore/mindrecord/tools/mnist_to_mr.py +24 -11
  213. mindspore/mindrecord/tools/tfrecord_to_mr.py +31 -26
  214. mindspore/nn/cell.py +463 -169
  215. mindspore/nn/dynamic_lr.py +47 -43
  216. mindspore/nn/layer/activation.py +225 -82
  217. mindspore/nn/layer/basic.py +121 -79
  218. mindspore/nn/layer/channel_shuffle.py +21 -21
  219. mindspore/nn/layer/combined.py +33 -26
  220. mindspore/nn/layer/container.py +277 -22
  221. mindspore/nn/layer/conv.py +441 -304
  222. mindspore/nn/layer/dense.py +19 -13
  223. mindspore/nn/layer/embedding.py +62 -49
  224. mindspore/nn/layer/flash_attention.py +264 -0
  225. mindspore/nn/layer/image.py +50 -39
  226. mindspore/nn/layer/math.py +62 -51
  227. mindspore/nn/layer/normalization.py +219 -167
  228. mindspore/nn/layer/padding.py +58 -70
  229. mindspore/nn/layer/pooling.py +334 -287
  230. mindspore/nn/layer/rnn_cells.py +53 -38
  231. mindspore/nn/layer/rnns.py +59 -56
  232. mindspore/nn/layer/thor_layer.py +52 -44
  233. mindspore/nn/layer/timedistributed.py +6 -4
  234. mindspore/nn/layer/transformer.py +284 -164
  235. mindspore/nn/learning_rate_schedule.py +34 -25
  236. mindspore/nn/loss/__init__.py +3 -2
  237. mindspore/nn/loss/loss.py +554 -311
  238. mindspore/nn/optim/ada_grad.py +12 -9
  239. mindspore/nn/optim/adadelta.py +14 -11
  240. mindspore/nn/optim/adafactor.py +19 -16
  241. mindspore/nn/optim/adam.py +62 -47
  242. mindspore/nn/optim/adamax.py +13 -10
  243. mindspore/nn/optim/adasum.py +12 -8
  244. mindspore/nn/optim/asgd.py +10 -9
  245. mindspore/nn/optim/ftrl.py +20 -17
  246. mindspore/nn/optim/lamb.py +16 -12
  247. mindspore/nn/optim/lars.py +8 -6
  248. mindspore/nn/optim/lazyadam.py +25 -20
  249. mindspore/nn/optim/momentum.py +10 -7
  250. mindspore/nn/optim/optimizer.py +61 -9
  251. mindspore/nn/optim/proximal_ada_grad.py +14 -13
  252. mindspore/nn/optim/rmsprop.py +17 -13
  253. mindspore/nn/optim/rprop.py +30 -17
  254. mindspore/nn/optim/sgd.py +40 -23
  255. mindspore/nn/optim/thor.py +24 -26
  256. mindspore/nn/probability/bijector/bijector.py +11 -11
  257. mindspore/nn/probability/bijector/exp.py +1 -1
  258. mindspore/nn/probability/bijector/gumbel_cdf.py +3 -3
  259. mindspore/nn/probability/bijector/invert.py +1 -1
  260. mindspore/nn/probability/bijector/power_transform.py +29 -29
  261. mindspore/nn/probability/bijector/scalar_affine.py +3 -3
  262. mindspore/nn/probability/bijector/softplus.py +5 -5
  263. mindspore/nn/probability/bnn_layers/bnn_cell_wrapper.py +4 -2
  264. mindspore/nn/probability/bnn_layers/conv_variational.py +13 -13
  265. mindspore/nn/probability/bnn_layers/dense_variational.py +12 -12
  266. mindspore/nn/probability/bnn_layers/layer_distribution.py +9 -8
  267. mindspore/nn/probability/distribution/_utils/custom_ops.py +19 -3
  268. mindspore/nn/probability/distribution/_utils/utils.py +1 -1
  269. mindspore/nn/probability/distribution/bernoulli.py +9 -9
  270. mindspore/nn/probability/distribution/beta.py +8 -8
  271. mindspore/nn/probability/distribution/categorical.py +23 -15
  272. mindspore/nn/probability/distribution/cauchy.py +5 -6
  273. mindspore/nn/probability/distribution/distribution.py +3 -3
  274. mindspore/nn/probability/distribution/exponential.py +4 -4
  275. mindspore/nn/probability/distribution/gamma.py +10 -10
  276. mindspore/nn/probability/distribution/geometric.py +8 -8
  277. mindspore/nn/probability/distribution/gumbel.py +8 -9
  278. mindspore/nn/probability/distribution/half_normal.py +5 -5
  279. mindspore/nn/probability/distribution/laplace.py +5 -5
  280. mindspore/nn/probability/distribution/log_normal.py +12 -11
  281. mindspore/nn/probability/distribution/logistic.py +8 -8
  282. mindspore/nn/probability/distribution/normal.py +6 -5
  283. mindspore/nn/probability/distribution/poisson.py +10 -11
  284. mindspore/nn/probability/distribution/student_t.py +8 -9
  285. mindspore/nn/probability/distribution/transformed_distribution.py +5 -5
  286. mindspore/nn/probability/distribution/uniform.py +11 -11
  287. mindspore/nn/reinforcement/tensor_array.py +2 -2
  288. mindspore/nn/sparse/sparse.py +9 -9
  289. mindspore/nn/wrap/cell_wrapper.py +188 -63
  290. mindspore/nn/wrap/grad_reducer.py +21 -12
  291. mindspore/nn/wrap/loss_scale.py +136 -49
  292. mindspore/numpy/__init__.py +4 -4
  293. mindspore/numpy/array_creations.py +55 -56
  294. mindspore/numpy/array_ops.py +134 -35
  295. mindspore/numpy/logic_ops.py +66 -20
  296. mindspore/numpy/math_ops.py +142 -139
  297. mindspore/numpy/utils_const.py +2 -2
  298. mindspore/offline_debug/convert_async.py +2 -2
  299. mindspore/ops/_grad_experimental/__init__.py +7 -5
  300. mindspore/ops/_grad_experimental/grad_array_ops.py +231 -348
  301. mindspore/ops/{_grad → _grad_experimental}/grad_base.py +1 -33
  302. mindspore/ops/{_grad → _grad_experimental}/grad_comm_ops.py +25 -13
  303. mindspore/ops/{_grad/__init__.py → _grad_experimental/grad_debug_ops.py} +15 -7
  304. mindspore/ops/{_grad → _grad_experimental}/grad_implementations.py +17 -11
  305. mindspore/ops/_grad_experimental/grad_inner_ops.py +33 -52
  306. mindspore/ops/_grad_experimental/grad_math_ops.py +151 -1224
  307. mindspore/ops/_grad_experimental/grad_nn_ops.py +141 -414
  308. mindspore/ops/{_grad → _grad_experimental}/grad_quant_ops.py +10 -6
  309. mindspore/ops/_grad_experimental/grad_sparse.py +317 -2
  310. mindspore/ops/_grad_experimental/grad_sparse_ops.py +3 -13
  311. mindspore/ops/{_grad → _grad_experimental}/taylor_rule.py +1 -1
  312. mindspore/ops/_op_impl/_custom_op/dsd_back_impl.py +1 -1
  313. mindspore/ops/_op_impl/_custom_op/flash_attention/__init__.py +0 -0
  314. mindspore/ops/_op_impl/_custom_op/flash_attention/attention.py +406 -0
  315. mindspore/{_extends/graph_kernel/expanders/complex/__init__.py → ops/_op_impl/_custom_op/flash_attention/constants.py} +27 -8
  316. mindspore/ops/_op_impl/_custom_op/flash_attention/flash_attention_bwd.py +467 -0
  317. mindspore/ops/_op_impl/_custom_op/flash_attention/flash_attention_fwd.py +563 -0
  318. mindspore/ops/_op_impl/_custom_op/flash_attention/flash_attention_impl.py +193 -0
  319. mindspore/ops/_op_impl/_custom_op/flash_attention/tik_ops_utils.py +435 -0
  320. mindspore/ops/_op_impl/_custom_op/flash_attention/tiling_strategy/__init__.py +0 -0
  321. mindspore/ops/_op_impl/_custom_op/flash_attention/tiling_strategy/sparse_tiling.py +45 -0
  322. mindspore/ops/_op_impl/_custom_op/flash_attention/tiling_strategy/strategy.py +67 -0
  323. mindspore/ops/_op_impl/_custom_op/flash_attention/tiling_strategy/wukong_tiling.py +62 -0
  324. mindspore/ops/_op_impl/_custom_op/matmul_cube_dense_left_impl.py +2 -2
  325. mindspore/ops/_op_impl/aicpu/__init__.py +41 -1
  326. mindspore/ops/_op_impl/aicpu/adaptive_max_pool_2d.py +37 -0
  327. mindspore/ops/_op_impl/aicpu/bias_add_grad.py +0 -1
  328. mindspore/ops/_op_impl/aicpu/cast.py +52 -0
  329. mindspore/ops/_op_impl/aicpu/coalesce.py +2 -0
  330. mindspore/ops/_op_impl/aicpu/col2im.py +3 -1
  331. mindspore/ops/_op_impl/aicpu/count_nonzero.py +43 -0
  332. mindspore/ops/_op_impl/aicpu/dropout_genmask.py +6 -0
  333. mindspore/ops/_op_impl/aicpu/eps.py +32 -0
  334. mindspore/ops/_op_impl/aicpu/eye.py +4 -4
  335. mindspore/ops/_op_impl/aicpu/fft_with_size.py +6 -0
  336. mindspore/ops/_op_impl/aicpu/fill_diagonal.py +5 -0
  337. mindspore/ops/_op_impl/aicpu/gamma.py +2 -2
  338. mindspore/ops/_op_impl/aicpu/im2col.py +3 -5
  339. mindspore/ops/_op_impl/aicpu/lgamma.py +1 -0
  340. mindspore/ops/_op_impl/aicpu/log_uniform_candidate_sampler.py +6 -3
  341. mindspore/ops/_op_impl/aicpu/lu.py +39 -0
  342. mindspore/ops/_op_impl/aicpu/lu_unpack_grad.py +0 -1
  343. mindspore/ops/_op_impl/aicpu/masked_scatter.py +1 -0
  344. mindspore/ops/_op_impl/aicpu/masked_select_grad.py +3 -0
  345. mindspore/ops/_op_impl/aicpu/matrix_band_part.py +59 -0
  346. mindspore/ops/_op_impl/aicpu/matrix_power.py +6 -1
  347. mindspore/ops/_op_impl/aicpu/median.py +1 -0
  348. mindspore/ops/_op_impl/aicpu/multinomial.py +9 -9
  349. mindspore/ops/_op_impl/aicpu/not_equal.py +0 -5
  350. mindspore/ops/_op_impl/aicpu/pad_v3.py +3 -1
  351. mindspore/ops/_op_impl/aicpu/pad_v3_grad.py +2 -0
  352. mindspore/ops/_op_impl/aicpu/parameterized_truncated_normal.py +15 -7
  353. mindspore/ops/_op_impl/aicpu/random_categorical.py +39 -19
  354. mindspore/ops/_op_impl/aicpu/random_choice_with_mask.py +5 -2
  355. mindspore/ops/_op_impl/aicpu/random_poisson.py +103 -52
  356. mindspore/ops/_op_impl/aicpu/random_shuffle.py +17 -15
  357. mindspore/ops/_op_impl/aicpu/resize_bilinear_grad.py +0 -1
  358. mindspore/ops/_op_impl/aicpu/resize_nearest_neighbor_v2.py +0 -6
  359. mindspore/ops/_op_impl/aicpu/resize_nearest_neighbor_v2_grad.py +0 -7
  360. mindspore/ops/_op_impl/aicpu/scatter_nd.py +2 -0
  361. mindspore/ops/_op_impl/aicpu/sequence_concat.py +40 -0
  362. mindspore/ops/_op_impl/aicpu/sequence_stack.py +40 -0
  363. mindspore/ops/_op_impl/aicpu/{sparseaddmm.py → sparse_addmm.py} +2 -2
  364. mindspore/ops/_op_impl/aicpu/{sparsesparsemaximum.py → sparse_sparse_maximum.py} +4 -4
  365. mindspore/ops/_op_impl/aicpu/standard_laplace.py +5 -4
  366. mindspore/ops/_op_impl/aicpu/standard_normal.py +5 -4
  367. mindspore/ops/_op_impl/aicpu/truncated_normal.py +9 -7
  368. mindspore/ops/_op_impl/aicpu/uniform.py +5 -3
  369. mindspore/ops/_op_impl/aicpu/uniform_candidate_sampler.py +8 -4
  370. mindspore/ops/_op_impl/aicpu/uniform_int.py +5 -5
  371. mindspore/ops/_op_impl/aicpu/uniform_real.py +4 -4
  372. mindspore/ops/_op_impl/aicpu/upsample_nearest_3d.py +14 -6
  373. mindspore/ops/_op_impl/aicpu/upsample_nearest_3d_grad.py +22 -8
  374. mindspore/ops/_op_impl/aicpu/upsample_trilinear_3d.py +11 -6
  375. mindspore/ops/_op_impl/aicpu/upsample_trilinear_3d_grad.py +21 -10
  376. mindspore/ops/_op_impl/tbe/__init__.py +6 -4
  377. mindspore/ops/_op_impl/tbe/atomic_addr_clean.py +1 -1
  378. mindspore/ops/_op_impl/tbe/avg_pool.py +2 -2
  379. mindspore/ops/_op_impl/tbe/avg_pool_3d.py +3 -3
  380. mindspore/ops/_op_impl/tbe/avg_pool_3d_grad.py +4 -4
  381. mindspore/ops/_op_impl/tbe/avg_pool_ds.py +2 -2
  382. mindspore/ops/_op_impl/tbe/avg_pool_grad.py +3 -3
  383. mindspore/ops/_op_impl/tbe/avg_pool_grad_vm.py +3 -3
  384. mindspore/ops/_op_impl/tbe/batch_to_space.py +1 -1
  385. mindspore/ops/_op_impl/tbe/batch_to_space_nd.py +2 -2
  386. mindspore/ops/_op_impl/tbe/bn_infer.py +2 -2
  387. mindspore/ops/_op_impl/tbe/bn_infer_ds.py +3 -2
  388. mindspore/ops/_op_impl/tbe/broadcast_to.py +1 -1
  389. mindspore/ops/_op_impl/tbe/depthwise_conv2d.py +3 -3
  390. mindspore/ops/_op_impl/tbe/expand_dims.py +1 -1
  391. mindspore/ops/_op_impl/tbe/gather_v2.py +56 -0
  392. mindspore/ops/_op_impl/tbe/im2col.py +4 -4
  393. mindspore/ops/_op_impl/tbe/inplace_index_add.py +7 -3
  394. mindspore/ops/_op_impl/tbe/mem_set.py +38 -0
  395. mindspore/ops/_op_impl/tbe/scatter_nd_add.py +3 -0
  396. mindspore/ops/_op_impl/tbe/scatter_nd_d.py +1 -1
  397. mindspore/ops/_op_impl/tbe/space_to_batch.py +1 -1
  398. mindspore/ops/_op_impl/tbe/space_to_batch_nd.py +2 -2
  399. mindspore/ops/_op_impl/tbe/trans_data_ds.py +2 -0
  400. mindspore/ops/_primitive_cache.py +1 -1
  401. mindspore/ops/_tracefunc.py +241 -0
  402. mindspore/ops/_utils/utils.py +10 -2
  403. mindspore/ops/_vmap/vmap_array_ops.py +5 -3
  404. mindspore/ops/_vmap/vmap_base.py +5 -4
  405. mindspore/ops/_vmap/vmap_convolution_ops.py +1 -1
  406. mindspore/ops/_vmap/vmap_grad_math_ops.py +6 -4
  407. mindspore/ops/_vmap/vmap_grad_nn_ops.py +11 -6
  408. mindspore/ops/_vmap/vmap_math_ops.py +5 -2
  409. mindspore/ops/_vmap/vmap_nn_ops.py +135 -11
  410. mindspore/ops/arg_dtype_cast.py +54 -0
  411. mindspore/ops/composite/__init__.py +7 -5
  412. mindspore/ops/composite/base.py +78 -34
  413. mindspore/ops/composite/math_ops.py +5 -695
  414. mindspore/ops/composite/multitype_ops/_compile_utils.py +403 -97
  415. mindspore/ops/composite/multitype_ops/_constexpr_utils.py +28 -22
  416. mindspore/ops/composite/multitype_ops/add_impl.py +69 -7
  417. mindspore/ops/composite/multitype_ops/bitwise_and_impl.py +2 -1
  418. mindspore/ops/composite/multitype_ops/bitwise_or_impl.py +2 -1
  419. mindspore/ops/composite/multitype_ops/bitwise_xor_impl.py +2 -0
  420. mindspore/ops/composite/multitype_ops/div_impl.py +1 -0
  421. mindspore/ops/composite/multitype_ops/floordiv_impl.py +1 -0
  422. mindspore/ops/composite/multitype_ops/getitem_impl.py +48 -10
  423. mindspore/ops/composite/multitype_ops/greater_equal_impl.py +2 -0
  424. mindspore/ops/composite/multitype_ops/greater_impl.py +2 -0
  425. mindspore/ops/composite/multitype_ops/left_shift_impl.py +2 -0
  426. mindspore/ops/composite/multitype_ops/less_equal_impl.py +2 -0
  427. mindspore/ops/composite/multitype_ops/less_impl.py +2 -0
  428. mindspore/ops/composite/multitype_ops/logic_not_impl.py +2 -2
  429. mindspore/ops/composite/multitype_ops/mod_impl.py +1 -0
  430. mindspore/ops/composite/multitype_ops/mul_impl.py +1 -0
  431. mindspore/ops/composite/multitype_ops/negative_impl.py +1 -0
  432. mindspore/ops/composite/multitype_ops/not_in_impl.py +1 -0
  433. mindspore/ops/composite/multitype_ops/ones_like_impl.py +6 -0
  434. mindspore/ops/composite/multitype_ops/pow_impl.py +1 -0
  435. mindspore/ops/composite/multitype_ops/right_shift_impl.py +2 -0
  436. mindspore/ops/composite/multitype_ops/setitem_impl.py +10 -7
  437. mindspore/ops/composite/multitype_ops/sub_impl.py +1 -0
  438. mindspore/ops/composite/multitype_ops/uadd_impl.py +2 -0
  439. mindspore/ops/composite/multitype_ops/zeros_like_impl.py +9 -0
  440. mindspore/ops/deprecated.py +304 -0
  441. mindspore/ops/function/__init__.py +41 -4
  442. mindspore/ops/function/array_func.py +1108 -467
  443. mindspore/ops/function/clip_func.py +94 -27
  444. mindspore/ops/function/debug_func.py +3 -1
  445. mindspore/ops/function/grad/grad_func.py +82 -73
  446. mindspore/ops/function/image_func.py +28 -12
  447. mindspore/ops/function/linalg_func.py +135 -39
  448. mindspore/ops/function/math_func.py +3779 -894
  449. mindspore/ops/function/nn_func.py +1584 -657
  450. mindspore/ops/function/parameter_func.py +13 -3
  451. mindspore/ops/function/random_func.py +247 -153
  452. mindspore/ops/function/sparse_func.py +14 -11
  453. mindspore/ops/function/sparse_unary_func.py +173 -47
  454. mindspore/ops/function/spectral_func.py +8 -4
  455. mindspore/ops/function/vmap_func.py +8 -7
  456. mindspore/ops/functional.py +47 -16
  457. mindspore/ops/op_info_register.py +346 -86
  458. mindspore/ops/operations/__init__.py +38 -22
  459. mindspore/ops/operations/_grad_ops.py +145 -149
  460. mindspore/ops/operations/_inner_ops.py +298 -56
  461. mindspore/ops/operations/_ms_kernel.py +3 -3
  462. mindspore/ops/operations/_quant_ops.py +24 -28
  463. mindspore/ops/operations/_rl_inner_ops.py +9 -7
  464. mindspore/ops/operations/_scalar_ops.py +115 -0
  465. mindspore/ops/operations/_sequence_ops.py +148 -10
  466. mindspore/ops/operations/_tensor_array.py +1 -1
  467. mindspore/ops/operations/_thor_ops.py +2 -2
  468. mindspore/ops/operations/array_ops.py +1239 -561
  469. mindspore/ops/operations/comm_ops.py +166 -90
  470. mindspore/ops/operations/control_ops.py +3 -3
  471. mindspore/ops/operations/custom_ops.py +124 -102
  472. mindspore/ops/operations/debug_ops.py +24 -11
  473. mindspore/ops/operations/image_ops.py +86 -71
  474. mindspore/ops/operations/inner_ops.py +18 -13
  475. mindspore/ops/operations/linalg_ops.py +30 -11
  476. mindspore/ops/operations/math_ops.py +1730 -435
  477. mindspore/ops/operations/nn_ops.py +1953 -943
  478. mindspore/ops/operations/other_ops.py +65 -43
  479. mindspore/ops/operations/random_ops.py +258 -98
  480. mindspore/ops/operations/rl_ops.py +4 -36
  481. mindspore/ops/operations/sparse_ops.py +38 -33
  482. mindspore/ops/operations/spectral_ops.py +8 -4
  483. mindspore/ops/primitive.py +66 -44
  484. mindspore/ops/signature.py +5 -5
  485. mindspore/parallel/_auto_parallel_context.py +80 -19
  486. mindspore/parallel/_cost_model_context.py +42 -0
  487. mindspore/parallel/_offload_context.py +162 -72
  488. mindspore/parallel/_parallel_serialization.py +2 -2
  489. mindspore/parallel/_ps_context.py +16 -4
  490. mindspore/parallel/_recovery_context.py +2 -1
  491. mindspore/parallel/_tensor.py +15 -13
  492. mindspore/parallel/_transformer/layers.py +8 -6
  493. mindspore/parallel/_transformer/loss.py +1 -0
  494. mindspore/parallel/_transformer/moe.py +7 -7
  495. mindspore/parallel/_transformer/op_parallel_config.py +12 -1
  496. mindspore/parallel/_transformer/transformer.py +34 -14
  497. mindspore/parallel/_utils.py +36 -14
  498. mindspore/parallel/algo_parameter_config.py +114 -20
  499. mindspore/parallel/checkpoint_transform.py +16 -18
  500. mindspore/parallel/shard.py +16 -13
  501. mindspore/profiler/__init__.py +1 -1
  502. mindspore/profiler/common/struct_type.py +3 -3
  503. mindspore/profiler/common/util.py +3 -2
  504. mindspore/profiler/envprofiling.py +11 -4
  505. mindspore/profiler/parser/aicpu_data_parser.py +5 -3
  506. mindspore/profiler/parser/ascend_flops_generator.py +94 -0
  507. mindspore/profiler/parser/ascend_fpbp_generator.py +76 -0
  508. mindspore/profiler/parser/ascend_hccl_generator.py +288 -0
  509. mindspore/profiler/parser/ascend_msprof_exporter.py +213 -0
  510. mindspore/profiler/parser/ascend_msprof_generator.py +199 -0
  511. mindspore/profiler/parser/ascend_op_generator.py +276 -0
  512. mindspore/profiler/parser/ascend_steptrace_generator.py +94 -0
  513. mindspore/profiler/parser/ascend_timeline_generator.py +110 -54
  514. mindspore/profiler/parser/base_timeline_generator.py +11 -7
  515. mindspore/profiler/parser/cpu_gpu_timeline_generator.py +45 -46
  516. mindspore/profiler/parser/flops_parser.py +15 -11
  517. mindspore/profiler/parser/framework_parser.py +92 -73
  518. mindspore/profiler/parser/hccl_parser.py +16 -12
  519. mindspore/profiler/parser/integrator.py +22 -11
  520. mindspore/profiler/parser/memory_usage_parser.py +36 -11
  521. mindspore/profiler/parser/minddata_analyzer.py +12 -14
  522. mindspore/profiler/parser/minddata_pipeline_parser.py +1 -1
  523. mindspore/profiler/parser/msadvisor_parser.py +8 -4
  524. mindspore/profiler/parser/op_intermediate_parser.py +5 -2
  525. mindspore/profiler/parser/optime_parser.py +1 -1
  526. mindspore/profiler/parser/profiler_info.py +4 -5
  527. mindspore/profiler/parser/step_trace_parser.py +11 -14
  528. mindspore/profiler/profiling.py +678 -377
  529. mindspore/rewrite/api/node.py +211 -54
  530. mindspore/rewrite/api/node_type.py +5 -0
  531. mindspore/rewrite/api/pattern_engine.py +22 -23
  532. mindspore/rewrite/api/scoped_value.py +20 -17
  533. mindspore/rewrite/api/symbol_tree.py +252 -106
  534. mindspore/rewrite/api/tree_node_helper.py +3 -0
  535. mindspore/rewrite/ast_helpers/__init__.py +2 -1
  536. mindspore/rewrite/ast_helpers/ast_finder.py +129 -0
  537. mindspore/rewrite/ast_helpers/ast_modifier.py +116 -104
  538. mindspore/rewrite/ast_transformers/flatten_recursive_stmt.py +97 -46
  539. mindspore/rewrite/common/rewrite_elog.py +5 -1
  540. mindspore/rewrite/namer.py +51 -51
  541. mindspore/rewrite/namespace.py +14 -5
  542. mindspore/{ops/bprop_mindir → rewrite/node}/__init__.py +9 -4
  543. mindspore/rewrite/node/call_function.py +79 -0
  544. mindspore/rewrite/node/cell_container.py +135 -0
  545. mindspore/rewrite/node/control_flow.py +88 -0
  546. mindspore/rewrite/{node.py → node/node.py} +313 -247
  547. mindspore/rewrite/node/node_manager.py +254 -0
  548. mindspore/rewrite/node/node_topological_manager.py +243 -0
  549. mindspore/rewrite/parsers/arguments_parser.py +22 -21
  550. mindspore/rewrite/parsers/assign_parser.py +225 -239
  551. mindspore/rewrite/parsers/attribute_parser.py +9 -7
  552. mindspore/rewrite/parsers/class_def_parser.py +179 -218
  553. mindspore/rewrite/parsers/constant_parser.py +9 -6
  554. mindspore/rewrite/parsers/container_parser.py +9 -7
  555. mindspore/rewrite/parsers/for_parser.py +36 -15
  556. mindspore/rewrite/parsers/function_def_parser.py +23 -20
  557. mindspore/rewrite/parsers/if_parser.py +28 -24
  558. mindspore/rewrite/parsers/module_parser.py +202 -25
  559. mindspore/rewrite/{parser.py → parsers/parser.py} +4 -2
  560. mindspore/rewrite/{parser_register.py → parsers/parser_register.py} +1 -1
  561. mindspore/rewrite/parsers/return_parser.py +6 -6
  562. mindspore/rewrite/sparsify/sparse_transformer.py +12 -3
  563. mindspore/rewrite/sparsify/sparsify.py +4 -1
  564. mindspore/rewrite/sparsify/utils.py +11 -5
  565. mindspore/rewrite/symbol_tree.py +577 -732
  566. mindspore/rewrite/symbol_tree_builder.py +9 -175
  567. mindspore/rewrite/symbol_tree_dumper.py +2 -2
  568. mindspore/run_check/_check_version.py +46 -39
  569. mindspore/run_check/run_check.py +3 -2
  570. mindspore/{scipy/sparse → safeguard}/__init__.py +4 -5
  571. mindspore/safeguard/rewrite_obfuscation.py +517 -0
  572. mindspore/scipy/__init__.py +1 -1
  573. mindspore/scipy/linalg.py +67 -61
  574. mindspore/scipy/ops.py +5 -41
  575. mindspore/scipy/ops_grad.py +3 -2
  576. mindspore/scipy/ops_wrapper.py +5 -5
  577. mindspore/scipy/optimize/line_search.py +8 -8
  578. mindspore/scipy/optimize/linear_sum_assignment.py +4 -4
  579. mindspore/scipy/optimize/minimize.py +16 -12
  580. mindspore/scipy/utils.py +1 -52
  581. mindspore/scipy/utils_const.py +4 -4
  582. mindspore/train/__init__.py +4 -4
  583. mindspore/train/_utils.py +13 -5
  584. mindspore/train/amp.py +410 -148
  585. mindspore/train/anf_ir_pb2.py +16 -4
  586. mindspore/train/callback/_backup_and_restore.py +8 -11
  587. mindspore/train/callback/_callback.py +80 -3
  588. mindspore/train/callback/_checkpoint.py +82 -51
  589. mindspore/train/callback/_early_stop.py +12 -15
  590. mindspore/train/callback/_history.py +1 -1
  591. mindspore/train/callback/_lambda_callback.py +13 -13
  592. mindspore/train/callback/_landscape.py +21 -17
  593. mindspore/train/callback/_loss_monitor.py +9 -10
  594. mindspore/train/callback/_on_request_exit.py +16 -33
  595. mindspore/train/callback/_reduce_lr_on_plateau.py +21 -24
  596. mindspore/train/callback/_summary_collector.py +44 -30
  597. mindspore/train/callback/_time_monitor.py +62 -12
  598. mindspore/train/data_sink.py +10 -16
  599. mindspore/train/dataset_helper.py +154 -86
  600. mindspore/train/loss_scale_manager.py +14 -9
  601. mindspore/train/metrics/__init__.py +10 -2
  602. mindspore/train/metrics/accuracy.py +1 -1
  603. mindspore/train/metrics/auc.py +1 -1
  604. mindspore/train/metrics/bleu_score.py +2 -2
  605. mindspore/train/metrics/confusion_matrix.py +14 -14
  606. mindspore/train/metrics/cosine_similarity.py +3 -3
  607. mindspore/train/metrics/dice.py +1 -1
  608. mindspore/train/metrics/fbeta.py +1 -1
  609. mindspore/train/metrics/hausdorff_distance.py +8 -6
  610. mindspore/train/metrics/mean_surface_distance.py +5 -4
  611. mindspore/train/metrics/metric.py +49 -17
  612. mindspore/train/metrics/occlusion_sensitivity.py +4 -4
  613. mindspore/train/metrics/perplexity.py +1 -1
  614. mindspore/train/metrics/precision.py +2 -2
  615. mindspore/train/metrics/recall.py +2 -3
  616. mindspore/train/metrics/roc.py +7 -7
  617. mindspore/train/metrics/root_mean_square_surface_distance.py +5 -4
  618. mindspore/train/metrics/topk.py +7 -4
  619. mindspore/train/mind_ir_pb2.py +193 -48
  620. mindspore/train/model.py +377 -133
  621. mindspore/train/serialization.py +697 -245
  622. mindspore/train/summary/_summary_adapter.py +5 -2
  623. mindspore/train/summary/_writer_pool.py +4 -3
  624. mindspore/train/summary/summary_record.py +25 -23
  625. mindspore/train/train_thor/convert_utils.py +39 -23
  626. mindspore/train/train_thor/dataset_helper.py +4 -3
  627. mindspore/train/train_thor/model_thor.py +8 -8
  628. mindspore/version.py +1 -1
  629. {mindspore-2.0.0rc1.dist-info → mindspore-2.2.0.dist-info}/METADATA +7 -8
  630. {mindspore-2.0.0rc1.dist-info → mindspore-2.2.0.dist-info}/RECORD +633 -804
  631. {mindspore-2.0.0rc1.dist-info → mindspore-2.2.0.dist-info}/entry_points.txt +0 -1
  632. mindspore/_akg/akg/tvm/contrib/debugger/__init__.py +0 -16
  633. mindspore/_akg/akg/tvm/contrib/debugger/debug_result.py +0 -274
  634. mindspore/_akg/akg/tvm/contrib/debugger/debug_runtime.py +0 -259
  635. mindspore/_akg/akg/tvm/contrib/peak.py +0 -341
  636. mindspore/_akg/akg/tvm/contrib/rpc.py +0 -25
  637. mindspore/_akg/akg/tvm/contrib/xcode.py +0 -257
  638. mindspore/_akg/akg/tvm/exec/__init__.py +0 -17
  639. mindspore/_akg/akg/tvm/exec/autotvm_log_editor.py +0 -60
  640. mindspore/_akg/akg/tvm/exec/measure_peak.py +0 -48
  641. mindspore/_akg/akg/tvm/exec/query_rpc_tracker.py +0 -48
  642. mindspore/_akg/akg/tvm/exec/rpc_proxy.py +0 -98
  643. mindspore/_akg/akg/tvm/exec/rpc_server.py +0 -88
  644. mindspore/_akg/akg/tvm/exec/rpc_tracker.py +0 -62
  645. mindspore/_akg/akg/tvm/rpc/__init__.py +0 -29
  646. mindspore/_akg/akg/tvm/rpc/base.py +0 -182
  647. mindspore/_akg/akg/tvm/rpc/client.py +0 -436
  648. mindspore/_akg/akg/tvm/rpc/proxy.py +0 -595
  649. mindspore/_akg/akg/tvm/rpc/server.py +0 -413
  650. mindspore/_akg/akg/tvm/rpc/tornado_util.py +0 -121
  651. mindspore/_akg/akg/tvm/rpc/tracker.py +0 -431
  652. mindspore/_extends/graph_kernel/expander.py +0 -80
  653. mindspore/_extends/graph_kernel/expanders/__init__.py +0 -57
  654. mindspore/_extends/graph_kernel/expanders/_utils.py +0 -269
  655. mindspore/_extends/graph_kernel/expanders/addn.py +0 -33
  656. mindspore/_extends/graph_kernel/expanders/batchnorm.py +0 -152
  657. mindspore/_extends/graph_kernel/expanders/batchnorm_grad.py +0 -105
  658. mindspore/_extends/graph_kernel/expanders/bias_add_grad.py +0 -49
  659. mindspore/_extends/graph_kernel/expanders/clip_by_norm_no_div_sum.py +0 -33
  660. mindspore/_extends/graph_kernel/expanders/complex/abs.py +0 -30
  661. mindspore/_extends/graph_kernel/expanders/complex/add.py +0 -44
  662. mindspore/_extends/graph_kernel/expanders/complex/div.py +0 -62
  663. mindspore/_extends/graph_kernel/expanders/complex/mul.py +0 -52
  664. mindspore/_extends/graph_kernel/expanders/complex/real_div.py +0 -62
  665. mindspore/_extends/graph_kernel/expanders/complex/sub.py +0 -45
  666. mindspore/_extends/graph_kernel/expanders/conv2d.py +0 -200
  667. mindspore/_extends/graph_kernel/expanders/dropout_grad.py +0 -30
  668. mindspore/_extends/graph_kernel/expanders/equal_count.py +0 -50
  669. mindspore/_extends/graph_kernel/expanders/erfc.py +0 -35
  670. mindspore/_extends/graph_kernel/expanders/expand_dims.py +0 -50
  671. mindspore/_extends/graph_kernel/expanders/fused_adam.py +0 -44
  672. mindspore/_extends/graph_kernel/expanders/fused_adam_weight_decay.py +0 -47
  673. mindspore/_extends/graph_kernel/expanders/fused_mul_add.py +0 -28
  674. mindspore/_extends/graph_kernel/expanders/gather.py +0 -43
  675. mindspore/_extends/graph_kernel/expanders/gelu_grad.py +0 -70
  676. mindspore/_extends/graph_kernel/expanders/gkdropout.py +0 -40
  677. mindspore/_extends/graph_kernel/expanders/identity.py +0 -25
  678. mindspore/_extends/graph_kernel/expanders/layernorm.py +0 -93
  679. mindspore/_extends/graph_kernel/expanders/layernorm_grad.py +0 -113
  680. mindspore/_extends/graph_kernel/expanders/logsoftmax.py +0 -46
  681. mindspore/_extends/graph_kernel/expanders/logsoftmax_grad.py +0 -36
  682. mindspore/_extends/graph_kernel/expanders/matmul.py +0 -80
  683. mindspore/_extends/graph_kernel/expanders/maximum_grad.py +0 -59
  684. mindspore/_extends/graph_kernel/expanders/minimum_grad.py +0 -80
  685. mindspore/_extends/graph_kernel/expanders/oneslike.py +0 -26
  686. mindspore/_extends/graph_kernel/expanders/reduce_mean.py +0 -43
  687. mindspore/_extends/graph_kernel/expanders/relu_grad.py +0 -32
  688. mindspore/_extends/graph_kernel/expanders/sigmoid_cross_entropy_with_logits.py +0 -41
  689. mindspore/_extends/graph_kernel/expanders/sigmoid_cross_entropy_with_logits_grad.py +0 -35
  690. mindspore/_extends/graph_kernel/expanders/sigmoid_grad.py +0 -31
  691. mindspore/_extends/graph_kernel/expanders/slice.py +0 -35
  692. mindspore/_extends/graph_kernel/expanders/softmax_cross_entropy_with_logits.py +0 -42
  693. mindspore/_extends/graph_kernel/expanders/softmax_grad_ext.py +0 -41
  694. mindspore/_extends/graph_kernel/expanders/softsign.py +0 -28
  695. mindspore/_extends/graph_kernel/expanders/sqrt_grad.py +0 -29
  696. mindspore/_extends/graph_kernel/expanders/square_sum_all.py +0 -44
  697. mindspore/_extends/graph_kernel/expanders/square_sum_v1.py +0 -37
  698. mindspore/_extends/graph_kernel/expanders/squared_difference.py +0 -43
  699. mindspore/_extends/graph_kernel/expanders/tanh_grad.py +0 -31
  700. mindspore/_extends/graph_kernel/expanders/tile.py +0 -54
  701. mindspore/_extends/graph_kernel/model/op_infer.py +0 -506
  702. mindspore/_extends/parse/jit_fallback_modules.py +0 -51
  703. mindspore/dataset/datapreprocess/preprocess_imagenet_validate_dataset.py +0 -54
  704. mindspore/dataset/engine/graphdata.py +0 -1586
  705. mindspore/include/api/net.h +0 -142
  706. mindspore/ops/_grad/grad_array_ops.py +0 -1347
  707. mindspore/ops/_grad/grad_clip_ops.py +0 -84
  708. mindspore/ops/_grad/grad_debug_ops.py +0 -68
  709. mindspore/ops/_grad/grad_inner_ops.py +0 -235
  710. mindspore/ops/_grad/grad_math_ops.py +0 -1684
  711. mindspore/ops/_grad/grad_nn_ops.py +0 -1529
  712. mindspore/ops/_grad/grad_other_ops.py +0 -89
  713. mindspore/ops/_grad/grad_sequence_ops.py +0 -296
  714. mindspore/ops/_grad/grad_sparse.py +0 -323
  715. mindspore/ops/_grad_experimental/grad_image_ops.py +0 -249
  716. mindspore/ops/_grad_experimental/grad_linalg_ops.py +0 -195
  717. mindspore/ops/_grad_experimental/grad_scalar_ops.py +0 -112
  718. mindspore/ops/bprop_mindir/AdaptiveAvgPool2D_bprop.mindir +0 -0
  719. mindspore/ops/bprop_mindir/AdaptiveMaxPool2D_bprop.mindir +0 -0
  720. mindspore/ops/bprop_mindir/ApproximateEqual_bprop.mindir +0 -19
  721. mindspore/ops/bprop_mindir/Argmax_bprop.mindir +0 -15
  722. mindspore/ops/bprop_mindir/Argmin_bprop.mindir +0 -15
  723. mindspore/ops/bprop_mindir/AssignSub_bprop.mindir +0 -19
  724. mindspore/ops/bprop_mindir/Assign_bprop.mindir +0 -17
  725. mindspore/ops/bprop_mindir/AvgPool3D_bprop.mindir +0 -150
  726. mindspore/ops/bprop_mindir/AvgPool_bprop.mindir +0 -66
  727. mindspore/ops/bprop_mindir/BCEWithLogitsLoss_bprop.mindir +0 -0
  728. mindspore/ops/bprop_mindir/BNTrainingReduce_bprop.mindir +0 -15
  729. mindspore/ops/bprop_mindir/BatchNormGrad_bprop.mindir +0 -0
  730. mindspore/ops/bprop_mindir/BatchToSpaceND_bprop.mindir +0 -28
  731. mindspore/ops/bprop_mindir/BiasAddGrad_bprop.mindir +0 -0
  732. mindspore/ops/bprop_mindir/BinaryCrossEntropy_bprop.mindir +0 -33
  733. mindspore/ops/bprop_mindir/BroadcastTo_bprop.mindir +0 -306
  734. mindspore/ops/bprop_mindir/Broadcast_bprop.mindir +0 -13
  735. mindspore/ops/bprop_mindir/CTCLoss_bprop.mindir +0 -0
  736. mindspore/ops/bprop_mindir/Concat_bprop.mindir +0 -0
  737. mindspore/ops/bprop_mindir/Conv2DBackpropFilter_bprop.mindir +0 -240
  738. mindspore/ops/bprop_mindir/Conv2DBackpropInput_bprop.mindir +0 -247
  739. mindspore/ops/bprop_mindir/Conv2DTranspose_bprop.mindir +0 -247
  740. mindspore/ops/bprop_mindir/Conv3DTranspose_bprop.mindir +0 -315
  741. mindspore/ops/bprop_mindir/Conv3D_bprop.mindir +0 -278
  742. mindspore/ops/bprop_mindir/DType_bprop.mindir +0 -14
  743. mindspore/ops/bprop_mindir/DeformableOffsets_bprop.mindir +0 -58
  744. mindspore/ops/bprop_mindir/Depend_bprop.mindir +0 -13
  745. mindspore/ops/bprop_mindir/DepthToSpace_bprop.mindir +0 -23
  746. mindspore/ops/bprop_mindir/DepthwiseConv2dNative_bprop.mindir +0 -138
  747. mindspore/ops/bprop_mindir/DiagPart_bprop.mindir +0 -15
  748. mindspore/ops/bprop_mindir/Dropout2D_bprop.mindir +0 -0
  749. mindspore/ops/bprop_mindir/Dropout3D_bprop.mindir +0 -0
  750. mindspore/ops/bprop_mindir/DropoutDoMask_bprop.mindir +0 -25
  751. mindspore/ops/bprop_mindir/DropoutGenMask_bprop.mindir +0 -18
  752. mindspore/ops/bprop_mindir/DropoutGrad_bprop.mindir +0 -27
  753. mindspore/ops/bprop_mindir/Dropout_bprop.mindir +0 -0
  754. mindspore/ops/bprop_mindir/DynamicGRUV2_bprop.mindir +0 -0
  755. mindspore/ops/bprop_mindir/DynamicRNN_bprop.mindir +0 -0
  756. mindspore/ops/bprop_mindir/DynamicShape_bprop.mindir +0 -14
  757. mindspore/ops/bprop_mindir/Elu_bprop.mindir +0 -16
  758. mindspore/ops/bprop_mindir/EmbeddingLookup_bprop.mindir +0 -0
  759. mindspore/ops/bprop_mindir/Equal_bprop.mindir +0 -19
  760. mindspore/ops/bprop_mindir/ExpandDims_bprop.mindir +0 -58
  761. mindspore/ops/bprop_mindir/FastGeLU_bprop.mindir +0 -16
  762. mindspore/ops/bprop_mindir/Flatten_bprop.mindir +0 -54
  763. mindspore/ops/bprop_mindir/FloorDiv_bprop.mindir +0 -19
  764. mindspore/ops/bprop_mindir/GatherD_bprop.mindir +0 -26
  765. mindspore/ops/bprop_mindir/GatherNd_bprop.mindir +0 -57
  766. mindspore/ops/bprop_mindir/Gather_bprop.mindir +0 -0
  767. mindspore/ops/bprop_mindir/GreaterEqual_bprop.mindir +0 -19
  768. mindspore/ops/bprop_mindir/Greater_bprop.mindir +0 -19
  769. mindspore/ops/bprop_mindir/HSigmoid_bprop.mindir +0 -16
  770. mindspore/ops/bprop_mindir/HSwish_bprop.mindir +0 -16
  771. mindspore/ops/bprop_mindir/IOU_bprop.mindir +0 -19
  772. mindspore/ops/bprop_mindir/InstanceNorm_bprop.mindir +0 -0
  773. mindspore/ops/bprop_mindir/IsFinite_bprop.mindir +0 -15
  774. mindspore/ops/bprop_mindir/IsInf_bprop.mindir +0 -15
  775. mindspore/ops/bprop_mindir/IsNan_bprop.mindir +0 -15
  776. mindspore/ops/bprop_mindir/KLDivLoss_bprop.mindir +0 -126
  777. mindspore/ops/bprop_mindir/L2Loss_bprop.mindir +0 -15
  778. mindspore/ops/bprop_mindir/L2Normalize_bprop.mindir +0 -30
  779. mindspore/ops/bprop_mindir/LRN_bprop.mindir +0 -43
  780. mindspore/ops/bprop_mindir/LayerNormGrad_bprop.mindir +0 -0
  781. mindspore/ops/bprop_mindir/LessEqual_bprop.mindir +0 -19
  782. mindspore/ops/bprop_mindir/Less_bprop.mindir +0 -19
  783. mindspore/ops/bprop_mindir/LinSpace_bprop.mindir +0 -23
  784. mindspore/ops/bprop_mindir/Load_bprop.mindir +0 -13
  785. mindspore/ops/bprop_mindir/LogSoftmax_bprop.mindir +0 -23
  786. mindspore/ops/bprop_mindir/LogicalAnd_bprop.mindir +0 -19
  787. mindspore/ops/bprop_mindir/LogicalNot_bprop.mindir +0 -15
  788. mindspore/ops/bprop_mindir/MaskedSelect_bprop.mindir +0 -21
  789. mindspore/ops/bprop_mindir/MaxPool3DGradGrad_bprop.mindir +0 -74
  790. mindspore/ops/bprop_mindir/MaxPool3DGrad_bprop.mindir +0 -74
  791. mindspore/ops/bprop_mindir/MaxPool3D_bprop.mindir +0 -75
  792. mindspore/ops/bprop_mindir/MaxPoolGradGrad_bprop.mindir +0 -65
  793. mindspore/ops/bprop_mindir/MaxPoolWithArgmax_bprop.mindir +0 -0
  794. mindspore/ops/bprop_mindir/Maximum_bprop.mindir +0 -0
  795. mindspore/ops/bprop_mindir/Minimum_bprop.mindir +0 -0
  796. mindspore/ops/bprop_mindir/MirrorPad_bprop.mindir +0 -27
  797. mindspore/ops/bprop_mindir/Mish_bprop.mindir +0 -35
  798. mindspore/ops/bprop_mindir/MulNoNan_bprop.mindir +0 -0
  799. mindspore/ops/bprop_mindir/NLLLoss_bprop.mindir +0 -0
  800. mindspore/ops/bprop_mindir/NonZero_bprop.mindir +0 -14
  801. mindspore/ops/bprop_mindir/NotEqual_bprop.mindir +0 -19
  802. mindspore/ops/bprop_mindir/OneHot_bprop.mindir +0 -26
  803. mindspore/ops/bprop_mindir/OnesLike_bprop.mindir +0 -14
  804. mindspore/ops/bprop_mindir/PReLU_bprop.mindir +0 -0
  805. mindspore/ops/bprop_mindir/Pad_bprop.mindir +0 -0
  806. mindspore/ops/bprop_mindir/Padding_bprop.mindir +0 -0
  807. mindspore/ops/bprop_mindir/RNNTLoss_bprop.mindir +0 -29
  808. mindspore/ops/bprop_mindir/ROIAlign_bprop.mindir +0 -82
  809. mindspore/ops/bprop_mindir/Range_bprop.mindir +0 -22
  810. mindspore/ops/bprop_mindir/Rank_bprop.mindir +0 -14
  811. mindspore/ops/bprop_mindir/ReLU6_bprop.mindir +0 -16
  812. mindspore/ops/bprop_mindir/ReLUV2_bprop.mindir +0 -0
  813. mindspore/ops/bprop_mindir/ReduceAll_bprop.mindir +0 -19
  814. mindspore/ops/bprop_mindir/ReduceAny_bprop.mindir +0 -19
  815. mindspore/ops/bprop_mindir/ReluGrad_bprop.mindir +0 -20
  816. mindspore/ops/bprop_mindir/Reshape_bprop.mindir +0 -60
  817. mindspore/ops/bprop_mindir/ResizeBilinear_bprop.mindir +0 -29
  818. mindspore/ops/bprop_mindir/ResizeNearestNeighbor_bprop.mindir +0 -89
  819. mindspore/ops/bprop_mindir/ReverseSequence_bprop.mindir +0 -52
  820. mindspore/ops/bprop_mindir/ReverseV2_bprop.mindir +0 -22
  821. mindspore/ops/bprop_mindir/Round_bprop.mindir +0 -15
  822. mindspore/ops/bprop_mindir/ScatterMax_bprop.mindir +0 -0
  823. mindspore/ops/bprop_mindir/ScatterMin_bprop.mindir +0 -0
  824. mindspore/ops/bprop_mindir/ScatterNdUpdate_bprop.mindir +0 -22
  825. mindspore/ops/bprop_mindir/ScatterNd_bprop.mindir +0 -24
  826. mindspore/ops/bprop_mindir/ScatterNonAliasingAdd_bprop.mindir +0 -22
  827. mindspore/ops/bprop_mindir/ScatterUpdate_bprop.mindir +0 -0
  828. mindspore/ops/bprop_mindir/SeLU_bprop.mindir +0 -21
  829. mindspore/ops/bprop_mindir/Select_bprop.mindir +0 -31
  830. mindspore/ops/bprop_mindir/Shape_bprop.mindir +0 -14
  831. mindspore/ops/bprop_mindir/SigmoidCrossEntropyWithLogits_bprop.mindir +0 -21
  832. mindspore/ops/bprop_mindir/SigmoidGrad_bprop.mindir +0 -0
  833. mindspore/ops/bprop_mindir/Sigmoid_bprop.mindir +0 -16
  834. mindspore/ops/bprop_mindir/Sign_bprop.mindir +0 -15
  835. mindspore/ops/bprop_mindir/Slice_bprop.mindir +0 -26
  836. mindspore/ops/bprop_mindir/SmoothL1Loss_bprop.mindir +0 -36
  837. mindspore/ops/bprop_mindir/SoftmaxCrossEntropyWithLogits_bprop.mindir +0 -0
  838. mindspore/ops/bprop_mindir/Softplus_bprop.mindir +0 -16
  839. mindspore/ops/bprop_mindir/Softsign_bprop.mindir +0 -33
  840. mindspore/ops/bprop_mindir/Sort_bprop.mindir +0 -0
  841. mindspore/ops/bprop_mindir/SpaceToBatchND_bprop.mindir +0 -28
  842. mindspore/ops/bprop_mindir/SpaceToDepth_bprop.mindir +0 -23
  843. mindspore/ops/bprop_mindir/SparseGatherV2_bprop.mindir +0 -0
  844. mindspore/ops/bprop_mindir/SparseSoftmaxCrossEntropyWithLogits_bprop.mindir +0 -0
  845. mindspore/ops/bprop_mindir/Split_bprop.mindir +0 -22
  846. mindspore/ops/bprop_mindir/Squeeze_bprop.mindir +0 -54
  847. mindspore/ops/bprop_mindir/StridedSliceGrad_bprop.mindir +0 -95
  848. mindspore/ops/bprop_mindir/StridedSlice_bprop.mindir +0 -98
  849. mindspore/ops/bprop_mindir/Switch_bprop.mindir +0 -29
  850. mindspore/ops/bprop_mindir/TanhGrad_bprop.mindir +0 -0
  851. mindspore/ops/bprop_mindir/Tanh_bprop.mindir +0 -66
  852. mindspore/ops/bprop_mindir/TensorScatterAdd_bprop.mindir +0 -22
  853. mindspore/ops/bprop_mindir/TensorScatterUpdate_bprop.mindir +0 -29
  854. mindspore/ops/bprop_mindir/TensorShape_bprop.mindir +0 -14
  855. mindspore/ops/bprop_mindir/Tile_bprop.mindir +0 -0
  856. mindspore/ops/bprop_mindir/TopK_bprop.mindir +0 -0
  857. mindspore/ops/bprop_mindir/TransShape_bprop.mindir +0 -23
  858. mindspore/ops/bprop_mindir/TruncateDiv_bprop.mindir +0 -19
  859. mindspore/ops/bprop_mindir/TupleGetItem_bprop.mindir +0 -20
  860. mindspore/ops/bprop_mindir/Unique_bprop.mindir +0 -16
  861. mindspore/ops/bprop_mindir/Unstack_bprop.mindir +0 -22
  862. mindspore/ops/bprop_mindir/UpsampleNearest3D_bprop.mindir +0 -32
  863. mindspore/ops/bprop_mindir/UpsampleTrilinear3D_bprop.mindir +0 -38
  864. mindspore/ops/bprop_mindir/ZerosLike_bprop.mindir +0 -15
  865. mindspore/ops/bprop_mindir/generate_mindir.py +0 -114
  866. mindspore/rewrite/node_visitor.py +0 -44
  867. mindspore/rewrite/topological_manager.py +0 -203
  868. mindspore/scipy/sparse/linalg.py +0 -192
  869. {mindspore-2.0.0rc1.dist-info → mindspore-2.2.0.dist-info}/WHEEL +0 -0
  870. {mindspore-2.0.0rc1.dist-info → mindspore-2.2.0.dist-info}/top_level.txt +0 -0
mindspore/nn/loss/loss.py CHANGED
@@ -43,14 +43,51 @@ class LossBase(Cell):
43
43
  to apply reduction to loss values.
44
44
 
45
45
  Args:
46
- reduction (str): Type of reduction to be applied to loss. The optional values are "mean", "sum", and "none".
47
- Default: "mean".
46
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
47
+ ``'sum'`` . Default: ``'mean'`` .
48
+
49
+ - ``'none'``: no reduction will be applied.
50
+ - ``'mean'``: compute and return the (weighted) mean of elements in the output.
51
+ - ``'sum'``: the output elements will be summed.
48
52
 
49
53
  Raises:
50
- ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
54
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
51
55
 
52
56
  Supported Platforms:
53
57
  ``Ascend`` ``GPU`` ``CPU``
58
+
59
+ Examples:
60
+ >>> import mindspore
61
+ >>> from mindspore import ops, Tensor, nn
62
+ >>> import numpy as np
63
+ >>>
64
+ >>> class Net(nn.LossBase):
65
+ ... def __init__(self, reduction='mean'):
66
+ ... super(Net, self).__init__(reduction)
67
+ ... self.abs = ops.Abs()
68
+ ...
69
+ ... def construct(self, logits, labels):
70
+ ... x = self.abs(logits - labels)
71
+ ... output = self.get_loss(x)
72
+ ... axis = self.get_axis(x)
73
+ ... return output, axis
74
+ >>> net = Net()
75
+ >>> # Case 1: logits.shape = labels.shape = (3,)
76
+ >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
77
+ >>> labels = Tensor(np.array([1, 2, 2]), mindspore.float32)
78
+ >>> output, axis = net(logits, labels)
79
+ >>> print(output)
80
+ 0.33333334
81
+ >>> print(axis)
82
+ (0,)
83
+ >>> # Case 2: logits.shape = labels.shape = (3, 3)
84
+ >>> logits = Tensor(np.array([[1, 2, 3],[1, 2, 3],[1, 2, 3]]), mindspore.float32)
85
+ >>> labels = Tensor(np.array([[1, 2, 2],[1, 2, 3],[1, 2, 3]]), mindspore.float32)
86
+ >>> output, axis = net(logits, labels)
87
+ >>> print(output)
88
+ 0.11111111
89
+ >>> print(axis)
90
+ (0, 1)
54
91
  """
55
92
 
56
93
  def __init__(self, reduction='mean'):
@@ -79,30 +116,6 @@ class LossBase(Cell):
79
116
 
80
117
  Args:
81
118
  x (Tensor): Tensor of any shape.
82
-
83
- Examples:
84
- >>> class Net(nn.LossBase):
85
- ... def __init__(self, reduction='mean'):
86
- ... super(Net, self).__init__(reduction)
87
- ... self.abs = ops.Abs()
88
- ...
89
- ... def construct(self, logits, labels):
90
- ... x = self.abs(logits - labels)
91
- ... axis = self.get_axis(x)
92
- ... return axis
93
- >>> net = Net()
94
- >>> # Case 1: logits.shape = labels.shape = (3,)
95
- >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
96
- >>> labels = Tensor(np.array([1, 2, 3]), mindspore.float32)
97
- >>> output = net(logits, labels)
98
- >>> print(output)
99
- (0,)
100
- >>> # Case 2: logits.shape = labels.shape = (3, 3)
101
- >>> logits = Tensor(np.array([[1, 2, 3],[1, 2, 3],[1, 2, 3]]), mindspore.float32)
102
- >>> labels = Tensor(np.array([[1, 2, 3],[1, 2, 3],[1, 2, 3]]), mindspore.float32)
103
- >>> output = net(logits, labels)
104
- >>> print(output)
105
- (0, 1)
106
119
  """
107
120
  shape = F.shape(x)
108
121
  length = F.tuple_len(shape)
@@ -118,34 +131,10 @@ class LossBase(Cell):
118
131
  additional dimensions.
119
132
  weights (Union[float, Tensor]): Optional `Tensor` whose rank is either 0, or the same rank as inputs,
120
133
  and must be broadcastable to inputs (i.e., all dimensions must be either `1`,
121
- or the same as the corresponding inputs dimension). Default: 1.0.
134
+ or the same as the corresponding inputs dimension). Default: ``1.0`` .
122
135
 
123
136
  Returns:
124
137
  Return the weighted loss.
125
-
126
- Examples:
127
- >>> class Net(nn.LossBase):
128
- ... def __init__(self, reduction='mean'):
129
- ... super(Net, self).__init__(reduction)
130
- ... self.abs = ops.Abs()
131
- ...
132
- ... def construct(self, logits, labels):
133
- ... x = self.abs(logits - labels)
134
- ... output = self.get_loss(x)
135
- ... return output
136
- >>> net = Net()
137
- >>> # Case 1: logits.shape = labels.shape = (3,)
138
- >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
139
- >>> labels = Tensor(np.array([1, 2, 2]), mindspore.float32)
140
- >>> output = net(logits, labels)
141
- >>> print(output)
142
- 0.33333334
143
- >>> # Case 2: logits.shape = labels.shape = (3, 3)
144
- >>> logits = Tensor(np.array([[1, 2, 3],[1, 2, 3],[1, 2, 3]]), mindspore.float32)
145
- >>> labels = Tensor(np.array([[1, 2, 2],[1, 2, 3],[1, 2, 3]]), mindspore.float32)
146
- >>> output = net(logits, labels)
147
- >>> print(output)
148
- 0.11111111
149
138
  """
150
139
  input_dtype = x.dtype
151
140
  x = self.cast(x, mstype.float32)
@@ -180,8 +169,8 @@ class _Loss(LossBase):
180
169
  @constexpr(check=False)
181
170
  def _check_is_tensor(param_name, input_data, cls_name):
182
171
  """Internal function, used to check whether the input data is Tensor."""
183
- if input_data is not None and not isinstance(F.typeof(input_data), mstype.tensor_type):
184
- raise TypeError(f"For '{cls_name}', the '{param_name}' must be '{mstype.tensor_type}', "
172
+ if input_data is not None and not isinstance(F.typeof(input_data), mstype.TensorType):
173
+ raise TypeError(f"For '{cls_name}', the '{param_name}' must be '{mstype.TensorType}', "
185
174
  f"but got '{F.typeof(input_data)}'")
186
175
 
187
176
 
@@ -195,7 +184,7 @@ class L1Loss(LossBase):
195
184
  .. math::
196
185
  \ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad \text{with } l_n = \left| x_n - y_n \right|,
197
186
 
198
- where :math:`N` is the batch size. If `reduction` is not 'none', then:
187
+ where :math:`N` is the batch size. If `reduction` is not ``'none'``, then:
199
188
 
200
189
  .. math::
201
190
  \ell(x, y) =
@@ -205,9 +194,12 @@ class L1Loss(LossBase):
205
194
  \end{cases}
206
195
 
207
196
  Args:
208
- reduction (str): Type of reduction to be applied to loss. The optional values are "mean", "sum", and "none".
209
- Default: "mean". If `reduction` is "mean" or "sum", then output a scalar Tensor, if `reduction` is "none",
210
- the shape of the output Tensor is the broadcasted shape.
197
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
198
+ ``'sum'`` . Default: ``'mean'`` .
199
+
200
+ - ``'none'``: no reduction will be applied.
201
+ - ``'mean'``: compute and return the mean of elements in the output.
202
+ - ``'sum'``: the output elements will be summed.
211
203
 
212
204
  Inputs:
213
205
  - **logits** (Tensor) - Predicted value, Tensor of any dimension.
@@ -219,13 +211,16 @@ class L1Loss(LossBase):
219
211
  Tensor, data type is float.
220
212
 
221
213
  Raises:
222
- ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
214
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
223
215
  ValueError: If `logits` and `labels` have different shapes and cannot be broadcasted to each other.
224
216
 
225
217
  Supported Platforms:
226
218
  ``Ascend`` ``GPU`` ``CPU``
227
219
 
228
220
  Examples:
221
+ >>> import mindspore
222
+ >>> from mindspore import Tensor, nn
223
+ >>> import numpy as np
229
224
  >>> # Case 1: logits.shape = labels.shape = (3,)
230
225
  >>> loss = nn.L1Loss()
231
226
  >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
@@ -262,7 +257,7 @@ class MSELoss(LossBase):
262
257
  .. math::
263
258
  \ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad \text{with} \quad l_n = (x_n - y_n)^2.
264
259
 
265
- where :math:`N` is the batch size. If `reduction` is not 'none', then:
260
+ where :math:`N` is the batch size. If `reduction` is not ``'none'``, then:
266
261
 
267
262
  .. math::
268
263
  \ell(x, y) =
@@ -272,8 +267,12 @@ class MSELoss(LossBase):
272
267
  \end{cases}
273
268
 
274
269
  Args:
275
- reduction (str): Type of reduction to be applied to loss. The optional values are "mean", "sum", and "none".
276
- Default: "mean".
270
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
271
+ ``'sum'`` . Default: ``'mean'`` .
272
+
273
+ - ``'none'``: no reduction will be applied.
274
+ - ``'mean'``: compute and return the mean of elements in the output.
275
+ - ``'sum'``: the output elements will be summed.
277
276
 
278
277
  Inputs:
279
278
  - **logits** (Tensor) - The predicted value of the input. Tensor of any dimension.
@@ -282,17 +281,20 @@ class MSELoss(LossBase):
282
281
  and they should be broadcasted to each other.
283
282
 
284
283
  Outputs:
285
- Tensor, loss of type float, the shape is zero if `reduction` is 'mean' or 'sum',
284
+ Tensor, loss of type float, the shape is zero if `reduction` is ``'mean'`` or ``'sum'`` .,
286
285
  while the shape of output is the broadcasted shape if `reduction` is 'none'.
287
286
 
288
287
  Raises:
289
- ValueError: If `reduction` is not one of 'none', 'mean' or 'sum'.
288
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'`` or ``'sum'``.
290
289
  ValueError: If `logits` and `labels` have different shapes and cannot be broadcasted.
291
290
 
292
291
  Supported Platforms:
293
292
  ``Ascend`` ``GPU`` ``CPU``
294
293
 
295
294
  Examples:
295
+ >>> import mindspore
296
+ >>> from mindspore import Tensor, nn
297
+ >>> import numpy as np
296
298
  >>> # Case 1: logits.shape = labels.shape = (3,)
297
299
  >>> loss = nn.MSELoss()
298
300
  >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
@@ -344,12 +346,15 @@ class RMSELoss(LossBase):
344
346
  and they should be broadcasted to each other.
345
347
 
346
348
  Outputs:
347
- Tensor, weighted loss float tensor and its shape is ().
349
+ Tensor, weighted loss float tensor and its shape is :math:`()`.
348
350
 
349
351
  Supported Platforms:
350
352
  ``Ascend`` ``GPU`` ``CPU``
351
353
 
352
354
  Examples:
355
+ >>> import mindspore
356
+ >>> from mindspore import Tensor, nn
357
+ >>> import numpy as np
353
358
  >>> # Case 1: logits.shape = labels.shape = (3,)
354
359
  >>> loss = nn.RMSELoss()
355
360
  >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
@@ -395,7 +400,7 @@ class MAELoss(LossBase):
395
400
  .. math::
396
401
  \ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad \text{with } l_n = \left| x_n - y_n \right|,
397
402
 
398
- where :math:`N` is the batch size. If `reduction` is not 'none', then:
403
+ where :math:`N` is the batch size. If `reduction` is not ``'none'``, then:
399
404
 
400
405
  .. math::
401
406
  \ell(x, y) =
@@ -405,8 +410,12 @@ class MAELoss(LossBase):
405
410
  \end{cases}
406
411
 
407
412
  Args:
408
- reduction (str): Type of reduction to be applied to loss. The optional values are "mean", "sum", and "none".
409
- Default: "mean".
413
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
414
+ ``'sum'`` . Default: ``'mean'`` .
415
+
416
+ - ``'none'``: no reduction will be applied.
417
+ - ``'mean'``: compute and return the mean of elements in the output.
418
+ - ``'sum'``: the output elements will be summed.
410
419
 
411
420
  Inputs:
412
421
  - **logits** (Tensor) - Tensor of shape :math:`(M, *)` where :math:`*` means, any number of
@@ -416,16 +425,19 @@ class MAELoss(LossBase):
416
425
  and they should be broadcasted to each other.
417
426
 
418
427
  Outputs:
419
- Tensor, weighted loss float tensor, the shape is zero if `reduction` is 'mean' or 'sum',
428
+ Tensor, weighted loss float tensor, the shape is zero if `reduction` is ``'mean'`` or ``'sum'`` .,
420
429
  while the shape of output is the broadcasted shape if `reduction` is 'none'.
421
430
 
422
431
  Raises:
423
- ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
432
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
424
433
 
425
434
  Supported Platforms:
426
435
  ``Ascend`` ``GPU`` ``CPU``
427
436
 
428
437
  Examples:
438
+ >>> import mindspore
439
+ >>> from mindspore import Tensor, nn
440
+ >>> import numpy as np
429
441
  >>> # Case 1: logits.shape = labels.shape = (3,)
430
442
  >>> loss = nn.MAELoss()
431
443
  >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
@@ -466,9 +478,13 @@ class MarginRankingLoss(LossBase):
466
478
  \text{loss}(input1, input2, target) = \max(0, -target * (input1 - input2) + \text{margin})
467
479
 
468
480
  Args:
469
- margin (float, optional): Specify the adjustment factor of the operation. Default 0.0.
470
- reduction (str, optional): Specifies which reduction to be applied to the output. It must be one of "none",
471
- "mean", and "sum", meaning no reduction, reduce mean and sum on output, respectively. Default: "mean".
481
+ margin (float, optional): Specify the adjustment factor of the operation. Default: ``0.0`` .
482
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
483
+ ``'sum'`` . Default: ``'mean'`` .
484
+
485
+ - ``'none'``: no reduction will be applied.
486
+ - ``'mean'``: compute and return the mean of elements in the output.
487
+ - ``'sum'``: the output elements will be summed.
472
488
 
473
489
  Inputs:
474
490
  - **input1** (Tensor) - Tensor of shape :math:`(N, *)` where :math:`*` means, any number
@@ -478,7 +494,7 @@ class MarginRankingLoss(LossBase):
478
494
  :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)`.
479
495
 
480
496
  Outputs:
481
- Tensor or Scalar. if `reduction` is "none", its shape is the same as `labels`.
497
+ Tensor or Scalar. if `reduction` is ``"none"``, its shape is the same as `labels`.
482
498
  Otherwise, a scalar value will be returned.
483
499
 
484
500
  Raises:
@@ -488,16 +504,14 @@ class MarginRankingLoss(LossBase):
488
504
  TypeError: If the types of `input1` and `target` are inconsistent.
489
505
  ValueError: If the shape of `input1` and `input2` are inconsistent.
490
506
  ValueError: If the shape of `input1` and `target` are inconsistent.
491
- ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
507
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'`` , ``'sum'``.
492
508
 
493
509
  Supported Platforms:
494
510
  ``Ascend`` ``GPU`` ``CPU``
495
511
 
496
512
  Examples:
497
513
  >>> import mindspore as ms
498
- >>> import mindspore.nn as nn
499
- >>> import mindspore.ops as ops
500
- >>> from mindspore.ops import Tensor
514
+ >>> from mindspore import Tensor, nn, ops
501
515
  >>> import numpy as np
502
516
  >>> loss1 = nn.MarginRankingLoss(reduction='none')
503
517
  >>> loss2 = nn.MarginRankingLoss(reduction='mean')
@@ -554,17 +568,21 @@ class SmoothL1Loss(LossBase):
554
568
  \end{cases}
555
569
 
556
570
  .. note::
557
- For Ascend platform, the float64 data type of `logits` is not support now.
558
- SmoothL1Loss can be regarded as modified version of L1Loss or a combination of L1Loss and L2Loss.
559
- L1Loss computes the element-wise absolute difference between two input tensors while L2Loss computes the
560
- squared difference between two input tensors. L2Loss often leads to faster convergence but it is less
561
- robust to outliers, and the loss function has better robustness.
571
+ - On the Ascend platform, float64 data type will result in low operator performance.
572
+ - SmoothL1Loss can be regarded as modified version of L1Loss or a combination of L1Loss and L2Loss.
573
+ - L1Loss computes the element-wise absolute difference between two input tensors while L2Loss computes the
574
+ - squared difference between two input tensors. L2Loss often leads to faster convergence but it is less
575
+ - robust to outliers, and the loss function has better robustness.
562
576
 
563
577
  Args:
564
578
  beta (float): The loss function calculates the threshold of the transformation between L1Loss and L2Loss.
565
- Default: 1.0.
566
- reduction (str): Type of reduction to be applied to loss. The optional values are "mean", "sum", and "none".
567
- Default: "none".
579
+ Default: ``1.0`` .
580
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
581
+ ``'sum'`` . Default: ``'none'`` .
582
+
583
+ - ``'none'``: no reduction will be applied.
584
+ - ``'mean'``: compute and return the mean of elements in the output.
585
+ - ``'sum'``: the output elements will be summed.
568
586
 
569
587
  Inputs:
570
588
  - **logits** (Tensor) - Predictive value. Tensor of any dimension. Data type must be one of float16,
@@ -572,23 +590,25 @@ class SmoothL1Loss(LossBase):
572
590
  - **labels** (Tensor) - Ground truth data, same shape and dtype as the `logits`.
573
591
 
574
592
  Outputs:
575
- Tensor, if `reduction` is 'none', then output is a tensor with the same shape as `logits`.
576
- Otherwise the shape of output tensor is `()`.
593
+ Tensor, if `reduction` is ``'none'``, then output is a tensor with the same shape as `logits`.
594
+ Otherwise the shape of output tensor is :math:`()`.
577
595
 
578
596
  Raises:
579
597
  TypeError: If `beta` is not a float.
580
- ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
598
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
581
599
  TypeError: If `logits` or `labels` are not Tensor.
582
600
  TypeError: If dtype of `logits` or `labels` is neither float16 not float32.
583
601
  TypeError: If dtype of `logits` is not the same as `labels`.
584
602
  ValueError: If `beta` is less than or equal to 0.
585
603
  ValueError: If shape of `logits` is not the same as `labels`.
586
- TypeError: The float64 data type of `logits` is support on Ascend platform.
587
604
 
588
605
  Supported Platforms:
589
606
  ``Ascend`` ``GPU`` ``CPU``
590
607
 
591
608
  Examples:
609
+ >>> import mindspore
610
+ >>> from mindspore import Tensor, nn
611
+ >>> import numpy as np
592
612
  >>> loss = nn.SmoothL1Loss()
593
613
  >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
594
614
  >>> labels = Tensor(np.array([1, 2, 2]), mindspore.float32)
@@ -617,31 +637,39 @@ class SoftMarginLoss(LossBase):
617
637
  (containing 1 or -1).
618
638
 
619
639
  .. math::
620
- \text{loss}(x, y) = \sum_i \frac{\log(1 + \exp(-y[i]*x[i]))}{\text{x.nelement}()}
640
+ \text{loss}(x, y) = \sum_i \frac{\log(1 + \exp(-y[i]*x[i]))}{x.nelement()}
621
641
 
622
642
  :math:`x.nelement()` represents the number of element of `x` .
623
643
 
624
644
  Args:
625
- reduction (str): Apply specific reduction method to the output: 'none', 'mean', 'sum'. Default: "mean".
645
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
646
+ ``'sum'`` . Default: ``'mean'`` .
647
+
648
+ - ``'none'``: no reduction will be applied.
649
+ - ``'mean'``: compute and return the mean of elements in the output.
650
+ - ``'sum'``: the output elements will be summed.
626
651
 
627
652
  Inputs:
628
653
  - **logits** (Tensor) - Predict data. Data type must be float16 or float32.
629
654
  - **labels** (Tensor) - Ground truth data, with the same type and shape as `logits`.
630
655
 
631
656
  Outputs:
632
- Tensor or Scalar, if `reduction` is "none", its shape is the same as `logits`.
657
+ Tensor or Scalar, if `reduction` is ``"none"``, its shape is the same as `logits`.
633
658
  Otherwise, a scalar value will be returned.
634
659
 
635
660
  Raises:
636
661
  TypeError: If `logits` or `labels` is not a Tensor.
637
662
  TypeError: If dtype of `logits` or `labels` is neither float16 nor float32.
638
663
  ValueError: If shape of `logits` is not the same as `labels`.
639
- ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
664
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
640
665
 
641
666
  Supported Platforms:
642
667
  ``Ascend`` ``GPU``
643
668
 
644
669
  Examples:
670
+ >>> import mindspore
671
+ >>> from mindspore import Tensor, nn
672
+ >>> import numpy as np
645
673
  >>> loss = nn.SoftMarginLoss()
646
674
  >>> logits = Tensor(np.array([[0.3, 0.7], [0.5, 0.5]]), mindspore.float32)
647
675
  >>> labels = Tensor(np.array([[-1, 1], [1, -1]]), mindspore.float32)
@@ -685,9 +713,13 @@ class SoftmaxCrossEntropyWithLogits(LossBase):
685
713
  of entry is a valid one.
686
714
 
687
715
  Args:
688
- sparse (bool): Specifies whether labels use sparse format or not. Default: False.
689
- reduction (str): Type of reduction to be applied to loss. The optional values are "mean", "sum", and "none".
690
- If "none", do not perform reduction. Default: "none".
716
+ sparse (bool, optional): Specifies whether labels use sparse format or not. Default: ``False`` .
717
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
718
+ ``'sum'`` . Default: ``'none'`` .
719
+
720
+ - ``'none'``: no reduction will be applied.
721
+ - ``'mean'``: compute and return the mean of elements in the output.
722
+ - ``'sum'``: the output elements will be summed.
691
723
 
692
724
  Inputs:
693
725
  - **logits** (Tensor) - Tensor of shape :math:`(N, C)` . Data type must be float16 or float32.
@@ -701,12 +733,15 @@ class SoftmaxCrossEntropyWithLogits(LossBase):
701
733
  TypeError: If `sparse` is not a bool.
702
734
  TypeError: If `sparse` is True and dtype of `labels` is neither int32 not int64.
703
735
  TypeError: If `sparse` is False and dtype of `labels` is neither float16 not float32.
704
- ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
736
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
705
737
 
706
738
  Supported Platforms:
707
739
  ``Ascend`` ``GPU`` ``CPU``
708
740
 
709
741
  Examples:
742
+ >>> import mindspore
743
+ >>> from mindspore import Tensor, nn
744
+ >>> import numpy as np
710
745
  >>> # case 1: sparse=True
711
746
  >>> loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True)
712
747
  >>> logits = Tensor(np.array([[3, 5, 6, 9, 12, 33, 42, 12, 32, 72]]), mindspore.float32)
@@ -771,7 +806,7 @@ class DiceLoss(LossBase):
771
806
 
772
807
  Args:
773
808
  smooth (float): A term added to the denominator to improve numerical stability. Should be greater than 0.
774
- Default: 1e-5.
809
+ Default: ``1e-5`` .
775
810
 
776
811
  Inputs:
777
812
  - **logits** (Tensor) - Input predicted value. The data type must be float16 or float32.
@@ -789,9 +824,12 @@ class DiceLoss(LossBase):
789
824
  ``Ascend`` ``GPU`` ``CPU``
790
825
 
791
826
  Examples:
827
+ >>> import mindspore
828
+ >>> from mindspore import Tensor, nn
829
+ >>> import numpy as np
792
830
  >>> loss = nn.DiceLoss(smooth=1e-5)
793
- >>> logits = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mstype.float32)
794
- >>> labels = Tensor(np.array([[0, 1], [1, 0], [0, 1]]), mstype.float32)
831
+ >>> logits = Tensor(np.array([[0.2, 0.5], [0.3, 0.1], [0.9, 0.6]]), mindspore.float32)
832
+ >>> labels = Tensor(np.array([[0, 1], [1, 0], [0, 1]]), mindspore.float32)
795
833
  >>> output = loss(logits, labels)
796
834
  >>> print(output)
797
835
  0.38596618
@@ -856,11 +894,12 @@ class MultiClassDiceLoss(LossBase):
856
894
  Args:
857
895
  weights (Union[Tensor, None]): Tensor of shape :math:`(num\_classes, dim)`. The weight shape[0] should be
858
896
  equal to labels shape[1].
859
- Default: None.
897
+ Default: ``None`` .
860
898
  ignore_indiex (Union[int, None]): Class index to ignore.
861
- Default: None.
899
+ Default: ``None`` .
862
900
  activation (Union[str, Cell]): Activate function applied to the output of the fully connected layer, eg. 'ReLU'.
863
- Default: 'softmax'. Choose from: ['softmax', 'logsoftmax', 'relu', 'relu6', 'tanh','Sigmoid']
901
+ Default: ``'softmax'`` . Choose from: [ ``'softmax'`` , ``'logsoftmax'`` , ``'relu'`` , ``'relu6'`` ,
902
+ ``'tanh'`` , ``'Sigmoid'`` ]
864
903
 
865
904
  Inputs:
866
905
  - **logits** (Tensor) - Tensor of shape :math:`(N, C, *)` where :math:`*` means, any number of additional
@@ -882,9 +921,12 @@ class MultiClassDiceLoss(LossBase):
882
921
  ``Ascend`` ``GPU`` ``CPU``
883
922
 
884
923
  Examples:
924
+ >>> import mindspore
925
+ >>> from mindspore import Tensor, nn
926
+ >>> import numpy as np
885
927
  >>> loss = nn.MultiClassDiceLoss(weights=None, ignore_indiex=None, activation="softmax")
886
- >>> logits = Tensor(np.array([[0.2, 0.5, 0.7], [0.3, 0.1, 0.5], [0.9, 0.6, 0.3]]), mstype.float32)
887
- >>> labels = Tensor(np.array([[0, 1, 0], [1, 0, 0], [0, 0, 1]]), mstype.float32)
928
+ >>> logits = Tensor(np.array([[0.2, 0.5, 0.7], [0.3, 0.1, 0.5], [0.9, 0.6, 0.3]]), mindspore.float32)
929
+ >>> labels = Tensor(np.array([[0, 1, 0], [1, 0, 0], [0, 0, 1]]), mindspore.float32)
888
930
  >>> output = loss(logits, labels)
889
931
  >>> print(output)
890
932
  0.54958105
@@ -942,15 +984,19 @@ class SampledSoftmaxLoss(LossBase):
942
984
  Args:
943
985
  num_sampled (int): The number of classes to randomly sample per batch.
944
986
  num_classes (int): The number of possible classes.
945
- num_true (int): The number of labels classes per training example. Default: 1.
987
+ num_true (int): The number of labels classes per training example. Default: ``1`` .
946
988
  sampled_values (Union[list, tuple]): List or tuple of (`sampled_candidates`, `true_expected_count`,
947
989
  `sampled_expected_count`) returned by a `*CandidateSampler` function.
948
- Default to None, `UniformCandidateSampler` is applied.
990
+ Default to None, `UniformCandidateSampler` is applied. Default: ``None`` .
949
991
  remove_accidental_hits (bool): Whether to remove "accidental hits"
950
- where a sampled class equals to one of the labels classes. Default: True.
992
+ where a sampled class equals to one of the labels classes. Default: ``True`` .
951
993
  seed (int): Random seed for candidate sampling. Default: 0
952
- reduction (str): Type of reduction to be applied to loss. The optional values are "mean", "sum", and "none".
953
- If "none", do not perform reduction. Default: "none".
994
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
995
+ ``'sum'`` . Default: ``'none'`` .
996
+
997
+ - ``'none'``: no reduction will be applied.
998
+ - ``'mean'``: compute and return the mean of elements in the output.
999
+ - ``'sum'``: the output elements will be summed.
954
1000
 
955
1001
  Inputs:
956
1002
  - **weights** (Tensor) - Tensor of shape :math:`(C, dim)`.
@@ -959,13 +1005,13 @@ class SampledSoftmaxLoss(LossBase):
959
1005
  - **logits** (Tensor) - Tensor of shape :math:`(N, dim)`. The forward activations of the input network.
960
1006
 
961
1007
  Outputs:
962
- Tensor or Scalar, if `reduction` is 'none', then output is a tensor with shape :math:`(N,)`.
1008
+ Tensor or Scalar, if `reduction` is ``'none'``, then output is a tensor with shape :math:`(N,)`.
963
1009
  Otherwise, the output is a scalar.
964
1010
 
965
1011
  Raises:
966
1012
  TypeError: If `sampled_values` is not a list or tuple.
967
1013
  TypeError: If dtype of `labels` is neither int32 not int64.
968
- ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
1014
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
969
1015
  ValueError: If `num_sampled` or `num_true` is greater than `num_classes`.
970
1016
  ValueError: If length of `sampled_values` is not equal to 3.
971
1017
 
@@ -973,6 +1019,9 @@ class SampledSoftmaxLoss(LossBase):
973
1019
  ``GPU``
974
1020
 
975
1021
  Examples:
1022
+ >>> import mindspore
1023
+ >>> from mindspore import Tensor, nn
1024
+ >>> import numpy as np
976
1025
  >>> mindspore.set_seed(1)
977
1026
  >>> loss = nn.SampledSoftmaxLoss(num_sampled=4, num_classes=7, num_true=1)
978
1027
  >>> weights = Tensor(np.random.randint(0, 9, [7, 10]), mindspore.float32)
@@ -1091,7 +1140,7 @@ class SampledSoftmaxLoss(LossBase):
1091
1140
  sampled_values: A tuple of (`sampled_candidates`, `true_expected_count`,
1092
1141
  `sampled_expected_count`) returned by a `UniformCandidateSampler` function.
1093
1142
  subtract_log_q: A `bool`. whether to subtract the log expected count of
1094
- the labels in the sample to get the logits of the true labels. Default: True.
1143
+ the labels in the sample to get the logits of the true labels. Default: ``True`` .
1095
1144
  Returns:
1096
1145
  out_logits: `Tensor` object with shape
1097
1146
  `[batch_size, num_true + num_sampled]`
@@ -1159,6 +1208,117 @@ class SampledSoftmaxLoss(LossBase):
1159
1208
  return out_logits, out_labels
1160
1209
 
1161
1210
 
1211
+ class TripletMarginWithDistanceLoss(LossBase):
1212
+ r"""
1213
+ TripletMarginWithDistanceLoss operation.
1214
+
1215
+ Creates a criterion that measures the triplet loss given an input
1216
+ tensors :math:`x1`, :math:`x2`, :math:`x3` and a margin with a value greater than :math:`0`.
1217
+ This is used for measuring a relative similarity between samples. A triplet
1218
+ is composed by `a`, `p` and `n` (i.e., `anchor`, `positive examples` and `negative
1219
+ examples` respectively). The shapes of all input tensors should be
1220
+ :math:`(N, D)`.
1221
+
1222
+ The distance swap is described in detail in the paper `Learning shallow
1223
+ convolutional feature descriptors with triplet losses` by
1224
+ V. Balntas, E. Riba et al.
1225
+
1226
+ The loss function for each sample in the mini-batch is:
1227
+
1228
+ .. math::
1229
+ L(a, p, n) = \max \{d(a_i, p_i) - d(a_i, n_i) + {\rm margin}, 0\}
1230
+
1231
+ where
1232
+
1233
+ .. math::
1234
+ d(x_i, y_i) = \left\lVert {\bf x}_i - {\bf y}_i \right\rVert_p
1235
+
1236
+ Args:
1237
+ distance_function (callable): The distance function needed to calculate the margin loss of a triplet.
1238
+ if no distance metric is specified, the pairwise distance will be used. Default: ``None`` .
1239
+ swap (bool): The distance swap is described in detail in the paper
1240
+ `Learning shallow convolutional feature descriptors with triplet losses` by
1241
+ V. Balntas, E. Riba et al. Default: ``False`` .
1242
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
1243
+ ``'sum'`` . Default: ``'mean'`` .
1244
+
1245
+ - ``'none'``: no reduction will be applied.
1246
+ - ``'mean'``: compute and return the mean of elements in the output.
1247
+ - ``'sum'``: the output elements will be summed.
1248
+
1249
+ margin (float): Make a margin between the positive pair and the negative pair. Default: ``1.0`` .
1250
+
1251
+ Inputs:
1252
+ - **x** (Tensor) - A sample randomly selected from the training set. Data type must be BasicType.
1253
+ The shape should be :math:`(N, D)`.
1254
+ - **positive** (Tensor) - A sample belonging to the same category as x,
1255
+ with the same type and shape as `x`.
1256
+ - **negative** (Tensor) - A sample belonging to the different class from x,
1257
+ with the same type and shape as `x`.
1258
+
1259
+ Outputs:
1260
+ Union[Tensor, Scalar], if `reduction` is ``'none'``, its shape is :math:`(N)`.
1261
+ Otherwise, a scalar value will be returned.
1262
+
1263
+ Raises:
1264
+ TypeError: If `x` or `positive` or `negative` is not a Tensor.
1265
+ TypeError: If `swap` is not a bool.
1266
+ ValueError: If dimensions of input `x`, `positive` and `negative` are less than or equal to 1 at the same time.
1267
+ ValueError: If length of shape of `margin` is not 0.
1268
+ ValueError: If shape of `x`, `positive` and `negative` cannot broadcast.
1269
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
1270
+
1271
+ Supported Platforms:
1272
+ ``Ascend`` ``GPU`` ``CPU``
1273
+
1274
+ Examples:
1275
+ >>> import mindspore
1276
+ >>> from mindspore import Tensor, nn
1277
+ >>> x = Tensor([[0.3, 0.7], [0.5, 0.5]])
1278
+ >>> positive = Tensor([[0.4, 0.6], [0.4, 0.6]])
1279
+ >>> negative = Tensor([[0.2, 0.9], [0.3, 0.7]])
1280
+ >>> loss = nn.TripletMarginWithDistanceLoss()
1281
+ >>> out = loss(x, positive, negative)
1282
+ >>> print(out.asnumpy())
1283
+ 0.8881968
1284
+ """
1285
+
1286
+ def __init__(self, distance_function=None, swap=False, reduction="mean", margin=1.0):
1287
+ """Initialize TripletMarginWithDistanceLoss."""
1288
+ super(TripletMarginWithDistanceLoss, self).__init__(reduction=reduction)
1289
+ validator.check_is_float(margin, "margin", self.cls_name)
1290
+ validator.check_bool(swap, "swap", self.cls_name)
1291
+ if distance_function is None:
1292
+ def pairwise_distance(x, y):
1293
+ d = (x - y).abs()
1294
+ if d.ndim == 0:
1295
+ raise ValueError(
1296
+ "For 'pairwise_distance' in 'TripletMarginWithDistanceLoss', "
1297
+ "'ndim' of the input must be positive, "
1298
+ f"but got {d.ndim}"
1299
+ )
1300
+ return P.LpNorm(axis=1, p=2)(d)
1301
+ self.distance_function = pairwise_distance
1302
+ else:
1303
+ self.distance_function = distance_function
1304
+ self.swap = swap
1305
+ self.reduction = reduction
1306
+ self.margin = margin
1307
+ self.minimum = P.Minimum()
1308
+ self.maximum = P.Maximum()
1309
+
1310
+ def construct(self, x, positive, negative):
1311
+ _check_is_tensor("x", x, self.cls_name)
1312
+ _check_is_tensor("positive", positive, self.cls_name)
1313
+ _check_is_tensor("negative", negative, self.cls_name)
1314
+ d1 = self.distance_function(x, positive)
1315
+ d2 = self.distance_function(x, negative)
1316
+ if self.swap:
1317
+ d2 = self.minimum(d2, self.distance_function(positive, negative))
1318
+ loss = self.maximum(d1 - d2 + self.margin, 0)
1319
+ return self.get_loss(loss)
1320
+
1321
+
1162
1322
  class PoissonNLLLoss(LossBase):
1163
1323
  r"""
1164
1324
  Poisson negative log likelihood loss.
@@ -1185,18 +1345,23 @@ class PoissonNLLLoss(LossBase):
1185
1345
  will have a different range of return values and results different from those under GPU and CPU.
1186
1346
 
1187
1347
  Args:
1188
- log_input (bool, optional): Whether use log input. Default: True.
1189
- full (bool, optional): Whether include the Stirling approximation term in the loss calculation. Default: False.
1190
- eps (float, optional): Lower bound of `input` when calculating logarithms. Default: 1e-08.
1191
- reduction (str, optional): Apply specific reduction method to the output:
1192
- 'none', 'mean', 'sum'. Default: 'mean'.
1348
+ log_input (bool, optional): Whether use log input. Default: ``True`` .
1349
+ full (bool, optional): Whether include the Stirling approximation term in the loss calculation.
1350
+ Default: ``False`` .
1351
+ eps (float, optional): Lower bound of `input` when calculating logarithms. Default: ``1e-08`` .
1352
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
1353
+ ``'sum'`` . Default: ``'mean'`` .
1354
+
1355
+ - ``'none'``: no reduction will be applied.
1356
+ - ``'mean'``: compute and return the mean of elements in the output.
1357
+ - ``'sum'``: the output elements will be summed.
1193
1358
 
1194
1359
  Inputs:
1195
1360
  - **input** (Tensor) - The input Tensor. The shape can be any number of dimensions.
1196
1361
  - **target** (Tensor) - The label Tensor which has the same shape as `input`.
1197
1362
 
1198
1363
  Outputs:
1199
- Tensor or Scalar, if `reduction` is 'none', then output is a tensor and has the same shape as `input`.
1364
+ Tensor or Scalar, if `reduction` is ``'none'``, then output is a tensor and has the same shape as `input`.
1200
1365
  Otherwise it is a scalar.
1201
1366
 
1202
1367
  Raises:
@@ -1208,8 +1373,10 @@ class PoissonNLLLoss(LossBase):
1208
1373
  ``Ascend`` ``GPU`` ``CPU``
1209
1374
 
1210
1375
  Examples:
1211
- >>> x = Tensor([[0.3, 0.7], [0.5, 0.5]])
1212
- >>> target = Tensor([[1.0, 2.0], [3.0, 4.0]])
1376
+ >>> import mindspore as ms
1377
+ >>> import mindspore.nn as nn
1378
+ >>> x = ms.Tensor([[0.3, 0.7], [0.5, 0.5]])
1379
+ >>> target = ms.Tensor([[1.0, 2.0], [3.0, 4.0]])
1213
1380
  >>> loss = nn.PoissonNLLLoss()
1214
1381
  >>> output = loss(x, target)
1215
1382
  >>> print(output.asnumpy())
@@ -1264,25 +1431,28 @@ class MultiLabelSoftMarginLoss(LossBase):
1264
1431
  \sum_{j = 1}^{C}\left(y_{ij}\log\frac{1}{1 + e^{- x_{ij}}} + \left( 1 - y_{ij}
1265
1432
  \right)\log\frac{e^{-x_{ij}}}{1 + e^{-x_{ij}}} \right)
1266
1433
 
1267
- where :math:`x{ij}` represents the predicted score of sample :math:`i` for class :math:`j`. :math:`y{ij}`
1434
+ where :math:`x_{ij}` represents the predicted score of sample :math:`i` for class :math:`j`. :math:`y_{ij}`
1268
1435
  represents the binary label of sample :math:`i` for class :math:`j`, where sample :math:`i` belongs to
1269
- class :math:`j` if :math:`y{ij}=1` , and sample :math:`i` does not belong to class :math:`j` if :math:`y{ij}=0`.
1436
+ class :math:`j` if :math:`y_{ij}=1` , and sample :math:`i` does not belong to class :math:`j` if :math:`y_{ij}=0`.
1270
1437
  For a multi-label classification task, each sample may have multiple labels with a value of 1 in the binary
1271
1438
  label :math:`y`. `weight` will multiply to the loss of each class if given.
1272
1439
 
1273
1440
  Args:
1274
- weight (Union[Tensor, int, float]): The manual rescaling weight given to each class. Default: None.
1275
- reduction (str): Specifies which reduction to be applied to the output. It must be one of
1276
- 'none', 'mean', and 'sum', meaning no reduction, reduce mean and sum on output, respectively.
1277
- Default: 'mean'.
1441
+ weight (Union[Tensor, int, float]): The manual rescaling weight given to each class. Default: ``None`` .
1442
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
1443
+ ``'sum'`` . Default: ``'mean'`` .
1444
+
1445
+ - ``'none'``: no reduction will be applied.
1446
+ - ``'mean'``: compute and return the weighted mean of elements in the output.
1447
+ - ``'sum'``: the output elements will be summed.
1278
1448
 
1279
1449
  Inputs:
1280
- - **x** (Tensor) - A tensor of shape (N, C), where N is batch size and C is number
1450
+ - **x** (Tensor) - A tensor of shape :math:`(N, C)`, where N is batch size and C is number
1281
1451
  of classes.
1282
1452
  - **target** (Tensor) - The label target Tensor which has the same shape as `x`.
1283
1453
 
1284
1454
  Outputs:
1285
- Tensor, the data type is the same as x, if the reduction is 'none', its shape is (N), otherwise it is zero.
1455
+ Tensor, the data type is the same as x, if the reduction is ``'none'``, its shape is (N), otherwise it is zero.
1286
1456
 
1287
1457
  Raises:
1288
1458
  ValueError: If the rank of `x` or `target` is not 2.
@@ -1291,8 +1461,10 @@ class MultiLabelSoftMarginLoss(LossBase):
1291
1461
  ``Ascend`` ``GPU`` ``CPU``
1292
1462
 
1293
1463
  Examples:
1294
- >>> x = Tensor([[0.3, 0.6, 0.6], [0.9, 0.4, 0.2]])
1295
- >>> target = Tensor([[0.0, 0.0, 1.0], [0.0, 0.0, 1.0]])
1464
+ >>> import mindspore as ms
1465
+ >>> import mindspore.nn as nn
1466
+ >>> x = ms.Tensor([[0.3, 0.6, 0.6], [0.9, 0.4, 0.2]])
1467
+ >>> target = ms.Tensor([[0.0, 0.0, 1.0], [0.0, 0.0, 1.0]])
1296
1468
  >>> loss = nn.MultiLabelSoftMarginLoss(reduction='mean')
1297
1469
  >>> out = loss(x, target)
1298
1470
  >>> print(out.asnumpy())
@@ -1326,25 +1498,26 @@ class MultiMarginLoss(LossBase):
1326
1498
  and :math:`i \neq y`.
1327
1499
 
1328
1500
  Args:
1329
- p (int, optional): The norm degree for pairwise distance. Should be 1 or 2. Default: 1.
1501
+ p (int, optional): The norm degree for pairwise distance. Should be 1 or 2. Default: ``1`` .
1330
1502
  margin (float, optional): A parameter to change pairwise distance. Default: 1.0.
1331
- reduction (str, optional): Apply specific reduction method to the output: 'none', 'mean', 'sum'.
1332
- Default: 'mean'.
1503
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
1504
+ ``'sum'`` . Default: ``'mean'`` .
1333
1505
 
1334
- - 'none': no reduction will be applied.
1335
- - 'mean': the sum of the output will be divided by the number of elements in the output.
1336
- - 'sum': the output will be summed.
1506
+ - ``'none'``: no reduction will be applied.
1507
+ - ``'mean'``: compute and return the weighted mean of elements in the output.
1508
+ - ``'sum'``: the output elements will be summed.
1337
1509
 
1338
1510
  weight (Tensor, optional): The rescaling weight to each class with shape :math:`(C,)`. Data type only
1339
- support float32, float16 or float64. Default: None, all classes are weighted equally.
1511
+ support float32, float16 or float64. Default: ``None`` , all classes are weighted equally.
1340
1512
 
1341
1513
  Inputs:
1342
1514
  - **x** (Tensor) - Input x, with shape :math:`(N, C)`. Data type only support float32, float16 or float64.
1515
+ x is :math:`x` in the above formula.
1343
1516
  - **target** (Tensor) - Ground truth labels, with shape :math:`(N,)`. Data type only support int64. The
1344
- value of target should be non-negative, less than C.
1517
+ value of target should be non-negative, less than C. `target` is :math:`y` in the above formula.
1345
1518
 
1346
1519
  Outputs:
1347
- Tensor, When `reduction` is 'none', the shape is :math:`(N,)`.
1520
+ Tensor, When `reduction` is ``'none'``, the shape is :math:`(N,)`.
1348
1521
  Otherwise, it is a scalar. Has the same data type with `x`.
1349
1522
 
1350
1523
  Raises:
@@ -1354,7 +1527,7 @@ class MultiMarginLoss(LossBase):
1354
1527
  TypeError: If dtype of `x` is not float16, float or float64.
1355
1528
  TypeError: If dtype of `weight` and `x` is not the same.
1356
1529
  ValueError: If 'p' is not 1 or 2.
1357
- ValueError: If 'reduction' is not one of {'none','sum','mean'}.
1530
+ ValueError: If 'reduction' is not one of { ``'none'`` , ``'sum'`` , ``'mean'`` }.
1358
1531
  ValueError: If shape[0] of `x` is not equal to shape[0] of `target`.
1359
1532
  ValueError: If shape[1] of `x` is not equal to shape[0] of `weight`.
1360
1533
  ValueError: IF rank of `weight` is not 1.
@@ -1364,8 +1537,11 @@ class MultiMarginLoss(LossBase):
1364
1537
  ``Ascend`` ``GPU`` ``CPU``
1365
1538
 
1366
1539
  Examples:
1367
- >>> x = Tensor(np.ones(shape=[3, 3]), mindspore.float32)
1368
- >>> target = Tensor(np.array([1, 2, 1]), mindspore.int64)
1540
+ >>> import mindspore as ms
1541
+ >>> import mindspore.nn as nn
1542
+ >>> import numpy as np
1543
+ >>> x = ms.Tensor(np.ones(shape=[3, 3]), ms.float32)
1544
+ >>> target = ms.Tensor(np.array([1, 2, 1]), ms.int64)
1369
1545
  >>> loss = nn.MultiMarginLoss()
1370
1546
  >>> output = loss(x, target)
1371
1547
  >>> print(output)
@@ -1376,7 +1552,6 @@ class MultiMarginLoss(LossBase):
1376
1552
  """Initialize MultiMarginLoss."""
1377
1553
  super(MultiMarginLoss, self).__init__()
1378
1554
  self.multi_margin_loss = MultiMarginLossOp(p=p, margin=margin, reduction=reduction)
1379
- self.generate_ones = ops.Fill()
1380
1555
  self.weight = weight
1381
1556
 
1382
1557
  def construct(self, x, target, weight=None):
@@ -1388,7 +1563,7 @@ class MultiMarginLoss(LossBase):
1388
1563
  if not weight_one:
1389
1564
  _check_is_tensor('weight', weight, self.cls_name)
1390
1565
  else:
1391
- weight = self.generate_ones(x.dtype, x.astype('float32')[0].shape, 1)
1566
+ weight = F.fill(x.dtype, x.astype('float32')[0].shape, 1)
1392
1567
  loss = self.multi_margin_loss(x, target, weight)
1393
1568
  return loss
1394
1569
 
@@ -1421,9 +1596,13 @@ class BCELoss(LossBase):
1421
1596
 
1422
1597
  Args:
1423
1598
  weight (Tensor, optional): A rescaling weight applied to the loss of each batch element.
1424
- And it must have the same shape and data type as `inputs`. Default: None
1425
- reduction (str): Specifies the reduction to be applied to the output.
1426
- Its value must be one of 'none', 'mean', 'sum'. Default: 'mean'.
1599
+ And it must have the same shape and data type as `inputs`. Default: ``None`` .
1600
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
1601
+ ``'sum'`` . Default: ``'mean'`` .
1602
+
1603
+ - ``'none'``: no reduction will be applied.
1604
+ - ``'mean'``: compute and return the weighted mean of elements in the output.
1605
+ - ``'sum'``: the output elements will be summed.
1427
1606
 
1428
1607
  Inputs:
1429
1608
  - **logits** (Tensor) - The input tensor with shape :math:`(N, *)` where :math:`*` means, any number
@@ -1432,22 +1611,25 @@ class BCELoss(LossBase):
1432
1611
  of additional dimensions. The same shape and data type as `logits`.
1433
1612
 
1434
1613
  Outputs:
1435
- Tensor, has the same dtype as `logits`. if `reduction` is 'none', then it has the same shape as `logits`.
1614
+ Tensor, has the same dtype as `logits`. if `reduction` is ``'none'``, then it has the same shape as `logits`.
1436
1615
  Otherwise, it is a scalar Tensor.
1437
1616
 
1438
1617
  Raises:
1439
1618
  TypeError: If dtype of `logits`, `labels` or `weight` (if given) is neither float16 not float32.
1440
- ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
1619
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
1441
1620
  ValueError: If shape of `logits` is not the same as `labels` or `weight` (if given).
1442
1621
 
1443
1622
  Supported Platforms:
1444
1623
  ``Ascend`` ``GPU`` ``CPU``
1445
1624
 
1446
1625
  Examples:
1447
- >>> weight = Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 3.3, 2.2]]), mindspore.float32)
1626
+ >>> import mindspore as ms
1627
+ >>> import mindspore.nn as nn
1628
+ >>> import numpy as np
1629
+ >>> weight = ms.Tensor(np.array([[1.0, 2.0, 3.0], [4.0, 3.3, 2.2]]), ms.float32)
1448
1630
  >>> loss = nn.BCELoss(weight=weight, reduction='mean')
1449
- >>> logits = Tensor(np.array([[0.1, 0.2, 0.3], [0.5, 0.7, 0.9]]), mindspore.float32)
1450
- >>> labels = Tensor(np.array([[0, 1, 0], [0, 0, 1]]), mindspore.float32)
1631
+ >>> logits = ms.Tensor(np.array([[0.1, 0.2, 0.3], [0.5, 0.7, 0.9]]), ms.float32)
1632
+ >>> labels = ms.Tensor(np.array([[0, 1, 0], [0, 0, 1]]), ms.float32)
1451
1633
  >>> output = loss(logits, labels)
1452
1634
  >>> print(output)
1453
1635
  1.8952923
@@ -1489,13 +1671,17 @@ class CosineEmbeddingLoss(LossBase):
1489
1671
  .. math::
1490
1672
  loss(x_1, x_2, y) = \begin{cases}
1491
1673
  1-cos(x_1, x_2), & \text{if } y = 1\\
1492
- max(0, cos(x_1, x_2)-margin), & \text{if } y = -1\\
1674
+ \max(0, cos(x_1, x_2)-margin), & \text{if } y = -1\\
1493
1675
  \end{cases}
1494
1676
 
1495
1677
  Args:
1496
- margin (float): Should be in [-1.0, 1.0]. Default 0.0.
1497
- reduction (str): Specifies which reduction to be applied to the output. It must be one of
1498
- "none", "mean", and "sum", meaning no reduction, reduce mean and sum on output, respectively. Default "mean".
1678
+ margin (float): Should be in [-1.0, 1.0]. Default: ``0.0`` .
1679
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
1680
+ ``'sum'`` . Default: ``'mean'`` .
1681
+
1682
+ - ``'none'``: no reduction will be applied.
1683
+ - ``'mean'``: compute and return the mean of elements in the output.
1684
+ - ``'sum'``: the output elements will be summed.
1499
1685
 
1500
1686
  Inputs:
1501
1687
  - **logits_x1** (Tensor) - Tensor of shape :math:`(N, *)` where :math:`*` means, any number
@@ -1505,21 +1691,24 @@ class CosineEmbeddingLoss(LossBase):
1505
1691
  :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)`.
1506
1692
 
1507
1693
  Outputs:
1508
- Tensor or Scalar, if `reduction` is "none", its shape is the same as `labels`.
1694
+ Tensor or Scalar, if `reduction` is ``"none"``, its shape is the same as `labels`.
1509
1695
  Otherwise, a scalar value will be returned.
1510
1696
 
1511
1697
  Raises:
1512
1698
  TypeError: If `margin` is not a float.
1513
- ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
1699
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
1514
1700
  ValueError: If `margin` is not in range [-1, 1].
1515
1701
 
1516
1702
  Supported Platforms:
1517
1703
  ``Ascend`` ``GPU`` ``CPU``
1518
1704
 
1519
1705
  Examples:
1520
- >>> logits_x1 = Tensor(np.array([[0.3, 0.8], [0.4, 0.3]]), mindspore.float32)
1521
- >>> logits_x2 = Tensor(np.array([[0.4, 1.2], [-0.4, -0.9]]), mindspore.float32)
1522
- >>> labels = Tensor(np.array([1, -1]), mindspore.int32)
1706
+ >>> import mindspore as ms
1707
+ >>> import mindspore.nn as nn
1708
+ >>> import numpy as np
1709
+ >>> logits_x1 = ms.Tensor(np.array([[0.3, 0.8], [0.4, 0.3]]), ms.float32)
1710
+ >>> logits_x2 = ms.Tensor(np.array([[0.4, 1.2], [-0.4, -0.9]]), ms.float32)
1711
+ >>> labels = ms.Tensor(np.array([1, -1]), ms.int32)
1523
1712
  >>> cosine_embedding_loss = nn.CosineEmbeddingLoss()
1524
1713
  >>> output = cosine_embedding_loss(logits_x1, logits_x2, labels)
1525
1714
  >>> print(output)
@@ -1583,8 +1772,12 @@ class MultilabelMarginLoss(LossBase):
1583
1772
  number of target classes.
1584
1773
 
1585
1774
  Args:
1586
- reduction (str, optional): Apply specific reduction method to the output: 'none', 'mean', 'sum'.
1587
- Default: "mean".
1775
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
1776
+ ``'sum'`` . Default: ``'mean'`` .
1777
+
1778
+ - ``'none'``: no reduction will be applied.
1779
+ - ``'mean'``: compute and return the mean of elements in the output.
1780
+ - ``'sum'``: the output elements will be summed.
1588
1781
 
1589
1782
  Inputs:
1590
1783
  - **x** (Tensor) - Predict data. Tensor of shape :math:`(C)` or :math:`(N, C)`, where :math:`N`
@@ -1593,7 +1786,7 @@ class MultilabelMarginLoss(LossBase):
1593
1786
  label targets padded by -1.
1594
1787
 
1595
1788
  Outputs:
1596
- - **y** (Union[Tensor, Scalar]) - The loss of MultilabelMarginLoss. If `reduction` is "none", its shape
1789
+ - **y** (Union[Tensor, Scalar]) - The loss of MultilabelMarginLoss. If `reduction` is ``"none"``, its shape
1597
1790
  is :math:`(N)`. Otherwise, a scalar value will be returned.
1598
1791
 
1599
1792
  Raises:
@@ -1602,18 +1795,21 @@ class MultilabelMarginLoss(LossBase):
1602
1795
  TypeError: If dtype of `target` is not int32.
1603
1796
  ValueError: If length of shape of `x` is neither 1 nor 2.
1604
1797
  ValueError: If shape of `x` is not the same as `target`.
1605
- ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
1798
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
1606
1799
 
1607
1800
  Supported Platforms:
1608
1801
  ``Ascend`` ``GPU``
1609
1802
 
1610
1803
  Examples:
1611
- >>> loss = nn.MultilabelMarginLoss()
1612
- >>> x = Tensor(np.array([[0.1, 0.2, 0.4, 0.8], [0.2, 0.3, 0.5, 0.7]]), mindspore.float32)
1613
- >>> target = Tensor(np.array([[1, 2, 0, 3], [2, 3, -1, 1]]), mindspore.int32)
1614
- >>> output = loss(x, target)
1615
- >>> print(output)
1616
- Tensor(shape=[], dtype=Float32, value=0.325)
1804
+ >>> import mindspore as ms
1805
+ >>> import mindspore.nn as nn
1806
+ >>> import numpy as np
1807
+ >>> loss = nn.MultilabelMarginLoss()
1808
+ >>> x = ms.Tensor(np.array([[0.1, 0.2, 0.4, 0.8], [0.2, 0.3, 0.5, 0.7]]), ms.float32)
1809
+ >>> target = ms.Tensor(np.array([[1, 2, 0, 3], [2, 3, -1, 1]]), ms.int32)
1810
+ >>> output = loss(x, target)
1811
+ >>> print(output)
1812
+ 0.325
1617
1813
  """
1618
1814
 
1619
1815
  def __init__(self, reduction='mean'):
@@ -1636,7 +1832,7 @@ class BCEWithLogitsLoss(LossBase):
1636
1832
  p_{ij} = sigmoid(X_{ij}) = \frac{1}{1 + e^{-X_{ij}}}
1637
1833
 
1638
1834
  .. math::
1639
- L_{ij} = -[Y_{ij} \cdot log(p_{ij}) + (1 - Y_{ij}) \cdot log(1 - p_{ij})]
1835
+ L_{ij} = -[Y_{ij} \cdot \log(p_{ij}) + (1 - Y_{ij}) \cdot \log(1 - p_{ij})]
1640
1836
 
1641
1837
  Then,
1642
1838
 
@@ -1648,14 +1844,19 @@ class BCEWithLogitsLoss(LossBase):
1648
1844
  \end{cases}
1649
1845
 
1650
1846
  Args:
1651
- reduction (str): Type of reduction to be applied to loss. The optional values are 'mean', 'sum', and 'none'.
1652
- If 'none', do not perform reduction. Default: 'mean'.
1847
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
1848
+ ``'sum'`` . Default: ``'mean'`` .
1849
+
1850
+ - ``'none'``: no reduction will be applied.
1851
+ - ``'mean'``: compute and return the weighted mean of elements in the output.
1852
+ - ``'sum'``: the output elements will be summed.
1853
+
1653
1854
  weight (Tensor, optional): A rescaling weight applied to the loss of each batch element.
1654
1855
  If not None, it can be broadcast to a tensor with shape of `logits`,
1655
- data type must be float16 or float32. Default: None.
1856
+ data type must be float16 or float32. Default: ``None`` .
1656
1857
  pos_weight (Tensor, optional): A weight of positive examples. Must be a vector with length equal to the
1657
1858
  number of classes. If not None, it must be broadcast to a tensor with shape of `logits`, data type
1658
- must be float16 or float32. Default: None.
1859
+ must be float16 or float32. Default: ``None`` .
1659
1860
 
1660
1861
  Inputs:
1661
1862
  - **logits** (Tensor) - Input logits with shape :math:`(N, *)` where :math:`*` means, any number
@@ -1664,7 +1865,7 @@ class BCEWithLogitsLoss(LossBase):
1664
1865
  of additional dimensions. The same shape and data type as `logits`.
1665
1866
 
1666
1867
  Outputs:
1667
- Tensor or Scalar, if `reduction` is 'none', its shape is the same as `logits`.
1868
+ Tensor or Scalar, if `reduction` is ``'none'``, its shape is the same as `logits`.
1668
1869
  Otherwise, a scalar value will be returned.
1669
1870
 
1670
1871
  Raises:
@@ -1674,14 +1875,17 @@ class BCEWithLogitsLoss(LossBase):
1674
1875
  TypeError: If data type of `weight` or `pos_weight` is neither float16 nor float32.
1675
1876
  TypeError: If data type of `reduction` is not string.
1676
1877
  ValueError: If `weight` or `pos_weight` can not be broadcast to a tensor with shape of `logits`.
1677
- ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
1878
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
1678
1879
 
1679
1880
  Supported Platforms:
1680
1881
  ``Ascend`` ``GPU`` ``CPU``
1681
1882
 
1682
1883
  Examples:
1683
- >>> logits = Tensor(np.array([[-0.8, 1.2, 0.7], [-0.1, -0.4, 0.7]]).astype(np.float32))
1684
- >>> labels = Tensor(np.array([[0.3, 0.8, 1.2], [-0.6, 0.1, 2.2]]).astype(np.float32))
1884
+ >>> import mindspore as ms
1885
+ >>> import mindspore.nn as nn
1886
+ >>> import numpy as np
1887
+ >>> logits = ms.Tensor(np.array([[-0.8, 1.2, 0.7], [-0.1, -0.4, 0.7]]).astype(np.float32))
1888
+ >>> labels = ms.Tensor(np.array([[0.3, 0.8, 1.2], [-0.6, 0.1, 2.2]]).astype(np.float32))
1685
1889
  >>> loss = nn.BCEWithLogitsLoss()
1686
1890
  >>> output = loss(logits, labels)
1687
1891
  >>> print(output)
@@ -1692,28 +1896,17 @@ class BCEWithLogitsLoss(LossBase):
1692
1896
  """Initialize BCEWithLogitsLoss."""
1693
1897
  super(BCEWithLogitsLoss, self).__init__()
1694
1898
  self.reduction = reduction
1695
- self.bce_with_logits_loss = P.BCEWithLogitsLoss(reduction=reduction)
1696
1899
  if isinstance(weight, Parameter):
1697
1900
  raise TypeError(f"For '{self.cls_name}', the 'weight' can not be a Parameter.")
1698
1901
  if isinstance(pos_weight, Parameter):
1699
1902
  raise TypeError(f"For '{self.cls_name}', the 'pos_weight' can not be a Parameter.")
1700
1903
  self.weight = weight
1701
1904
  self.pos_weight = pos_weight
1702
- self.ones = P.OnesLike()
1703
1905
 
1704
1906
  def construct(self, logits, labels):
1705
1907
  _check_is_tensor('logits', logits, self.cls_name)
1706
1908
  _check_is_tensor('labels', labels, self.cls_name)
1707
- ones_input = self.ones(logits)
1708
- if self.weight is not None:
1709
- weight = self.weight
1710
- else:
1711
- weight = ones_input
1712
- if self.pos_weight is not None:
1713
- pos_weight = self.pos_weight
1714
- else:
1715
- pos_weight = ones_input
1716
- loss = self.bce_with_logits_loss(logits, labels, weight, pos_weight)
1909
+ loss = ops.binary_cross_entropy_with_logits(logits, labels, self.weight, self.pos_weight, self.reduction)
1717
1910
  return loss
1718
1911
 
1719
1912
 
@@ -1760,14 +1953,18 @@ class FocalLoss(LossBase):
1760
1953
  The function is shown as follows:
1761
1954
 
1762
1955
  .. math::
1763
- FL(p_t) = -(1-p_t)^\gamma log(p_t)
1956
+ FL(p_t) = -(1-p_t)^\gamma \log(p_t)
1764
1957
 
1765
1958
  Args:
1766
- gamma (float): Gamma is used to adjust the steepness of weight curve in focal loss. Default: 2.0.
1959
+ gamma (float): Gamma is used to adjust the steepness of weight curve in focal loss. Default: ``2.0`` .
1767
1960
  weight (Union[Tensor, None]): A rescaling weight applied to the loss of each batch element. The dimension of
1768
- weight should be 1. If None, no weight is applied. Default: None.
1769
- reduction (str): Type of reduction to be applied to loss. The optional values are "mean", "sum", and "none".
1770
- If "none", do not perform reduction. Default: "mean".
1961
+ weight should be 1. If None, no weight is applied. Default: ``None`` .
1962
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
1963
+ ``'sum'`` . Default: ``'mean'`` .
1964
+
1965
+ - ``'none'``: no reduction will be applied.
1966
+ - ``'mean'``: compute and return the weighted mean of elements in the output.
1967
+ - ``'sum'``: the output elements will be summed.
1771
1968
 
1772
1969
  Inputs:
1773
1970
  - **logits** (Tensor) - Tensor of shape should be :math:`(N, C)` or :math:`(N, C, H)` or :math:`(N, C, H, W)`.
@@ -1781,7 +1978,7 @@ class FocalLoss(LossBase):
1781
1978
  range [-:math:`C`, :math:`C`). Where :math:`C` is the number of classes in logits.
1782
1979
 
1783
1980
  Outputs:
1784
- Tensor or Scalar, if `reduction` is "none", its shape is the same as `logits`.
1981
+ Tensor or Scalar, if `reduction` is ``"none"``, its shape is the same as `logits`.
1785
1982
  Otherwise, a scalar value will be returned.
1786
1983
 
1787
1984
  Raises:
@@ -1789,14 +1986,16 @@ class FocalLoss(LossBase):
1789
1986
  TypeError: If `weight` is not a Tensor.
1790
1987
  ValueError: If `labels` dim is different from `logits`.
1791
1988
  ValueError: If `labels` channel is not 1 and `labels` shape is different from `logits`.
1792
- ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
1989
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
1793
1990
 
1794
1991
  Supported Platforms:
1795
1992
  ``Ascend``
1796
1993
 
1797
1994
  Examples:
1798
- >>> logits = Tensor([[0.8, 1.4], [0.5, 0.9], [1.2, 0.9]], mstype.float32)
1799
- >>> labels = Tensor([[1], [1], [0]], mstype.int32)
1995
+ >>> import mindspore as ms
1996
+ >>> import mindspore.nn as nn
1997
+ >>> logits = ms.Tensor([[0.8, 1.4], [0.5, 0.9], [1.2, 0.9]], ms.float32)
1998
+ >>> labels = ms.Tensor([[1], [1], [0]], ms.int32)
1800
1999
  >>> focalloss = nn.FocalLoss(weight=Tensor([1, 2]), gamma=2.0, reduction='mean')
1801
2000
  >>> output = focalloss(logits, labels)
1802
2001
  >>> print(output)
@@ -1881,7 +2080,7 @@ class HuberLoss(LossBase):
1881
2080
  delta * (|x_n - y_n| - 0.5 * delta), & \text{otherwise. }
1882
2081
  \end{cases}
1883
2082
 
1884
- where :math:`N` is the batch size. If `reduction` is not "none", then:
2083
+ where :math:`N` is the batch size. If `reduction` is not ``"none"``, then:
1885
2084
 
1886
2085
  .. math::
1887
2086
  \ell(x, y) =
@@ -1891,11 +2090,15 @@ class HuberLoss(LossBase):
1891
2090
  \end{cases}
1892
2091
 
1893
2092
  Args:
1894
- reduction (str): Type of reduction to be applied to loss. The optional values are "mean", "sum", and "none".
1895
- Default: "mean". If `reduction` is "mean" or "sum", then output a scalar Tensor, if `reduction` is "none",
1896
- the shape of the output Tensor is the broadcasted shape.
2093
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
2094
+ ``'sum'`` . Default: ``'mean'`` .
2095
+
2096
+ - ``'none'``: no reduction will be applied.
2097
+ - ``'mean'``: compute and return the mean of elements in the output.
2098
+ - ``'sum'``: the output elements will be summed.
2099
+
1897
2100
  delta (Union[int, float]): The threshold to change between two type of loss.
1898
- The value must be positive. Default: 1.0.
2101
+ The value must be positive. Default: ``1.0`` .
1899
2102
 
1900
2103
  Inputs:
1901
2104
  - **logits** (Tensor) - Predicted value, Tensor of any dimension. The data type must be float16 or float32.
@@ -1904,7 +2107,7 @@ class HuberLoss(LossBase):
1904
2107
  and they should be broadcasted to each other.
1905
2108
 
1906
2109
  Outputs:
1907
- Tensor or Scalar, if `reduction` is "none", return a Tensor with same shape and dtype as `logits`.
2110
+ Tensor or Scalar, if `reduction` is ``"none"``, return a Tensor with same shape and dtype as `logits`.
1908
2111
  Otherwise, a scalar value will be returned.
1909
2112
 
1910
2113
  Raises:
@@ -1912,34 +2115,34 @@ class HuberLoss(LossBase):
1912
2115
  TypeError: If data type of `logits` or `labels` are not the same.
1913
2116
  TypeError: If dtype of `delta` is neither float nor int.
1914
2117
  ValueError: If `delta` is less than or equal to 0.
1915
- ValueError: If `reduction` is not one of "none", "mean", "sum".
2118
+ ValueError: If `reduction` is not one of ``"none"``, ``"mean"``, ``"sum"``.
1916
2119
  ValueError: If `logits` and `labels` have different shapes and cannot be broadcasted to each other.
1917
2120
 
1918
2121
  Supported Platforms:
1919
2122
  ``Ascend`` ``GPU`` ``CPU``
1920
2123
 
1921
2124
  Examples:
1922
- >>> import mindspore
1923
- >>> from mindspore import Tensor, nn
2125
+ >>> import mindspore as ms
2126
+ >>> import mindspore.nn as nn
1924
2127
  >>> import numpy as np
1925
2128
  >>> # Case 1: logits.shape = labels.shape = (3,)
1926
2129
  >>> loss = nn.HuberLoss()
1927
- >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
1928
- >>> labels = Tensor(np.array([1, 2, 2]), mindspore.float32)
2130
+ >>> logits = ms.Tensor(np.array([1, 2, 3]), ms.float32)
2131
+ >>> labels = ms.Tensor(np.array([1, 2, 2]), ms.float32)
1929
2132
  >>> output = loss(logits, labels)
1930
2133
  >>> print(output)
1931
2134
  0.16666667
1932
2135
  >>> # Case 2: logits.shape = (3,), labels.shape = (2, 3)
1933
2136
  >>> loss = nn.HuberLoss(reduction="none")
1934
- >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32)
1935
- >>> labels = Tensor(np.array([[1, 1, 1], [1, 2, 2]]), mindspore.float32)
2137
+ >>> logits = ms.Tensor(np.array([1, 2, 3]), ms.float32)
2138
+ >>> labels = ms.Tensor(np.array([[1, 1, 1], [1, 2, 2]]), ms.float32)
1936
2139
  >>> output = loss(logits, labels)
1937
2140
  >>> print(output)
1938
2141
  [[0. 0.5 1.5]
1939
2142
  [0. 0. 0.5]]
1940
2143
  """
1941
2144
 
1942
- def __init__(self, reduction='mean', delta=1.0):
2145
+ def __init__(self, reduction="mean", delta=1.0):
1943
2146
  """Initialize HuberLoss."""
1944
2147
  super(HuberLoss, self).__init__(reduction=reduction)
1945
2148
  self.reduction = reduction
@@ -1976,13 +2179,19 @@ class TripletMarginLoss(LossBase):
1976
2179
  d(x_i, y_i) = \left\lVert {\bf x}_i - {\bf y}_i \right\rVert_p
1977
2180
 
1978
2181
  Args:
1979
- p (int, optional): The degree of norm for pairwise distance. Default: 2.
1980
- eps (float, optional): Add small value to avoid division by zero. Default: 1e-06.
2182
+ p (int, optional): The degree of norm for pairwise distance. Default: ``2`` .
2183
+ eps (float, optional): Add small value to avoid division by zero. Default: ``1e-06`` .
1981
2184
  swap (bool, optional): The distance swap change the negative distance to the distance between positive
1982
- sample and negative sample. Default: "False".
1983
- reduction (str, optional): Apply specific reduction method to the output: 'none', 'mean', 'sum'.
1984
- Default: "mean".
1985
- margin (Union[Tensor, float]) - Make a margin between the positive pair and the negative pair. Default: 1.0.
2185
+ sample and negative sample. Default: ``False`` .
2186
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
2187
+ ``'sum'`` . Default: ``'mean'`` .
2188
+
2189
+ - ``'none'``: no reduction will be applied.
2190
+ - ``'mean'``: compute and return the mean of elements in the output.
2191
+ - ``'sum'``: the output elements will be summed.
2192
+
2193
+ margin (Union[Tensor, float]) - Make a margin between the positive pair and the negative pair.
2194
+ Default: ``1.0`` .
1986
2195
 
1987
2196
  Inputs:
1988
2197
  - **x** (Tensor) - A sample randomly selected from the training set. Data type must be BasicType.
@@ -1992,10 +2201,10 @@ class TripletMarginLoss(LossBase):
1992
2201
  - **negative** (Tensor) - A sample belonging to the different class from `x`, with the same type and shape
1993
2202
  as `x`. :math:`n` in the above formula.
1994
2203
  - **margin** (Union[Tensor, float]) - Make a margin between the positive pair and the negative pair.
1995
- Default: 1.0.
2204
+ Default: ``1.0`` .
1996
2205
 
1997
2206
  Outputs:
1998
- Tensor. If `reduction` is "none", its shape is :math:`(N)`. Otherwise, a scalar value will be returned.
2207
+ Tensor. If `reduction` is ``"none"``, its shape is :math:`(N)`. Otherwise, a scalar value will be returned.
1999
2208
 
2000
2209
  Raises:
2001
2210
  TypeError: If `x` or `positive` or 'negative' is not a Tensor.
@@ -2007,22 +2216,25 @@ class TripletMarginLoss(LossBase):
2007
2216
  ValueError: If the dimension of input `x` or `positive` or `negative` is bigger than or equal to 8.
2008
2217
  ValueError: If length of shape of `margin` is not 0.
2009
2218
  ValueError: If shape of `x`, `positive` and `negative` cannot broadcast.
2010
- ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
2219
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
2011
2220
 
2012
2221
  Supported Platforms:
2013
2222
  ``GPU``
2014
2223
 
2015
2224
  Examples:
2225
+ >>> import mindspore as ms
2226
+ >>> import mindspore.nn as nn
2227
+ >>> import numpy as np
2016
2228
  >>> loss = nn.TripletMarginLoss()
2017
- >>> x = Tensor(np.array([[0.3, 0.7], [0.5, 0.5]]), mindspore.float32)
2018
- >>> positive = Tensor(np.array([[0.4, 0.6], [0.4, 0.6]]), mindspore.float32)
2019
- >>> negative = Tensor(np.array([[0.2, 0.9], [0.3, 0.7]]), mindspore.float32)
2229
+ >>> x = ms.Tensor(np.array([[0.3, 0.7], [0.5, 0.5]]), ms.float32)
2230
+ >>> positive = ms.Tensor(np.array([[0.4, 0.6], [0.4, 0.6]]), ms.float32)
2231
+ >>> negative = ms.Tensor(np.array([[0.2, 0.9], [0.3, 0.7]]), ms.float32)
2020
2232
  >>> output = loss(x, positive, negative)
2021
2233
  >>> print(output)
2022
2234
  0.8881968
2023
2235
  """
2024
2236
 
2025
- def __init__(self, p=2, swap=False, eps=1e-06, reduction='mean', margin=1.):
2237
+ def __init__(self, p=2, swap=False, eps=1e-06, reduction="mean", margin=1.):
2026
2238
  super(TripletMarginLoss, self).__init__()
2027
2239
  self.p = p
2028
2240
  self.swap = swap
@@ -2058,7 +2270,6 @@ class NLLLoss(LossBase):
2058
2270
  The nll loss with :math:`reduction = none` can be described as:
2059
2271
 
2060
2272
  .. math::
2061
-
2062
2273
  \ell(x, t)=L=\left\{l_{1}, \ldots, l_{N}\right\}^{\top},
2063
2274
  \quad l_{n}=-w_{t_{n}} x_{n, t_{n}},
2064
2275
  \quad w_{c}=\text { weight }[c] \cdot \mathbb{1}\{c \not= \text{ignore_index}\}
@@ -2067,7 +2278,7 @@ class NLLLoss(LossBase):
2067
2278
  :math:`N` is the batch size, :math:`c` belonging to :math:`[0, C-1]` is class index,
2068
2279
  where :math:`C` is the number of classes.
2069
2280
 
2070
- If `reduction` is not 'none' (default 'mean'), then
2281
+ If `reduction` is not ``'none'`` (default 'mean'), then
2071
2282
 
2072
2283
  .. math::
2073
2284
 
@@ -2078,11 +2289,15 @@ class NLLLoss(LossBase):
2078
2289
 
2079
2290
  Args:
2080
2291
  weight (Tensor): The rescaling weight to each class. If the value is not None, the shape is :math:`(C,)`.
2081
- The data type only supports float32 or float16. Default: None.
2292
+ The data type only supports float32 or float16. Default: ``None`` .
2082
2293
  ignore_index (int): Specifies a target value that is ignored (typically for padding value)
2083
- and does not contribute to the gradient. Default: -100.
2084
- reduction (str): Apply specific reduction method to the output: 'none', 'mean', or 'sum'.
2085
- Default: 'mean'.
2294
+ and does not contribute to the gradient. Default: ``-100`` .
2295
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
2296
+ ``'sum'`` . Default: ``'mean'`` .
2297
+
2298
+ - ``'none'``: no reduction will be applied.
2299
+ - ``'mean'``: compute and return the weighted mean of elements in the output.
2300
+ - ``'sum'``: the output elements will be summed.
2086
2301
 
2087
2302
  Inputs:
2088
2303
  - **logits** (Tensor) - Tensor of shape :math:`(N, C)`
@@ -2098,7 +2313,7 @@ class NLLLoss(LossBase):
2098
2313
  TypeError: If `weight` is not a Tensor.
2099
2314
  TypeError: If `ignore_index` is not an int.
2100
2315
  TypeError: If the data type of `weight` is not float16 or float32.
2101
- ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
2316
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
2102
2317
  TypeError: If `logits` is not a Tensor.
2103
2318
  TypeError: If `labels` is not a Tensor.
2104
2319
 
@@ -2106,9 +2321,11 @@ class NLLLoss(LossBase):
2106
2321
  ``Ascend`` ``GPU`` ``CPU``
2107
2322
 
2108
2323
  Examples:
2109
-
2110
- >>> logits = mindspore.Tensor(np.random.randn(3, 5), mindspore.float32)
2111
- >>> labels = mindspore.Tensor(np.array([1, 0, 4]), mindspore.int32)
2324
+ >>> import mindspore as ms
2325
+ >>> import mindspore.nn as nn
2326
+ >>> import numpy as np
2327
+ >>> logits = ms.Tensor(np.random.randn(3, 5), ms.float32)
2328
+ >>> labels = ms.Tensor(np.array([1, 0, 4]), ms.int32)
2112
2329
  >>> loss = nn.NLLLoss()
2113
2330
  >>> output = loss(logits, labels)
2114
2331
  """
@@ -2125,9 +2342,6 @@ class NLLLoss(LossBase):
2125
2342
  self.reduction = reduction
2126
2343
 
2127
2344
  def construct(self, logits, labels):
2128
- _check_is_tensor('logits', logits, self.cls_name)
2129
- _check_is_tensor('labels', labels, self.cls_name)
2130
- _check_nll_loss_inputs(logits.shape, labels.shape, logits.dtype, labels.dtype, self.cls_name)
2131
2345
  return F.nll_loss(logits, labels, self.weight, self.ignore_index, self.reduction)
2132
2346
 
2133
2347
 
@@ -2185,7 +2399,7 @@ class CrossEntropyLoss(LossBase):
2185
2399
  where :math:`x` is the inputs, :math:`t` is the target, :math:`w` is the weight,
2186
2400
  N is the batch size, :math:`c` belonging to [0, C-1] is class index, where :math:`C` is the number of classes.
2187
2401
 
2188
- If reduction is not 'none' (default 'mean'), then
2402
+ If reduction is not ``'none'`` (default 'mean'), then
2189
2403
 
2190
2404
  .. math::
2191
2405
 
@@ -2207,7 +2421,7 @@ class CrossEntropyLoss(LossBase):
2207
2421
  where :math:`x` is the inputs, :math:`t` is the target, :math:`w` is the weight,
2208
2422
  N is the batch size, :math:`c` belonging to [0, C-1] is class index, where :math:`C` is the number of classes.
2209
2423
 
2210
- If reduction is not 'none' (default 'mean'), then
2424
+ If reduction is not ``'none'`` (default 'mean'), then
2211
2425
 
2212
2426
  .. math::
2213
2427
 
@@ -2219,14 +2433,19 @@ class CrossEntropyLoss(LossBase):
2219
2433
  \end{cases}
2220
2434
 
2221
2435
  Args:
2222
- weight (Tensor): The rescaling weight to each class. If the value is not None, the shape is (C,).
2223
- The data type only supports float32 or float16. Default: None.
2436
+ weight (Tensor): The rescaling weight to each class. If the value is not None, the shape is :math:`(C,)`.
2437
+ The data type only supports float32 or float16. Default: ``None`` .
2224
2438
  ignore_index (int): Specifies a target value that is ignored (typically for padding value)
2225
- and does not contribute to the gradient. Default: -100.
2226
- reduction (str): Apply specific reduction method to the output: 'none', 'mean', or 'sum'.
2227
- Default: 'mean'.
2439
+ and does not contribute to the gradient. Default: ``-100`` .
2440
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
2441
+ ``'sum'`` . Default: ``'mean'`` .
2442
+
2443
+ - ``'none'``: no reduction will be applied.
2444
+ - ``'mean'``: compute and return the weighted mean of elements in the output.
2445
+ - ``'sum'``: the output elements will be summed.
2446
+
2228
2447
  label_smoothing (float): Label smoothing values, a regularization tool used to prevent the model
2229
- from overfitting when calculating Loss. The value range is [0.0, 1.0]. Default value: 0.0.
2448
+ from overfitting when calculating Loss. The value range is [0.0, 1.0]. Default value: ``0.0`` .
2230
2449
 
2231
2450
  Inputs:
2232
2451
  - **logits** (Tensor) - Tensor of shape :math:`(C,)` :math:`(N, C)` or :math:`(N, C, d_1, d_2, ..., d_K)`,
@@ -2243,7 +2462,7 @@ class CrossEntropyLoss(LossBase):
2243
2462
  TypeError: If `weight` is not a Tensor.
2244
2463
  TypeError: If `ignore_index` is not an int.
2245
2464
  TypeError: If the data type of `weight` is not float16 or float32.
2246
- ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
2465
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
2247
2466
  TypeError: If `label_smoothing` is not a float.
2248
2467
  TypeError: If `logits` is not a Tensor.
2249
2468
  TypeError: If `labels` is not a Tensor.
@@ -2252,15 +2471,17 @@ class CrossEntropyLoss(LossBase):
2252
2471
  ``Ascend`` ``GPU`` ``CPU``
2253
2472
 
2254
2473
  Examples:
2255
-
2474
+ >>> import mindspore as ms
2475
+ >>> import mindspore.nn as nn
2476
+ >>> import numpy as np
2256
2477
  >>> # Case 1: Indices labels
2257
- >>> inputs = mindspore.Tensor(np.random.randn(3, 5), mindspore.float32)
2258
- >>> target = mindspore.Tensor(np.array([1, 0, 4]), mindspore.int32)
2478
+ >>> inputs = ms.Tensor(np.random.randn(3, 5), ms.float32)
2479
+ >>> target = ms.Tensor(np.array([1, 0, 4]), ms.int32)
2259
2480
  >>> loss = nn.CrossEntropyLoss()
2260
2481
  >>> output = loss(inputs, target)
2261
2482
  >>> # Case 2: Probability labels
2262
- >>> inputs = mindspore.Tensor(np.random.randn(3, 5), mindspore.float32)
2263
- >>> target = mindspore.Tensor(np.random.randn(3, 5), mindspore.float32)
2483
+ >>> inputs = ms.Tensor(np.random.randn(3, 5), ms.float32)
2484
+ >>> target = ms.Tensor(np.random.randn(3, 5), ms.float32)
2264
2485
  >>> loss = nn.CrossEntropyLoss()
2265
2486
  >>> output = loss(inputs, target)
2266
2487
  """
@@ -2323,19 +2544,18 @@ class KLDivLoss(LossBase):
2323
2544
  only when `reduction` is set to 'batchmean'.
2324
2545
 
2325
2546
  Args:
2326
- reduction (str): Specifies the reduction to be applied to the output.
2327
- Default: 'mean'.
2547
+ reduction (str): Specifies the reduction to be applied to the output. Default: ``'mean'`` .
2328
2548
 
2329
- - On Ascend, the value of `reduction` must be one of 'batchmean', 'none' or 'sum'.
2330
- - On GPU, the value of `reduction` must be one of 'mean', 'none' or 'sum'.
2331
- - On CPU, the value of `reduction` must be one of 'mean', 'batchmean', 'none' or 'sum'.
2549
+ - On Ascend, the value of `reduction` must be one of ``'batchmean'`` , ``'none'`` or ``'sum'`` .
2550
+ - On GPU, the value of `reduction` must be one of ``'mean'`` , ``'none'`` or ``'sum'`` .
2551
+ - On CPU, the value of `reduction` must be one of ``'mean'`` , ``'batchmean'`` , ``'none'`` or ``'sum'`` .
2332
2552
 
2333
2553
  Inputs:
2334
2554
  - **logits** (Tensor) - The input Tensor. The data type must be float16, float32 or float64.
2335
2555
  - **labels** (Tensor) - The label Tensor which has the same shape and data type as `logits`.
2336
2556
 
2337
2557
  Outputs:
2338
- Tensor or Scalar, if `reduction` is 'none', then output is a tensor and has the same shape as `logits`.
2558
+ Tensor or Scalar, if `reduction` is ``'none'``, then output is a tensor and has the same shape as `logits`.
2339
2559
  Otherwise, it is a scalar.
2340
2560
 
2341
2561
  Raises:
@@ -2349,8 +2569,11 @@ class KLDivLoss(LossBase):
2349
2569
  ``Ascend`` ``GPU`` ``CPU``
2350
2570
 
2351
2571
  Examples:
2352
- >>> logits = Tensor(np.array([0.2, 0.7, 0.1]), mindspore.float32)
2353
- >>> labels = Tensor(np.array([0., 1., 0.]), mindspore.float32)
2572
+ >>> import mindspore as ms
2573
+ >>> import mindspore.nn as nn
2574
+ >>> import numpy as np
2575
+ >>> logits = ms.Tensor(np.array([0.2, 0.7, 0.1]), ms.float32)
2576
+ >>> labels = ms.Tensor(np.array([0., 1., 0.]), ms.float32)
2354
2577
  >>> loss = nn.KLDivLoss(reduction='mean')
2355
2578
  >>> output = loss(logits, labels)
2356
2579
  >>> print(output)
@@ -2367,6 +2590,16 @@ class KLDivLoss(LossBase):
2367
2590
  return F.kl_div(logits, labels, self.reduction)
2368
2591
 
2369
2592
 
2593
+ def _check_ctcloss_targets_shape(targets):
2594
+ """Internal function, used to check whether the shape of CTC targets meets the requirements."""
2595
+ if targets.ndim > 2:
2596
+ raise ValueError(f"For CTCLoss, when the shape of log_probs is (T, C), the dimension of targets should"
2597
+ f"be 1 or 2, but got {targets.ndim}.")
2598
+ if targets.ndim == 2 and targets.shape[0] != 1:
2599
+ raise ValueError(f"For CTCLoss, the first dimension of 2-D targets should be 1,"
2600
+ f"but got {targets.shape[0]}.")
2601
+
2602
+
2370
2603
  class CTCLoss(LossBase):
2371
2604
  """
2372
2605
  Calculates the CTC (Connectionist Temporal Classification) loss. It's mainly used to calculate the loss between
@@ -2376,11 +2609,16 @@ class CTCLoss(LossBase):
2376
2609
  Recurrent Neural Networks <http://www.cs.toronto.edu/~graves/icml_2006.pdf>`_ .
2377
2610
 
2378
2611
  Args:
2379
- blank (int, optional): The blank label. Default: 0.
2380
- reduction (str, optional): Implements the reduction method to the output with
2381
- 'none', 'mean', or 'sum'. Default: 'mean'.
2612
+ blank (int, optional): The blank label. Default: ``0`` .
2613
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
2614
+ ``'sum'`` . Default: ``'mean'`` .
2615
+
2616
+ - ``'none'``: no reduction will be applied.
2617
+ - ``'mean'``: compute and return the mean of elements in the output.
2618
+ - ``'sum'``: the output elements will be summed.
2619
+
2382
2620
  zero_infinity (bool, optional): If loss is infinite, this parameter determines whether to set that loss
2383
- and its correlated gradient to zero. Default: False.
2621
+ and its correlated gradient to zero. Default: ``False`` .
2384
2622
 
2385
2623
  Inputs:
2386
2624
  - **log_probs** (Tensor) - A tensor of shape :math:`(T, N, C)` or :math:`(T, C)`, where T is length of input,
@@ -2398,10 +2636,10 @@ class CTCLoss(LossBase):
2398
2636
  TypeError: If `zero_infinity` is not a bool, `reduction` is not string.
2399
2637
  TypeError: If the dtype of `log_probs` is not float or double.
2400
2638
  TypeError: If the dtype of `targets`, `input_lengths` or `target_lengths` is not int32 or int64.
2401
- ValueError: If `reduction` is not "none", "mean" or "sum".
2402
- ValueError: If the types of `targets`, `input_lengths` or `target_lengths` are different.
2639
+ ValueError: If `reduction` is not ``"none"``, ``"mean"`` or ``"sum"``.
2403
2640
  ValueError: If the value of `blank` is not in range [0, C). C is number of classes of `log_probs` .
2404
- ValueError: If the dimension of `targets` is not one when the shape of `log_prob` is :math:`(T, C)`.
2641
+ ValueError: If the shape of `log_probs` is :math:`(T, C)`, the dimension of `targets` is not 1 or 2.
2642
+ ValueError: If the shape of `log_probs` is :math:`(T, C)`, the first dimension of 2-D `target` is not 1.
2405
2643
  RuntimeError: If any value of `input_lengths` is larger than T. T is length of `log_probs` .
2406
2644
  RuntimeError: If any target_lengths[i] is not in range [0, input_length[i]].
2407
2645
 
@@ -2409,34 +2647,33 @@ class CTCLoss(LossBase):
2409
2647
  ``Ascend`` ``GPU`` ``CPU``
2410
2648
 
2411
2649
  Examples:
2650
+ >>> import mindspore as ms
2651
+ >>> import mindspore.nn as nn
2412
2652
  >>> import numpy as np
2413
- >>> from mindspore import Tensor
2414
- >>> from mindspore import dtype as mstype
2415
- >>> from mindspore.nn.loss import CTCLoss
2416
2653
  >>> T = 5 # Input sequence length
2417
2654
  >>> C = 2 # Number of classes
2418
2655
  >>> N = 2 # Batch size
2419
2656
  >>> S = 3 # Target sequence length of longest target in batch (padding length)
2420
2657
  >>> S_min = 2 # Minimum target length, for demonstration purposes
2421
2658
  >>> arr = np.arange(T*N*C).reshape((T, N, C))
2422
- >>> ms_input = Tensor(arr, dtype=mstype.float32)
2659
+ >>> ms_input = ms.Tensor(arr, dtype=ms.float32)
2423
2660
  >>> input_lengths = np.full(shape=(N), fill_value=T)
2424
- >>> input_lengths = Tensor(input_lengths, dtype=mstype.int32)
2661
+ >>> input_lengths = ms.Tensor(input_lengths, dtype=ms.int32)
2425
2662
  >>> target_lengths = np.full(shape=(N), fill_value=S_min)
2426
- >>> target_lengths = Tensor(target_lengths, dtype=mstype.int32)
2663
+ >>> target_lengths = ms.Tensor(target_lengths, dtype=ms.int32)
2427
2664
  >>> target = np.random.randint(1, C, size=(N, S))
2428
- >>> target = Tensor(target, dtype=mstype.int32)
2429
- >>> ctc_loss = CTCLoss(blank=0, reduction='none', zero_infinity=False)
2665
+ >>> target = ms.Tensor(target, dtype=ms.int32)
2666
+ >>> ctc_loss = nn.CTCLoss(blank=0, reduction='none', zero_infinity=False)
2430
2667
  >>> loss = ctc_loss(ms_input, target, input_lengths, target_lengths)
2431
2668
  >>> print(loss)
2432
2669
  [-45.79497 -55.794968]
2433
2670
  >>> arr = np.arange(T*C).reshape((T, C))
2434
- >>> ms_input = Tensor(arr, dtype=mstype.float32)
2435
- >>> input_lengths = Tensor([T], dtype=mstype.int32)
2436
- >>> target_lengths = Tensor([S_min], dtype=mstype.int32)
2671
+ >>> ms_input = ms.Tensor(arr, dtype=ms.float32)
2672
+ >>> input_lengths = ms.Tensor([T], dtype=ms.int32)
2673
+ >>> target_lengths = ms.Tensor([S_min], dtype=ms.int32)
2437
2674
  >>> target = np.random.randint(1, C, size=(S_min,))
2438
- >>> target = Tensor(target, dtype=mstype.int32)
2439
- >>> ctc_loss = CTCLoss(blank=0, reduction='none', zero_infinity=False)
2675
+ >>> target = ms.Tensor(target, dtype=ms.int32)
2676
+ >>> ctc_loss = nn.CTCLoss(blank=0, reduction='none', zero_infinity=False)
2440
2677
  >>> loss = ctc_loss(ms_input, target, input_lengths, target_lengths)
2441
2678
  >>> print(loss)
2442
2679
  -25.794968
@@ -2452,14 +2689,13 @@ class CTCLoss(LossBase):
2452
2689
  _check_is_tensor('log_probs', log_probs, self.cls_name)
2453
2690
  _check_is_tensor('targets', targets, self.cls_name)
2454
2691
  if log_probs.ndim == 2:
2455
- if targets.ndim > 1:
2456
- raise ValueError("For CTCLoss, when the shape of log_probs is (T, C), the dimension of targets should"
2457
- "be equal to one.")
2692
+ _check_ctcloss_targets_shape(targets)
2693
+ if targets.ndim == 1:
2694
+ targets = targets.expand_dims(0)
2458
2695
  log_probs = log_probs.expand_dims(-2)
2459
- targets = targets.expand_dims(0)
2460
2696
  neg_log_hood, _ = F.ctc_loss(log_probs, targets, input_lengths, target_lengths, self.blank, self.reduction,
2461
2697
  self.zero_infinity)
2462
- return neg_log_hood.squeeze()
2698
+ return neg_log_hood.squeeze(axis=0)
2463
2699
  neg_log_hood, _ = F.ctc_loss(log_probs, targets, input_lengths, target_lengths, self.blank, self.reduction,
2464
2700
  self.zero_infinity)
2465
2701
  return neg_log_hood
@@ -2484,10 +2720,14 @@ class GaussianNLLLoss(LossBase):
2484
2720
 
2485
2721
  Keyword Args:
2486
2722
  full (bool, optional): Whether include the constant term in the loss calculation. When :math:`full=True`,
2487
- the constant term `const.` will be :math:`0.5 * log(2\pi)`. Default: False.
2488
- eps (float, optional): Used to improve the stability of log function. Default: 1e-6.
2489
- reduction (str, optional): Apply specific reduction method to the output: 'none', 'mean', or 'sum'.
2490
- Default: 'mean'.
2723
+ the constant term `const.` will be :math:`0.5 * log(2\pi)`. Default: ``False`` .
2724
+ eps (float, optional): Used to improve the stability of log function. Default: ``1e-6`` .
2725
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
2726
+ ``'sum'`` . Default: ``'mean'`` .
2727
+
2728
+ - ``'none'``: no reduction will be applied.
2729
+ - ``'mean'``: compute and return the mean of elements in the output.
2730
+ - ``'sum'``: the output elements will be summed.
2491
2731
 
2492
2732
  Inputs:
2493
2733
  - **logits** (Tensor) - Tensor of shape :math:`(N, *)` or :math:`(*)` where :math:`*` means any number of
@@ -2507,22 +2747,21 @@ class GaussianNLLLoss(LossBase):
2507
2747
  TypeError: If `full` is not a bool.
2508
2748
  TypeError: If `eps` is not a float.
2509
2749
  ValueError: If `eps` is not a float within (0, inf).
2510
- ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
2750
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
2511
2751
 
2512
2752
  Supported Platforms:
2513
2753
  ``Ascend`` ``GPU`` ``CPU``
2514
2754
 
2515
2755
  Examples:
2516
- >>> import numpy as np
2517
- >>> from mindspore import Tensor
2756
+ >>> import mindspore as ms
2518
2757
  >>> import mindspore.nn as nn
2519
- >>> import mindspore.common.dtype as mstype
2758
+ >>> import numpy as np
2520
2759
  >>> arr1 = np.arange(8).reshape((4, 2))
2521
2760
  >>> arr2 = np.array([2, 3, 1, 4, 6, 4, 4, 9]).reshape((4, 2))
2522
- >>> logits = Tensor(arr1, mstype.float32)
2523
- >>> labels = Tensor(arr2, mstype.float32)
2761
+ >>> logits = ms.Tensor(arr1, ms.float32)
2762
+ >>> labels = ms.Tensor(arr2, ms.float32)
2524
2763
  >>> loss = nn.GaussianNLLLoss(reduction='mean')
2525
- >>> var = Tensor(np.ones((4, 1)), mstype.float32)
2764
+ >>> var = ms.Tensor(np.ones((4, 1)), ms.float32)
2526
2765
  >>> output = loss(logits, labels, var)
2527
2766
  >>> print(output)
2528
2767
  1.4374993
@@ -2575,14 +2814,19 @@ class HingeEmbeddingLoss(LossBase):
2575
2814
 
2576
2815
  Args:
2577
2816
  margin (float, int): Threshold defined by Hinge Embedding Loss :math:`margin`.
2578
- Represented as :math:`\Delta` in the formula. Default: 1.0.
2579
- reduction (str): Specify the computing method to be applied to the outputs: 'none', 'mean', or 'sum'.
2580
- Default: 'mean'.
2817
+ Represented as :math:`\Delta` in the formula. Default: ``1.0`` .
2818
+ reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` ,
2819
+ ``'sum'`` . Default: ``'mean'`` .
2820
+
2821
+ - ``'none'``: no reduction will be applied.
2822
+ - ``'mean'``: compute and return the mean of elements in the output.
2823
+ - ``'sum'``: the output elements will be summed.
2581
2824
 
2582
2825
  Inputs:
2583
2826
  - **logits** (Tensor) - The predicted value, expressed as :math:`x` in the equation.
2584
2827
  Tensor of shape :math:`(*)` where :math:`*` means any number of dimensions.
2585
- - **labels** (Tensor) - Same shape as the logits, contains -1 or 1.
2828
+ - **labels** (Tensor) - Label value, represented as :math:`y` in the equation.
2829
+ Same shape as the logits, contains -1 or 1.
2586
2830
 
2587
2831
  Returns:
2588
2832
  Tensor or Tensor scalar, the computed loss depending on :math:`reduction`.
@@ -2592,20 +2836,19 @@ class HingeEmbeddingLoss(LossBase):
2592
2836
  TypeError: If `labels` is not a Tensor.
2593
2837
  TypeError: If `margin` is not a float or int.
2594
2838
  ValueError: If `labels` does not have the same shape as `logits` or they could not broadcast to each other.
2595
- ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
2839
+ ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``.
2596
2840
 
2597
2841
  Supported Platforms:
2598
2842
  ``Ascend`` ``GPU`` ``CPU``
2599
2843
 
2600
2844
  Examples:
2601
- >>> import numpy as np
2602
- >>> from mindspore import Tensor
2845
+ >>> import mindspore as ms
2603
2846
  >>> import mindspore.nn as nn
2604
- >>> import mindspore.common.dtype as mstype
2847
+ >>> import numpy as np
2605
2848
  >>> arr1 = np.array([0.9, -1.2, 2, 0.8, 3.9, 2, 1, 0, -1]).reshape((3, 3))
2606
2849
  >>> arr2 = np.array([1, 1, -1, 1, -1, 1, -1, 1, 1]).reshape((3, 3))
2607
- >>> logits = Tensor(arr1, mstype.float32)
2608
- >>> labels = Tensor(arr2, mstype.float32)
2850
+ >>> logits = ms.Tensor(arr1, ms.float32)
2851
+ >>> labels = ms.Tensor(arr2, ms.float32)
2609
2852
  >>> loss = nn.HingeEmbeddingLoss(reduction='mean')
2610
2853
  >>> output = loss(logits, labels)
2611
2854
  >>> print(output)