mindspore 2.0.0a0__cp37-cp37m-win_amd64.whl → 2.0.0rc1__cp37-cp37m-win_amd64.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/__init__.py +4 -2
- mindspore/_c_dataengine.cp37-win_amd64.pyd +0 -0
- mindspore/_c_expression.cp37-win_amd64.pyd +0 -0
- mindspore/_c_mindrecord.cp37-win_amd64.pyd +0 -0
- mindspore/_check_jit_forbidden_api.py +102 -0
- mindspore/_checkparam.py +1066 -1001
- mindspore/_extends/parallel_compile/akg_compiler/akg_process.py +4 -3
- mindspore/_extends/parallel_compile/akg_compiler/tbe_topi.py +50 -48
- mindspore/_extends/parallel_compile/akg_compiler/util.py +9 -4
- mindspore/_extends/parallel_compile/tbe_compiler/tbe_adapter.py +4 -4
- mindspore/_extends/parallel_compile/tbe_compiler/tbe_helper.py +9 -4
- mindspore/_extends/parse/__init__.py +5 -3
- mindspore/_extends/parse/namespace.py +16 -1
- mindspore/_extends/parse/parser.py +107 -22
- mindspore/_extends/parse/resources.py +0 -7
- mindspore/_extends/parse/standard_method.py +885 -413
- mindspore/amp.py +52 -57
- mindspore/boost/boost.py +2 -2
- mindspore/boost/boost_cell_wrapper.py +38 -20
- mindspore/boost/dim_reduce.py +3 -3
- mindspore/boost/group_loss_scale_manager.py +1 -1
- mindspore/common/__init__.py +4 -6
- 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 +41 -7
- mindspore/common/api.py +215 -141
- mindspore/common/dtype.py +8 -1
- mindspore/common/dump.py +2 -2
- mindspore/common/initializer.py +4 -2
- mindspore/common/jit_config.py +17 -13
- mindspore/common/mutable.py +33 -13
- mindspore/common/parameter.py +23 -21
- mindspore/common/seed.py +8 -24
- mindspore/common/sparse_tensor.py +62 -41
- mindspore/common/tensor.py +852 -1154
- mindspore/communication/__init__.py +2 -2
- mindspore/communication/_comm_helper.py +11 -4
- mindspore/communication/management.py +22 -21
- mindspore/config/op_info.config +501 -1008
- mindspore/context.py +201 -23
- mindspore/dataset/__init__.py +6 -6
- mindspore/dataset/audio/__init__.py +7 -7
- mindspore/dataset/audio/transforms.py +670 -30
- mindspore/dataset/audio/utils.py +47 -4
- mindspore/dataset/audio/validators.py +223 -1
- mindspore/dataset/callback/ds_callback.py +2 -2
- mindspore/dataset/core/config.py +210 -14
- mindspore/dataset/core/validator_helpers.py +2 -2
- 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 +1 -1
- mindspore/dataset/engine/datasets.py +322 -66
- mindspore/dataset/engine/datasets_audio.py +80 -76
- mindspore/dataset/engine/datasets_standard_format.py +51 -38
- mindspore/dataset/engine/datasets_text.py +232 -118
- mindspore/dataset/engine/datasets_user_defined.py +41 -17
- mindspore/dataset/engine/datasets_vision.py +746 -225
- mindspore/dataset/engine/graphdata.py +75 -10
- mindspore/dataset/engine/iterators.py +45 -5
- mindspore/dataset/engine/offload.py +48 -28
- mindspore/dataset/engine/validators.py +117 -8
- mindspore/dataset/text/__init__.py +6 -5
- mindspore/dataset/text/transforms.py +86 -3
- mindspore/dataset/text/utils.py +6 -4
- mindspore/dataset/text/validators.py +25 -0
- mindspore/dataset/transforms/__init__.py +3 -2
- mindspore/dataset/transforms/c_transforms.py +1 -1
- mindspore/dataset/transforms/transforms.py +2 -2
- mindspore/dataset/utils/__init__.py +2 -1
- mindspore/dataset/utils/line_reader.py +121 -0
- mindspore/dataset/vision/__init__.py +2 -3
- mindspore/dataset/vision/c_transforms.py +9 -9
- mindspore/dataset/vision/py_transforms.py +5 -5
- mindspore/dataset/vision/py_transforms_util.py +2 -0
- mindspore/dataset/vision/transforms.py +160 -161
- mindspore/dataset/vision/utils.py +3 -3
- mindspore/experimental/map_parameter.py +38 -26
- 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 +44 -9
- mindspore/include/api/delegate.h +1 -1
- 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_parallel_runner.h +2 -2
- mindspore/include/api/net.h +12 -11
- mindspore/include/api/serialization.h +19 -3
- mindspore/include/api/types.h +3 -3
- mindspore/include/dataset/constants.h +7 -0
- mindspore/include/dataset/text.h +59 -0
- mindspore/jpeg62.dll +0 -0
- mindspore/log.py +1 -1
- mindspore/mindrecord/filereader.py +18 -0
- mindspore/mindrecord/filewriter.py +197 -34
- mindspore/mindrecord/shardreader.py +9 -0
- mindspore/mindrecord/shardwriter.py +1 -1
- mindspore/mindrecord/tools/cifar100_to_mr.py +3 -3
- mindspore/mindrecord/tools/cifar10_to_mr.py +3 -3
- mindspore/mindrecord/tools/csv_to_mr.py +3 -3
- mindspore/mindrecord/tools/imagenet_to_mr.py +16 -11
- mindspore/mindrecord/tools/mnist_to_mr.py +2 -2
- mindspore/mindrecord/tools/tfrecord_to_mr.py +6 -6
- mindspore/mindspore_backend.dll +0 -0
- mindspore/mindspore_common.dll +0 -0
- mindspore/mindspore_core.dll +0 -0
- mindspore/mindspore_glog.dll +0 -0
- mindspore/mindspore_shared_lib.dll +0 -0
- mindspore/nn/__init__.py +0 -4
- mindspore/nn/cell.py +204 -132
- mindspore/nn/dynamic_lr.py +1 -1
- mindspore/nn/grad/cell_grad.py +7 -6
- mindspore/nn/layer/__init__.py +5 -4
- mindspore/nn/layer/activation.py +40 -89
- mindspore/nn/layer/basic.py +255 -624
- mindspore/nn/layer/channel_shuffle.py +7 -6
- mindspore/nn/layer/combined.py +1 -1
- mindspore/nn/layer/container.py +41 -4
- mindspore/nn/layer/conv.py +64 -28
- mindspore/nn/layer/dense.py +9 -8
- mindspore/nn/layer/embedding.py +27 -25
- mindspore/nn/layer/image.py +53 -46
- mindspore/nn/layer/math.py +97 -105
- mindspore/nn/layer/normalization.py +117 -86
- mindspore/nn/layer/padding.py +185 -95
- mindspore/nn/layer/pooling.py +817 -414
- mindspore/nn/layer/rnn_cells.py +10 -15
- mindspore/nn/layer/rnns.py +37 -38
- mindspore/nn/layer/thor_layer.py +11 -12
- 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 +5 -4
- mindspore/nn/loss/loss.py +334 -199
- mindspore/nn/optim/ada_grad.py +6 -6
- mindspore/nn/optim/adadelta.py +2 -3
- mindspore/nn/optim/adafactor.py +4 -5
- mindspore/nn/optim/adam.py +126 -62
- mindspore/nn/optim/adamax.py +3 -4
- mindspore/nn/optim/adasum.py +6 -6
- mindspore/nn/optim/asgd.py +2 -2
- mindspore/nn/optim/ftrl.py +67 -38
- mindspore/nn/optim/lamb.py +4 -5
- mindspore/nn/optim/lars.py +2 -2
- mindspore/nn/optim/lazyadam.py +43 -4
- mindspore/nn/optim/momentum.py +6 -5
- mindspore/nn/optim/optimizer.py +3 -1
- mindspore/nn/optim/proximal_ada_grad.py +2 -2
- mindspore/nn/optim/rmsprop.py +1 -1
- mindspore/nn/optim/rprop.py +8 -9
- mindspore/nn/optim/sgd.py +19 -13
- mindspore/nn/optim/thor.py +10 -15
- mindspore/nn/probability/__init__.py +0 -2
- mindspore/nn/probability/bijector/bijector.py +4 -4
- mindspore/nn/probability/bijector/invert.py +1 -1
- 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/_utils/utils.py +9 -15
- mindspore/nn/probability/distribution/bernoulli.py +3 -3
- mindspore/nn/probability/distribution/beta.py +1 -1
- mindspore/nn/probability/distribution/categorical.py +5 -7
- mindspore/nn/probability/distribution/cauchy.py +3 -3
- mindspore/nn/probability/distribution/distribution.py +2 -2
- mindspore/nn/probability/distribution/exponential.py +2 -2
- mindspore/nn/probability/distribution/gamma.py +3 -3
- mindspore/nn/probability/distribution/geometric.py +1 -1
- mindspore/nn/probability/distribution/gumbel.py +3 -3
- mindspore/nn/probability/distribution/half_normal.py +15 -11
- mindspore/nn/probability/distribution/laplace.py +16 -13
- mindspore/nn/probability/distribution/logistic.py +2 -2
- mindspore/nn/probability/distribution/normal.py +1 -1
- mindspore/nn/probability/distribution/poisson.py +1 -1
- mindspore/nn/probability/distribution/student_t.py +20 -15
- mindspore/nn/probability/distribution/transformed_distribution.py +4 -4
- mindspore/nn/probability/distribution/uniform.py +2 -2
- mindspore/nn/reinforcement/_tensors_queue.py +3 -3
- mindspore/nn/reinforcement/tensor_array.py +2 -2
- mindspore/nn/sparse/sparse.py +2 -2
- mindspore/nn/wrap/cell_wrapper.py +27 -10
- mindspore/nn/wrap/grad_reducer.py +2 -2
- mindspore/nn/wrap/loss_scale.py +40 -24
- mindspore/numpy/array_creations.py +33 -22
- mindspore/numpy/array_ops.py +35 -30
- mindspore/numpy/logic_ops.py +6 -27
- mindspore/numpy/math_ops.py +22 -19
- mindspore/numpy/utils.py +1 -1
- mindspore/numpy/utils_const.py +108 -58
- mindspore/opencv_core452.dll +0 -0
- mindspore/opencv_imgcodecs452.dll +0 -0
- mindspore/opencv_imgproc452.dll +0 -0
- mindspore/ops/_constants.py +0 -6
- mindspore/ops/_grad/__init__.py +2 -1
- mindspore/ops/_grad/grad_array_ops.py +86 -117
- mindspore/ops/_grad/grad_base.py +23 -1
- mindspore/ops/_grad/grad_clip_ops.py +2 -3
- mindspore/ops/_grad/grad_comm_ops.py +34 -24
- mindspore/ops/_grad/grad_implementations.py +9 -45
- mindspore/ops/_grad/grad_inner_ops.py +47 -4
- mindspore/ops/_grad/grad_math_ops.py +142 -117
- mindspore/ops/_grad/grad_nn_ops.py +71 -165
- mindspore/ops/_grad/grad_sequence_ops.py +296 -0
- mindspore/ops/_grad/grad_sparse.py +7 -6
- mindspore/ops/_grad_experimental/__init__.py +1 -0
- mindspore/ops/_grad_experimental/grad_array_ops.py +150 -15
- mindspore/ops/_grad_experimental/grad_image_ops.py +16 -7
- mindspore/ops/_grad_experimental/grad_inner_ops.py +1 -22
- mindspore/ops/_grad_experimental/grad_linalg_ops.py +4 -11
- mindspore/ops/_grad_experimental/grad_math_ops.py +210 -89
- mindspore/ops/_grad_experimental/grad_nn_ops.py +26 -22
- mindspore/ops/_grad_experimental/grad_scalar_ops.py +112 -0
- mindspore/ops/_grad_experimental/grad_sparse_ops.py +49 -8
- mindspore/ops/_op_impl/_custom_op/batch_matmul_impl.py +1 -1
- mindspore/ops/_op_impl/_custom_op/batchnorm_fold.py +2 -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 +4 -4
- 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 +2 -2
- mindspore/ops/_op_impl/_custom_op/correction_mul_grad.py +2 -2
- mindspore/ops/_op_impl/_custom_op/dsd_back_impl.py +1 -5
- 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_impl.py +0 -4
- 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 +236 -4
- mindspore/ops/_op_impl/aicpu/abs.py +36 -0
- mindspore/ops/_op_impl/aicpu/{adaptive_avg_pool_2d_v1.py → adaptive_avg_pool_2d.py} +6 -5
- mindspore/ops/_op_impl/aicpu/adaptive_avg_pool_2d_grad.py +34 -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/batch_matmul.py +43 -43
- mindspore/ops/_op_impl/aicpu/bernoulli.py +48 -0
- mindspore/{compression/common/__init__.py → ops/_op_impl/aicpu/bessel_i0.py} +15 -8
- mindspore/ops/_op_impl/aicpu/channel_shuffle.py +40 -0
- mindspore/ops/_op_impl/aicpu/conj.py +11 -0
- mindspore/ops/_op_impl/aicpu/cumulative_logsumexp.py +0 -3
- mindspore/ops/_op_impl/aicpu/deformable_offsets.py +38 -0
- mindspore/ops/_op_impl/aicpu/deformable_offsets_grad.py +43 -0
- mindspore/ops/_op_impl/aicpu/{adaptive_avg_pool_2d_grad_v1.py → digamma.py} +7 -9
- 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/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/aicpu/less.py +41 -0
- mindspore/{nn/probability/infer/variational/__init__.py → ops/_op_impl/aicpu/lgamma.py} +16 -10
- mindspore/ops/_op_impl/aicpu/mirror_pad.py +0 -4
- 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/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/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/randperm_v2.py +41 -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/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/smooth_l1_loss.py +35 -0
- mindspore/ops/_op_impl/aicpu/smooth_l1_loss_grad.py +37 -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_slice.py +4 -0
- mindspore/ops/_op_impl/aicpu/sparse_slice_grad.py +6 -0
- mindspore/ops/_op_impl/aicpu/tensor_scatter_update.py +59 -0
- mindspore/ops/_op_impl/aicpu/trans_data.py +1 -0
- mindspore/ops/_op_impl/aicpu/tril_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/dynamic_shape.py +5 -1
- mindspore/ops/_op_impl/cpu/sparse_slice.py +4 -0
- mindspore/ops/_op_impl/cpu/sparse_slice_grad.py +6 -0
- mindspore/ops/_op_impl/cpu/tensor_shape.py +5 -1
- mindspore/ops/_op_impl/tbe/__init__.py +27 -611
- 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/batch_matmul_ds.py +1 -0
- 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/bn_infer_grad.py +4 -2
- 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/data_format_dim_map_ds.py +1 -0
- mindspore/ops/_op_impl/tbe/depthwise_conv2d.py +2 -2
- 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/{index_add.py → inplace_index_add.py} +3 -6
- mindspore/ops/_op_impl/tbe/matmul_ds.py +2 -0
- 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/scatter_mul.py +2 -0
- mindspore/ops/_op_impl/tbe/scatter_nd_add.py +0 -2
- 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/trans_data_ds.py +15 -5
- mindspore/ops/_register_for_op.py +1 -0
- mindspore/ops/_utils/__init__.py +1 -2
- mindspore/ops/_utils/utils.py +19 -40
- mindspore/ops/_vmap/vmap_array_ops.py +116 -38
- mindspore/ops/_vmap/vmap_base.py +16 -9
- 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 +7 -5
- mindspore/ops/_vmap/vmap_image_ops.py +12 -5
- mindspore/ops/_vmap/vmap_math_ops.py +46 -5
- mindspore/ops/_vmap/vmap_nn_ops.py +15 -21
- mindspore/ops/_vmap/vmap_random_ops.py +1 -1
- mindspore/ops/bprop_mindir/AdaptiveAvgPool2D_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/AdaptiveMaxPool2D_bprop.mindir +0 -0
- 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/BatchNormGrad_bprop.mindir +0 -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 +220 -106
- mindspore/ops/bprop_mindir/CTCLoss_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/DeformableOffsets_bprop.mindir +58 -0
- mindspore/ops/bprop_mindir/DepthwiseConv2dNative_bprop.mindir +138 -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 -23
- mindspore/ops/bprop_mindir/DropoutGenMask_bprop.mindir +16 -17
- 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/Elu_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/EmbeddingLookup_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/ExpandDims_bprop.mindir +39 -41
- mindspore/ops/bprop_mindir/FastGeLU_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/Flatten_bprop.mindir +41 -43
- mindspore/ops/bprop_mindir/GatherNd_bprop.mindir +51 -57
- mindspore/ops/bprop_mindir/Gather_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/HSigmoid_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/HSwish_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/InstanceNorm_bprop.mindir +0 -0
- 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/LogSoftmax_bprop.mindir +23 -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/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/OneHot_bprop.mindir +24 -25
- 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/ReLU6_bprop.mindir +16 -0
- mindspore/ops/bprop_mindir/ReLUV2_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/ReluGrad_bprop.mindir +18 -19
- mindspore/ops/bprop_mindir/Reshape_bprop.mindir +53 -53
- mindspore/ops/bprop_mindir/ResizeBilinear_bprop.mindir +29 -0
- mindspore/ops/bprop_mindir/ResizeNearestNeighbor_bprop.mindir +77 -85
- mindspore/ops/bprop_mindir/SeLU_bprop.mindir +21 -0
- 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/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/SparseSoftmaxCrossEntropyWithLogits_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Squeeze_bprop.mindir +37 -39
- mindspore/ops/bprop_mindir/StridedSlice_bprop.mindir +70 -72
- mindspore/ops/bprop_mindir/TanhGrad_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/Tanh_bprop.mindir +66 -0
- mindspore/ops/bprop_mindir/Tile_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/TopK_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/TupleGetItem_bprop.mindir +17 -17
- mindspore/ops/bprop_mindir/UpsampleNearest3D_bprop.mindir +32 -0
- mindspore/ops/bprop_mindir/UpsampleTrilinear3D_bprop.mindir +38 -0
- mindspore/ops/bprop_mindir/generate_mindir.py +2 -0
- mindspore/ops/composite/__init__.py +7 -8
- mindspore/ops/composite/base.py +101 -47
- mindspore/ops/composite/math_ops.py +188 -158
- mindspore/ops/composite/multitype_ops/_compile_utils.py +415 -170
- mindspore/ops/composite/multitype_ops/_constexpr_utils.py +142 -87
- mindspore/ops/composite/multitype_ops/add_impl.py +6 -1
- mindspore/ops/composite/multitype_ops/div_impl.py +2 -3
- mindspore/ops/composite/multitype_ops/getitem_impl.py +31 -3
- 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 +9 -0
- mindspore/ops/composite/multitype_ops/less_equal_impl.py +31 -0
- mindspore/ops/composite/multitype_ops/less_impl.py +31 -0
- mindspore/ops/composite/multitype_ops/mul_impl.py +21 -5
- mindspore/ops/composite/multitype_ops/not_in_impl.py +9 -0
- mindspore/ops/composite/multitype_ops/ones_like_impl.py +2 -4
- mindspore/ops/composite/multitype_ops/setitem_impl.py +21 -3
- mindspore/ops/composite/multitype_ops/sub_impl.py +1 -1
- mindspore/ops/composite/multitype_ops/zeros_like_impl.py +35 -4
- mindspore/ops/function/__init__.py +152 -8
- mindspore/ops/function/array_func.py +2555 -674
- mindspore/ops/function/clip_func.py +209 -13
- mindspore/ops/function/debug_func.py +2 -2
- mindspore/ops/function/grad/__init__.py +2 -1
- mindspore/ops/function/grad/grad_func.py +147 -62
- mindspore/ops/function/image_func.py +54 -38
- mindspore/ops/function/linalg_func.py +167 -16
- mindspore/ops/function/math_func.py +4849 -1492
- mindspore/ops/function/nn_func.py +2573 -988
- mindspore/ops/function/other_func.py +115 -0
- mindspore/ops/function/parameter_func.py +3 -3
- mindspore/ops/function/random_func.py +790 -73
- mindspore/ops/function/sparse_func.py +98 -78
- mindspore/ops/function/sparse_unary_func.py +54 -53
- mindspore/ops/function/spectral_func.py +27 -24
- mindspore/ops/function/vmap_func.py +22 -2
- mindspore/ops/functional.py +97 -37
- mindspore/ops/op_info_register.py +70 -28
- mindspore/ops/operations/__init__.py +47 -14
- mindspore/ops/operations/_csr_ops.py +7 -7
- mindspore/ops/operations/_embedding_cache_ops.py +5 -5
- mindspore/ops/operations/_grad_ops.py +276 -187
- mindspore/ops/operations/_inner_ops.py +319 -113
- mindspore/ops/operations/_ms_kernel.py +10 -8
- mindspore/ops/operations/_ocr_ops.py +9 -9
- mindspore/ops/operations/_opaque_predicate_registry.py +4 -0
- mindspore/ops/operations/_quant_ops.py +137 -102
- mindspore/ops/operations/_rl_inner_ops.py +121 -60
- mindspore/ops/operations/_scalar_ops.py +466 -0
- mindspore/ops/operations/_sequence_ops.py +1004 -2
- mindspore/ops/operations/_tensor_array.py +10 -11
- mindspore/ops/operations/_thor_ops.py +1 -1
- mindspore/ops/operations/array_ops.py +801 -466
- mindspore/ops/operations/comm_ops.py +51 -49
- mindspore/ops/operations/control_ops.py +2 -2
- mindspore/ops/operations/custom_ops.py +123 -44
- mindspore/ops/operations/debug_ops.py +24 -24
- mindspore/ops/operations/image_ops.py +240 -153
- mindspore/ops/operations/inner_ops.py +34 -50
- mindspore/ops/operations/linalg_ops.py +31 -9
- mindspore/ops/operations/math_ops.py +988 -757
- mindspore/ops/operations/nn_ops.py +965 -819
- mindspore/ops/operations/other_ops.py +51 -40
- mindspore/ops/operations/random_ops.py +204 -122
- mindspore/ops/operations/rl_ops.py +8 -9
- mindspore/ops/operations/sparse_ops.py +254 -93
- mindspore/ops/operations/spectral_ops.py +35 -3
- mindspore/ops/primitive.py +111 -9
- mindspore/parallel/_auto_parallel_context.py +189 -83
- mindspore/parallel/_offload_context.py +185 -0
- mindspore/parallel/_parallel_serialization.py +99 -7
- mindspore/parallel/_ps_context.py +9 -5
- mindspore/parallel/_recovery_context.py +1 -1
- mindspore/parallel/_tensor.py +7 -1
- mindspore/{nn/transformer → parallel/_transformer}/__init__.py +6 -6
- mindspore/{nn/transformer → parallel/_transformer}/layers.py +6 -37
- mindspore/{nn/transformer → parallel/_transformer}/loss.py +4 -7
- mindspore/{nn/transformer → parallel/_transformer}/moe.py +20 -16
- mindspore/{nn/transformer → parallel/_transformer}/op_parallel_config.py +3 -3
- mindspore/{nn/transformer → parallel/_transformer}/transformer.py +48 -111
- mindspore/parallel/_utils.py +1 -2
- mindspore/parallel/algo_parameter_config.py +1 -1
- mindspore/parallel/checkpoint_transform.py +37 -34
- mindspore/parallel/shard.py +17 -18
- mindspore/profiler/common/validator/validate_path.py +2 -2
- mindspore/profiler/envprofiling.py +69 -47
- mindspore/profiler/parser/ascend_timeline_generator.py +49 -42
- mindspore/profiler/parser/base_timeline_generator.py +49 -56
- mindspore/profiler/parser/cpu_gpu_timeline_generator.py +98 -78
- mindspore/profiler/parser/hwts_log_parser.py +1 -1
- mindspore/profiler/parser/integrator.py +15 -14
- mindspore/profiler/parser/minddata_analyzer.py +2 -2
- mindspore/profiler/parser/msadvisor_analyzer.py +12 -25
- mindspore/profiler/parser/msadvisor_parser.py +2 -4
- mindspore/profiler/parser/optime_parser.py +17 -18
- mindspore/profiler/parser/profiler_info.py +2 -1
- mindspore/profiler/profiling.py +218 -186
- mindspore/rewrite/__init__.py +3 -1
- mindspore/rewrite/api/node.py +1 -114
- mindspore/rewrite/api/node_type.py +3 -0
- mindspore/rewrite/api/pattern_engine.py +31 -1
- mindspore/rewrite/api/scoped_value.py +4 -4
- mindspore/rewrite/api/symbol_tree.py +3 -78
- mindspore/rewrite/api/tree_node_helper.py +1 -1
- mindspore/rewrite/ast_creator_register.py +1 -0
- mindspore/rewrite/ast_helpers/__init__.py +2 -2
- mindspore/rewrite/ast_helpers/ast_creator.py +1 -2
- mindspore/rewrite/ast_helpers/ast_finder.py +65 -0
- mindspore/rewrite/ast_helpers/ast_modifier.py +11 -3
- mindspore/rewrite/ast_transformers/flatten_recursive_stmt.py +18 -2
- mindspore/rewrite/namespace.py +0 -2
- mindspore/rewrite/node.py +157 -11
- mindspore/rewrite/parsers/assign_parser.py +231 -53
- mindspore/rewrite/parsers/class_def_parser.py +187 -109
- mindspore/rewrite/parsers/for_parser.py +24 -14
- mindspore/rewrite/parsers/function_def_parser.py +21 -4
- mindspore/rewrite/parsers/if_parser.py +6 -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 +256 -133
- mindspore/rewrite/symbol_tree_builder.py +38 -1
- mindspore/run_check/_check_version.py +69 -63
- mindspore/run_check/run_check.py +2 -1
- mindspore/tinyxml2.dll +0 -0
- mindspore/train/__init__.py +1 -1
- mindspore/train/_utils.py +28 -5
- mindspore/train/amp.py +273 -102
- mindspore/train/callback/_backup_and_restore.py +5 -5
- mindspore/train/callback/_callback.py +2 -2
- mindspore/train/callback/_checkpoint.py +3 -3
- mindspore/train/callback/_early_stop.py +3 -3
- mindspore/train/callback/_lambda_callback.py +2 -2
- mindspore/train/callback/_landscape.py +29 -31
- mindspore/train/callback/_loss_monitor.py +3 -3
- mindspore/train/callback/_on_request_exit.py +3 -3
- mindspore/train/callback/_reduce_lr_on_plateau.py +4 -4
- mindspore/train/callback/_summary_collector.py +23 -16
- mindspore/train/callback/_time_monitor.py +3 -3
- mindspore/train/checkpoint_pb2.py +68 -8
- mindspore/train/data_sink.py +15 -3
- mindspore/train/dataset_helper.py +10 -15
- mindspore/train/loss_scale_manager.py +8 -11
- mindspore/train/metrics/__init__.py +1 -1
- mindspore/train/metrics/bleu_score.py +1 -1
- mindspore/train/metrics/confusion_matrix.py +1 -1
- mindspore/train/metrics/cosine_similarity.py +1 -1
- mindspore/train/metrics/dice.py +2 -2
- mindspore/train/metrics/fbeta.py +1 -1
- mindspore/train/metrics/hausdorff_distance.py +4 -3
- mindspore/train/metrics/mean_surface_distance.py +2 -2
- mindspore/train/metrics/occlusion_sensitivity.py +1 -1
- mindspore/train/metrics/perplexity.py +1 -1
- mindspore/train/metrics/precision.py +1 -1
- mindspore/train/metrics/recall.py +1 -1
- mindspore/train/metrics/roc.py +2 -2
- mindspore/train/metrics/root_mean_square_surface_distance.py +2 -2
- mindspore/train/mind_ir_pb2.py +116 -37
- mindspore/train/model.py +45 -28
- mindspore/train/serialization.py +295 -188
- mindspore/train/summary/_summary_adapter.py +1 -1
- mindspore/train/summary/summary_record.py +43 -13
- mindspore/train/train_thor/convert_utils.py +2 -2
- mindspore/train/train_thor/dataset_helper.py +3 -3
- mindspore/turbojpeg.dll +0 -0
- mindspore/version.py +1 -1
- {mindspore-2.0.0a0.dist-info → mindspore-2.0.0rc1.dist-info}/METADATA +3 -2
- {mindspore-2.0.0a0.dist-info → mindspore-2.0.0rc1.dist-info}/RECORD +610 -541
- mindspore/compression/__init__.py +0 -19
- mindspore/compression/common/constant.py +0 -124
- mindspore/compression/export/__init__.py +0 -19
- mindspore/compression/export/quant_export.py +0 -515
- mindspore/compression/quant/__init__.py +0 -28
- mindspore/compression/quant/qat.py +0 -634
- 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 -140
- mindspore/nn/probability/dpn/vae/vae.py +0 -124
- 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 -364
- 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/aicpu/parallel_concat.py +0 -42
- mindspore/ops/_op_impl/tbe/gather_v2.py +0 -56
- mindspore/ops/bprop_mindir/AssignAdd_bprop.mindir +0 -19
- mindspore/ops/bprop_mindir/Cast_bprop.mindir +0 -19
- mindspore/ops/bprop_mindir/LogicalOr_bprop.mindir +0 -19
- mindspore/ops/bprop_mindir/MatMul_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/ReLU_bprop.mindir +0 -17
- mindspore/ops/bprop_mindir/Transpose_bprop.mindir +0 -0
- mindspore/ops/bprop_mindir/UpdateState_bprop.mindir +0 -15
- mindspore/ops/composite/array_ops.py +0 -241
- mindspore/ops/composite/clip_ops.py +0 -134
- mindspore/ops/composite/random_ops.py +0 -426
- mindspore/ops/composite/vmap_ops.py +0 -38
- 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-2.0.0a0.dist-info → mindspore-2.0.0rc1.dist-info}/WHEEL +0 -0
- {mindspore-2.0.0a0.dist-info → mindspore-2.0.0rc1.dist-info}/entry_points.txt +0 -0
- {mindspore-2.0.0a0.dist-info → mindspore-2.0.0rc1.dist-info}/top_level.txt +0 -0
mindspore/nn/loss/loss.py
CHANGED
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
# ============================================================================
|
|
15
15
|
"""loss"""
|
|
16
16
|
from __future__ import absolute_import, division
|
|
17
|
+
import math
|
|
17
18
|
|
|
18
19
|
import mindspore
|
|
19
20
|
import mindspore.common.dtype as mstype
|
|
@@ -25,15 +26,12 @@ from mindspore.ops import operations as P
|
|
|
25
26
|
from mindspore.ops.operations import _inner_ops as inner
|
|
26
27
|
from mindspore.ops.operations.nn_ops import MultiMarginLoss as MultiMarginLossOp
|
|
27
28
|
from mindspore.ops.operations.nn_ops import MultilabelMarginLoss as MultilabelMarginLossOp
|
|
28
|
-
from mindspore.ops.operations.nn_ops import TripletMarginLoss as TripletMarginLossOp
|
|
29
|
-
from mindspore.ops.operations.nn_ops import CTCLossV2
|
|
30
29
|
from mindspore.ops import functional as F
|
|
31
30
|
from mindspore import nn
|
|
32
|
-
from mindspore.ops.primitive import constexpr
|
|
31
|
+
from mindspore.ops.primitive import constexpr, _primexpr
|
|
33
32
|
from mindspore.nn.cell import Cell
|
|
34
33
|
from mindspore.nn.layer.activation import get_activation
|
|
35
|
-
from mindspore
|
|
36
|
-
from mindspore._checkparam import Rel
|
|
34
|
+
from mindspore import _checkparam as validator
|
|
37
35
|
from mindspore import context
|
|
38
36
|
|
|
39
37
|
|
|
@@ -248,13 +246,10 @@ class L1Loss(LossBase):
|
|
|
248
246
|
def __init__(self, reduction='mean'):
|
|
249
247
|
"""Initialize L1Loss."""
|
|
250
248
|
super(L1Loss, self).__init__(reduction)
|
|
251
|
-
self.
|
|
249
|
+
self.reduction = reduction
|
|
252
250
|
|
|
253
251
|
def construct(self, logits, labels):
|
|
254
|
-
|
|
255
|
-
_check_is_tensor('labels', labels, self.cls_name)
|
|
256
|
-
x = self.abs(logits - labels)
|
|
257
|
-
return self.get_loss(x)
|
|
252
|
+
return F.l1_loss(logits, labels, self.reduction)
|
|
258
253
|
|
|
259
254
|
|
|
260
255
|
class MSELoss(LossBase):
|
|
@@ -464,23 +459,23 @@ class MarginRankingLoss(LossBase):
|
|
|
464
459
|
r"""
|
|
465
460
|
MarginRankingLoss creates a criterion that measures the loss.
|
|
466
461
|
|
|
467
|
-
Given two tensors :math:`
|
|
462
|
+
Given two tensors :math:`input1`, :math:`input2` and a Tensor label :math:`target` with values 1 or -1,
|
|
468
463
|
the operation is as follows:
|
|
469
464
|
|
|
470
465
|
.. math::
|
|
471
|
-
\text{loss}(
|
|
466
|
+
\text{loss}(input1, input2, target) = \max(0, -target * (input1 - input2) + \text{margin})
|
|
472
467
|
|
|
473
468
|
Args:
|
|
474
|
-
margin (float): Specify the adjustment factor of the operation. Default 0.0.
|
|
475
|
-
reduction (str): Specifies which reduction to be applied to the output. It must be one of
|
|
476
|
-
|
|
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".
|
|
477
472
|
|
|
478
473
|
Inputs:
|
|
479
474
|
- **input1** (Tensor) - Tensor of shape :math:`(N, *)` where :math:`*` means, any number
|
|
480
475
|
of additional dimensions.
|
|
481
476
|
- **input2** (Tensor) - Tensor of shape :math:`(N, *)`, same shape and dtype as `input1`.
|
|
482
477
|
- **target** (Tensor) - Contains value 1 or -1. Suppose the shape of `input1` is
|
|
483
|
-
:math:`(x_1, x_2, x_3, ..., x_R)`, then the shape of `
|
|
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)`.
|
|
484
479
|
|
|
485
480
|
Outputs:
|
|
486
481
|
Tensor or Scalar. if `reduction` is "none", its shape is the same as `labels`.
|
|
@@ -670,11 +665,11 @@ class SoftmaxCrossEntropyWithLogits(LossBase):
|
|
|
670
665
|
Measures the distribution error between the probabilities of the input (computed with softmax function) and the
|
|
671
666
|
labels where the classes are mutually exclusive (only one class is positive) using cross entropy loss.
|
|
672
667
|
|
|
673
|
-
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)` ,
|
|
674
669
|
and the corresponding targets.
|
|
675
670
|
|
|
676
671
|
Typically, the input to this function is the fractional value of each category and the corresponding target value,
|
|
677
|
-
and the input format is (N, C).
|
|
672
|
+
and the input format is :math:`(N, C)` .
|
|
678
673
|
|
|
679
674
|
For each instance :math:`x_i`, i ranges from 0 to N-1, the loss is given as:
|
|
680
675
|
|
|
@@ -695,8 +690,8 @@ class SoftmaxCrossEntropyWithLogits(LossBase):
|
|
|
695
690
|
If "none", do not perform reduction. Default: "none".
|
|
696
691
|
|
|
697
692
|
Inputs:
|
|
698
|
-
- **logits** (Tensor) - Tensor of shape (N, C). Data type must be float16 or float32.
|
|
699
|
-
- **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
|
|
700
695
|
`labels` is int32 or int64. Otherwise, the type of `labels` is the same as the type of `logits`.
|
|
701
696
|
|
|
702
697
|
Outputs:
|
|
@@ -779,9 +774,8 @@ class DiceLoss(LossBase):
|
|
|
779
774
|
Default: 1e-5.
|
|
780
775
|
|
|
781
776
|
Inputs:
|
|
782
|
-
- **logits** (Tensor) -
|
|
783
|
-
|
|
784
|
-
- **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`.
|
|
785
779
|
The data type must be float16 or float32.
|
|
786
780
|
|
|
787
781
|
Outputs:
|
|
@@ -827,13 +821,13 @@ class DiceLoss(LossBase):
|
|
|
827
821
|
return dice_loss
|
|
828
822
|
|
|
829
823
|
|
|
830
|
-
@
|
|
824
|
+
@_primexpr
|
|
831
825
|
def _check_shape(logits_shape, label_shape, prim_name=None):
|
|
832
826
|
"""Internal function, used to check whether the shape of logits and labels meets the requirements."""
|
|
833
827
|
validator.check('logits_shape', logits_shape, 'label_shape', label_shape, prim_name=prim_name)
|
|
834
828
|
|
|
835
829
|
|
|
836
|
-
@
|
|
830
|
+
@_primexpr
|
|
837
831
|
def _check_ndim_multi(logits_dim, label_dim, prim_name=None):
|
|
838
832
|
"""Internal function, used to check whether the dimension of logits and label meets the requirements."""
|
|
839
833
|
msg_prefix = f'For \'{prim_name}\', the' if prim_name else "The"
|
|
@@ -843,7 +837,7 @@ def _check_ndim_multi(logits_dim, label_dim, prim_name=None):
|
|
|
843
837
|
raise ValueError(f"{msg_prefix} 'labels' dimension must be greater than 1, but got {label_dim}.")
|
|
844
838
|
|
|
845
839
|
|
|
846
|
-
@
|
|
840
|
+
@_primexpr
|
|
847
841
|
def _check_weights(weight_shape, label_shape, prim_name=None):
|
|
848
842
|
"""Internal function, used to check whether the reduced shape meets the requirements."""
|
|
849
843
|
msg_prefix = f'For \'{prim_name}\', the' if prim_name else "The"
|
|
@@ -1070,7 +1064,7 @@ class SampledSoftmaxLoss(LossBase):
|
|
|
1070
1064
|
def _softmax_cross_entropy(self, logits, targets):
|
|
1071
1065
|
stable_exp_logits = self.exp(logits - self.reduce_max_true(logits, 1))
|
|
1072
1066
|
pred = stable_exp_logits / self.reduce_sum_true(stable_exp_logits, 1)
|
|
1073
|
-
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)
|
|
1074
1068
|
|
|
1075
1069
|
def _compute_sampled_logits(self, weights,
|
|
1076
1070
|
biases,
|
|
@@ -1165,6 +1159,156 @@ class SampledSoftmaxLoss(LossBase):
|
|
|
1165
1159
|
return out_logits, out_labels
|
|
1166
1160
|
|
|
1167
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
|
+
|
|
1168
1312
|
class MultiMarginLoss(LossBase):
|
|
1169
1313
|
r"""
|
|
1170
1314
|
Creates a criterion that optimizes a multi-class classification hinge
|
|
@@ -1176,25 +1320,28 @@ class MultiMarginLoss(LossBase):
|
|
|
1176
1320
|
output :math:`y` is:
|
|
1177
1321
|
|
|
1178
1322
|
.. math::
|
|
1179
|
-
\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)}
|
|
1180
1324
|
|
|
1181
1325
|
where :math:`x \in \left\{0, \; \cdots , \; \text{x.size}(0) - 1\right\}`
|
|
1182
1326
|
and :math:`i \neq y`.
|
|
1183
1327
|
|
|
1184
|
-
Optionally, you can give non-equal weighting on the classes by passing
|
|
1185
|
-
a 1D input `weight` tensor w into the constructor.
|
|
1186
|
-
|
|
1187
1328
|
Args:
|
|
1188
|
-
p (int):
|
|
1189
|
-
margin (float):
|
|
1190
|
-
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.
|
|
1191
1340
|
|
|
1192
1341
|
Inputs:
|
|
1193
1342
|
- **x** (Tensor) - Input x, with shape :math:`(N, C)`. Data type only support float32, float16 or float64.
|
|
1194
1343
|
- **target** (Tensor) - Ground truth labels, with shape :math:`(N,)`. Data type only support int64. The
|
|
1195
1344
|
value of target should be non-negative, less than C.
|
|
1196
|
-
- **weight** (Tensor, optional) - The rescaling weight to each class with shape :math:`(C,)`. Data type only
|
|
1197
|
-
support float32, float16 or float64. Default: None.
|
|
1198
1345
|
|
|
1199
1346
|
Outputs:
|
|
1200
1347
|
Tensor, When `reduction` is 'none', the shape is :math:`(N,)`.
|
|
@@ -1214,7 +1361,7 @@ class MultiMarginLoss(LossBase):
|
|
|
1214
1361
|
ValueError: If rank of `x` is not 2 or rank of 'target' is not 1.
|
|
1215
1362
|
|
|
1216
1363
|
Supported Platforms:
|
|
1217
|
-
``Ascend``
|
|
1364
|
+
``Ascend`` ``GPU`` ``CPU``
|
|
1218
1365
|
|
|
1219
1366
|
Examples:
|
|
1220
1367
|
>>> x = Tensor(np.ones(shape=[3, 3]), mindspore.float32)
|
|
@@ -1225,20 +1372,23 @@ class MultiMarginLoss(LossBase):
|
|
|
1225
1372
|
0.6666667
|
|
1226
1373
|
"""
|
|
1227
1374
|
|
|
1228
|
-
def __init__(self, p=1, margin=1.0, reduction='mean'):
|
|
1375
|
+
def __init__(self, p=1, margin=1.0, reduction='mean', weight=None):
|
|
1229
1376
|
"""Initialize MultiMarginLoss."""
|
|
1230
1377
|
super(MultiMarginLoss, self).__init__()
|
|
1231
1378
|
self.multi_margin_loss = MultiMarginLossOp(p=p, margin=margin, reduction=reduction)
|
|
1232
|
-
self.
|
|
1379
|
+
self.generate_ones = ops.Fill()
|
|
1380
|
+
self.weight = weight
|
|
1233
1381
|
|
|
1234
1382
|
def construct(self, x, target, weight=None):
|
|
1235
1383
|
_check_is_tensor('x', x, self.cls_name)
|
|
1236
1384
|
_check_is_tensor('target', target, self.cls_name)
|
|
1385
|
+
if self.weight is not None:
|
|
1386
|
+
weight = self.weight
|
|
1237
1387
|
weight_one = weight is None
|
|
1238
1388
|
if not weight_one:
|
|
1239
1389
|
_check_is_tensor('weight', weight, self.cls_name)
|
|
1240
1390
|
else:
|
|
1241
|
-
weight = self.
|
|
1391
|
+
weight = self.generate_ones(x.dtype, x.astype('float32')[0].shape, 1)
|
|
1242
1392
|
loss = self.multi_margin_loss(x, target, weight)
|
|
1243
1393
|
return loss
|
|
1244
1394
|
|
|
@@ -1273,12 +1423,13 @@ class BCELoss(LossBase):
|
|
|
1273
1423
|
weight (Tensor, optional): A rescaling weight applied to the loss of each batch element.
|
|
1274
1424
|
And it must have the same shape and data type as `inputs`. Default: None
|
|
1275
1425
|
reduction (str): Specifies the reduction to be applied to the output.
|
|
1276
|
-
Its value must be one of 'none', 'mean', 'sum'. Default: '
|
|
1426
|
+
Its value must be one of 'none', 'mean', 'sum'. Default: 'mean'.
|
|
1277
1427
|
|
|
1278
1428
|
Inputs:
|
|
1279
1429
|
- **logits** (Tensor) - The input tensor with shape :math:`(N, *)` where :math:`*` means, any number
|
|
1280
1430
|
of additional dimensions. The data type must be float16 or float32.
|
|
1281
|
-
- **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`.
|
|
1282
1433
|
|
|
1283
1434
|
Outputs:
|
|
1284
1435
|
Tensor, has the same dtype as `logits`. if `reduction` is 'none', then it has the same shape as `logits`.
|
|
@@ -1302,9 +1453,9 @@ class BCELoss(LossBase):
|
|
|
1302
1453
|
1.8952923
|
|
1303
1454
|
"""
|
|
1304
1455
|
|
|
1305
|
-
def __init__(self, weight=None, reduction='
|
|
1456
|
+
def __init__(self, weight=None, reduction='mean'):
|
|
1306
1457
|
"""Initialize BCELoss."""
|
|
1307
|
-
super(BCELoss, self).__init__()
|
|
1458
|
+
super(BCELoss, self).__init__(reduction)
|
|
1308
1459
|
self.binary_cross_entropy = P.BinaryCrossEntropy(reduction=reduction)
|
|
1309
1460
|
self.weight_one = weight is None
|
|
1310
1461
|
if not self.weight_one:
|
|
@@ -1323,7 +1474,7 @@ class BCELoss(LossBase):
|
|
|
1323
1474
|
return loss
|
|
1324
1475
|
|
|
1325
1476
|
|
|
1326
|
-
@
|
|
1477
|
+
@_primexpr
|
|
1327
1478
|
def _check_reduced_shape_valid(ori_shape, reduced_shape, axis, cls_name, arg_name1, arg_name2):
|
|
1328
1479
|
"""Internal function, used to check whether the reduced shape meets the requirements."""
|
|
1329
1480
|
validator.check_reduce_shape(ori_shape, reduced_shape, axis, cls_name, arg_name1, arg_name2)
|
|
@@ -1381,7 +1532,7 @@ class CosineEmbeddingLoss(LossBase):
|
|
|
1381
1532
|
self.reduce_sum = P.ReduceSum()
|
|
1382
1533
|
self.maximum = P.Maximum()
|
|
1383
1534
|
validator.check_value_type("margin", margin, [float], self.cls_name)
|
|
1384
|
-
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)
|
|
1385
1536
|
|
|
1386
1537
|
def construct(self, logits_x1, logits_x2, labels):
|
|
1387
1538
|
_check_is_tensor('logits_x1', logits_x1, self.cls_name)
|
|
@@ -1409,12 +1560,12 @@ class CosineEmbeddingLoss(LossBase):
|
|
|
1409
1560
|
|
|
1410
1561
|
class MultilabelMarginLoss(LossBase):
|
|
1411
1562
|
r"""
|
|
1412
|
-
|
|
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.
|
|
1413
1567
|
|
|
1414
|
-
|
|
1415
|
-
hinge loss (margin-based loss) between input :math:`x` (a 2D mini-batch `Tensor`)
|
|
1416
|
-
and output :math:`y` (which is a 2D `Tensor` of target class indices).
|
|
1417
|
-
For each sample in the mini-batch:
|
|
1568
|
+
Each sample in the mini-batch, the loss is computed as follows:
|
|
1418
1569
|
|
|
1419
1570
|
.. math::
|
|
1420
1571
|
\text{loss}(x, y) = \sum_{ij}\frac{\max(0, 1 - (x[y[j]] - x[i]))}{\text{x.size}(0)}
|
|
@@ -1422,17 +1573,18 @@ class MultilabelMarginLoss(LossBase):
|
|
|
1422
1573
|
where :math:`x \in \left\{0, \; \cdots , \; \text{x.size}(0) - 1\right\}`, \
|
|
1423
1574
|
:math:`y \in \left\{0, \; \cdots , \; \text{y.size}(0) - 1\right\}`, \
|
|
1424
1575
|
:math:`0 \leq y[j] \leq \text{x.size}(0)-1`, \
|
|
1425
|
-
and :math:`i
|
|
1426
|
-
|
|
1427
|
-
:math:`y` and :math:`x` must have the same size.
|
|
1576
|
+
and for all :math:`i` and :math:`j`, :math:`i` does not equal to :math:`y[j]`.
|
|
1428
1577
|
|
|
1429
|
-
|
|
1430
|
-
starts at the front.
|
|
1578
|
+
Furthermore, both :math:`y` and :math:`x` should have identical sizes.
|
|
1431
1579
|
|
|
1432
|
-
|
|
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.
|
|
1433
1584
|
|
|
1434
1585
|
Args:
|
|
1435
|
-
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".
|
|
1436
1588
|
|
|
1437
1589
|
Inputs:
|
|
1438
1590
|
- **x** (Tensor) - Predict data. Tensor of shape :math:`(C)` or :math:`(N, C)`, where :math:`N`
|
|
@@ -1443,8 +1595,6 @@ class MultilabelMarginLoss(LossBase):
|
|
|
1443
1595
|
Outputs:
|
|
1444
1596
|
- **y** (Union[Tensor, Scalar]) - The loss of MultilabelMarginLoss. If `reduction` is "none", its shape
|
|
1445
1597
|
is :math:`(N)`. Otherwise, a scalar value will be returned.
|
|
1446
|
-
- **is_target** (Tensor) - Output tensor for backward input, with the same shape as `target`,
|
|
1447
|
-
data type must be int32.
|
|
1448
1598
|
|
|
1449
1599
|
Raises:
|
|
1450
1600
|
TypeError: If `x` or `target` is not a Tensor.
|
|
@@ -1463,8 +1613,7 @@ class MultilabelMarginLoss(LossBase):
|
|
|
1463
1613
|
>>> target = Tensor(np.array([[1, 2, 0, 3], [2, 3, -1, 1]]), mindspore.int32)
|
|
1464
1614
|
>>> output = loss(x, target)
|
|
1465
1615
|
>>> print(output)
|
|
1466
|
-
|
|
1467
|
-
[[1, 1, 1, 1], [0, 0, 1, 1]]))
|
|
1616
|
+
Tensor(shape=[], dtype=Float32, value=0.325)
|
|
1468
1617
|
"""
|
|
1469
1618
|
|
|
1470
1619
|
def __init__(self, reduction='mean'):
|
|
@@ -1472,7 +1621,8 @@ class MultilabelMarginLoss(LossBase):
|
|
|
1472
1621
|
self.multilabel_margin_loss = MultilabelMarginLossOp(reduction=reduction)
|
|
1473
1622
|
|
|
1474
1623
|
def construct(self, x, target):
|
|
1475
|
-
|
|
1624
|
+
loss, _ = self.multilabel_margin_loss(x, target)
|
|
1625
|
+
return loss
|
|
1476
1626
|
|
|
1477
1627
|
|
|
1478
1628
|
class BCEWithLogitsLoss(LossBase):
|
|
@@ -1510,7 +1660,8 @@ class BCEWithLogitsLoss(LossBase):
|
|
|
1510
1660
|
Inputs:
|
|
1511
1661
|
- **logits** (Tensor) - Input logits with shape :math:`(N, *)` where :math:`*` means, any number
|
|
1512
1662
|
of additional dimensions. The data type must be float16 or float32.
|
|
1513
|
-
- **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`.
|
|
1514
1665
|
|
|
1515
1666
|
Outputs:
|
|
1516
1667
|
Tensor or Scalar, if `reduction` is 'none', its shape is the same as `logits`.
|
|
@@ -1566,7 +1717,7 @@ class BCEWithLogitsLoss(LossBase):
|
|
|
1566
1717
|
return loss
|
|
1567
1718
|
|
|
1568
1719
|
|
|
1569
|
-
@
|
|
1720
|
+
@_primexpr
|
|
1570
1721
|
def _check_ndim(logits_nidm, labels_ndim, prime_name=None):
|
|
1571
1722
|
'''Internal function, used to check whether the dimension of logits and labels meets the requirements.'''
|
|
1572
1723
|
msg_prefix = f'For \'{prime_name}\', the' if prime_name else "The"
|
|
@@ -1581,7 +1732,7 @@ def _check_ndim(logits_nidm, labels_ndim, prime_name=None):
|
|
|
1581
1732
|
f"dimension of 'logits' {logits_nidm} and dimension of 'labels' {labels_ndim}.")
|
|
1582
1733
|
|
|
1583
1734
|
|
|
1584
|
-
@
|
|
1735
|
+
@_primexpr
|
|
1585
1736
|
def _check_channel_and_shape(logits, labels, prime_name=None):
|
|
1586
1737
|
'''Internal function, used to check whether the channels or shape of logits and labels meets the requirements.'''
|
|
1587
1738
|
msg_prefix = f'For \'{prime_name}\', the' if prime_name else "The"
|
|
@@ -1711,18 +1862,6 @@ class FocalLoss(LossBase):
|
|
|
1711
1862
|
return self.get_loss(loss)
|
|
1712
1863
|
|
|
1713
1864
|
|
|
1714
|
-
@constexpr
|
|
1715
|
-
def _dtype_check(logits_dtype, labels_dtype, prim_name):
|
|
1716
|
-
"""Check dtype."""
|
|
1717
|
-
if logits_dtype not in [mstype.float32, mstype.float16]:
|
|
1718
|
-
raise TypeError("For {}, the logits_dtype must be float32 or float16, but got {}.".format(prim_name,
|
|
1719
|
-
logits_dtype))
|
|
1720
|
-
if logits_dtype != labels_dtype:
|
|
1721
|
-
raise TypeError("For {}, the labels_dtype must equal to logits_dtype {}, but got {}".format(prim_name,
|
|
1722
|
-
logits_dtype,
|
|
1723
|
-
labels_dtype))
|
|
1724
|
-
|
|
1725
|
-
|
|
1726
1865
|
class HuberLoss(LossBase):
|
|
1727
1866
|
r"""
|
|
1728
1867
|
HuberLoss calculate the error between the predicted value and the target value.
|
|
@@ -1803,47 +1942,28 @@ class HuberLoss(LossBase):
|
|
|
1803
1942
|
def __init__(self, reduction='mean', delta=1.0):
|
|
1804
1943
|
"""Initialize HuberLoss."""
|
|
1805
1944
|
super(HuberLoss, self).__init__(reduction=reduction)
|
|
1806
|
-
|
|
1807
|
-
validator.check_number("delta", delta, 0.0, Rel.GT, self.cls_name)
|
|
1808
|
-
self.sub = P.Sub()
|
|
1809
|
-
self.mul = P.Mul()
|
|
1810
|
-
self.abs = P.Abs()
|
|
1811
|
-
self.less = P.Less()
|
|
1812
|
-
self.square = P.Square()
|
|
1813
|
-
self.select = P.Select()
|
|
1814
|
-
self.dtype = P.DType()
|
|
1945
|
+
self.reduction = reduction
|
|
1815
1946
|
self.delta = delta
|
|
1816
|
-
self.delta_half = 0.5 * self.delta
|
|
1817
1947
|
|
|
1818
1948
|
def construct(self, logits, labels):
|
|
1819
|
-
|
|
1820
|
-
_check_is_tensor('labels', labels, self.cls_name)
|
|
1821
|
-
logits_dtype = self.dtype(logits)
|
|
1822
|
-
labels_dtype = self.dtype(labels)
|
|
1823
|
-
_dtype_check(logits_dtype, labels_dtype, self.cls_name)
|
|
1824
|
-
z = self.abs(self.sub(logits, labels))
|
|
1825
|
-
condition = self.less(z, self.delta)
|
|
1826
|
-
l1 = self.mul(0.5, self.square(z))
|
|
1827
|
-
l2 = self.mul(self.delta, self.sub(z, self.delta_half))
|
|
1828
|
-
loss = self.select(condition, l1, l2)
|
|
1829
|
-
|
|
1830
|
-
return self.get_loss(loss)
|
|
1949
|
+
return F.huber_loss(logits, labels, self.reduction, self.delta)
|
|
1831
1950
|
|
|
1832
1951
|
|
|
1833
1952
|
class TripletMarginLoss(LossBase):
|
|
1834
1953
|
r"""
|
|
1835
1954
|
TripletMarginLoss operation.
|
|
1836
1955
|
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
is composed by `a`, `p` and `n` (i.e., `anchor`, `positive examples` and `negative
|
|
1841
|
-
examples` respectively). The shapes of all input tensors should be
|
|
1842
|
-
:math:`(N, D)`.
|
|
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.
|
|
1843
1959
|
|
|
1844
|
-
The
|
|
1845
|
-
|
|
1846
|
-
|
|
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.
|
|
1962
|
+
|
|
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.
|
|
1847
1967
|
|
|
1848
1968
|
The loss function for each sample in the mini-batch is:
|
|
1849
1969
|
|
|
@@ -1856,28 +1976,30 @@ class TripletMarginLoss(LossBase):
|
|
|
1856
1976
|
d(x_i, y_i) = \left\lVert {\bf x}_i - {\bf y}_i \right\rVert_p
|
|
1857
1977
|
|
|
1858
1978
|
Args:
|
|
1859
|
-
p (int): The norm
|
|
1860
|
-
eps (float): Default: 1e-06.
|
|
1861
|
-
swap (bool): The distance swap
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
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.
|
|
1865
1986
|
|
|
1866
1987
|
Inputs:
|
|
1867
1988
|
- **x** (Tensor) - A sample randomly selected from the training set. Data type must be BasicType.
|
|
1868
|
-
|
|
1869
|
-
- **
|
|
1870
|
-
|
|
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.
|
|
1871
1996
|
|
|
1872
1997
|
Outputs:
|
|
1873
|
-
|
|
1874
|
-
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.
|
|
1875
1999
|
|
|
1876
2000
|
Raises:
|
|
1877
|
-
TypeError: If `x` or `positive` or 'negative'
|
|
1878
|
-
TypeError: If dtype of `x` or `positive` or `negative` is not BasicType.
|
|
2001
|
+
TypeError: If `x` or `positive` or 'negative' is not a Tensor.
|
|
1879
2002
|
TypeError: If dtype of `x`, `positive` and `negative` is not the same.
|
|
1880
|
-
TypeError: If `margin` is not float32.
|
|
1881
2003
|
TypeError: If `p` is not an int.
|
|
1882
2004
|
TypeError: If `eps` is not a float.
|
|
1883
2005
|
TypeError: If `swap` is not a bool.
|
|
@@ -1888,25 +2010,31 @@ class TripletMarginLoss(LossBase):
|
|
|
1888
2010
|
ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
|
|
1889
2011
|
|
|
1890
2012
|
Supported Platforms:
|
|
1891
|
-
``
|
|
2013
|
+
``GPU``
|
|
1892
2014
|
|
|
1893
2015
|
Examples:
|
|
1894
2016
|
>>> loss = nn.TripletMarginLoss()
|
|
1895
2017
|
>>> x = Tensor(np.array([[0.3, 0.7], [0.5, 0.5]]), mindspore.float32)
|
|
1896
2018
|
>>> positive = Tensor(np.array([[0.4, 0.6], [0.4, 0.6]]), mindspore.float32)
|
|
1897
2019
|
>>> negative = Tensor(np.array([[0.2, 0.9], [0.3, 0.7]]), mindspore.float32)
|
|
1898
|
-
>>>
|
|
1899
|
-
>>> output = loss(x, positive, negative, margin)
|
|
2020
|
+
>>> output = loss(x, positive, negative)
|
|
1900
2021
|
>>> print(output)
|
|
1901
2022
|
0.8881968
|
|
1902
2023
|
"""
|
|
1903
2024
|
|
|
1904
|
-
def __init__(self, p=2, swap=False, eps=1e-
|
|
2025
|
+
def __init__(self, p=2, swap=False, eps=1e-06, reduction='mean', margin=1.):
|
|
1905
2026
|
super(TripletMarginLoss, self).__init__()
|
|
1906
|
-
self.
|
|
2027
|
+
self.p = p
|
|
2028
|
+
self.swap = swap
|
|
2029
|
+
self.eps = eps
|
|
2030
|
+
self.reduction = reduction
|
|
2031
|
+
self.margin = margin
|
|
1907
2032
|
|
|
1908
|
-
def construct(self, x, positive, negative, margin):
|
|
1909
|
-
|
|
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)
|
|
1910
2038
|
|
|
1911
2039
|
|
|
1912
2040
|
@constexpr
|
|
@@ -1927,7 +2055,7 @@ class NLLLoss(LossBase):
|
|
|
1927
2055
|
r"""
|
|
1928
2056
|
Gets the negative log likelihood loss between logits and labels.
|
|
1929
2057
|
|
|
1930
|
-
The nll loss with reduction=none can be described as:
|
|
2058
|
+
The nll loss with :math:`reduction = none` can be described as:
|
|
1931
2059
|
|
|
1932
2060
|
.. math::
|
|
1933
2061
|
|
|
@@ -2020,11 +2148,12 @@ def _check_cross_entropy_inputs(logits_shape, label_shape,
|
|
|
2020
2148
|
f"but get 'logits' shape: {logits_shape} and 'labels' shape: {label_shape}.")
|
|
2021
2149
|
elif label_rank == logits_rank - 1:
|
|
2022
2150
|
validator.check_type_name('labels', label_dtype, [mstype.int32], prim_name)
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
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}.")
|
|
2028
2157
|
else:
|
|
2029
2158
|
raise ValueError(f"{msg_prefix} rank of 'logits' and 'labels' should be:\n"
|
|
2030
2159
|
f"1. 'logits.ndim == labels.ndim' for probabilities, \n"
|
|
@@ -2141,7 +2270,7 @@ class CrossEntropyLoss(LossBase):
|
|
|
2141
2270
|
super().__init__(reduction)
|
|
2142
2271
|
validator.check_value_type('ignore_index', ignore_index, int, self.cls_name)
|
|
2143
2272
|
validator.check_value_type('label_smoothing', label_smoothing, float, self.cls_name)
|
|
2144
|
-
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)
|
|
2145
2274
|
|
|
2146
2275
|
if weight is not None:
|
|
2147
2276
|
validator.check_value_type("weight", weight, [Tensor], self.cls_name)
|
|
@@ -2166,7 +2295,7 @@ class CrossEntropyLoss(LossBase):
|
|
|
2166
2295
|
|
|
2167
2296
|
class KLDivLoss(LossBase):
|
|
2168
2297
|
r"""
|
|
2169
|
-
Computes the Kullback-Leibler divergence between the logits and the labels
|
|
2298
|
+
Computes the Kullback-Leibler divergence between the `logits` and the `labels`.
|
|
2170
2299
|
|
|
2171
2300
|
For tensors of the same shape :math:`x` and :math:`target`,
|
|
2172
2301
|
the updating formulas of KLDivLoss algorithm are as follows,
|
|
@@ -2240,41 +2369,44 @@ class KLDivLoss(LossBase):
|
|
|
2240
2369
|
|
|
2241
2370
|
class CTCLoss(LossBase):
|
|
2242
2371
|
"""
|
|
2243
|
-
Calculates the CTC (Connectionist Temporal Classification) loss.
|
|
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.
|
|
2244
2374
|
|
|
2245
2375
|
For the CTC algorithm, refer to `Connectionist Temporal Classification: Labeling Unsegmented Sequence Data with
|
|
2246
2376
|
Recurrent Neural Networks <http://www.cs.toronto.edu/~graves/icml_2006.pdf>`_ .
|
|
2247
2377
|
|
|
2248
2378
|
Args:
|
|
2249
|
-
blank (int): The blank label. Default: 0.
|
|
2250
|
-
reduction (str):
|
|
2251
|
-
|
|
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.
|
|
2252
2384
|
|
|
2253
2385
|
Inputs:
|
|
2254
|
-
- **log_probs** (Tensor) - A tensor of shape (T, N, C) or (T, C)
|
|
2255
|
-
C is number of classes
|
|
2256
|
-
- **targets** (Tensor) - A tensor of shape (N, S) or (sum( `target_lengths` )),
|
|
2257
|
-
means the target sequences.
|
|
2258
|
-
- **input_lengths** (Union[tuple, Tensor
|
|
2259
|
-
|
|
2260
|
-
- **target_lengths** (Union[tuple, Tensor, int]) - A tuple or Tensor of shape(N), or a number.
|
|
2261
|
-
It means the lengths of the target.
|
|
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.
|
|
2262
2392
|
|
|
2263
2393
|
Outputs:
|
|
2264
2394
|
- **neg_log_likelihood** (Tensor) - A loss value which is differentiable with respect to each input node.
|
|
2265
2395
|
|
|
2266
2396
|
Raises:
|
|
2397
|
+
TypeError: If `log_probs` or `targets` is not a Tensor.
|
|
2267
2398
|
TypeError: If `zero_infinity` is not a bool, `reduction` is not string.
|
|
2268
2399
|
TypeError: If the dtype of `log_probs` is not float or double.
|
|
2269
2400
|
TypeError: If the dtype of `targets`, `input_lengths` or `target_lengths` is not int32 or int64.
|
|
2270
2401
|
ValueError: If `reduction` is not "none", "mean" or "sum".
|
|
2271
2402
|
ValueError: If the types of `targets`, `input_lengths` or `target_lengths` are different.
|
|
2272
2403
|
ValueError: If the value of `blank` is not in range [0, C). C is number of classes of `log_probs` .
|
|
2273
|
-
ValueError: If
|
|
2274
|
-
|
|
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]].
|
|
2275
2407
|
|
|
2276
2408
|
Supported Platforms:
|
|
2277
|
-
``Ascend`` ``CPU``
|
|
2409
|
+
``Ascend`` ``GPU`` ``CPU``
|
|
2278
2410
|
|
|
2279
2411
|
Examples:
|
|
2280
2412
|
>>> import numpy as np
|
|
@@ -2297,62 +2429,65 @@ class CTCLoss(LossBase):
|
|
|
2297
2429
|
>>> ctc_loss = CTCLoss(blank=0, reduction='none', zero_infinity=False)
|
|
2298
2430
|
>>> loss = ctc_loss(ms_input, target, input_lengths, target_lengths)
|
|
2299
2431
|
>>> print(loss)
|
|
2300
|
-
|
|
2432
|
+
[-45.79497 -55.794968]
|
|
2301
2433
|
>>> arr = np.arange(T*C).reshape((T, C))
|
|
2302
2434
|
>>> ms_input = Tensor(arr, dtype=mstype.float32)
|
|
2303
|
-
>>> input_lengths = T
|
|
2304
|
-
>>> target_lengths = S_min
|
|
2435
|
+
>>> input_lengths = Tensor([T], dtype=mstype.int32)
|
|
2436
|
+
>>> target_lengths = Tensor([S_min], dtype=mstype.int32)
|
|
2305
2437
|
>>> target = np.random.randint(1, C, size=(S_min,))
|
|
2306
2438
|
>>> target = Tensor(target, dtype=mstype.int32)
|
|
2307
2439
|
>>> ctc_loss = CTCLoss(blank=0, reduction='none', zero_infinity=False)
|
|
2308
2440
|
>>> loss = ctc_loss(ms_input, target, input_lengths, target_lengths)
|
|
2309
2441
|
>>> print(loss)
|
|
2310
|
-
|
|
2442
|
+
-25.794968
|
|
2311
2443
|
"""
|
|
2312
2444
|
|
|
2313
2445
|
def __init__(self, blank=0, reduction='mean', zero_infinity=False):
|
|
2314
|
-
super().__init__(
|
|
2315
|
-
self.
|
|
2446
|
+
super().__init__()
|
|
2447
|
+
self.blank = blank
|
|
2448
|
+
self.reduction = reduction
|
|
2449
|
+
self.zero_infinity = zero_infinity
|
|
2316
2450
|
|
|
2317
2451
|
def construct(self, log_probs, targets, input_lengths, target_lengths):
|
|
2318
|
-
|
|
2319
|
-
|
|
2320
|
-
|
|
2321
|
-
targets
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2330
|
-
|
|
2331
|
-
return
|
|
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
|
|
2332
2466
|
|
|
2333
2467
|
|
|
2334
2468
|
class GaussianNLLLoss(LossBase):
|
|
2335
|
-
r"""
|
|
2469
|
+
r"""
|
|
2470
|
+
Gaussian negative log likelihood loss.
|
|
2336
2471
|
|
|
2337
|
-
The
|
|
2338
|
-
neural network. For
|
|
2339
|
-
`
|
|
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:
|
|
2340
2475
|
|
|
2341
2476
|
.. math::
|
|
2342
2477
|
\text{loss} = \frac{1}{2}\left(\log\left(\text{max}\left(\text{var},
|
|
2343
2478
|
\ \text{eps}\right)\right) + \frac{\left(\text{logits} - \text{labels}\right)^2}
|
|
2344
2479
|
{\text{max}\left(\text{var}, \ \text{eps}\right)}\right) + \text{const.}
|
|
2345
2480
|
|
|
2346
|
-
where
|
|
2347
|
-
|
|
2348
|
-
|
|
2349
|
-
(with all other sizes being the same) for correct broadcasting.
|
|
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.
|
|
2350
2484
|
|
|
2351
|
-
Args:
|
|
2352
|
-
full (bool):
|
|
2353
|
-
`const.` will be :math:`0.5 * log(2\pi)`. Default: False.
|
|
2354
|
-
eps (float): Used to improve the stability of log function. Default: 1e-6.
|
|
2355
|
-
reduction (str): Apply specific reduction method to the output: 'none', 'mean', or 'sum'.
|
|
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'.
|
|
2356
2491
|
|
|
2357
2492
|
Inputs:
|
|
2358
2493
|
- **logits** (Tensor) - Tensor of shape :math:`(N, *)` or :math:`(*)` where :math:`*` means any number of
|
|
@@ -2364,14 +2499,14 @@ class GaussianNLLLoss(LossBase):
|
|
|
2364
2499
|
(to allow for broadcasting).
|
|
2365
2500
|
|
|
2366
2501
|
Returns:
|
|
2367
|
-
Tensor or Tensor scalar, the computed loss depending on
|
|
2502
|
+
Tensor or Tensor scalar, the computed loss depending on :math:`reduction`.
|
|
2368
2503
|
|
|
2369
2504
|
Raises:
|
|
2370
2505
|
TypeError: If `logits` is not a Tensor.
|
|
2371
2506
|
TypeError: If `labels` is not a Tensor.
|
|
2372
2507
|
TypeError: If `full` is not a bool.
|
|
2373
2508
|
TypeError: If `eps` is not a float.
|
|
2374
|
-
ValueError: If `eps` is not a float within
|
|
2509
|
+
ValueError: If `eps` is not a float within (0, inf).
|
|
2375
2510
|
ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
|
|
2376
2511
|
|
|
2377
2512
|
Supported Platforms:
|
|
@@ -2401,7 +2536,7 @@ class GaussianNLLLoss(LossBase):
|
|
|
2401
2536
|
|
|
2402
2537
|
def __init__(self, *, full=False, eps=1e-6, reduction='mean'):
|
|
2403
2538
|
super(GaussianNLLLoss, self).__init__()
|
|
2404
|
-
validator.check_float_range(eps, 0, float('inf'),
|
|
2539
|
+
validator.check_float_range(eps, 0, float('inf'), validator.INC_NEITHER, "eps", self.cls_name)
|
|
2405
2540
|
validator.check_value_type('full', full, [bool], self.cls_name)
|
|
2406
2541
|
validator.check_string(reduction, ['none', 'mean', 'sum'], 'reduction', 'gaussian_nll_loss')
|
|
2407
2542
|
self.full = full
|
|
@@ -2417,9 +2552,8 @@ class GaussianNLLLoss(LossBase):
|
|
|
2417
2552
|
|
|
2418
2553
|
class HingeEmbeddingLoss(LossBase):
|
|
2419
2554
|
r"""
|
|
2420
|
-
Hinge Embedding Loss
|
|
2421
|
-
|
|
2422
|
-
This is usually used for measuring the similarity between two inputs.
|
|
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.
|
|
2423
2557
|
|
|
2424
2558
|
The loss function for :math:`n`-th sample in the mini-batch is
|
|
2425
2559
|
|
|
@@ -2440,29 +2574,30 @@ class HingeEmbeddingLoss(LossBase):
|
|
|
2440
2574
|
where :math:`L = \{l_1,\dots,l_N\}^\top`.
|
|
2441
2575
|
|
|
2442
2576
|
Args:
|
|
2443
|
-
margin (float): Threshold defined by Hinge Embedding Loss :math:`margin`.
|
|
2577
|
+
margin (float, int): Threshold defined by Hinge Embedding Loss :math:`margin`.
|
|
2444
2578
|
Represented as :math:`\Delta` in the formula. Default: 1.0.
|
|
2445
2579
|
reduction (str): Specify the computing method to be applied to the outputs: 'none', 'mean', or 'sum'.
|
|
2446
2580
|
Default: 'mean'.
|
|
2447
2581
|
|
|
2448
2582
|
Inputs:
|
|
2449
|
-
- **logits** (Tensor) -
|
|
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.
|
|
2450
2585
|
- **labels** (Tensor) - Same shape as the logits, contains -1 or 1.
|
|
2451
2586
|
|
|
2452
2587
|
Returns:
|
|
2453
|
-
Tensor or Tensor scalar, the computed loss depending on
|
|
2588
|
+
Tensor or Tensor scalar, the computed loss depending on :math:`reduction`.
|
|
2454
2589
|
|
|
2455
2590
|
Raises:
|
|
2456
2591
|
TypeError: If `logits` is not a Tensor.
|
|
2457
2592
|
TypeError: If `labels` is not a Tensor.
|
|
2458
|
-
TypeError: If `margin` is not a float.
|
|
2459
|
-
ValueError: If `labels` does not have the same shape as `logits
|
|
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.
|
|
2460
2595
|
ValueError: If `reduction` is not one of 'none', 'mean', 'sum'.
|
|
2461
2596
|
|
|
2462
2597
|
Supported Platforms:
|
|
2463
2598
|
``Ascend`` ``GPU`` ``CPU``
|
|
2464
2599
|
|
|
2465
|
-
|
|
2600
|
+
Examples:
|
|
2466
2601
|
>>> import numpy as np
|
|
2467
2602
|
>>> from mindspore import Tensor
|
|
2468
2603
|
>>> import mindspore.nn as nn
|
|
@@ -2479,7 +2614,7 @@ class HingeEmbeddingLoss(LossBase):
|
|
|
2479
2614
|
|
|
2480
2615
|
def __init__(self, margin=1.0, reduction='mean'):
|
|
2481
2616
|
super(HingeEmbeddingLoss, self).__init__()
|
|
2482
|
-
validator.check_value_type('margin', margin, [float], self.cls_name)
|
|
2617
|
+
validator.check_value_type('margin', margin, [float, int], self.cls_name)
|
|
2483
2618
|
validator.check_string(reduction, ['none', 'sum', 'mean'], 'reduction', self.cls_name)
|
|
2484
2619
|
self.margin = margin
|
|
2485
2620
|
self.reduction = reduction
|