mindspore 1.10.0__cp37-none-any.whl → 2.0.0rc1__cp37-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.
- mindspore/.commit_id +1 -1
- mindspore/Third_Party_Open_Source_Software_Notice +9064 -0
- mindspore/__init__.py +9 -4
- mindspore/_akg/akg/composite/build_module.py +11 -0
- mindspore/_akg/akg/config/repository_cuda.json +11 -0
- mindspore/_akg/akg/tvm/contrib/nvcc.py +4 -3
- mindspore/_c_dataengine.cpython-37m-aarch64-linux-gnu.so +0 -0
- mindspore/_c_expression.cpython-37m-aarch64-linux-gnu.so +0 -0
- mindspore/_c_mindrecord.cpython-37m-aarch64-linux-gnu.so +0 -0
- mindspore/_check_jit_forbidden_api.py +102 -0
- mindspore/_checkparam.py +1066 -1001
- mindspore/_extends/builtin_operations.py +32 -4
- mindspore/_extends/graph_kernel/model/graph_split.py +66 -222
- mindspore/_extends/parallel_compile/akg_compiler/akg_process.py +12 -9
- mindspore/_extends/parallel_compile/akg_compiler/build_tbe_kernel.py +119 -26
- mindspore/_extends/parallel_compile/akg_compiler/tbe_topi.py +50 -50
- mindspore/_extends/parallel_compile/akg_compiler/util.py +9 -6
- mindspore/_extends/parallel_compile/tbe_compiler/tbe_adapter.py +4 -25
- mindspore/_extends/parallel_compile/tbe_compiler/tbe_helper.py +9 -4
- mindspore/_extends/parallel_compile/tbe_compiler/tbe_job_manager.py +1 -27
- mindspore/_extends/parse/__init__.py +5 -3
- mindspore/_extends/parse/namespace.py +17 -2
- mindspore/_extends/parse/parser.py +193 -34
- mindspore/_extends/parse/resources.py +7 -8
- mindspore/_extends/parse/standard_method.py +1780 -435
- mindspore/_extends/parse/trope.py +3 -1
- mindspore/_mindspore_offline_debug.cpython-37m-aarch64-linux-gnu.so +0 -0
- mindspore/amp.py +53 -58
- mindspore/bin/cache_admin +0 -0
- mindspore/bin/cache_server +0 -0
- mindspore/boost/adasum.py +3 -2
- mindspore/boost/boost.py +2 -2
- mindspore/boost/boost_cell_wrapper.py +46 -26
- mindspore/boost/dim_reduce.py +6 -5
- mindspore/boost/grad_accumulation.py +2 -1
- mindspore/boost/group_loss_scale_manager.py +1 -1
- mindspore/common/__init__.py +11 -10
- mindspore/common/_decorator.py +2 -0
- mindspore/common/_register_for_adapter.py +55 -0
- mindspore/common/_stub_tensor.py +201 -0
- mindspore/common/_utils.py +57 -0
- mindspore/common/api.py +582 -297
- mindspore/common/dtype.py +66 -18
- mindspore/common/dump.py +2 -2
- mindspore/common/initializer.py +38 -1
- mindspore/common/jit_config.py +25 -13
- mindspore/common/mutable.py +53 -24
- mindspore/common/parameter.py +60 -37
- mindspore/common/seed.py +8 -24
- mindspore/common/sparse_tensor.py +927 -0
- mindspore/common/tensor.py +1627 -3900
- mindspore/communication/__init__.py +10 -5
- mindspore/communication/_comm_helper.py +78 -214
- mindspore/communication/_hccl_management.py +2 -1
- mindspore/communication/management.py +136 -47
- mindspore/config/op_info.config +501 -1008
- mindspore/config/super_bar_config.json +512 -0
- mindspore/context.py +291 -56
- mindspore/dataset/__init__.py +12 -8
- mindspore/dataset/audio/__init__.py +9 -9
- mindspore/dataset/audio/transforms.py +1090 -228
- mindspore/dataset/audio/utils.py +87 -39
- mindspore/dataset/audio/validators.py +223 -1
- mindspore/dataset/callback/ds_callback.py +17 -15
- mindspore/dataset/core/config.py +246 -17
- mindspore/dataset/core/py_util_helpers.py +4 -3
- mindspore/dataset/core/validator_helpers.py +10 -10
- mindspore/{parallel/nn/layers.py → dataset/debug/__init__.py} +7 -8
- mindspore/dataset/debug/debug_hook.py +65 -0
- mindspore/dataset/debug/pre_defined_hook.py +67 -0
- mindspore/dataset/engine/__init__.py +7 -3
- mindspore/dataset/engine/cache_client.py +9 -9
- mindspore/dataset/engine/datasets.py +648 -477
- mindspore/dataset/engine/datasets_audio.py +165 -167
- mindspore/dataset/engine/datasets_standard_format.py +93 -67
- mindspore/dataset/engine/datasets_text.py +492 -342
- mindspore/dataset/engine/datasets_user_defined.py +85 -50
- mindspore/dataset/engine/datasets_vision.py +1224 -699
- mindspore/dataset/engine/graphdata.py +134 -69
- mindspore/dataset/engine/iterators.py +50 -9
- mindspore/dataset/engine/offload.py +52 -31
- mindspore/dataset/engine/samplers.py +27 -24
- mindspore/dataset/engine/serializer_deserializer.py +14 -15
- mindspore/dataset/engine/validators.py +213 -52
- mindspore/dataset/text/__init__.py +10 -8
- mindspore/dataset/text/transforms.py +152 -57
- mindspore/dataset/text/utils.py +98 -49
- mindspore/dataset/text/validators.py +25 -0
- mindspore/dataset/transforms/__init__.py +4 -2
- mindspore/dataset/transforms/c_transforms.py +11 -13
- mindspore/dataset/transforms/py_transforms.py +2 -2
- mindspore/dataset/transforms/py_transforms_util.py +10 -0
- mindspore/dataset/transforms/transforms.py +13 -15
- mindspore/dataset/transforms/validators.py +7 -7
- mindspore/dataset/utils/__init__.py +2 -1
- mindspore/dataset/utils/browse_dataset.py +13 -13
- mindspore/dataset/utils/line_reader.py +121 -0
- mindspore/dataset/vision/__init__.py +8 -7
- mindspore/dataset/vision/c_transforms.py +125 -126
- mindspore/dataset/vision/py_transforms.py +37 -37
- mindspore/dataset/vision/py_transforms_util.py +23 -20
- mindspore/dataset/vision/transforms.py +316 -315
- mindspore/dataset/vision/utils.py +313 -17
- mindspore/dataset/vision/validators.py +6 -6
- mindspore/default_config.py +0 -1
- mindspore/{compression → experimental}/__init__.py +6 -5
- mindspore/experimental/map_parameter.py +275 -0
- mindspore/include/OWNERS +0 -1
- mindspore/include/api/callback/callback.h +9 -13
- mindspore/include/api/callback/ckpt_saver.h +2 -2
- mindspore/include/api/callback/loss_monitor.h +2 -2
- mindspore/include/api/callback/lr_scheduler.h +5 -5
- mindspore/include/api/callback/time_monitor.h +2 -2
- mindspore/include/api/callback/train_accuracy.h +4 -6
- mindspore/include/api/cfg.h +19 -6
- mindspore/include/api/context.h +70 -9
- mindspore/include/api/delegate.h +8 -1
- mindspore/include/api/dual_abi_helper.h +8 -24
- mindspore/include/api/metrics/accuracy.h +2 -2
- mindspore/include/api/metrics/metrics.h +4 -3
- mindspore/include/api/model.h +9 -4
- mindspore/include/api/model_group.h +68 -0
- mindspore/include/api/model_parallel_runner.h +17 -17
- mindspore/include/api/net.h +12 -11
- mindspore/include/api/serialization.h +20 -4
- mindspore/include/api/status.h +7 -1
- mindspore/include/api/types.h +25 -21
- mindspore/include/api/visible.h +4 -0
- mindspore/include/c_api/model_c.h +5 -0
- mindspore/include/c_api/status_c.h +1 -1
- mindspore/include/dataset/config.h +1 -1
- mindspore/include/dataset/constants.h +14 -0
- mindspore/include/dataset/text.h +59 -0
- mindspore/include/dataset/vision.h +56 -117
- mindspore/include/dataset/vision_lite.h +102 -0
- mindspore/include/mindapi/base/type_id.h +42 -3
- mindspore/lib/libdnnl.so.2 +0 -0
- mindspore/lib/libicudata.so.69 +0 -0
- mindspore/lib/libicui18n.so.69 +0 -0
- mindspore/lib/libicuuc.so.69 +0 -0
- mindspore/lib/libmindspore.so +0 -0
- mindspore/lib/libmindspore_backend.so +0 -0
- mindspore/lib/libmindspore_common.so +0 -0
- mindspore/lib/libmindspore_core.so +0 -0
- mindspore/lib/libmindspore_glog.so.0 +0 -0
- mindspore/lib/libmindspore_gpr.so.15 +0 -0
- mindspore/lib/libmindspore_grpc++.so.1 +0 -0
- mindspore/lib/libmindspore_grpc.so.15 +0 -0
- mindspore/lib/libmindspore_shared_lib.so +0 -0
- mindspore/lib/libmpi_adapter.so +0 -0
- mindspore/lib/libmpi_collective.so +0 -0
- mindspore/lib/libnnacl.so +0 -0
- mindspore/lib/libopencv_core.so.4.5 +0 -0
- mindspore/lib/libopencv_imgcodecs.so.4.5 +0 -0
- mindspore/lib/libopencv_imgproc.so.4.5 +0 -0
- mindspore/lib/libps_cache.so +0 -0
- mindspore/lib/plugin/ascend/libakg.so +0 -0
- mindspore/lib/plugin/ascend/libascend_collective.so +0 -0
- mindspore/lib/plugin/ascend/libdvpp_utils.so +0 -0
- mindspore/lib/plugin/ascend/libhccl_plugin.so +0 -0
- mindspore/lib/plugin/ascend/libmindspore_aicpu_kernels.so +0 -0
- mindspore/lib/plugin/ascend/libmindspore_cpu_kernels.so +0 -0
- mindspore/lib/{libakg.so → plugin/cpu/libakg.so} +0 -0
- mindspore/lib/plugin/libmindspore_ascend.so.1 +0 -0
- mindspore/lib/plugin/libmindspore_ascend.so.2 +0 -0
- mindspore/log.py +28 -28
- mindspore/mindrecord/common/exceptions.py +2 -4
- mindspore/mindrecord/filereader.py +19 -1
- mindspore/mindrecord/filewriter.py +250 -88
- mindspore/mindrecord/mindpage.py +13 -13
- mindspore/mindrecord/shardheader.py +15 -15
- mindspore/mindrecord/shardreader.py +9 -0
- mindspore/mindrecord/shardwriter.py +29 -29
- mindspore/mindrecord/tools/cifar100_to_mr.py +9 -9
- mindspore/mindrecord/tools/cifar10_to_mr.py +9 -9
- mindspore/mindrecord/tools/csv_to_mr.py +4 -4
- mindspore/mindrecord/tools/imagenet_to_mr.py +70 -65
- mindspore/mindrecord/tools/mnist_to_mr.py +41 -41
- mindspore/mindrecord/tools/tfrecord_to_mr.py +6 -6
- mindspore/nn/__init__.py +1 -5
- mindspore/nn/cell.py +297 -234
- mindspore/nn/dynamic_lr.py +1 -1
- mindspore/nn/grad/cell_grad.py +17 -42
- mindspore/nn/layer/__init__.py +7 -4
- mindspore/nn/layer/activation.py +131 -88
- mindspore/nn/layer/basic.py +313 -613
- mindspore/nn/layer/channel_shuffle.py +103 -0
- mindspore/nn/layer/combined.py +1 -1
- mindspore/nn/layer/container.py +52 -6
- mindspore/nn/layer/conv.py +112 -43
- mindspore/nn/layer/dense.py +10 -9
- mindspore/nn/layer/embedding.py +36 -34
- mindspore/nn/layer/image.py +123 -27
- mindspore/nn/layer/math.py +108 -107
- mindspore/nn/layer/normalization.py +212 -366
- mindspore/nn/layer/padding.py +370 -42
- mindspore/nn/layer/pooling.py +1443 -219
- mindspore/nn/layer/rnn_cells.py +11 -16
- mindspore/nn/layer/rnns.py +38 -39
- mindspore/nn/layer/thor_layer.py +24 -25
- mindspore/nn/layer/timedistributed.py +5 -5
- mindspore/nn/layer/transformer.py +701 -0
- mindspore/nn/learning_rate_schedule.py +8 -8
- mindspore/nn/loss/__init__.py +9 -6
- mindspore/nn/loss/loss.py +678 -142
- mindspore/nn/metrics.py +53 -0
- mindspore/nn/optim/_dist_optimizer_registry.py +2 -2
- mindspore/nn/optim/ada_grad.py +8 -8
- mindspore/nn/optim/adadelta.py +2 -3
- mindspore/nn/optim/adafactor.py +18 -14
- mindspore/nn/optim/adam.py +429 -87
- mindspore/nn/optim/adamax.py +5 -6
- mindspore/nn/optim/adasum.py +10 -8
- mindspore/nn/optim/asgd.py +7 -7
- mindspore/nn/optim/ftrl.py +81 -11
- mindspore/nn/optim/lamb.py +7 -8
- mindspore/nn/optim/lars.py +4 -4
- mindspore/nn/optim/lazyadam.py +82 -7
- mindspore/nn/optim/momentum.py +8 -7
- mindspore/nn/optim/optimizer.py +19 -10
- mindspore/nn/optim/proximal_ada_grad.py +6 -5
- mindspore/nn/optim/rmsprop.py +3 -3
- mindspore/nn/optim/rprop.py +20 -16
- mindspore/nn/optim/sgd.py +21 -15
- mindspore/nn/optim/thor.py +23 -21
- mindspore/nn/probability/__init__.py +0 -2
- mindspore/nn/probability/bijector/bijector.py +7 -6
- mindspore/nn/probability/bijector/invert.py +4 -2
- mindspore/nn/probability/bijector/softplus.py +2 -2
- mindspore/nn/probability/bnn_layers/dense_variational.py +1 -1
- mindspore/nn/probability/bnn_layers/layer_distribution.py +2 -2
- mindspore/nn/probability/distribution/__init__.py +6 -0
- mindspore/nn/probability/distribution/_utils/custom_ops.py +3 -2
- mindspore/nn/probability/distribution/_utils/utils.py +11 -17
- mindspore/nn/probability/distribution/bernoulli.py +6 -6
- mindspore/nn/probability/distribution/beta.py +1 -1
- mindspore/nn/probability/distribution/categorical.py +9 -9
- mindspore/nn/probability/distribution/cauchy.py +8 -8
- mindspore/nn/probability/distribution/distribution.py +12 -6
- mindspore/nn/probability/distribution/exponential.py +5 -5
- mindspore/nn/probability/distribution/gamma.py +3 -3
- mindspore/nn/probability/distribution/geometric.py +6 -5
- mindspore/nn/probability/distribution/gumbel.py +5 -5
- mindspore/nn/probability/distribution/half_normal.py +133 -0
- mindspore/nn/probability/distribution/laplace.py +128 -0
- mindspore/nn/probability/distribution/log_normal.py +0 -1
- mindspore/nn/probability/distribution/logistic.py +4 -5
- mindspore/nn/probability/distribution/normal.py +11 -15
- mindspore/nn/probability/distribution/poisson.py +6 -2
- mindspore/nn/probability/distribution/student_t.py +150 -0
- mindspore/nn/probability/distribution/transformed_distribution.py +4 -4
- mindspore/nn/probability/distribution/uniform.py +5 -5
- mindspore/nn/reinforcement/_tensors_queue.py +3 -3
- mindspore/nn/reinforcement/tensor_array.py +2 -2
- mindspore/nn/sparse/sparse.py +8 -1
- mindspore/nn/wrap/cell_wrapper.py +55 -27
- mindspore/nn/wrap/grad_reducer.py +20 -11
- mindspore/nn/wrap/loss_scale.py +47 -30
- mindspore/numpy/array_creations.py +33 -22
- mindspore/numpy/array_ops.py +46 -42
- mindspore/numpy/logic_ops.py +6 -27
- mindspore/numpy/math_ops.py +26 -19
- mindspore/numpy/utils.py +1 -8
- mindspore/numpy/utils_const.py +112 -62
- mindspore/ops/__init__.py +6 -3
- mindspore/ops/_constants.py +0 -6
- mindspore/ops/_grad/__init__.py +2 -1
- mindspore/ops/_grad/grad_array_ops.py +209 -152
- mindspore/ops/_grad/grad_base.py +55 -17
- mindspore/ops/_grad/grad_clip_ops.py +11 -3
- mindspore/ops/_grad/grad_comm_ops.py +58 -47
- mindspore/ops/_grad/grad_implementations.py +21 -61
- mindspore/ops/_grad/grad_inner_ops.py +48 -6
- mindspore/ops/_grad/grad_math_ops.py +306 -161
- mindspore/ops/_grad/grad_nn_ops.py +192 -181
- mindspore/ops/_grad/grad_other_ops.py +1 -1
- mindspore/ops/_grad/grad_quant_ops.py +5 -5
- mindspore/ops/_grad/grad_sequence_ops.py +296 -0
- mindspore/ops/_grad/grad_sparse.py +15 -9
- mindspore/ops/_grad_experimental/__init__.py +1 -0
- mindspore/ops/_grad_experimental/grad_array_ops.py +441 -55
- mindspore/ops/_grad_experimental/grad_image_ops.py +25 -7
- mindspore/ops/_grad_experimental/grad_inner_ops.py +3 -44
- mindspore/ops/_grad_experimental/grad_linalg_ops.py +16 -21
- mindspore/ops/_grad_experimental/grad_math_ops.py +979 -49
- mindspore/ops/_grad_experimental/grad_nn_ops.py +78 -8
- mindspore/ops/_grad_experimental/grad_scalar_ops.py +112 -0
- mindspore/ops/_grad_experimental/grad_sparse_ops.py +197 -13
- mindspore/ops/_op_impl/__init__.py +3 -3
- mindspore/ops/_op_impl/_custom_op/__init__.py +0 -1
- mindspore/ops/_op_impl/_custom_op/_basic.py +0 -1
- mindspore/ops/_op_impl/_custom_op/batch_matmul_impl.py +1 -1
- mindspore/ops/_op_impl/_custom_op/batchnorm_fold.py +4 -2
- mindspore/ops/_op_impl/_custom_op/batchnorm_fold2.py +2 -2
- mindspore/ops/_op_impl/_custom_op/batchnorm_fold2_grad.py +2 -2
- mindspore/ops/_op_impl/_custom_op/batchnorm_fold2_grad_reduce.py +5 -5
- mindspore/ops/_op_impl/_custom_op/batchnorm_fold_grad.py +3 -3
- mindspore/ops/_op_impl/_custom_op/cholesky_trsm_impl.py +1 -1
- mindspore/ops/_op_impl/_custom_op/correction_mul.py +3 -3
- mindspore/ops/_op_impl/_custom_op/correction_mul_grad.py +2 -2
- mindspore/ops/_op_impl/_custom_op/dsd_back_impl.py +4 -8
- mindspore/ops/_op_impl/_custom_op/dsd_impl.py +1 -1
- mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perchannel.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perchannel_grad.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perchannel_grad_reduce.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perlayer.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perlayer_grad.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fake_learned_scale_quant_perlayer_grad_reduce.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fake_quant_perchannel.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fake_quant_perchannel_grad.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fake_quant_perlayer.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fake_quant_perlayer_grad.py +2 -2
- mindspore/ops/_op_impl/_custom_op/fused_abs_max1_impl.py +1 -1
- mindspore/ops/_op_impl/_custom_op/img2col_impl.py +1 -1
- mindspore/ops/_op_impl/_custom_op/matmul_cube_dense_left_impl.py +2 -2
- mindspore/ops/_op_impl/_custom_op/matmul_cube_dense_right_impl.py +1 -1
- mindspore/ops/_op_impl/_custom_op/matmul_cube_fracz_left_cast_impl.py +1 -1
- mindspore/ops/_op_impl/_custom_op/matmul_cube_fracz_right_mul_impl.py +1 -1
- mindspore/ops/_op_impl/_custom_op/matmul_cube_impl.py +2 -2
- mindspore/ops/_op_impl/_custom_op/matmul_dds_grad_impl.py +0 -1
- mindspore/ops/_op_impl/_custom_op/matmul_dds_impl.py +0 -1
- mindspore/ops/_op_impl/_custom_op/matrix_combine_impl.py +1 -1
- mindspore/ops/_op_impl/_custom_op/minmax_update_perchannel.py +2 -2
- mindspore/ops/_op_impl/_custom_op/minmax_update_perlayer.py +2 -2
- mindspore/ops/_op_impl/_custom_op/transpose02314_impl.py +1 -1
- mindspore/ops/_op_impl/aicpu/__init__.py +238 -3
- mindspore/ops/_op_impl/aicpu/abs.py +36 -0
- mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_2d.py +34 -0
- mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_2d_grad.py +34 -0
- mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_3d.py +39 -0
- mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_3d_grad.py +39 -0
- mindspore/ops/_op_impl/aicpu/adaptive_max_pool_2d_grad.py +37 -0
- mindspore/ops/_op_impl/aicpu/adaptive_max_pool_3d.py +42 -0
- mindspore/ops/_op_impl/aicpu/adaptive_max_pool_3d_grad.py +152 -0
- mindspore/ops/_op_impl/aicpu/add.py +43 -0
- mindspore/ops/_op_impl/aicpu/addcdiv.py +0 -32
- mindspore/ops/_op_impl/aicpu/addcmul.py +0 -84
- mindspore/ops/_op_impl/aicpu/affine_grid_grad.py +35 -0
- mindspore/ops/_op_impl/aicpu/arg_max.py +75 -0
- mindspore/ops/_op_impl/aicpu/arg_min.py +75 -0
- mindspore/ops/_op_impl/aicpu/argmin_with_value.py +43 -0
- mindspore/ops/_op_impl/aicpu/batch_matmul.py +43 -0
- mindspore/ops/_op_impl/aicpu/batch_norm_grad_grad.py +49 -0
- mindspore/ops/_op_impl/aicpu/bernoulli.py +48 -0
- mindspore/ops/_op_impl/aicpu/bessel_i0.py +31 -0
- mindspore/ops/_op_impl/aicpu/bias_add.py +44 -0
- mindspore/ops/_op_impl/aicpu/bias_add_grad.py +43 -0
- mindspore/ops/_op_impl/aicpu/bincount.py +33 -0
- mindspore/{nn/probability/infer/variational/__init__.py → ops/_op_impl/aicpu/cauchy.py} +17 -10
- mindspore/ops/_op_impl/aicpu/channel_shuffle.py +40 -0
- mindspore/ops/_op_impl/aicpu/cholesky.py +1 -1
- mindspore/ops/_op_impl/{cpu/bias_add.py → aicpu/choleskygrad.py} +9 -7
- mindspore/ops/_op_impl/aicpu/combined_non_max_suppression.py +42 -0
- mindspore/ops/_op_impl/aicpu/concat_offset.py +42 -0
- mindspore/ops/_op_impl/aicpu/concat_offset_v1.py +31 -0
- mindspore/ops/_op_impl/aicpu/conj.py +11 -0
- mindspore/ops/_op_impl/aicpu/crop_and_resize_grad_image.py +38 -0
- mindspore/ops/_op_impl/aicpu/cumulative_logsumexp.py +36 -0
- mindspore/ops/_op_impl/aicpu/deformable_offsets.py +38 -0
- mindspore/ops/_op_impl/aicpu/deformable_offsets_grad.py +2 -2
- mindspore/ops/_op_impl/aicpu/dense_to_sparse_set_operation.py +48 -0
- mindspore/ops/_op_impl/aicpu/diag.py +36 -0
- mindspore/ops/_op_impl/aicpu/diag_part.py +36 -0
- mindspore/ops/_op_impl/aicpu/diagonal.py +35 -0
- mindspore/ops/_op_impl/{cpu/bias_add_grad.py → aicpu/digamma.py} +9 -7
- mindspore/ops/_op_impl/aicpu/eig.py +35 -0
- mindspore/ops/_op_impl/aicpu/fft_with_size.py +41 -0
- mindspore/ops/_op_impl/aicpu/flatten.py +1 -0
- mindspore/ops/_op_impl/aicpu/fmax.py +36 -0
- mindspore/ops/_op_impl/aicpu/fmin.py +37 -0
- mindspore/ops/_op_impl/aicpu/fractional_max_pool3d_with_fixed_ksize.py +1 -1
- mindspore/ops/_op_impl/aicpu/fse_decode.py +43 -0
- mindspore/ops/_op_impl/aicpu/glu.py +33 -0
- mindspore/ops/_op_impl/aicpu/glu_grad.py +34 -0
- mindspore/ops/_op_impl/aicpu/greater.py +41 -0
- mindspore/ops/_op_impl/aicpu/greater_equal.py +41 -0
- mindspore/ops/_op_impl/aicpu/index_put.py +50 -0
- mindspore/ops/_op_impl/{tbe/scatter_add_ds.py → aicpu/inplace_index_add.py} +17 -21
- mindspore/ops/_op_impl/aicpu/instance_norm_v2.py +41 -0
- mindspore/ops/_op_impl/aicpu/instance_norm_v2_grad.py +44 -0
- mindspore/ops/_op_impl/aicpu/layer_norm_grad_grad.py +47 -0
- mindspore/ops/_op_impl/aicpu/less.py +41 -0
- mindspore/ops/_op_impl/aicpu/less_equal.py +41 -0
- mindspore/ops/_op_impl/aicpu/lgamma.py +32 -0
- mindspore/ops/_op_impl/aicpu/log_normal_reverse.py +33 -0
- mindspore/ops/_op_impl/aicpu/logit.py +33 -0
- mindspore/ops/_op_impl/aicpu/logit_grad.py +34 -0
- mindspore/ops/_op_impl/aicpu/masked_fill.py +42 -0
- mindspore/ops/_op_impl/aicpu/masked_scatter.py +39 -0
- mindspore/ops/_op_impl/aicpu/matmul.py +39 -0
- mindspore/ops/_op_impl/aicpu/matrix_logarithm.py +31 -0
- mindspore/ops/_op_impl/aicpu/matrix_power.py +32 -0
- mindspore/ops/_op_impl/aicpu/matrix_solve_ls.py +36 -0
- mindspore/ops/_op_impl/aicpu/matrix_triangular_solve.py +36 -0
- mindspore/ops/_op_impl/aicpu/mirror_pad.py +2 -0
- mindspore/ops/_op_impl/aicpu/mirror_pad_grad.py +0 -4
- mindspore/ops/_op_impl/aicpu/mul.py +3 -1
- mindspore/ops/_op_impl/aicpu/multinomial.py +14 -6
- mindspore/ops/_op_impl/aicpu/multinomial_with_replacement.py +35 -0
- mindspore/ops/_op_impl/aicpu/nan_to_num.py +34 -0
- mindspore/ops/_op_impl/aicpu/nllloss.py +38 -0
- mindspore/ops/_op_impl/aicpu/nllloss_grad.py +39 -0
- mindspore/ops/_op_impl/aicpu/ones_like.py +0 -2
- mindspore/ops/_op_impl/aicpu/polar.py +32 -0
- mindspore/ops/_op_impl/aicpu/polygamma.py +34 -0
- mindspore/ops/_op_impl/aicpu/qr.py +36 -0
- mindspore/ops/_op_impl/aicpu/quant_dtype_cast.py +40 -0
- mindspore/ops/_op_impl/aicpu/quantile.py +35 -0
- mindspore/ops/_op_impl/aicpu/ragged_tensor_to_sparse.py +73 -0
- mindspore/ops/_op_impl/aicpu/ragged_tensor_to_tensor.py +74 -0
- mindspore/ops/_op_impl/aicpu/random_shuffle.py +3 -0
- mindspore/ops/_op_impl/aicpu/randperm_v2.py +41 -0
- mindspore/ops/_op_impl/aicpu/range.py +36 -0
- mindspore/ops/_op_impl/aicpu/reciprocal.py +34 -0
- mindspore/ops/_op_impl/aicpu/reciprocal_grad.py +35 -0
- mindspore/ops/_op_impl/aicpu/reduce_sum.py +57 -0
- mindspore/ops/_op_impl/aicpu/resize_bicubic.py +2 -8
- mindspore/ops/_op_impl/aicpu/resize_bicubic_grad.py +1 -1
- mindspore/ops/_op_impl/aicpu/resize_v2.py +68 -0
- mindspore/ops/_op_impl/aicpu/resize_v2_grad.py +68 -0
- mindspore/ops/_op_impl/aicpu/scatter_elements.py +4 -0
- mindspore/ops/_op_impl/aicpu/scatter_nd_update.py +2 -0
- mindspore/ops/_op_impl/aicpu/search_sorted.py +12 -6
- mindspore/ops/_op_impl/aicpu/self_adjoint_eig.py +34 -0
- mindspore/ops/_op_impl/aicpu/sequence_add.py +34 -0
- mindspore/ops/_op_impl/aicpu/sequence_add_offset.py +34 -0
- mindspore/ops/_op_impl/aicpu/sequence_addn.py +38 -0
- mindspore/ops/_op_impl/aicpu/slice_grad.py +76 -0
- mindspore/ops/_op_impl/aicpu/smooth_l1_loss.py +35 -0
- mindspore/ops/_op_impl/aicpu/smooth_l1_loss_grad.py +37 -0
- mindspore/ops/_op_impl/aicpu/sort.py +39 -0
- mindspore/ops/_op_impl/aicpu/sparse_apply_adagrad_da.py +0 -24
- mindspore/ops/_op_impl/aicpu/sparse_cross.py +42 -0
- mindspore/ops/_op_impl/aicpu/sparse_fill_empty_rows.py +63 -0
- mindspore/ops/_op_impl/aicpu/sparse_fill_empty_rows_grad.py +45 -0
- mindspore/ops/_op_impl/aicpu/sparse_matrix_mat_mul.py +56 -0
- mindspore/ops/_op_impl/{tbe/slice_ds.py → aicpu/sparse_segment_sum.py} +16 -24
- mindspore/ops/_op_impl/aicpu/sparse_segment_sum_with_num_segments.py +68 -0
- mindspore/ops/_op_impl/aicpu/sparse_slice.py +63 -0
- mindspore/ops/_op_impl/aicpu/sparse_slice_grad.py +61 -0
- mindspore/ops/_op_impl/aicpu/squared_difference.py +2 -0
- mindspore/ops/_op_impl/aicpu/strided_slice_v2.py +93 -0
- mindspore/ops/_op_impl/aicpu/strided_slice_v2_grad.py +66 -0
- mindspore/ops/_op_impl/aicpu/tensor_scatter_update.py +59 -0
- mindspore/ops/_op_impl/{tbe/gather_v2.py → aicpu/tile.py} +24 -24
- mindspore/ops/_op_impl/aicpu/tridiagonal_solve.py +35 -0
- mindspore/ops/_op_impl/aicpu/tril_indices.py +34 -0
- mindspore/ops/_op_impl/aicpu/triu_indices.py +34 -0
- mindspore/ops/_op_impl/aicpu/uniform.py +34 -0
- mindspore/ops/_op_impl/aicpu/uniform_candidate_sampler.py +1 -0
- mindspore/ops/_op_impl/aicpu/unique_consecutive.py +10 -2
- mindspore/ops/_op_impl/cpu/__init__.py +1 -2
- mindspore/ops/_op_impl/cpu/dynamic_shape.py +5 -1
- mindspore/ops/_op_impl/cpu/maximum_grad.py +2 -0
- mindspore/{compression/common/__init__.py → ops/_op_impl/cpu/pyexecute.py} +13 -8
- mindspore/ops/_op_impl/cpu/reduce_sum.py +8 -0
- mindspore/ops/_op_impl/cpu/sparse_slice.py +62 -0
- mindspore/ops/_op_impl/cpu/sparse_slice_grad.py +60 -0
- mindspore/ops/_op_impl/cpu/tensor_shape.py +5 -1
- mindspore/ops/_op_impl/tbe/__init__.py +27 -608
- mindspore/ops/_op_impl/tbe/addcdiv_ds.py +42 -0
- mindspore/ops/_op_impl/tbe/addcmul_ds.py +44 -0
- mindspore/ops/_op_impl/tbe/assign_add_ds.py +1 -0
- mindspore/ops/_op_impl/tbe/atomic_addr_clean.py +1 -1
- mindspore/ops/_op_impl/tbe/avg_pool_3d_grad.py +1 -1
- mindspore/ops/_op_impl/tbe/basic_lstm_cell_c_state_grad_v2.py +0 -1
- mindspore/ops/_op_impl/tbe/batch_to_space.py +1 -1
- mindspore/ops/_op_impl/tbe/batch_to_space_nd.py +1 -1
- mindspore/ops/_op_impl/tbe/batch_to_space_nd_v2.py +41 -0
- mindspore/ops/_op_impl/tbe/bce_with_logits_loss.py +1 -0
- mindspore/ops/_op_impl/tbe/bias_add_grad.py +2 -0
- mindspore/ops/_op_impl/tbe/bn_infer_grad.py +4 -2
- mindspore/ops/_op_impl/tbe/bn_infer_grad_ds.py +40 -0
- mindspore/ops/_op_impl/tbe/bn_training_update.py +0 -1
- mindspore/ops/_op_impl/tbe/bn_training_update_ds.py +0 -1
- mindspore/ops/_op_impl/tbe/broadcast_to_ds.py +6 -4
- mindspore/ops/_op_impl/tbe/cast.py +0 -2
- mindspore/ops/_op_impl/tbe/cast_ds.py +3 -3
- mindspore/ops/_op_impl/tbe/ctc_loss_v2.py +0 -2
- mindspore/ops/_op_impl/tbe/ctc_loss_v2_grad.py +0 -2
- mindspore/ops/_op_impl/tbe/data_format_dim_map_ds.py +1 -0
- mindspore/ops/_op_impl/tbe/deformable_offsets.py +1 -0
- mindspore/ops/_op_impl/tbe/depthwise_conv2d.py +1 -1
- mindspore/ops/_op_impl/tbe/dynamic_atomic_addr_clean.py +1 -1
- mindspore/ops/_op_impl/tbe/gather_nd.py +1 -0
- mindspore/ops/_op_impl/tbe/greater.py +2 -0
- mindspore/ops/_op_impl/tbe/{index_add.py → inplace_index_add.py} +3 -6
- mindspore/ops/_op_impl/tbe/layer_norm_beta_gamma_backprop_v2.py +0 -1
- mindspore/ops/_op_impl/tbe/npu_clear_float_status_v2.py +35 -0
- mindspore/ops/_op_impl/tbe/npu_get_float_status_v2.py +35 -0
- mindspore/ops/_op_impl/tbe/one_hot_ds.py +0 -6
- mindspore/ops/_op_impl/tbe/{greater_ds.py → reduce_all_ds.py} +13 -16
- mindspore/ops/_op_impl/tbe/reduce_any_ds.py +39 -0
- mindspore/ops/_op_impl/tbe/roi_align_ds.py +44 -0
- mindspore/ops/_op_impl/tbe/roi_align_grad_ds.py +44 -0
- mindspore/ops/_op_impl/tbe/scatter_add.py +2 -0
- mindspore/ops/_op_impl/tbe/scatter_nd_add.py +2 -2
- mindspore/ops/_op_impl/tbe/slice.py +26 -15
- mindspore/ops/_op_impl/tbe/space_to_batch.py +1 -1
- mindspore/ops/_op_impl/tbe/space_to_batch_nd.py +1 -1
- mindspore/ops/_op_impl/tbe/strided_slice_grad_d.py +1 -0
- mindspore/ops/_op_impl/tbe/trans_data_ds.py +15 -5
- mindspore/ops/_op_impl/tbe/unsorted_segment_sum.py +1 -1
- mindspore/ops/_op_impl/tbe/unsorted_segment_sum_ds.py +2 -0
- mindspore/ops/_primitive_cache.py +3 -2
- mindspore/ops/_register_for_op.py +11 -0
- mindspore/ops/_utils/__init__.py +1 -1
- mindspore/ops/_utils/utils.py +20 -41
- mindspore/ops/_vmap/__init__.py +2 -2
- mindspore/ops/_vmap/vmap_array_ops.py +170 -78
- mindspore/ops/_vmap/vmap_base.py +24 -10
- mindspore/ops/_vmap/vmap_convolution_ops.py +7 -10
- mindspore/ops/_vmap/vmap_grad_math_ops.py +4 -4
- mindspore/ops/_vmap/vmap_grad_nn_ops.py +41 -9
- mindspore/ops/_vmap/vmap_image_ops.py +52 -0
- mindspore/ops/_vmap/vmap_math_ops.py +77 -6
- mindspore/ops/_vmap/vmap_nn_ops.py +78 -29
- mindspore/ops/_vmap/vmap_other_ops.py +3 -1
- mindspore/ops/_vmap/vmap_random_ops.py +55 -3
- mindspore/ops/_vmap/vmap_sparse_ops.py +1 -0
- mindspore/ops/bprop_mindir/AdaptiveAvgPool2D_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/AdaptiveMaxPool2D_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/ApproximateEqual_bprop.mindir +18 -19
- mindspore/ops/bprop_mindir/Argmax_bprop.mindir +13 -12
- mindspore/ops/bprop_mindir/Argmin_bprop.mindir +14 -13
- mindspore/ops/bprop_mindir/AssignSub_bprop.mindir +17 -18
- mindspore/ops/bprop_mindir/Assign_bprop.mindir +16 -16
- mindspore/ops/bprop_mindir/AvgPool3D_bprop.mindir +150 -0
- mindspore/ops/bprop_mindir/AvgPool_bprop.mindir +66 -0
- mindspore/ops/bprop_mindir/BCEWithLogitsLoss_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/BNTrainingReduce_bprop.mindir +13 -12
- mindspore/ops/bprop_mindir/BatchNormGrad_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/BatchToSpaceND_bprop.mindir +28 -0
- mindspore/ops/bprop_mindir/BiasAddGrad_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/BinaryCrossEntropy_bprop.mindir +33 -0
- mindspore/ops/bprop_mindir/BroadcastTo_bprop.mindir +306 -0
- mindspore/ops/bprop_mindir/Broadcast_bprop.mindir +12 -8
- mindspore/ops/bprop_mindir/CTCLoss_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Concat_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Conv2DBackpropFilter_bprop.mindir +240 -0
- mindspore/ops/bprop_mindir/Conv2DBackpropInput_bprop.mindir +247 -0
- mindspore/ops/bprop_mindir/Conv2DTranspose_bprop.mindir +247 -0
- mindspore/ops/bprop_mindir/Conv3DTranspose_bprop.mindir +315 -0
- mindspore/ops/bprop_mindir/Conv3D_bprop.mindir +278 -0
- mindspore/ops/bprop_mindir/DType_bprop.mindir +12 -12
- mindspore/ops/bprop_mindir/DeformableOffsets_bprop.mindir +58 -0
- mindspore/ops/bprop_mindir/Depend_bprop.mindir +12 -13
- mindspore/ops/bprop_mindir/DepthToSpace_bprop.mindir +23 -0
- mindspore/ops/bprop_mindir/DepthwiseConv2dNative_bprop.mindir +138 -0
- mindspore/ops/bprop_mindir/DiagPart_bprop.mindir +15 -0
- mindspore/ops/bprop_mindir/Dropout2D_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Dropout3D_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/DropoutDoMask_bprop.mindir +22 -24
- mindspore/ops/bprop_mindir/DropoutGenMask_bprop.mindir +16 -14
- mindspore/ops/bprop_mindir/DropoutGrad_bprop.mindir +27 -0
- mindspore/ops/bprop_mindir/Dropout_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/DynamicGRUV2_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/DynamicRNN_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/DynamicShape_bprop.mindir +12 -12
- mindspore/ops/bprop_mindir/Elu_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/EmbeddingLookup_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Equal_bprop.mindir +18 -19
- mindspore/ops/bprop_mindir/ExpandDims_bprop.mindir +58 -0
- mindspore/ops/bprop_mindir/FastGeLU_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/Flatten_bprop.mindir +54 -0
- mindspore/ops/bprop_mindir/FloorDiv_bprop.mindir +18 -15
- mindspore/ops/bprop_mindir/GatherD_bprop.mindir +26 -0
- mindspore/ops/bprop_mindir/GatherNd_bprop.mindir +57 -0
- mindspore/ops/bprop_mindir/Gather_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/GreaterEqual_bprop.mindir +17 -18
- mindspore/ops/bprop_mindir/Greater_bprop.mindir +18 -19
- mindspore/ops/bprop_mindir/HSigmoid_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/HSwish_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/IOU_bprop.mindir +18 -19
- mindspore/ops/bprop_mindir/InstanceNorm_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/IsFinite_bprop.mindir +13 -12
- mindspore/ops/bprop_mindir/IsInf_bprop.mindir +13 -10
- mindspore/ops/bprop_mindir/IsNan_bprop.mindir +14 -11
- mindspore/ops/bprop_mindir/KLDivLoss_bprop.mindir +126 -0
- mindspore/ops/bprop_mindir/L2Loss_bprop.mindir +15 -0
- mindspore/ops/bprop_mindir/L2Normalize_bprop.mindir +30 -0
- mindspore/ops/bprop_mindir/LRN_bprop.mindir +43 -0
- mindspore/ops/bprop_mindir/LayerNormGrad_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/LessEqual_bprop.mindir +18 -19
- mindspore/ops/bprop_mindir/Less_bprop.mindir +17 -18
- mindspore/ops/bprop_mindir/LinSpace_bprop.mindir +22 -19
- mindspore/ops/bprop_mindir/Load_bprop.mindir +12 -13
- mindspore/ops/bprop_mindir/LogSoftmax_bprop.mindir +23 -0
- mindspore/ops/bprop_mindir/LogicalAnd_bprop.mindir +17 -18
- mindspore/ops/bprop_mindir/LogicalNot_bprop.mindir +14 -13
- mindspore/ops/bprop_mindir/MaskedSelect_bprop.mindir +21 -0
- mindspore/ops/bprop_mindir/MaxPool3DGradGrad_bprop.mindir +74 -0
- mindspore/ops/bprop_mindir/MaxPool3DGrad_bprop.mindir +74 -0
- mindspore/ops/bprop_mindir/MaxPool3D_bprop.mindir +75 -0
- mindspore/ops/bprop_mindir/MaxPoolGradGrad_bprop.mindir +65 -0
- mindspore/ops/bprop_mindir/MaxPoolWithArgmax_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Maximum_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Minimum_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/MirrorPad_bprop.mindir +27 -0
- mindspore/ops/bprop_mindir/Mish_bprop.mindir +35 -0
- mindspore/ops/bprop_mindir/MulNoNan_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/NLLLoss_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/NonZero_bprop.mindir +14 -0
- mindspore/ops/bprop_mindir/NotEqual_bprop.mindir +18 -19
- mindspore/ops/bprop_mindir/OneHot_bprop.mindir +25 -23
- mindspore/ops/bprop_mindir/OnesLike_bprop.mindir +13 -13
- mindspore/ops/bprop_mindir/PReLU_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Pad_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Padding_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/RNNTLoss_bprop.mindir +29 -0
- mindspore/ops/bprop_mindir/ROIAlign_bprop.mindir +82 -0
- mindspore/ops/bprop_mindir/Range_bprop.mindir +21 -19
- mindspore/ops/bprop_mindir/Rank_bprop.mindir +11 -11
- mindspore/ops/bprop_mindir/ReLU6_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/ReLUV2_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/ReduceAll_bprop.mindir +18 -17
- mindspore/ops/bprop_mindir/ReduceAny_bprop.mindir +18 -17
- mindspore/ops/bprop_mindir/ReluGrad_bprop.mindir +19 -23
- mindspore/ops/bprop_mindir/Reshape_bprop.mindir +60 -0
- mindspore/ops/bprop_mindir/ResizeBilinear_bprop.mindir +29 -0
- mindspore/ops/bprop_mindir/ResizeNearestNeighbor_bprop.mindir +89 -0
- mindspore/ops/bprop_mindir/ReverseSequence_bprop.mindir +52 -0
- mindspore/ops/bprop_mindir/ReverseV2_bprop.mindir +22 -0
- mindspore/ops/bprop_mindir/Round_bprop.mindir +14 -13
- mindspore/ops/bprop_mindir/ScatterMax_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/ScatterMin_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/ScatterNdUpdate_bprop.mindir +22 -0
- mindspore/ops/bprop_mindir/ScatterNd_bprop.mindir +24 -0
- mindspore/ops/bprop_mindir/ScatterNonAliasingAdd_bprop.mindir +22 -0
- mindspore/ops/bprop_mindir/ScatterUpdate_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/SeLU_bprop.mindir +21 -0
- mindspore/ops/bprop_mindir/Select_bprop.mindir +30 -34
- mindspore/ops/bprop_mindir/Shape_bprop.mindir +12 -12
- mindspore/ops/bprop_mindir/SigmoidCrossEntropyWithLogits_bprop.mindir +21 -0
- mindspore/ops/bprop_mindir/SigmoidGrad_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Sigmoid_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/Sign_bprop.mindir +13 -12
- mindspore/ops/bprop_mindir/Slice_bprop.mindir +26 -0
- mindspore/ops/bprop_mindir/SmoothL1Loss_bprop.mindir +36 -0
- mindspore/ops/bprop_mindir/SoftmaxCrossEntropyWithLogits_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Softplus_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/Softsign_bprop.mindir +33 -0
- mindspore/ops/bprop_mindir/Sort_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/SpaceToBatchND_bprop.mindir +28 -0
- mindspore/ops/bprop_mindir/SpaceToDepth_bprop.mindir +23 -0
- mindspore/ops/bprop_mindir/SparseGatherV2_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/SparseSoftmaxCrossEntropyWithLogits_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Split_bprop.mindir +22 -0
- mindspore/ops/bprop_mindir/Squeeze_bprop.mindir +54 -0
- mindspore/ops/bprop_mindir/StridedSliceGrad_bprop.mindir +95 -0
- mindspore/ops/bprop_mindir/StridedSlice_bprop.mindir +98 -0
- mindspore/ops/bprop_mindir/Switch_bprop.mindir +28 -32
- mindspore/ops/bprop_mindir/TanhGrad_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Tanh_bprop.mindir +66 -0
- mindspore/ops/bprop_mindir/TensorScatterAdd_bprop.mindir +22 -0
- mindspore/ops/bprop_mindir/TensorScatterUpdate_bprop.mindir +29 -0
- mindspore/ops/bprop_mindir/TensorShape_bprop.mindir +14 -0
- mindspore/ops/bprop_mindir/Tile_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/TopK_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/TransShape_bprop.mindir +23 -0
- mindspore/ops/bprop_mindir/TruncateDiv_bprop.mindir +18 -15
- mindspore/ops/bprop_mindir/TupleGetItem_bprop.mindir +11 -13
- mindspore/ops/bprop_mindir/Unique_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/Unstack_bprop.mindir +22 -0
- mindspore/ops/bprop_mindir/UpsampleNearest3D_bprop.mindir +32 -0
- mindspore/ops/bprop_mindir/UpsampleTrilinear3D_bprop.mindir +38 -0
- mindspore/ops/bprop_mindir/ZerosLike_bprop.mindir +13 -12
- mindspore/ops/bprop_mindir/__init__.py +1 -4
- mindspore/ops/bprop_mindir/generate_mindir.py +32 -20
- mindspore/ops/composite/__init__.py +12 -13
- mindspore/ops/composite/base.py +261 -254
- mindspore/ops/composite/env_ops.py +41 -0
- mindspore/ops/composite/math_ops.py +197 -156
- mindspore/ops/composite/multitype_ops/_compile_utils.py +428 -176
- mindspore/ops/composite/multitype_ops/_constexpr_utils.py +188 -87
- mindspore/ops/composite/multitype_ops/add_impl.py +23 -1
- mindspore/ops/composite/multitype_ops/div_impl.py +3 -3
- mindspore/ops/composite/multitype_ops/equal_impl.py +1 -0
- mindspore/ops/composite/multitype_ops/floordiv_impl.py +1 -1
- mindspore/ops/composite/multitype_ops/getitem_impl.py +52 -5
- mindspore/ops/composite/multitype_ops/greater_equal_impl.py +31 -0
- mindspore/ops/composite/multitype_ops/greater_impl.py +31 -0
- mindspore/ops/composite/multitype_ops/in_impl.py +15 -3
- mindspore/ops/composite/multitype_ops/less_equal_impl.py +33 -2
- mindspore/ops/composite/multitype_ops/less_impl.py +33 -0
- mindspore/ops/composite/multitype_ops/logical_and_impl.py +2 -2
- mindspore/ops/composite/multitype_ops/logical_or_impl.py +2 -1
- mindspore/ops/composite/multitype_ops/mod_impl.py +1 -1
- mindspore/ops/composite/multitype_ops/mul_impl.py +21 -7
- mindspore/ops/composite/multitype_ops/not_in_impl.py +15 -3
- mindspore/ops/composite/multitype_ops/ones_like_impl.py +2 -4
- mindspore/ops/composite/multitype_ops/pow_impl.py +1 -0
- mindspore/ops/composite/multitype_ops/setitem_impl.py +62 -70
- mindspore/ops/composite/multitype_ops/sub_impl.py +3 -3
- mindspore/ops/composite/multitype_ops/zeros_like_impl.py +41 -4
- mindspore/ops/function/__init__.py +323 -8
- mindspore/ops/function/array_func.py +3511 -780
- mindspore/ops/function/clip_func.py +329 -0
- mindspore/ops/function/debug_func.py +6 -6
- mindspore/ops/function/grad/__init__.py +5 -1
- mindspore/ops/function/grad/grad_func.py +736 -65
- mindspore/ops/function/image_func.py +270 -0
- mindspore/ops/function/linalg_func.py +268 -8
- mindspore/ops/function/math_func.py +8032 -3164
- mindspore/ops/function/nn_func.py +5619 -1855
- mindspore/ops/function/other_func.py +115 -0
- mindspore/ops/function/parameter_func.py +11 -10
- mindspore/ops/function/random_func.py +939 -77
- mindspore/ops/function/sparse_func.py +249 -84
- mindspore/ops/function/sparse_unary_func.py +2303 -0
- mindspore/ops/function/spectral_func.py +146 -0
- mindspore/ops/function/vmap_func.py +114 -0
- mindspore/ops/functional.py +182 -254
- mindspore/ops/op_info_register.py +79 -34
- mindspore/ops/operations/__init__.py +210 -118
- mindspore/ops/operations/_csr_ops.py +7 -7
- mindspore/ops/operations/_embedding_cache_ops.py +25 -15
- mindspore/ops/operations/_grad_ops.py +447 -322
- mindspore/ops/operations/_inner_ops.py +547 -176
- mindspore/ops/operations/_map_tensor_ops.py +112 -0
- mindspore/ops/operations/_ms_kernel.py +29 -27
- mindspore/ops/operations/_ocr_ops.py +11 -11
- mindspore/ops/operations/_opaque_predicate_registry.py +41 -0
- mindspore/ops/operations/_quant_ops.py +186 -101
- mindspore/ops/operations/_rl_inner_ops.py +122 -61
- mindspore/ops/operations/_scalar_ops.py +466 -0
- mindspore/ops/operations/_sequence_ops.py +1047 -0
- mindspore/ops/operations/_tensor_array.py +10 -11
- mindspore/ops/operations/_thor_ops.py +4 -4
- mindspore/ops/operations/array_ops.py +1428 -1226
- mindspore/ops/operations/comm_ops.py +180 -117
- mindspore/ops/operations/control_ops.py +4 -2
- mindspore/ops/operations/custom_ops.py +185 -98
- mindspore/ops/operations/debug_ops.py +92 -54
- mindspore/ops/operations/image_ops.py +406 -211
- mindspore/ops/operations/inner_ops.py +42 -53
- mindspore/ops/operations/linalg_ops.py +32 -29
- mindspore/ops/operations/math_ops.py +2076 -897
- mindspore/ops/operations/nn_ops.py +1282 -1252
- mindspore/ops/operations/other_ops.py +124 -278
- mindspore/ops/operations/random_ops.py +345 -178
- mindspore/ops/operations/rl_ops.py +8 -9
- mindspore/ops/operations/sparse_ops.py +502 -157
- mindspore/ops/operations/spectral_ops.py +107 -0
- mindspore/ops/primitive.py +192 -15
- mindspore/ops/vm_impl_registry.py +23 -2
- mindspore/parallel/__init__.py +6 -1
- mindspore/parallel/_auto_parallel_context.py +199 -92
- mindspore/parallel/_cell_wrapper.py +4 -2
- mindspore/parallel/_cost_model_context.py +3 -0
- mindspore/parallel/_dp_allreduce_fusion.py +2 -1
- mindspore/parallel/_offload_context.py +185 -0
- mindspore/parallel/_parallel_serialization.py +167 -28
- mindspore/parallel/_ps_context.py +9 -5
- mindspore/parallel/_recovery_context.py +1 -1
- mindspore/parallel/_tensor.py +9 -1
- mindspore/{nn/transformer → parallel/_transformer}/__init__.py +6 -6
- mindspore/{nn/transformer → parallel/_transformer}/layers.py +59 -37
- mindspore/{nn/transformer → parallel/_transformer}/loss.py +4 -7
- mindspore/{nn/transformer → parallel/_transformer}/moe.py +160 -35
- mindspore/{nn/transformer → parallel/_transformer}/op_parallel_config.py +3 -3
- mindspore/{nn/transformer → parallel/_transformer}/transformer.py +235 -196
- mindspore/parallel/_utils.py +47 -7
- mindspore/parallel/algo_parameter_config.py +5 -1
- mindspore/parallel/checkpoint_transform.py +329 -0
- mindspore/parallel/shard.py +229 -0
- mindspore/profiler/__init__.py +2 -1
- mindspore/profiler/common/util.py +4 -3
- mindspore/profiler/common/validator/validate_path.py +2 -2
- mindspore/profiler/envprofiling.py +249 -0
- mindspore/profiler/parser/aicpu_data_parser.py +38 -39
- mindspore/profiler/parser/ascend_timeline_generator.py +497 -0
- mindspore/profiler/parser/base_timeline_generator.py +471 -0
- mindspore/profiler/parser/cpu_gpu_timeline_generator.py +684 -0
- mindspore/profiler/parser/framework_parser.py +42 -16
- mindspore/profiler/parser/hccl_parser.py +158 -158
- mindspore/profiler/parser/hwts_log_parser.py +7 -6
- mindspore/profiler/parser/integrator.py +18 -1579
- mindspore/profiler/parser/minddata_analyzer.py +8 -8
- mindspore/profiler/parser/msadvisor_analyzer.py +14 -27
- mindspore/profiler/parser/msadvisor_parser.py +2 -4
- mindspore/profiler/parser/optime_parser.py +17 -18
- mindspore/profiler/parser/profiler_info.py +108 -0
- mindspore/profiler/parser/step_trace_parser.py +1 -1
- mindspore/profiler/profiling.py +396 -194
- mindspore/rewrite/__init__.py +6 -2
- mindspore/rewrite/api/node.py +51 -110
- mindspore/rewrite/api/node_type.py +10 -6
- mindspore/rewrite/api/pattern_engine.py +51 -7
- mindspore/rewrite/api/scoped_value.py +64 -53
- mindspore/rewrite/api/symbol_tree.py +108 -61
- mindspore/rewrite/api/tree_node_helper.py +2 -3
- mindspore/{compression/quant/__init__.py → rewrite/ast_creator_register.py} +20 -11
- mindspore/rewrite/ast_helpers/__init__.py +6 -3
- mindspore/rewrite/ast_helpers/ast_creator.py +115 -0
- mindspore/rewrite/ast_helpers/ast_finder.py +99 -1
- mindspore/rewrite/ast_helpers/ast_modifier.py +17 -4
- mindspore/rewrite/ast_helpers/ast_replacer.py +1 -1
- mindspore/rewrite/ast_transformers/__init__.py +0 -1
- mindspore/rewrite/ast_transformers/flatten_recursive_stmt.py +46 -5
- mindspore/rewrite/ast_transformers/remove_return_out_of_if.py +6 -3
- mindspore/rewrite/common/__init__.py +2 -0
- mindspore/rewrite/common/event.py +1 -1
- mindspore/rewrite/common/observable.py +1 -1
- mindspore/rewrite/common/observer.py +1 -1
- mindspore/rewrite/common/rewrite_elog.py +35 -0
- mindspore/rewrite/namer.py +2 -2
- mindspore/rewrite/namespace.py +14 -4
- mindspore/rewrite/node.py +161 -13
- mindspore/rewrite/parser.py +0 -1
- mindspore/rewrite/parser_register.py +0 -1
- mindspore/rewrite/parsers/arguments_parser.py +3 -2
- mindspore/rewrite/parsers/assign_parser.py +267 -67
- mindspore/rewrite/parsers/attribute_parser.py +56 -0
- mindspore/rewrite/parsers/class_def_parser.py +191 -108
- mindspore/rewrite/parsers/constant_parser.py +101 -0
- mindspore/rewrite/parsers/container_parser.py +88 -0
- mindspore/rewrite/parsers/for_parser.py +28 -15
- mindspore/rewrite/parsers/function_def_parser.py +21 -5
- mindspore/rewrite/parsers/if_parser.py +11 -28
- mindspore/rewrite/parsers/module_parser.py +9 -6
- mindspore/rewrite/parsers/return_parser.py +3 -2
- mindspore/rewrite/sparsify/__init__.py +0 -0
- mindspore/rewrite/sparsify/sparse_transformer.py +448 -0
- mindspore/rewrite/sparsify/sparsify.py +109 -0
- mindspore/rewrite/sparsify/utils.py +173 -0
- mindspore/rewrite/symbol_tree.py +322 -109
- mindspore/rewrite/symbol_tree_builder.py +45 -8
- mindspore/rewrite/symbol_tree_dumper.py +0 -1
- mindspore/rewrite/topological_manager.py +1 -2
- mindspore/run_check/_check_version.py +209 -112
- mindspore/run_check/run_check.py +2 -1
- mindspore/scipy/linalg.py +13 -117
- mindspore/scipy/ops.py +5 -71
- mindspore/scipy/ops_grad.py +1 -25
- mindspore/scipy/ops_wrapper.py +1 -1
- mindspore/scipy/optimize/_bfgs.py +1 -1
- mindspore/scipy/optimize/_lagrange.py +200 -0
- mindspore/scipy/optimize/line_search.py +3 -2
- mindspore/scipy/optimize/minimize.py +43 -6
- mindspore/scipy/sparse/__init__.py +2 -2
- mindspore/scipy/sparse/linalg.py +5 -465
- mindspore/scipy/utils.py +2 -1
- mindspore/scipy/utils_const.py +7 -1
- mindspore/train/__init__.py +6 -4
- mindspore/train/_utils.py +28 -5
- mindspore/train/amp.py +321 -50
- mindspore/train/callback/__init__.py +3 -1
- mindspore/train/callback/_backup_and_restore.py +120 -0
- mindspore/train/callback/_callback.py +8 -8
- mindspore/train/callback/_checkpoint.py +12 -9
- mindspore/train/callback/_early_stop.py +13 -7
- mindspore/train/callback/_history.py +8 -8
- mindspore/train/callback/_lambda_callback.py +6 -6
- mindspore/train/callback/_landscape.py +36 -38
- mindspore/train/callback/_loss_monitor.py +12 -6
- mindspore/train/callback/_lr_scheduler_callback.py +2 -4
- mindspore/train/callback/_on_request_exit.py +212 -0
- mindspore/train/callback/_reduce_lr_on_plateau.py +13 -7
- mindspore/train/callback/_summary_collector.py +27 -19
- mindspore/train/callback/_time_monitor.py +13 -7
- mindspore/train/checkpoint_pb2.py +68 -8
- mindspore/train/data_sink.py +122 -33
- mindspore/train/dataset_helper.py +28 -87
- mindspore/train/loss_scale_manager.py +4 -7
- mindspore/{nn → train}/metrics/__init__.py +20 -20
- mindspore/{nn → train}/metrics/accuracy.py +12 -10
- mindspore/{nn → train}/metrics/auc.py +4 -4
- mindspore/{nn → train}/metrics/bleu_score.py +4 -4
- mindspore/{nn → train}/metrics/confusion_matrix.py +10 -8
- mindspore/{nn → train}/metrics/cosine_similarity.py +4 -4
- mindspore/{nn → train}/metrics/dice.py +6 -5
- mindspore/{nn → train}/metrics/error.py +7 -5
- mindspore/{nn → train}/metrics/fbeta.py +9 -7
- mindspore/{nn → train}/metrics/hausdorff_distance.py +8 -6
- mindspore/{nn → train}/metrics/loss.py +4 -3
- mindspore/{nn → train}/metrics/mean_surface_distance.py +6 -5
- mindspore/{nn → train}/metrics/metric.py +6 -5
- mindspore/{nn → train}/metrics/occlusion_sensitivity.py +4 -3
- mindspore/{nn → train}/metrics/perplexity.py +5 -4
- mindspore/{nn → train}/metrics/precision.py +5 -4
- mindspore/{nn → train}/metrics/recall.py +5 -4
- mindspore/{nn → train}/metrics/roc.py +7 -6
- mindspore/{nn → train}/metrics/root_mean_square_surface_distance.py +6 -5
- mindspore/{nn → train}/metrics/topk.py +7 -5
- mindspore/train/mind_ir_pb2.py +339 -32
- mindspore/train/model.py +113 -84
- mindspore/train/serialization.py +547 -167
- mindspore/train/summary/_summary_adapter.py +1 -1
- mindspore/train/summary/summary_record.py +43 -12
- mindspore/train/train_thor/convert_utils.py +7 -1
- mindspore/train/train_thor/dataset_helper.py +3 -3
- mindspore/train/train_thor/model_thor.py +0 -4
- mindspore/version.py +1 -1
- {mindspore-1.10.0.dist-info → mindspore-2.0.0rc1.dist-info}/METADATA +4 -3
- {mindspore-1.10.0.dist-info → mindspore-2.0.0rc1.dist-info}/RECORD +899 -675
- mindspore/compression/common/constant.py +0 -124
- mindspore/compression/export/__init__.py +0 -19
- mindspore/compression/export/quant_export.py +0 -514
- mindspore/compression/quant/qat.py +0 -636
- mindspore/compression/quant/quant_utils.py +0 -462
- mindspore/compression/quant/quantizer.py +0 -68
- mindspore/nn/layer/quant.py +0 -1868
- mindspore/nn/layer/rnn_utils.py +0 -90
- mindspore/nn/probability/dpn/__init__.py +0 -22
- mindspore/nn/probability/dpn/vae/__init__.py +0 -25
- mindspore/nn/probability/dpn/vae/cvae.py +0 -138
- mindspore/nn/probability/dpn/vae/vae.py +0 -122
- mindspore/nn/probability/infer/__init__.py +0 -22
- mindspore/nn/probability/infer/variational/elbo.py +0 -70
- mindspore/nn/probability/infer/variational/svi.py +0 -84
- mindspore/nn/probability/toolbox/__init__.py +0 -22
- mindspore/nn/probability/toolbox/anomaly_detection.py +0 -99
- mindspore/nn/probability/toolbox/uncertainty_evaluation.py +0 -363
- mindspore/nn/probability/transforms/__init__.py +0 -22
- mindspore/nn/probability/transforms/transform_bnn.py +0 -262
- mindspore/nn/probability/zhusuan/__init__.py +0 -18
- mindspore/nn/probability/zhusuan/framework/__init__.py +0 -18
- mindspore/nn/probability/zhusuan/framework/bn.py +0 -95
- mindspore/nn/probability/zhusuan/variational/__init__.py +0 -18
- mindspore/nn/probability/zhusuan/variational/elbo.py +0 -46
- mindspore/ops/_op_impl/tbe/bias_add_grad_ds.py +0 -52
- mindspore/ops/_op_impl/tbe/scatter_nd_add_ds.py +0 -43
- mindspore/ops/bprop_mindir/AssignAdd_bprop.mindir +0 -20
- mindspore/ops/bprop_mindir/Identity_bprop.mindir +0 -9
- mindspore/ops/bprop_mindir/LogicalOr_bprop.mindir +0 -20
- mindspore/ops/bprop_mindir/ReLU_bprop.mindir +0 -16
- mindspore/ops/bprop_mindir/UpdateState_bprop.mindir +0 -17
- mindspore/ops/bprop_mindir/stop_gradient_bprop.mindir +0 -12
- mindspore/ops/composite/array_ops.py +0 -210
- mindspore/ops/composite/clip_ops.py +0 -238
- mindspore/ops/composite/random_ops.py +0 -426
- mindspore/ops/composite/vmap_ops.py +0 -38
- mindspore/ops/operations/sponge_ops.py +0 -3531
- mindspore/ops/operations/sponge_update_ops.py +0 -2546
- mindspore/parallel/nn/__init__.py +0 -42
- mindspore/parallel/nn/loss.py +0 -22
- mindspore/parallel/nn/moe.py +0 -21
- mindspore/parallel/nn/op_parallel_config.py +0 -22
- mindspore/parallel/nn/transformer.py +0 -31
- mindspore/run_check/_check_deps_version.py +0 -84
- {mindspore-1.10.0.dist-info → mindspore-2.0.0rc1.dist-info}/WHEEL +0 -0
- {mindspore-1.10.0.dist-info → mindspore-2.0.0rc1.dist-info}/entry_points.txt +0 -0
- {mindspore-1.10.0.dist-info → mindspore-2.0.0rc1.dist-info}/top_level.txt +0 -0
mindspore/nn/loss/loss.py
CHANGED
|
@@ -13,24 +13,25 @@
|
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
# ============================================================================
|
|
15
15
|
"""loss"""
|
|
16
|
-
from __future__ import absolute_import
|
|
16
|
+
from __future__ import absolute_import, division
|
|
17
|
+
import math
|
|
17
18
|
|
|
18
19
|
import mindspore
|
|
19
20
|
import mindspore.common.dtype as mstype
|
|
21
|
+
import mindspore.ops as ops
|
|
20
22
|
from mindspore import log
|
|
21
23
|
from mindspore.common.tensor import Tensor
|
|
22
24
|
from mindspore.common.parameter import Parameter
|
|
23
25
|
from mindspore.ops import operations as P
|
|
26
|
+
from mindspore.ops.operations import _inner_ops as inner
|
|
24
27
|
from mindspore.ops.operations.nn_ops import MultiMarginLoss as MultiMarginLossOp
|
|
25
28
|
from mindspore.ops.operations.nn_ops import MultilabelMarginLoss as MultilabelMarginLossOp
|
|
26
|
-
from mindspore.ops.operations.nn_ops import TripletMarginLoss as TripletMarginLossOp
|
|
27
29
|
from mindspore.ops import functional as F
|
|
28
30
|
from mindspore import nn
|
|
29
|
-
from mindspore.ops.primitive import constexpr
|
|
31
|
+
from mindspore.ops.primitive import constexpr, _primexpr
|
|
30
32
|
from mindspore.nn.cell import Cell
|
|
31
33
|
from mindspore.nn.layer.activation import get_activation
|
|
32
|
-
from mindspore
|
|
33
|
-
from mindspore._checkparam import Rel
|
|
34
|
+
from mindspore import _checkparam as validator
|
|
34
35
|
from mindspore import context
|
|
35
36
|
|
|
36
37
|
|
|
@@ -245,13 +246,10 @@ class L1Loss(LossBase):
|
|
|
245
246
|
def __init__(self, reduction='mean'):
|
|
246
247
|
"""Initialize L1Loss."""
|
|
247
248
|
super(L1Loss, self).__init__(reduction)
|
|
248
|
-
self.
|
|
249
|
+
self.reduction = reduction
|
|
249
250
|
|
|
250
251
|
def construct(self, logits, labels):
|
|
251
|
-
|
|
252
|
-
_check_is_tensor('labels', labels, self.cls_name)
|
|
253
|
-
x = self.abs(logits - labels)
|
|
254
|
-
return self.get_loss(x)
|
|
252
|
+
return F.l1_loss(logits, labels, self.reduction)
|
|
255
253
|
|
|
256
254
|
|
|
257
255
|
class MSELoss(LossBase):
|
|
@@ -457,6 +455,79 @@ class MAELoss(LossBase):
|
|
|
457
455
|
return self.get_loss(x)
|
|
458
456
|
|
|
459
457
|
|
|
458
|
+
class MarginRankingLoss(LossBase):
|
|
459
|
+
r"""
|
|
460
|
+
MarginRankingLoss creates a criterion that measures the loss.
|
|
461
|
+
|
|
462
|
+
Given two tensors :math:`input1`, :math:`input2` and a Tensor label :math:`target` with values 1 or -1,
|
|
463
|
+
the operation is as follows:
|
|
464
|
+
|
|
465
|
+
.. math::
|
|
466
|
+
\text{loss}(input1, input2, target) = \max(0, -target * (input1 - input2) + \text{margin})
|
|
467
|
+
|
|
468
|
+
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".
|
|
472
|
+
|
|
473
|
+
Inputs:
|
|
474
|
+
- **input1** (Tensor) - Tensor of shape :math:`(N, *)` where :math:`*` means, any number
|
|
475
|
+
of additional dimensions.
|
|
476
|
+
- **input2** (Tensor) - Tensor of shape :math:`(N, *)`, same shape and dtype as `input1`.
|
|
477
|
+
- **target** (Tensor) - Contains value 1 or -1. Suppose the shape of `input1` is
|
|
478
|
+
: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
|
+
|
|
480
|
+
Outputs:
|
|
481
|
+
Tensor or Scalar. if `reduction` is "none", its shape is the same as `labels`.
|
|
482
|
+
Otherwise, a scalar value will be returned.
|
|
483
|
+
|
|
484
|
+
Raises:
|
|
485
|
+
TypeError: If `margin` is not a float.
|
|
486
|
+
TypeError: If `input1`, `input2` or `target` is not a Tensor.
|
|
487
|
+
TypeError: If the types of `input1` and `input2` are inconsistent.
|
|
488
|
+
TypeError: If the types of `input1` and `target` are inconsistent.
|
|
489
|
+
ValueError: If the shape of `input1` and `input2` are inconsistent.
|
|
490
|
+
ValueError: If the shape of `input1` and `target` are inconsistent.
|
|
491
|
+
ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
|
|
492
|
+
|
|
493
|
+
Supported Platforms:
|
|
494
|
+
``Ascend`` ``GPU`` ``CPU``
|
|
495
|
+
|
|
496
|
+
Examples:
|
|
497
|
+
>>> import mindspore as ms
|
|
498
|
+
>>> import mindspore.nn as nn
|
|
499
|
+
>>> import mindspore.ops as ops
|
|
500
|
+
>>> from mindspore.ops import Tensor
|
|
501
|
+
>>> import numpy as np
|
|
502
|
+
>>> loss1 = nn.MarginRankingLoss(reduction='none')
|
|
503
|
+
>>> loss2 = nn.MarginRankingLoss(reduction='mean')
|
|
504
|
+
>>> loss3 = nn.MarginRankingLoss(reduction='sum')
|
|
505
|
+
>>> sign = ops.Sign()
|
|
506
|
+
>>> input1 = Tensor(np.array([0.3864, -2.4093, -1.4076]), ms.float32)
|
|
507
|
+
>>> input2 = Tensor(np.array([-0.6012, -1.6681, 1.2928]), ms.float32)
|
|
508
|
+
>>> target = sign(Tensor(np.array([-2, -2, 3]), ms.float32))
|
|
509
|
+
>>> output1 = loss1(input1, input2, target)
|
|
510
|
+
>>> print(output1)
|
|
511
|
+
[0.98759997 0. 2.7003999 ]
|
|
512
|
+
>>> output2 = loss2(input1, input2, target)
|
|
513
|
+
>>> print(output2)
|
|
514
|
+
1.2293333
|
|
515
|
+
>>> output3 = loss3(input1, input2, target)
|
|
516
|
+
>>> print(output3)
|
|
517
|
+
3.6879997
|
|
518
|
+
"""
|
|
519
|
+
|
|
520
|
+
def __init__(self, margin=0.0, reduction='mean'):
|
|
521
|
+
"""Initialize MarginRankingLoss."""
|
|
522
|
+
super(MarginRankingLoss, self).__init__(reduction)
|
|
523
|
+
self.reduction = reduction
|
|
524
|
+
self.margin = margin
|
|
525
|
+
|
|
526
|
+
def construct(self, input1, input2, target):
|
|
527
|
+
x = ops.margin_ranking_loss(input1, input2, target, self.margin, self.reduction)
|
|
528
|
+
return x
|
|
529
|
+
|
|
530
|
+
|
|
460
531
|
class SmoothL1Loss(LossBase):
|
|
461
532
|
r"""
|
|
462
533
|
SmoothL1 loss function, if the absolute error element-wise between the predicted value and the target value
|
|
@@ -502,7 +573,7 @@ class SmoothL1Loss(LossBase):
|
|
|
502
573
|
|
|
503
574
|
Outputs:
|
|
504
575
|
Tensor, if `reduction` is 'none', then output is a tensor with the same shape as `logits`.
|
|
505
|
-
Otherwise the shape of output tensor is `(
|
|
576
|
+
Otherwise the shape of output tensor is `()`.
|
|
506
577
|
|
|
507
578
|
Raises:
|
|
508
579
|
TypeError: If `beta` is not a float.
|
|
@@ -568,7 +639,7 @@ class SoftMarginLoss(LossBase):
|
|
|
568
639
|
ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
|
|
569
640
|
|
|
570
641
|
Supported Platforms:
|
|
571
|
-
``Ascend``
|
|
642
|
+
``Ascend`` ``GPU``
|
|
572
643
|
|
|
573
644
|
Examples:
|
|
574
645
|
>>> loss = nn.SoftMarginLoss()
|
|
@@ -594,11 +665,11 @@ class SoftmaxCrossEntropyWithLogits(LossBase):
|
|
|
594
665
|
Measures the distribution error between the probabilities of the input (computed with softmax function) and the
|
|
595
666
|
labels where the classes are mutually exclusive (only one class is positive) using cross entropy loss.
|
|
596
667
|
|
|
597
|
-
Typical input into this function is unnormalized scores denoted as x whose shape is (N, C),
|
|
668
|
+
Typical input into this function is unnormalized scores denoted as x whose shape is :math:`(N, C)` ,
|
|
598
669
|
and the corresponding targets.
|
|
599
670
|
|
|
600
671
|
Typically, the input to this function is the fractional value of each category and the corresponding target value,
|
|
601
|
-
and the input format is (N, C).
|
|
672
|
+
and the input format is :math:`(N, C)` .
|
|
602
673
|
|
|
603
674
|
For each instance :math:`x_i`, i ranges from 0 to N-1, the loss is given as:
|
|
604
675
|
|
|
@@ -619,8 +690,8 @@ class SoftmaxCrossEntropyWithLogits(LossBase):
|
|
|
619
690
|
If "none", do not perform reduction. Default: "none".
|
|
620
691
|
|
|
621
692
|
Inputs:
|
|
622
|
-
- **logits** (Tensor) - Tensor of shape (N, C). Data type must be float16 or float32.
|
|
623
|
-
- **labels** (Tensor) - Tensor of shape (N, ). If `sparse` is True, The type of
|
|
693
|
+
- **logits** (Tensor) - Tensor of shape :math:`(N, C)` . Data type must be float16 or float32.
|
|
694
|
+
- **labels** (Tensor) - Tensor of shape :math:`(N, )` . If `sparse` is True, The type of
|
|
624
695
|
`labels` is int32 or int64. Otherwise, the type of `labels` is the same as the type of `logits`.
|
|
625
696
|
|
|
626
697
|
Outputs:
|
|
@@ -703,9 +774,8 @@ class DiceLoss(LossBase):
|
|
|
703
774
|
Default: 1e-5.
|
|
704
775
|
|
|
705
776
|
Inputs:
|
|
706
|
-
- **logits** (Tensor) -
|
|
707
|
-
|
|
708
|
-
- **labels** (Tensor) - Tensor of shape :math:`(N, *)`, same shape as the `logits`.
|
|
777
|
+
- **logits** (Tensor) - Input predicted value. The data type must be float16 or float32.
|
|
778
|
+
- **labels** (Tensor) - Input target value. Same shape as the `logits`.
|
|
709
779
|
The data type must be float16 or float32.
|
|
710
780
|
|
|
711
781
|
Outputs:
|
|
@@ -751,13 +821,13 @@ class DiceLoss(LossBase):
|
|
|
751
821
|
return dice_loss
|
|
752
822
|
|
|
753
823
|
|
|
754
|
-
@
|
|
824
|
+
@_primexpr
|
|
755
825
|
def _check_shape(logits_shape, label_shape, prim_name=None):
|
|
756
826
|
"""Internal function, used to check whether the shape of logits and labels meets the requirements."""
|
|
757
827
|
validator.check('logits_shape', logits_shape, 'label_shape', label_shape, prim_name=prim_name)
|
|
758
828
|
|
|
759
829
|
|
|
760
|
-
@
|
|
830
|
+
@_primexpr
|
|
761
831
|
def _check_ndim_multi(logits_dim, label_dim, prim_name=None):
|
|
762
832
|
"""Internal function, used to check whether the dimension of logits and label meets the requirements."""
|
|
763
833
|
msg_prefix = f'For \'{prim_name}\', the' if prim_name else "The"
|
|
@@ -767,7 +837,7 @@ def _check_ndim_multi(logits_dim, label_dim, prim_name=None):
|
|
|
767
837
|
raise ValueError(f"{msg_prefix} 'labels' dimension must be greater than 1, but got {label_dim}.")
|
|
768
838
|
|
|
769
839
|
|
|
770
|
-
@
|
|
840
|
+
@_primexpr
|
|
771
841
|
def _check_weights(weight_shape, label_shape, prim_name=None):
|
|
772
842
|
"""Internal function, used to check whether the reduced shape meets the requirements."""
|
|
773
843
|
msg_prefix = f'For \'{prim_name}\', the' if prim_name else "The"
|
|
@@ -994,7 +1064,7 @@ class SampledSoftmaxLoss(LossBase):
|
|
|
994
1064
|
def _softmax_cross_entropy(self, logits, targets):
|
|
995
1065
|
stable_exp_logits = self.exp(logits - self.reduce_max_true(logits, 1))
|
|
996
1066
|
pred = stable_exp_logits / self.reduce_sum_true(stable_exp_logits, 1)
|
|
997
|
-
return -self.reduce_sum(targets * self.log(pred + 1.0e-20), 1)
|
|
1067
|
+
return -1 * self.reduce_sum(targets * self.log(pred + 1.0e-20), 1)
|
|
998
1068
|
|
|
999
1069
|
def _compute_sampled_logits(self, weights,
|
|
1000
1070
|
biases,
|
|
@@ -1089,6 +1159,156 @@ class SampledSoftmaxLoss(LossBase):
|
|
|
1089
1159
|
return out_logits, out_labels
|
|
1090
1160
|
|
|
1091
1161
|
|
|
1162
|
+
class PoissonNLLLoss(LossBase):
|
|
1163
|
+
r"""
|
|
1164
|
+
Poisson negative log likelihood loss.
|
|
1165
|
+
|
|
1166
|
+
The loss is:
|
|
1167
|
+
|
|
1168
|
+
.. math::
|
|
1169
|
+
\mathcal{L}_{D} = \sum_{i = 0}^{|D|}\left( x_{i} - y_{i}\ln x_{i} + \ln{y_{i}!} \right)
|
|
1170
|
+
|
|
1171
|
+
where :math:`\mathcal{L}_{D}` is the loss, :math:`y_{i}` is the `target`,
|
|
1172
|
+
:math:`x_{i}` is the `input`.
|
|
1173
|
+
|
|
1174
|
+
If `log_input` is True, use :math:`e^{x_{i}} - y_{i} x_{i}` instead of :math:`x_{i} - y_{i}\ln x_{i}`.
|
|
1175
|
+
When calculating logarithms, the lower bound of `input` is set to `eps` to avoid numerical errors.
|
|
1176
|
+
|
|
1177
|
+
If `full` is False, the last term :math:`\ln{y_{i}!}` will be omitted,
|
|
1178
|
+
otherwise the last term will be approximated using Stirling formula:
|
|
1179
|
+
|
|
1180
|
+
.. math::
|
|
1181
|
+
n! \approx \sqrt{2\pi n}\left( \frac{n}{e} \right)^{n}
|
|
1182
|
+
|
|
1183
|
+
Note:
|
|
1184
|
+
Calculating the logarithm of a negative number or the exponent of a large positive number under Ascend
|
|
1185
|
+
will have a different range of return values and results different from those under GPU and CPU.
|
|
1186
|
+
|
|
1187
|
+
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'.
|
|
1193
|
+
|
|
1194
|
+
Inputs:
|
|
1195
|
+
- **input** (Tensor) - The input Tensor. The shape can be any number of dimensions.
|
|
1196
|
+
- **target** (Tensor) - The label Tensor which has the same shape as `input`.
|
|
1197
|
+
|
|
1198
|
+
Outputs:
|
|
1199
|
+
Tensor or Scalar, if `reduction` is 'none', then output is a tensor and has the same shape as `input`.
|
|
1200
|
+
Otherwise it is a scalar.
|
|
1201
|
+
|
|
1202
|
+
Raises:
|
|
1203
|
+
TypeError: If `reduction` is not a str.
|
|
1204
|
+
TypeError: If neither `input` nor `target` is a tensor.
|
|
1205
|
+
TypeError: If dtype of `input` or `target` is not currently supported.
|
|
1206
|
+
|
|
1207
|
+
Supported Platforms:
|
|
1208
|
+
``Ascend`` ``GPU`` ``CPU``
|
|
1209
|
+
|
|
1210
|
+
Examples:
|
|
1211
|
+
>>> x = Tensor([[0.3, 0.7], [0.5, 0.5]])
|
|
1212
|
+
>>> target = Tensor([[1.0, 2.0], [3.0, 4.0]])
|
|
1213
|
+
>>> loss = nn.PoissonNLLLoss()
|
|
1214
|
+
>>> output = loss(x, target)
|
|
1215
|
+
>>> print(output.asnumpy())
|
|
1216
|
+
0.3652635
|
|
1217
|
+
"""
|
|
1218
|
+
|
|
1219
|
+
def __init__(self, log_input=True, full=False, eps=1e-08, reduction="mean"):
|
|
1220
|
+
"""Initialize PoissonNLLLoss."""
|
|
1221
|
+
super(PoissonNLLLoss, self).__init__(reduction=reduction)
|
|
1222
|
+
self.log_input = log_input
|
|
1223
|
+
self.full = full
|
|
1224
|
+
self.eps = eps
|
|
1225
|
+
self.maximum = P.Maximum()
|
|
1226
|
+
self.cast = P.Cast()
|
|
1227
|
+
|
|
1228
|
+
def construct(self, input, target):
|
|
1229
|
+
_check_is_tensor('input', input, self.cls_name)
|
|
1230
|
+
_check_is_tensor('target', target, self.cls_name)
|
|
1231
|
+
if input.ndim == 0 or target.ndim == 0:
|
|
1232
|
+
raise ValueError(
|
|
1233
|
+
"For 'PoissonNLLLoss', the inputs must be non-scalar, but got shapes: "
|
|
1234
|
+
f"input: {input.shape}, target: {target.shape}"
|
|
1235
|
+
)
|
|
1236
|
+
target = self.cast(target, input.dtype)
|
|
1237
|
+
if self.log_input:
|
|
1238
|
+
loss = input.exp() - target * input
|
|
1239
|
+
else:
|
|
1240
|
+
loss = input - target * ((input + self.eps).log())
|
|
1241
|
+
if self.full:
|
|
1242
|
+
target = self.maximum(target, self.eps)
|
|
1243
|
+
stirling_term = (target > 1) * ((target + 0.5) * target.log() - target + get_half_ln_2_pi())
|
|
1244
|
+
loss += F.masked_fill(stirling_term, target <= 1, 0)
|
|
1245
|
+
out = self.get_loss(loss)
|
|
1246
|
+
return out
|
|
1247
|
+
|
|
1248
|
+
|
|
1249
|
+
@constexpr
|
|
1250
|
+
def get_half_ln_2_pi():
|
|
1251
|
+
return 0.5 * math.log(2 * math.pi)
|
|
1252
|
+
|
|
1253
|
+
|
|
1254
|
+
class MultiLabelSoftMarginLoss(LossBase):
|
|
1255
|
+
r"""
|
|
1256
|
+
Calculates the MultiLabelSoftMarginLoss.
|
|
1257
|
+
The multi-label soft margin loss is a commonly used loss function in multi-label classification tasks
|
|
1258
|
+
where an input sample can belong to multiple classes.
|
|
1259
|
+
Given an input :math:`x` and binary labels :math:`y` of size :math:`(N,C)`, where :math:`N` denotes
|
|
1260
|
+
the number of samples and :math:`C` denotes the number of classes.
|
|
1261
|
+
|
|
1262
|
+
.. math::
|
|
1263
|
+
\mathcal{loss\left( x , y \right)} = - \frac{1}{N}\frac{1}{C}\sum_{i = 1}^{N}
|
|
1264
|
+
\sum_{j = 1}^{C}\left(y_{ij}\log\frac{1}{1 + e^{- x_{ij}}} + \left( 1 - y_{ij}
|
|
1265
|
+
\right)\log\frac{e^{-x_{ij}}}{1 + e^{-x_{ij}}} \right)
|
|
1266
|
+
|
|
1267
|
+
where :math:`x{ij}` represents the predicted score of sample :math:`i` for class :math:`j`. :math:`y{ij}`
|
|
1268
|
+
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`.
|
|
1270
|
+
For a multi-label classification task, each sample may have multiple labels with a value of 1 in the binary
|
|
1271
|
+
label :math:`y`. `weight` will multiply to the loss of each class if given.
|
|
1272
|
+
|
|
1273
|
+
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'.
|
|
1278
|
+
|
|
1279
|
+
Inputs:
|
|
1280
|
+
- **x** (Tensor) - A tensor of shape (N, C), where N is batch size and C is number
|
|
1281
|
+
of classes.
|
|
1282
|
+
- **target** (Tensor) - The label target Tensor which has the same shape as `x`.
|
|
1283
|
+
|
|
1284
|
+
Outputs:
|
|
1285
|
+
Tensor, the data type is the same as x, if the reduction is 'none', its shape is (N), otherwise it is zero.
|
|
1286
|
+
|
|
1287
|
+
Raises:
|
|
1288
|
+
ValueError: If the rank of `x` or `target` is not 2.
|
|
1289
|
+
|
|
1290
|
+
Supported Platforms:
|
|
1291
|
+
``Ascend`` ``GPU`` ``CPU``
|
|
1292
|
+
|
|
1293
|
+
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]])
|
|
1296
|
+
>>> loss = nn.MultiLabelSoftMarginLoss(reduction='mean')
|
|
1297
|
+
>>> out = loss(x, target)
|
|
1298
|
+
>>> print(out.asnumpy())
|
|
1299
|
+
0.84693956
|
|
1300
|
+
"""
|
|
1301
|
+
|
|
1302
|
+
def __init__(self, weight=None, reduction="mean"):
|
|
1303
|
+
"""Initialize MultiLabelSoftMarginLoss."""
|
|
1304
|
+
super(MultiLabelSoftMarginLoss, self).__init__(reduction)
|
|
1305
|
+
self.weight = weight
|
|
1306
|
+
self.reduction = reduction
|
|
1307
|
+
|
|
1308
|
+
def construct(self, x, target):
|
|
1309
|
+
return F.multilabel_soft_margin_loss(x, target, self.weight, self.reduction)
|
|
1310
|
+
|
|
1311
|
+
|
|
1092
1312
|
class MultiMarginLoss(LossBase):
|
|
1093
1313
|
r"""
|
|
1094
1314
|
Creates a criterion that optimizes a multi-class classification hinge
|
|
@@ -1100,25 +1320,28 @@ class MultiMarginLoss(LossBase):
|
|
|
1100
1320
|
output :math:`y` is:
|
|
1101
1321
|
|
|
1102
1322
|
.. math::
|
|
1103
|
-
\text{loss}(x, y) = \frac{\sum_i \max(0, w[y] * (\text{margin} - x[y] + x[i]))^p
|
|
1323
|
+
\text{loss}(x, y) = \frac{\sum_i \max(0, w[y] * (\text{margin} - x[y] + x[i]))^p}{\text{x.size}(0)}
|
|
1104
1324
|
|
|
1105
1325
|
where :math:`x \in \left\{0, \; \cdots , \; \text{x.size}(0) - 1\right\}`
|
|
1106
1326
|
and :math:`i \neq y`.
|
|
1107
1327
|
|
|
1108
|
-
Optionally, you can give non-equal weighting on the classes by passing
|
|
1109
|
-
a 1D input `weight` tensor w into the constructor.
|
|
1110
|
-
|
|
1111
1328
|
Args:
|
|
1112
|
-
p (int):
|
|
1113
|
-
margin (float):
|
|
1114
|
-
reduction (str): Apply specific reduction method to the output: 'none', 'mean', 'sum'.
|
|
1329
|
+
p (int, optional): The norm degree for pairwise distance. Should be 1 or 2. Default: 1.
|
|
1330
|
+
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'.
|
|
1333
|
+
|
|
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.
|
|
1337
|
+
|
|
1338
|
+
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.
|
|
1115
1340
|
|
|
1116
1341
|
Inputs:
|
|
1117
1342
|
- **x** (Tensor) - Input x, with shape :math:`(N, C)`. Data type only support float32, float16 or float64.
|
|
1118
1343
|
- **target** (Tensor) - Ground truth labels, with shape :math:`(N,)`. Data type only support int64. The
|
|
1119
1344
|
value of target should be non-negative, less than C.
|
|
1120
|
-
- **weight** (Tensor, optional) - The rescaling weight to each class with shape :math:`(C,)`. Data type only
|
|
1121
|
-
support float32, float16 or float64. Default: None.
|
|
1122
1345
|
|
|
1123
1346
|
Outputs:
|
|
1124
1347
|
Tensor, When `reduction` is 'none', the shape is :math:`(N,)`.
|
|
@@ -1138,7 +1361,7 @@ class MultiMarginLoss(LossBase):
|
|
|
1138
1361
|
ValueError: If rank of `x` is not 2 or rank of 'target' is not 1.
|
|
1139
1362
|
|
|
1140
1363
|
Supported Platforms:
|
|
1141
|
-
``Ascend``
|
|
1364
|
+
``Ascend`` ``GPU`` ``CPU``
|
|
1142
1365
|
|
|
1143
1366
|
Examples:
|
|
1144
1367
|
>>> x = Tensor(np.ones(shape=[3, 3]), mindspore.float32)
|
|
@@ -1149,20 +1372,23 @@ class MultiMarginLoss(LossBase):
|
|
|
1149
1372
|
0.6666667
|
|
1150
1373
|
"""
|
|
1151
1374
|
|
|
1152
|
-
def __init__(self, p=1, margin=1.0, reduction='mean'):
|
|
1375
|
+
def __init__(self, p=1, margin=1.0, reduction='mean', weight=None):
|
|
1153
1376
|
"""Initialize MultiMarginLoss."""
|
|
1154
1377
|
super(MultiMarginLoss, self).__init__()
|
|
1155
1378
|
self.multi_margin_loss = MultiMarginLossOp(p=p, margin=margin, reduction=reduction)
|
|
1156
|
-
self.
|
|
1379
|
+
self.generate_ones = ops.Fill()
|
|
1380
|
+
self.weight = weight
|
|
1157
1381
|
|
|
1158
1382
|
def construct(self, x, target, weight=None):
|
|
1159
1383
|
_check_is_tensor('x', x, self.cls_name)
|
|
1160
1384
|
_check_is_tensor('target', target, self.cls_name)
|
|
1385
|
+
if self.weight is not None:
|
|
1386
|
+
weight = self.weight
|
|
1161
1387
|
weight_one = weight is None
|
|
1162
1388
|
if not weight_one:
|
|
1163
1389
|
_check_is_tensor('weight', weight, self.cls_name)
|
|
1164
1390
|
else:
|
|
1165
|
-
weight = self.
|
|
1391
|
+
weight = self.generate_ones(x.dtype, x.astype('float32')[0].shape, 1)
|
|
1166
1392
|
loss = self.multi_margin_loss(x, target, weight)
|
|
1167
1393
|
return loss
|
|
1168
1394
|
|
|
@@ -1197,12 +1423,13 @@ class BCELoss(LossBase):
|
|
|
1197
1423
|
weight (Tensor, optional): A rescaling weight applied to the loss of each batch element.
|
|
1198
1424
|
And it must have the same shape and data type as `inputs`. Default: None
|
|
1199
1425
|
reduction (str): Specifies the reduction to be applied to the output.
|
|
1200
|
-
Its value must be one of 'none', 'mean', 'sum'. Default: '
|
|
1426
|
+
Its value must be one of 'none', 'mean', 'sum'. Default: 'mean'.
|
|
1201
1427
|
|
|
1202
1428
|
Inputs:
|
|
1203
1429
|
- **logits** (Tensor) - The input tensor with shape :math:`(N, *)` where :math:`*` means, any number
|
|
1204
1430
|
of additional dimensions. The data type must be float16 or float32.
|
|
1205
|
-
- **labels** (Tensor) - The label tensor with shape :math:`(N, *)
|
|
1431
|
+
- **labels** (Tensor) - The label tensor with shape :math:`(N, *)` where :math:`*` means, any number
|
|
1432
|
+
of additional dimensions. The same shape and data type as `logits`.
|
|
1206
1433
|
|
|
1207
1434
|
Outputs:
|
|
1208
1435
|
Tensor, has the same dtype as `logits`. if `reduction` is 'none', then it has the same shape as `logits`.
|
|
@@ -1226,9 +1453,9 @@ class BCELoss(LossBase):
|
|
|
1226
1453
|
1.8952923
|
|
1227
1454
|
"""
|
|
1228
1455
|
|
|
1229
|
-
def __init__(self, weight=None, reduction='
|
|
1456
|
+
def __init__(self, weight=None, reduction='mean'):
|
|
1230
1457
|
"""Initialize BCELoss."""
|
|
1231
|
-
super(BCELoss, self).__init__()
|
|
1458
|
+
super(BCELoss, self).__init__(reduction)
|
|
1232
1459
|
self.binary_cross_entropy = P.BinaryCrossEntropy(reduction=reduction)
|
|
1233
1460
|
self.weight_one = weight is None
|
|
1234
1461
|
if not self.weight_one:
|
|
@@ -1247,7 +1474,7 @@ class BCELoss(LossBase):
|
|
|
1247
1474
|
return loss
|
|
1248
1475
|
|
|
1249
1476
|
|
|
1250
|
-
@
|
|
1477
|
+
@_primexpr
|
|
1251
1478
|
def _check_reduced_shape_valid(ori_shape, reduced_shape, axis, cls_name, arg_name1, arg_name2):
|
|
1252
1479
|
"""Internal function, used to check whether the reduced shape meets the requirements."""
|
|
1253
1480
|
validator.check_reduce_shape(ori_shape, reduced_shape, axis, cls_name, arg_name1, arg_name2)
|
|
@@ -1305,13 +1532,13 @@ class CosineEmbeddingLoss(LossBase):
|
|
|
1305
1532
|
self.reduce_sum = P.ReduceSum()
|
|
1306
1533
|
self.maximum = P.Maximum()
|
|
1307
1534
|
validator.check_value_type("margin", margin, [float], self.cls_name)
|
|
1308
|
-
self.margin = validator.check_float_range(margin, -1.0, 1.0,
|
|
1535
|
+
self.margin = validator.check_float_range(margin, -1.0, 1.0, validator.INC_BOTH, "margin", self.cls_name)
|
|
1309
1536
|
|
|
1310
1537
|
def construct(self, logits_x1, logits_x2, labels):
|
|
1311
1538
|
_check_is_tensor('logits_x1', logits_x1, self.cls_name)
|
|
1312
1539
|
_check_is_tensor('logits_x2', logits_x2, self.cls_name)
|
|
1313
1540
|
_check_is_tensor('labels', labels, self.cls_name)
|
|
1314
|
-
|
|
1541
|
+
inner.same_type_shape_(logits_x1, logits_x2)
|
|
1315
1542
|
_check_reduced_shape_valid(F.shape(logits_x1), F.shape(labels), (1,), self.cls_name, "logits_x1", "labels")
|
|
1316
1543
|
# if labels > 0, 1-cosine(logits_x1, logits_x2)
|
|
1317
1544
|
# else, max(0, cosine(logits_x1, logits_x2)-margin)
|
|
@@ -1333,12 +1560,12 @@ class CosineEmbeddingLoss(LossBase):
|
|
|
1333
1560
|
|
|
1334
1561
|
class MultilabelMarginLoss(LossBase):
|
|
1335
1562
|
r"""
|
|
1336
|
-
|
|
1563
|
+
Creates a loss criterion that minimizes the hinge loss for multi-class
|
|
1564
|
+
classification tasks.
|
|
1565
|
+
It takes a 2D mini-batch Tensor :math:`x` as input and a 2D
|
|
1566
|
+
Tensor :math:`y` containing target class indices as output.
|
|
1337
1567
|
|
|
1338
|
-
|
|
1339
|
-
hinge loss (margin-based loss) between input :math:`x` (a 2D mini-batch `Tensor`)
|
|
1340
|
-
and output :math:`y` (which is a 2D `Tensor` of target class indices).
|
|
1341
|
-
For each sample in the mini-batch:
|
|
1568
|
+
Each sample in the mini-batch, the loss is computed as follows:
|
|
1342
1569
|
|
|
1343
1570
|
.. math::
|
|
1344
1571
|
\text{loss}(x, y) = \sum_{ij}\frac{\max(0, 1 - (x[y[j]] - x[i]))}{\text{x.size}(0)}
|
|
@@ -1346,17 +1573,18 @@ class MultilabelMarginLoss(LossBase):
|
|
|
1346
1573
|
where :math:`x \in \left\{0, \; \cdots , \; \text{x.size}(0) - 1\right\}`, \
|
|
1347
1574
|
:math:`y \in \left\{0, \; \cdots , \; \text{y.size}(0) - 1\right\}`, \
|
|
1348
1575
|
:math:`0 \leq y[j] \leq \text{x.size}(0)-1`, \
|
|
1349
|
-
and :math:`i
|
|
1576
|
+
and for all :math:`i` and :math:`j`, :math:`i` does not equal to :math:`y[j]`.
|
|
1350
1577
|
|
|
1351
|
-
:math:`y` and :math:`x`
|
|
1578
|
+
Furthermore, both :math:`y` and :math:`x` should have identical sizes.
|
|
1352
1579
|
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1580
|
+
Note:
|
|
1581
|
+
For this operator, only a contiguous sequence of non-negative targets that starts at
|
|
1582
|
+
the beginning is taken into consideration, which means that different samples can have different
|
|
1583
|
+
number of target classes.
|
|
1357
1584
|
|
|
1358
1585
|
Args:
|
|
1359
|
-
reduction (str): Apply specific reduction method to the output: 'none', 'mean', 'sum'.
|
|
1586
|
+
reduction (str, optional): Apply specific reduction method to the output: 'none', 'mean', 'sum'.
|
|
1587
|
+
Default: "mean".
|
|
1360
1588
|
|
|
1361
1589
|
Inputs:
|
|
1362
1590
|
- **x** (Tensor) - Predict data. Tensor of shape :math:`(C)` or :math:`(N, C)`, where :math:`N`
|
|
@@ -1367,8 +1595,6 @@ class MultilabelMarginLoss(LossBase):
|
|
|
1367
1595
|
Outputs:
|
|
1368
1596
|
- **y** (Union[Tensor, Scalar]) - The loss of MultilabelMarginLoss. If `reduction` is "none", its shape
|
|
1369
1597
|
is :math:`(N)`. Otherwise, a scalar value will be returned.
|
|
1370
|
-
- **is_target** (Tensor) - Output tensor for backward input, with the same shape as `target`,
|
|
1371
|
-
data type must be int32.
|
|
1372
1598
|
|
|
1373
1599
|
Raises:
|
|
1374
1600
|
TypeError: If `x` or `target` is not a Tensor.
|
|
@@ -1379,7 +1605,7 @@ class MultilabelMarginLoss(LossBase):
|
|
|
1379
1605
|
ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
|
|
1380
1606
|
|
|
1381
1607
|
Supported Platforms:
|
|
1382
|
-
``Ascend``
|
|
1608
|
+
``Ascend`` ``GPU``
|
|
1383
1609
|
|
|
1384
1610
|
Examples:
|
|
1385
1611
|
>>> loss = nn.MultilabelMarginLoss()
|
|
@@ -1387,8 +1613,7 @@ class MultilabelMarginLoss(LossBase):
|
|
|
1387
1613
|
>>> target = Tensor(np.array([[1, 2, 0, 3], [2, 3, -1, 1]]), mindspore.int32)
|
|
1388
1614
|
>>> output = loss(x, target)
|
|
1389
1615
|
>>> print(output)
|
|
1390
|
-
|
|
1391
|
-
[[1, 1, 1, 1], [0, 0, 1, 1]]))
|
|
1616
|
+
Tensor(shape=[], dtype=Float32, value=0.325)
|
|
1392
1617
|
"""
|
|
1393
1618
|
|
|
1394
1619
|
def __init__(self, reduction='mean'):
|
|
@@ -1396,7 +1621,8 @@ class MultilabelMarginLoss(LossBase):
|
|
|
1396
1621
|
self.multilabel_margin_loss = MultilabelMarginLossOp(reduction=reduction)
|
|
1397
1622
|
|
|
1398
1623
|
def construct(self, x, target):
|
|
1399
|
-
|
|
1624
|
+
loss, _ = self.multilabel_margin_loss(x, target)
|
|
1625
|
+
return loss
|
|
1400
1626
|
|
|
1401
1627
|
|
|
1402
1628
|
class BCEWithLogitsLoss(LossBase):
|
|
@@ -1434,7 +1660,8 @@ class BCEWithLogitsLoss(LossBase):
|
|
|
1434
1660
|
Inputs:
|
|
1435
1661
|
- **logits** (Tensor) - Input logits with shape :math:`(N, *)` where :math:`*` means, any number
|
|
1436
1662
|
of additional dimensions. The data type must be float16 or float32.
|
|
1437
|
-
- **labels** (Tensor) - Ground truth label with shape :math:`(N, *)
|
|
1663
|
+
- **labels** (Tensor) - Ground truth label with shape :math:`(N, *)` where :math:`*` means, any number
|
|
1664
|
+
of additional dimensions. The same shape and data type as `logits`.
|
|
1438
1665
|
|
|
1439
1666
|
Outputs:
|
|
1440
1667
|
Tensor or Scalar, if `reduction` is 'none', its shape is the same as `logits`.
|
|
@@ -1490,7 +1717,7 @@ class BCEWithLogitsLoss(LossBase):
|
|
|
1490
1717
|
return loss
|
|
1491
1718
|
|
|
1492
1719
|
|
|
1493
|
-
@
|
|
1720
|
+
@_primexpr
|
|
1494
1721
|
def _check_ndim(logits_nidm, labels_ndim, prime_name=None):
|
|
1495
1722
|
'''Internal function, used to check whether the dimension of logits and labels meets the requirements.'''
|
|
1496
1723
|
msg_prefix = f'For \'{prime_name}\', the' if prime_name else "The"
|
|
@@ -1505,7 +1732,7 @@ def _check_ndim(logits_nidm, labels_ndim, prime_name=None):
|
|
|
1505
1732
|
f"dimension of 'logits' {logits_nidm} and dimension of 'labels' {labels_ndim}.")
|
|
1506
1733
|
|
|
1507
1734
|
|
|
1508
|
-
@
|
|
1735
|
+
@_primexpr
|
|
1509
1736
|
def _check_channel_and_shape(logits, labels, prime_name=None):
|
|
1510
1737
|
'''Internal function, used to check whether the channels or shape of logits and labels meets the requirements.'''
|
|
1511
1738
|
msg_prefix = f'For \'{prime_name}\', the' if prime_name else "The"
|
|
@@ -1635,18 +1862,6 @@ class FocalLoss(LossBase):
|
|
|
1635
1862
|
return self.get_loss(loss)
|
|
1636
1863
|
|
|
1637
1864
|
|
|
1638
|
-
@constexpr
|
|
1639
|
-
def _dtype_check(logits_dtype, labels_dtype, prim_name):
|
|
1640
|
-
"""Check dtype."""
|
|
1641
|
-
if logits_dtype not in [mstype.float32, mstype.float16]:
|
|
1642
|
-
raise TypeError("For {}, the logits_dtype must be float32 or float16, but got {}.".format(prim_name,
|
|
1643
|
-
logits_dtype))
|
|
1644
|
-
if logits_dtype != labels_dtype:
|
|
1645
|
-
raise TypeError("For {}, the labels_dtype must equal to logits_dtype {}, but got {}".format(prim_name,
|
|
1646
|
-
logits_dtype,
|
|
1647
|
-
labels_dtype))
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
1865
|
class HuberLoss(LossBase):
|
|
1651
1866
|
r"""
|
|
1652
1867
|
HuberLoss calculate the error between the predicted value and the target value.
|
|
@@ -1727,47 +1942,28 @@ class HuberLoss(LossBase):
|
|
|
1727
1942
|
def __init__(self, reduction='mean', delta=1.0):
|
|
1728
1943
|
"""Initialize HuberLoss."""
|
|
1729
1944
|
super(HuberLoss, self).__init__(reduction=reduction)
|
|
1730
|
-
|
|
1731
|
-
validator.check_number("delta", delta, 0.0, Rel.GT, self.cls_name)
|
|
1732
|
-
self.sub = P.Sub()
|
|
1733
|
-
self.mul = P.Mul()
|
|
1734
|
-
self.abs = P.Abs()
|
|
1735
|
-
self.less = P.Less()
|
|
1736
|
-
self.square = P.Square()
|
|
1737
|
-
self.select = P.Select()
|
|
1738
|
-
self.dtype = P.DType()
|
|
1945
|
+
self.reduction = reduction
|
|
1739
1946
|
self.delta = delta
|
|
1740
|
-
self.delta_half = 0.5 * self.delta
|
|
1741
1947
|
|
|
1742
1948
|
def construct(self, logits, labels):
|
|
1743
|
-
|
|
1744
|
-
_check_is_tensor('labels', labels, self.cls_name)
|
|
1745
|
-
logits_dtype = self.dtype(logits)
|
|
1746
|
-
labels_dtype = self.dtype(labels)
|
|
1747
|
-
_dtype_check(logits_dtype, labels_dtype, self.cls_name)
|
|
1748
|
-
z = self.abs(self.sub(logits, labels))
|
|
1749
|
-
condition = self.less(z, self.delta)
|
|
1750
|
-
l1 = self.mul(0.5, self.square(z))
|
|
1751
|
-
l2 = self.mul(self.delta, self.sub(z, self.delta_half))
|
|
1752
|
-
loss = self.select(condition, l1, l2)
|
|
1753
|
-
|
|
1754
|
-
return self.get_loss(loss)
|
|
1949
|
+
return F.huber_loss(logits, labels, self.reduction, self.delta)
|
|
1755
1950
|
|
|
1756
1951
|
|
|
1757
1952
|
class TripletMarginLoss(LossBase):
|
|
1758
1953
|
r"""
|
|
1759
1954
|
TripletMarginLoss operation.
|
|
1760
1955
|
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
:math
|
|
1956
|
+
Triple loss is used to measure the relative similarity between samples,
|
|
1957
|
+
which is measured by a triplet and a :math:`margin` with a value greater than :math:`0` .
|
|
1958
|
+
The triplet is composed by :math:`a`, :math:`p`, :math:`n` in the following formula.
|
|
1959
|
+
|
|
1960
|
+
The shapes of all input tensors should be :math:`(N, *)` , where :math:`N` is batch size
|
|
1961
|
+
and :math:`*` means any number of additional dimensions.
|
|
1767
1962
|
|
|
1768
|
-
The distance swap is described in detail in the paper
|
|
1769
|
-
|
|
1770
|
-
|
|
1963
|
+
The distance swap is described in detail in the paper
|
|
1964
|
+
`Learning local feature descriptors with triplets and shallow convolutional neural
|
|
1965
|
+
networks <http://158.109.8.37/files/BRP2016.pdf>`_
|
|
1966
|
+
by V. Balntas, E. Riba et al.
|
|
1771
1967
|
|
|
1772
1968
|
The loss function for each sample in the mini-batch is:
|
|
1773
1969
|
|
|
@@ -1780,28 +1976,30 @@ class TripletMarginLoss(LossBase):
|
|
|
1780
1976
|
d(x_i, y_i) = \left\lVert {\bf x}_i - {\bf y}_i \right\rVert_p
|
|
1781
1977
|
|
|
1782
1978
|
Args:
|
|
1783
|
-
p (int): The norm
|
|
1784
|
-
eps (float): Default: 1e-06.
|
|
1785
|
-
swap (bool): The distance swap
|
|
1786
|
-
|
|
1787
|
-
|
|
1788
|
-
|
|
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.
|
|
1981
|
+
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.
|
|
1789
1986
|
|
|
1790
1987
|
Inputs:
|
|
1791
1988
|
- **x** (Tensor) - A sample randomly selected from the training set. Data type must be BasicType.
|
|
1792
|
-
|
|
1793
|
-
- **
|
|
1794
|
-
|
|
1989
|
+
:math:`a` in the above formula.
|
|
1990
|
+
- **positive** (Tensor) - A sample belonging to the same category as `x`, with the same type and
|
|
1991
|
+
shape as `x`. :math:`p` in the above formula.
|
|
1992
|
+
- **negative** (Tensor) - A sample belonging to the different class from `x`, with the same type and shape
|
|
1993
|
+
as `x`. :math:`n` in the above formula.
|
|
1994
|
+
- **margin** (Union[Tensor, float]) - Make a margin between the positive pair and the negative pair.
|
|
1995
|
+
Default: 1.0.
|
|
1795
1996
|
|
|
1796
1997
|
Outputs:
|
|
1797
|
-
|
|
1798
|
-
Otherwise, a scalar value will be returned.
|
|
1998
|
+
Tensor. If `reduction` is "none", its shape is :math:`(N)`. Otherwise, a scalar value will be returned.
|
|
1799
1999
|
|
|
1800
2000
|
Raises:
|
|
1801
|
-
TypeError: If `x` or `positive` or 'negative'
|
|
1802
|
-
TypeError: If dtype of `x` or `positive` or `negative` is not BasicType.
|
|
2001
|
+
TypeError: If `x` or `positive` or 'negative' is not a Tensor.
|
|
1803
2002
|
TypeError: If dtype of `x`, `positive` and `negative` is not the same.
|
|
1804
|
-
TypeError: If `margin` is not float32.
|
|
1805
2003
|
TypeError: If `p` is not an int.
|
|
1806
2004
|
TypeError: If `eps` is not a float.
|
|
1807
2005
|
TypeError: If `swap` is not a bool.
|
|
@@ -1812,25 +2010,31 @@ class TripletMarginLoss(LossBase):
|
|
|
1812
2010
|
ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
|
|
1813
2011
|
|
|
1814
2012
|
Supported Platforms:
|
|
1815
|
-
``
|
|
2013
|
+
``GPU``
|
|
1816
2014
|
|
|
1817
2015
|
Examples:
|
|
1818
2016
|
>>> loss = nn.TripletMarginLoss()
|
|
1819
2017
|
>>> x = Tensor(np.array([[0.3, 0.7], [0.5, 0.5]]), mindspore.float32)
|
|
1820
2018
|
>>> positive = Tensor(np.array([[0.4, 0.6], [0.4, 0.6]]), mindspore.float32)
|
|
1821
2019
|
>>> negative = Tensor(np.array([[0.2, 0.9], [0.3, 0.7]]), mindspore.float32)
|
|
1822
|
-
>>>
|
|
1823
|
-
>>> output = loss(x, positive, negative, margin)
|
|
2020
|
+
>>> output = loss(x, positive, negative)
|
|
1824
2021
|
>>> print(output)
|
|
1825
2022
|
0.8881968
|
|
1826
2023
|
"""
|
|
1827
2024
|
|
|
1828
|
-
def __init__(self, p=2, swap=False, eps=1e-
|
|
2025
|
+
def __init__(self, p=2, swap=False, eps=1e-06, reduction='mean', margin=1.):
|
|
1829
2026
|
super(TripletMarginLoss, self).__init__()
|
|
1830
|
-
self.
|
|
2027
|
+
self.p = p
|
|
2028
|
+
self.swap = swap
|
|
2029
|
+
self.eps = eps
|
|
2030
|
+
self.reduction = reduction
|
|
2031
|
+
self.margin = margin
|
|
1831
2032
|
|
|
1832
|
-
def construct(self, x, positive, negative, margin):
|
|
1833
|
-
|
|
2033
|
+
def construct(self, x, positive, negative, margin=1.):
|
|
2034
|
+
if self.margin != 1.0:
|
|
2035
|
+
margin = self.margin
|
|
2036
|
+
return F.triplet_margin_loss(x, positive, negative, margin=margin, p=self.p,
|
|
2037
|
+
eps=self.eps, swap=self.swap, reduction=self.reduction)
|
|
1834
2038
|
|
|
1835
2039
|
|
|
1836
2040
|
@constexpr
|
|
@@ -1851,7 +2055,7 @@ class NLLLoss(LossBase):
|
|
|
1851
2055
|
r"""
|
|
1852
2056
|
Gets the negative log likelihood loss between logits and labels.
|
|
1853
2057
|
|
|
1854
|
-
The nll loss with reduction=none can be described as:
|
|
2058
|
+
The nll loss with :math:`reduction = none` can be described as:
|
|
1855
2059
|
|
|
1856
2060
|
.. math::
|
|
1857
2061
|
|
|
@@ -1928,9 +2132,9 @@ class NLLLoss(LossBase):
|
|
|
1928
2132
|
|
|
1929
2133
|
|
|
1930
2134
|
@constexpr
|
|
1931
|
-
def _check_cross_entropy_inputs(logits_shape, label_shape,
|
|
1932
|
-
logits_rank, label_rank,
|
|
1933
|
-
logits_dtype, label_dtype,
|
|
2135
|
+
def _check_cross_entropy_inputs(logits_shape, label_shape,
|
|
2136
|
+
logits_rank, label_rank,
|
|
2137
|
+
logits_dtype, label_dtype,
|
|
1934
2138
|
prim_name=None):
|
|
1935
2139
|
"""Internal function, used to check whether the shape of logits and labels meets the requirements."""
|
|
1936
2140
|
validator.check_type_name('logits', logits_dtype, [mstype.float16, mstype.float32], prim_name)
|
|
@@ -1944,11 +2148,12 @@ def _check_cross_entropy_inputs(logits_shape, label_shape, \
|
|
|
1944
2148
|
f"but get 'logits' shape: {logits_shape} and 'labels' shape: {label_shape}.")
|
|
1945
2149
|
elif label_rank == logits_rank - 1:
|
|
1946
2150
|
validator.check_type_name('labels', label_dtype, [mstype.int32], prim_name)
|
|
1947
|
-
|
|
1948
|
-
|
|
1949
|
-
|
|
1950
|
-
|
|
1951
|
-
|
|
2151
|
+
if logits_rank != 1:
|
|
2152
|
+
logits_shape_new = (logits_shape[0], *logits_shape[2:])
|
|
2153
|
+
if logits_shape_new != label_shape:
|
|
2154
|
+
raise ValueError(f"{msg_prefix} shape of 'logits' should be (N, C, d_0, d_1, ...), "
|
|
2155
|
+
f"and the shape of 'labels' should be (N, d_0, d_1, ...), "
|
|
2156
|
+
f"but get 'logits' shape: {logits_shape} and 'labels' shape: {label_shape}.")
|
|
1952
2157
|
else:
|
|
1953
2158
|
raise ValueError(f"{msg_prefix} rank of 'logits' and 'labels' should be:\n"
|
|
1954
2159
|
f"1. 'logits.ndim == labels.ndim' for probabilities, \n"
|
|
@@ -1958,7 +2163,7 @@ def _check_cross_entropy_inputs(logits_shape, label_shape, \
|
|
|
1958
2163
|
|
|
1959
2164
|
@constexpr
|
|
1960
2165
|
def _cross_entropy_ignore_index_warning(prim_name):
|
|
1961
|
-
"""Internal function, used to
|
|
2166
|
+
"""Internal function, used to warning when ignore_index > 0 for probabilities."""
|
|
1962
2167
|
log.warning(f"For \'{prim_name}\', 'ignore_index' does not work when 'labels' is Probability.")
|
|
1963
2168
|
|
|
1964
2169
|
|
|
@@ -2065,7 +2270,7 @@ class CrossEntropyLoss(LossBase):
|
|
|
2065
2270
|
super().__init__(reduction)
|
|
2066
2271
|
validator.check_value_type('ignore_index', ignore_index, int, self.cls_name)
|
|
2067
2272
|
validator.check_value_type('label_smoothing', label_smoothing, float, self.cls_name)
|
|
2068
|
-
validator.check_float_range(label_smoothing, 0.0, 1.0,
|
|
2273
|
+
validator.check_float_range(label_smoothing, 0.0, 1.0, validator.INC_BOTH, 'label_smoothing', self.cls_name)
|
|
2069
2274
|
|
|
2070
2275
|
if weight is not None:
|
|
2071
2276
|
validator.check_value_type("weight", weight, [Tensor], self.cls_name)
|
|
@@ -2079,10 +2284,341 @@ class CrossEntropyLoss(LossBase):
|
|
|
2079
2284
|
def construct(self, logits, labels):
|
|
2080
2285
|
_check_is_tensor('logits', logits, self.cls_name)
|
|
2081
2286
|
_check_is_tensor('labels', labels, self.cls_name)
|
|
2082
|
-
_check_cross_entropy_inputs(logits.shape, labels.shape,
|
|
2083
|
-
logits.ndim, labels.ndim,
|
|
2084
|
-
logits.dtype, labels.dtype,
|
|
2287
|
+
_check_cross_entropy_inputs(logits.shape, labels.shape,
|
|
2288
|
+
logits.ndim, labels.ndim,
|
|
2289
|
+
logits.dtype, labels.dtype,
|
|
2085
2290
|
self.cls_name)
|
|
2086
2291
|
if logits.ndim == labels.ndim and self.ignore_index > 0:
|
|
2087
2292
|
_cross_entropy_ignore_index_warning(self.cls_name)
|
|
2088
2293
|
return F.cross_entropy(logits, labels, self.weight, self.ignore_index, self.reduction, self.label_smoothing)
|
|
2294
|
+
|
|
2295
|
+
|
|
2296
|
+
class KLDivLoss(LossBase):
|
|
2297
|
+
r"""
|
|
2298
|
+
Computes the Kullback-Leibler divergence between the `logits` and the `labels`.
|
|
2299
|
+
|
|
2300
|
+
For tensors of the same shape :math:`x` and :math:`target`,
|
|
2301
|
+
the updating formulas of KLDivLoss algorithm are as follows,
|
|
2302
|
+
|
|
2303
|
+
.. math::
|
|
2304
|
+
L(x, target) = target \cdot (\log target - x)
|
|
2305
|
+
|
|
2306
|
+
Then,
|
|
2307
|
+
|
|
2308
|
+
.. math::
|
|
2309
|
+
\ell(x, target) = \begin{cases}
|
|
2310
|
+
L(x, target), & \text{if reduction} = \text{'none';}\\
|
|
2311
|
+
\operatorname{mean}(L(x, target)), & \text{if reduction} = \text{'mean';}\\
|
|
2312
|
+
\operatorname{sum}(L(x, target)) / x.\operatorname{shape}[0], & \text{if reduction} = \text{'batchmean';}\\
|
|
2313
|
+
\operatorname{sum}(L(x, target)), & \text{if reduction} = \text{'sum'.}
|
|
2314
|
+
\end{cases}
|
|
2315
|
+
|
|
2316
|
+
where :math:`x` represents `logits`,
|
|
2317
|
+
:math:`target` represents `labels`, and
|
|
2318
|
+
:math:`\ell(x, target)` represents `output`.
|
|
2319
|
+
|
|
2320
|
+
Note:
|
|
2321
|
+
- Currently it does not support float64 input on `Ascend`.
|
|
2322
|
+
- The output aligns with the mathematical definition of Kullback-Leibler divergence
|
|
2323
|
+
only when `reduction` is set to 'batchmean'.
|
|
2324
|
+
|
|
2325
|
+
Args:
|
|
2326
|
+
reduction (str): Specifies the reduction to be applied to the output.
|
|
2327
|
+
Default: 'mean'.
|
|
2328
|
+
|
|
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'.
|
|
2332
|
+
|
|
2333
|
+
Inputs:
|
|
2334
|
+
- **logits** (Tensor) - The input Tensor. The data type must be float16, float32 or float64.
|
|
2335
|
+
- **labels** (Tensor) - The label Tensor which has the same shape and data type as `logits`.
|
|
2336
|
+
|
|
2337
|
+
Outputs:
|
|
2338
|
+
Tensor or Scalar, if `reduction` is 'none', then output is a tensor and has the same shape as `logits`.
|
|
2339
|
+
Otherwise, it is a scalar.
|
|
2340
|
+
|
|
2341
|
+
Raises:
|
|
2342
|
+
TypeError: If `reduction` is not a str.
|
|
2343
|
+
TypeError: If neither `logits` nor `labels` is a Tensor.
|
|
2344
|
+
TypeError: If dtype of `logits` or `labels` is not currently supported.
|
|
2345
|
+
ValueError: If shape of `logits` is not the same as `labels`.
|
|
2346
|
+
RuntimeError: If `logits` or `labels` is a scalar when `reduction` is 'batchmean'.
|
|
2347
|
+
|
|
2348
|
+
Supported Platforms:
|
|
2349
|
+
``Ascend`` ``GPU`` ``CPU``
|
|
2350
|
+
|
|
2351
|
+
Examples:
|
|
2352
|
+
>>> logits = Tensor(np.array([0.2, 0.7, 0.1]), mindspore.float32)
|
|
2353
|
+
>>> labels = Tensor(np.array([0., 1., 0.]), mindspore.float32)
|
|
2354
|
+
>>> loss = nn.KLDivLoss(reduction='mean')
|
|
2355
|
+
>>> output = loss(logits, labels)
|
|
2356
|
+
>>> print(output)
|
|
2357
|
+
-0.23333333
|
|
2358
|
+
"""
|
|
2359
|
+
|
|
2360
|
+
def __init__(self, reduction='mean'):
|
|
2361
|
+
super().__init__()
|
|
2362
|
+
self.reduction = reduction
|
|
2363
|
+
|
|
2364
|
+
def construct(self, logits, labels):
|
|
2365
|
+
_check_is_tensor('logits', logits, self.cls_name)
|
|
2366
|
+
_check_is_tensor('labels', labels, self.cls_name)
|
|
2367
|
+
return F.kl_div(logits, labels, self.reduction)
|
|
2368
|
+
|
|
2369
|
+
|
|
2370
|
+
class CTCLoss(LossBase):
|
|
2371
|
+
"""
|
|
2372
|
+
Calculates the CTC (Connectionist Temporal Classification) loss. It's mainly used to calculate the loss between
|
|
2373
|
+
the continuous, unsegemented time series and the target series.
|
|
2374
|
+
|
|
2375
|
+
For the CTC algorithm, refer to `Connectionist Temporal Classification: Labeling Unsegmented Sequence Data with
|
|
2376
|
+
Recurrent Neural Networks <http://www.cs.toronto.edu/~graves/icml_2006.pdf>`_ .
|
|
2377
|
+
|
|
2378
|
+
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'.
|
|
2382
|
+
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.
|
|
2384
|
+
|
|
2385
|
+
Inputs:
|
|
2386
|
+
- **log_probs** (Tensor) - A tensor of shape :math:`(T, N, C)` or :math:`(T, C)`, where T is length of input,
|
|
2387
|
+
N is size of the batch and C is the number of classes. T, N and C are positive integers.
|
|
2388
|
+
- **targets** (Tensor) - A tensor of shape :math:`(N, S)` or (sum( `target_lengths` )),
|
|
2389
|
+
where S is max target length, means the target sequences.
|
|
2390
|
+
- **input_lengths** (Union[tuple, Tensor]) - A tuple or Tensor of shape(N). It means the lengths of the input.
|
|
2391
|
+
- **target_lengths** (Union[tuple, Tensor]) - A tuple or Tensor of shape(N). It means the lengths of the target.
|
|
2392
|
+
|
|
2393
|
+
Outputs:
|
|
2394
|
+
- **neg_log_likelihood** (Tensor) - A loss value which is differentiable with respect to each input node.
|
|
2395
|
+
|
|
2396
|
+
Raises:
|
|
2397
|
+
TypeError: If `log_probs` or `targets` is not a Tensor.
|
|
2398
|
+
TypeError: If `zero_infinity` is not a bool, `reduction` is not string.
|
|
2399
|
+
TypeError: If the dtype of `log_probs` is not float or double.
|
|
2400
|
+
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.
|
|
2403
|
+
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)`.
|
|
2405
|
+
RuntimeError: If any value of `input_lengths` is larger than T. T is length of `log_probs` .
|
|
2406
|
+
RuntimeError: If any target_lengths[i] is not in range [0, input_length[i]].
|
|
2407
|
+
|
|
2408
|
+
Supported Platforms:
|
|
2409
|
+
``Ascend`` ``GPU`` ``CPU``
|
|
2410
|
+
|
|
2411
|
+
Examples:
|
|
2412
|
+
>>> import numpy as np
|
|
2413
|
+
>>> from mindspore import Tensor
|
|
2414
|
+
>>> from mindspore import dtype as mstype
|
|
2415
|
+
>>> from mindspore.nn.loss import CTCLoss
|
|
2416
|
+
>>> T = 5 # Input sequence length
|
|
2417
|
+
>>> C = 2 # Number of classes
|
|
2418
|
+
>>> N = 2 # Batch size
|
|
2419
|
+
>>> S = 3 # Target sequence length of longest target in batch (padding length)
|
|
2420
|
+
>>> S_min = 2 # Minimum target length, for demonstration purposes
|
|
2421
|
+
>>> arr = np.arange(T*N*C).reshape((T, N, C))
|
|
2422
|
+
>>> ms_input = Tensor(arr, dtype=mstype.float32)
|
|
2423
|
+
>>> input_lengths = np.full(shape=(N), fill_value=T)
|
|
2424
|
+
>>> input_lengths = Tensor(input_lengths, dtype=mstype.int32)
|
|
2425
|
+
>>> target_lengths = np.full(shape=(N), fill_value=S_min)
|
|
2426
|
+
>>> target_lengths = Tensor(target_lengths, dtype=mstype.int32)
|
|
2427
|
+
>>> 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)
|
|
2430
|
+
>>> loss = ctc_loss(ms_input, target, input_lengths, target_lengths)
|
|
2431
|
+
>>> print(loss)
|
|
2432
|
+
[-45.79497 -55.794968]
|
|
2433
|
+
>>> 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)
|
|
2437
|
+
>>> 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)
|
|
2440
|
+
>>> loss = ctc_loss(ms_input, target, input_lengths, target_lengths)
|
|
2441
|
+
>>> print(loss)
|
|
2442
|
+
-25.794968
|
|
2443
|
+
"""
|
|
2444
|
+
|
|
2445
|
+
def __init__(self, blank=0, reduction='mean', zero_infinity=False):
|
|
2446
|
+
super().__init__()
|
|
2447
|
+
self.blank = blank
|
|
2448
|
+
self.reduction = reduction
|
|
2449
|
+
self.zero_infinity = zero_infinity
|
|
2450
|
+
|
|
2451
|
+
def construct(self, log_probs, targets, input_lengths, target_lengths):
|
|
2452
|
+
_check_is_tensor('log_probs', log_probs, self.cls_name)
|
|
2453
|
+
_check_is_tensor('targets', targets, self.cls_name)
|
|
2454
|
+
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.")
|
|
2458
|
+
log_probs = log_probs.expand_dims(-2)
|
|
2459
|
+
targets = targets.expand_dims(0)
|
|
2460
|
+
neg_log_hood, _ = F.ctc_loss(log_probs, targets, input_lengths, target_lengths, self.blank, self.reduction,
|
|
2461
|
+
self.zero_infinity)
|
|
2462
|
+
return neg_log_hood.squeeze()
|
|
2463
|
+
neg_log_hood, _ = F.ctc_loss(log_probs, targets, input_lengths, target_lengths, self.blank, self.reduction,
|
|
2464
|
+
self.zero_infinity)
|
|
2465
|
+
return neg_log_hood
|
|
2466
|
+
|
|
2467
|
+
|
|
2468
|
+
class GaussianNLLLoss(LossBase):
|
|
2469
|
+
r"""
|
|
2470
|
+
Gaussian negative log likelihood loss.
|
|
2471
|
+
|
|
2472
|
+
The target values are considered to be samples from a Gaussian distribution, where the expectation and variance are
|
|
2473
|
+
predicted by a neural network. For `labels` modeled on a Gaussian distribution, `logits` to record expectations,
|
|
2474
|
+
and the variance `var` (elements are all positive), the calculated loss is:
|
|
2475
|
+
|
|
2476
|
+
.. math::
|
|
2477
|
+
\text{loss} = \frac{1}{2}\left(\log\left(\text{max}\left(\text{var},
|
|
2478
|
+
\ \text{eps}\right)\right) + \frac{\left(\text{logits} - \text{labels}\right)^2}
|
|
2479
|
+
{\text{max}\left(\text{var}, \ \text{eps}\right)}\right) + \text{const.}
|
|
2480
|
+
|
|
2481
|
+
where :math:`eps` is used for stability of :math:`log`. When :math:`full=True`, a constant will be added to
|
|
2482
|
+
the loss. If the shape of :math:`var` and :math:`logits` are not the same (due to a homoscedastic assumption),
|
|
2483
|
+
their shapes must allow correct broadcasting.
|
|
2484
|
+
|
|
2485
|
+
Keyword Args:
|
|
2486
|
+
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'.
|
|
2491
|
+
|
|
2492
|
+
Inputs:
|
|
2493
|
+
- **logits** (Tensor) - Tensor of shape :math:`(N, *)` or :math:`(*)` where :math:`*` means any number of
|
|
2494
|
+
additional dimensions.
|
|
2495
|
+
- **labels** (Tensor) - Tensor of shape :math:`(N, *)` or :math:`(*)`, same shape as the logits, or same shape
|
|
2496
|
+
as the logits but with one dimension equal to 1 (to allow for broadcasting).
|
|
2497
|
+
- **var** - Tensor of shape :math:`(N, *)` or :math:`(*)`, same shape as logits, or same shape as the logits
|
|
2498
|
+
but with one dimension equal to 1, or same shape as the logits but with one fewer dimension
|
|
2499
|
+
(to allow for broadcasting).
|
|
2500
|
+
|
|
2501
|
+
Returns:
|
|
2502
|
+
Tensor or Tensor scalar, the computed loss depending on :math:`reduction`.
|
|
2503
|
+
|
|
2504
|
+
Raises:
|
|
2505
|
+
TypeError: If `logits` is not a Tensor.
|
|
2506
|
+
TypeError: If `labels` is not a Tensor.
|
|
2507
|
+
TypeError: If `full` is not a bool.
|
|
2508
|
+
TypeError: If `eps` is not a float.
|
|
2509
|
+
ValueError: If `eps` is not a float within (0, inf).
|
|
2510
|
+
ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
|
|
2511
|
+
|
|
2512
|
+
Supported Platforms:
|
|
2513
|
+
``Ascend`` ``GPU`` ``CPU``
|
|
2514
|
+
|
|
2515
|
+
Examples:
|
|
2516
|
+
>>> import numpy as np
|
|
2517
|
+
>>> from mindspore import Tensor
|
|
2518
|
+
>>> import mindspore.nn as nn
|
|
2519
|
+
>>> import mindspore.common.dtype as mstype
|
|
2520
|
+
>>> arr1 = np.arange(8).reshape((4, 2))
|
|
2521
|
+
>>> 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)
|
|
2524
|
+
>>> loss = nn.GaussianNLLLoss(reduction='mean')
|
|
2525
|
+
>>> var = Tensor(np.ones((4, 1)), mstype.float32)
|
|
2526
|
+
>>> output = loss(logits, labels, var)
|
|
2527
|
+
>>> print(output)
|
|
2528
|
+
1.4374993
|
|
2529
|
+
|
|
2530
|
+
Reference:
|
|
2531
|
+
Nix, D. A. and Weigend, A. S., "Estimating the mean and variance of the
|
|
2532
|
+
target probability distribution", Proceedings of 1994 IEEE International
|
|
2533
|
+
Conference on Neural Networks (ICNN'94), Orlando, FL, USA, 1994, pp. 55-60
|
|
2534
|
+
vol.1, doi: 10.1109/ICNN.1994.374138.
|
|
2535
|
+
"""
|
|
2536
|
+
|
|
2537
|
+
def __init__(self, *, full=False, eps=1e-6, reduction='mean'):
|
|
2538
|
+
super(GaussianNLLLoss, self).__init__()
|
|
2539
|
+
validator.check_float_range(eps, 0, float('inf'), validator.INC_NEITHER, "eps", self.cls_name)
|
|
2540
|
+
validator.check_value_type('full', full, [bool], self.cls_name)
|
|
2541
|
+
validator.check_string(reduction, ['none', 'mean', 'sum'], 'reduction', 'gaussian_nll_loss')
|
|
2542
|
+
self.full = full
|
|
2543
|
+
self.eps = eps
|
|
2544
|
+
self.reduction = reduction
|
|
2545
|
+
|
|
2546
|
+
def construct(self, logits, labels, var):
|
|
2547
|
+
_check_is_tensor('logits', logits, self.cls_name)
|
|
2548
|
+
_check_is_tensor('labels', labels, self.cls_name)
|
|
2549
|
+
_check_is_tensor('var', var, self.cls_name)
|
|
2550
|
+
return ops.gaussian_nll_loss(logits, labels, var, self.full, self.eps, self.reduction)
|
|
2551
|
+
|
|
2552
|
+
|
|
2553
|
+
class HingeEmbeddingLoss(LossBase):
|
|
2554
|
+
r"""
|
|
2555
|
+
Calculate the Hinge Embedding Loss value based on the input 'logits' and' labels' (only including 1 or -1).
|
|
2556
|
+
Usually used to measure the similarity between two inputs.
|
|
2557
|
+
|
|
2558
|
+
The loss function for :math:`n`-th sample in the mini-batch is
|
|
2559
|
+
|
|
2560
|
+
.. math::
|
|
2561
|
+
l_n = \begin{cases}
|
|
2562
|
+
x_n, & \text{if}\; y_n = 1,\\
|
|
2563
|
+
\max \{0, \Delta - x_n\}, & \text{if}\; y_n = -1,
|
|
2564
|
+
\end{cases}
|
|
2565
|
+
|
|
2566
|
+
and the total loss functions is
|
|
2567
|
+
|
|
2568
|
+
.. math::
|
|
2569
|
+
\ell(x, y) = \begin{cases}
|
|
2570
|
+
\operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\
|
|
2571
|
+
\operatorname{sum}(L), & \text{if reduction} = \text{'sum'.}
|
|
2572
|
+
\end{cases}
|
|
2573
|
+
|
|
2574
|
+
where :math:`L = \{l_1,\dots,l_N\}^\top`.
|
|
2575
|
+
|
|
2576
|
+
Args:
|
|
2577
|
+
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'.
|
|
2581
|
+
|
|
2582
|
+
Inputs:
|
|
2583
|
+
- **logits** (Tensor) - The predicted value, expressed as :math:`x` in the equation.
|
|
2584
|
+
Tensor of shape :math:`(*)` where :math:`*` means any number of dimensions.
|
|
2585
|
+
- **labels** (Tensor) - Same shape as the logits, contains -1 or 1.
|
|
2586
|
+
|
|
2587
|
+
Returns:
|
|
2588
|
+
Tensor or Tensor scalar, the computed loss depending on :math:`reduction`.
|
|
2589
|
+
|
|
2590
|
+
Raises:
|
|
2591
|
+
TypeError: If `logits` is not a Tensor.
|
|
2592
|
+
TypeError: If `labels` is not a Tensor.
|
|
2593
|
+
TypeError: If `margin` is not a float or int.
|
|
2594
|
+
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'.
|
|
2596
|
+
|
|
2597
|
+
Supported Platforms:
|
|
2598
|
+
``Ascend`` ``GPU`` ``CPU``
|
|
2599
|
+
|
|
2600
|
+
Examples:
|
|
2601
|
+
>>> import numpy as np
|
|
2602
|
+
>>> from mindspore import Tensor
|
|
2603
|
+
>>> import mindspore.nn as nn
|
|
2604
|
+
>>> import mindspore.common.dtype as mstype
|
|
2605
|
+
>>> arr1 = np.array([0.9, -1.2, 2, 0.8, 3.9, 2, 1, 0, -1]).reshape((3, 3))
|
|
2606
|
+
>>> 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)
|
|
2609
|
+
>>> loss = nn.HingeEmbeddingLoss(reduction='mean')
|
|
2610
|
+
>>> output = loss(logits, labels)
|
|
2611
|
+
>>> print(output)
|
|
2612
|
+
0.16666667
|
|
2613
|
+
"""
|
|
2614
|
+
|
|
2615
|
+
def __init__(self, margin=1.0, reduction='mean'):
|
|
2616
|
+
super(HingeEmbeddingLoss, self).__init__()
|
|
2617
|
+
validator.check_value_type('margin', margin, [float, int], self.cls_name)
|
|
2618
|
+
validator.check_string(reduction, ['none', 'sum', 'mean'], 'reduction', self.cls_name)
|
|
2619
|
+
self.margin = margin
|
|
2620
|
+
self.reduction = reduction
|
|
2621
|
+
|
|
2622
|
+
def construct(self, logits, labels):
|
|
2623
|
+
loss = ops.hinge_embedding_loss(logits, labels, self.margin, self.reduction)
|
|
2624
|
+
return loss
|