1# Copyright 2023-2024 Huawei Technologies Co., Ltd 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================ 15 16"""Defines nn operators with functional form.""" 17from __future__ import absolute_import 18from math import pi, log, floor 19 20from mindspore import context 21from mindspore import log as logger 22import mindspore.ops as ops 23from mindspore.ops.primitive import constexpr, _primexpr 24from mindspore.ops import operations as P 25from mindspore.ops import functional as F 26from mindspore.ops.operations import nn_ops as NN_OPS 27from mindspore.ops.operations import _sequence_ops as seq 28import mindspore.common.dtype as mstype 29from mindspore.ops.function.math_func import logsumexp 30from mindspore.ops.function.random_func import _get_seed, _set_prim_op_user_data 31from mindspore.common.tensor import Tensor 32from mindspore.common.parameter import Parameter 33from mindspore._c_expression import Tensor as Tensor_ 34from mindspore.ops._primitive_cache import _get_cache_prim 35from mindspore import _checkparam as validator 36from mindspore.ops.composite.multitype_ops._constexpr_utils import raise_value_error 37from mindspore.ops.operations.nn_ops import MaxUnpool2D, MaxUnpool3D 38from mindspore.ops.operations.nn_ops import FractionalMaxPoolWithFixedKsize, FractionalMaxPool3DWithFixedKsize 39from mindspore.ops.operations.nn_ops import PadV3 40from mindspore.ops.operations.nn_ops import ChannelShuffle 41from mindspore.ops.operations.nn_ops import TripletMarginLoss 42from mindspore.ops.operations._sequence_ops import TupleToTensor, TensorToTuple, ListToTensor 43from mindspore.common.api import _function_forbid_reuse 44from mindspore.ops.auto_generate import log_softmax, dense, prelu, celu, relu, fast_gelu, silu, elu, sigmoid, relu6 45from mindspore.ops.auto_generate import group_norm_op, rms_norm, layer_norm_ext_op, batch_norm_ext_op 46from mindspore.ops.auto_generate import (reflection_pad_1d_op, reflection_pad_2d_op, reflection_pad_3d_op, # pylint: disable=W0611 47 replication_pad_1d_op, replication_pad_2d_op, replication_pad_3d_op, 48 constant_pad_nd_op, dropout_ext_op, reverse_v2_impl) 49from mindspore.ops.auto_generate.gen_ops_prim import embedding_op, Convolution 50from mindspore.common.generator import default_generator 51 52abs_ = P.Abs() 53add_ = P.Add() 54bias_add_ = P.BiasAdd() 55cast_ = P.Cast() 56div_ = P.Div() 57dtype_ = P.DType() 58equal_ = P.Equal() 59erf_ = P.Erf() 60exp_ = P.Exp() 61expand_dims_ = P.ExpandDims() 62fillv2_ = P.FillV2() 63gather_ = P.Gather() 64gather_d_ = P.GatherD() 65gelu_ = P.GeLU() 66greater_ = P.Greater() 67hardswish_ = P.HSwish() 68less_ = P.Less() 69list_to_tensor_ = ListToTensor() 70log_ = P.Log() 71matmul_ = P.MatMul() 72maximum_ = P.Maximum() 73minimum_ = P.Minimum() 74mish_ = NN_OPS.Mish() 75mul_ = P.Mul() 76neg_ = P.Neg() 77ones_like_ = P.OnesLike() 78reduce_mean_ = P.ReduceMean() 79reduce_sum_ = P.ReduceSum() 80reshape_ = P.Reshape() 81scalar_to_tensor_ = P.ScalarToTensor() 82select_ = P.Select() 83selu_ = NN_OPS.SeLU() 84shape_ = P.Shape() 85sigmoid_ = P.Sigmoid() 86sign_ = P.Sign() 87slice_ = P.Slice() 88softplus_ = P.Softplus() 89softsign_ = P.Softsign() 90sqrt_ = P.Sqrt() 91square_ = P.Square() 92sub_ = P.Sub() 93tensor_shape_ = P.TensorShape() 94tensor_to_tuple_ = TensorToTuple() 95transpose_ = P.Transpose() 96tuple_to_tensor_ = TupleToTensor() 97 98check_positive_int_const = validator.check_positive_int 99check_positive_int_sequence_const = validator.check_positive_int_sequence 100check_positive_float_const = validator.check_positive_float 101check_positive_float_sequence_const = validator.check_positive_float_sequence 102check_bool_const = constexpr(validator.check_bool) 103check_int_const = validator.check_is_int 104check_non_negative_float_const = validator.check_non_negative_float 105check_string_const = constexpr(validator.check_string) 106 107generator_step_ = Tensor(1, mstype.int64) 108 109def adaptive_avg_pool2d(input, output_size): 110 r""" 111 Performs 2D adaptive average pooling on a multi-plane input signal. 112 That is, for any input size, the size of the specified output is H x W. 113 The number of output features is equal to the number of input features. 114 115 The input and output data format can be "NCHW" and "CHW". N is the batch size, C is the number of channels, 116 H is the feature height, and W is the feature width. 117 118 For adaptive average pooling for 2D: 119 120 .. math:: 121 \begin{align} 122 h_{start} &= floor(i * H_{in} / H_{out})\\ 123 h_{end} &= ceil((i + 1) * H_{in} / H_{out})\\ 124 w_{start} &= floor(j * W_{in} / W_{out})\\ 125 w_{end} &= ceil((j + 1) * W_{in} / W_{out})\\ 126 Output(i,j) &= \frac{\sum Input[h_{start}:h_{end}, w_{start}:w_{end}]}{(h_{end}- h_{start}) 127 * (w_{end}- w_{start})} 128 \end{align} 129 130 .. warning:: 131 This is an experimental API that is subject to change or deletion. 132 133 Args: 134 input (Tensor): The input of adaptive_avg_pool2d, which is a 3D or 4D tensor, 135 with float16, float32 or float64 data type. 136 output_size (Union[int, tuple]): The target output size. `output_size` can be a tuple :math:`(H, W)`, 137 or an int H for :math:`(H, H)`. :math:`H` and :math:`W` can be int or None. 138 If it is None, it means the output size is the same as the input size. 139 140 Returns: 141 Tensor, with the same type as the `input`. 142 143 Shape of the output is `input_shape[:len(input_shape) - len(out_shape)] + out_shape`. 144 145 .. math:: 146 147 out\_shape = \begin{cases} 148 input\_shape[-2] + output\_size[1], & \text{if } output\_size text{ is (None, w);}\\ 149 output\_size[0] + input\_shape[-1], & \text{if } output\_size text{ is (h, None);}\\ 150 input\_shape[-2:], & \text{if } output\_size text{ is (None, None);}\\ 151 (h, h), & \text{if } output\_size text{ is h;}\\ 152 (h, w), & \text{if } output\_size text{ is (h, w)} 153 \end{cases} 154 155 Raises: 156 ValueError: If `output_size` is a tuple and the length of `output_size` is not 2. 157 TypeError: If `input` is not a Tensor. 158 TypeError: If dtype of `input` is not float16, float32 or float64. 159 ValueError: If the dimension of `input` is less than or equal to the dimension of `output_size`. 160 161 Supported Platforms: 162 ``Ascend`` ``GPU`` ``CPU`` 163 164 Examples: 165 >>> import mindspore 166 >>> import numpy as np 167 >>> from mindspore import Tensor, ops 168 >>> # case 1: output_size=(None, 2) 169 >>> input = Tensor(np.array([[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], 170 ... [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], 171 ... [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]), mindspore.float32) 172 >>> output = ops.adaptive_avg_pool2d(input, (None, 2)) 173 >>> print(output) 174 [[[1.5 2.5] 175 [4.5 5.5] 176 [7.5 8.5]] 177 [[1.5 2.5] 178 [4.5 5.5] 179 [7.5 8.5]] 180 [[1.5 2.5] 181 [4.5 5.5] 182 [7.5 8.5]]] 183 >>> # case 2: output_size=2 184 >>> output = ops.adaptive_avg_pool2d(input, 2) 185 >>> print(output) 186 [[[3. 4.] 187 [6. 7.]] 188 [[3. 4.] 189 [6. 7.]] 190 [[3. 4.] 191 [6. 7.]]] 192 >>> # case 3: output_size=(1, 2) 193 >>> output = ops.adaptive_avg_pool2d(input, (1, 2)) 194 >>> print(output) 195 [[[4.5 5.5]] 196 [[4.5 5.5]] 197 [[4.5 5.5]]] 198 """ 199 adaptive_avgpool2d_ = _get_cache_prim(P.AdaptiveAvgPool2D)(output_size) 200 return adaptive_avgpool2d_(input) 201 202 203def adaptive_avg_pool3d(input, output_size): 204 r""" 205 Performs 3D adaptive average pooling on a multi-plane input signal. 206 That is, for any input size, the size of the specified output is :math:`(D, H, W)`. 207 The number of output features is equal to the number of input planes. 208 209 Suppose the last 3 dimension size of x is :math:`(inD, inH, inW)`, the last 3 dimension size of output is 210 :math:`(outD, outH, outW)`. 211 212 .. math:: 213 \begin{array}{ll} \\ 214 \forall \quad od \in [0,outD-1], oh \in [0,outH-1], ow \in [0,outW-1]\\ 215 output[od,oh,ow] = \\ 216 \qquad mean(x[istartD:iendD+1,istartH:iendH+1,istartW:iendW+1])\\ 217 where,\\ 218 \qquad istartD= \left\lceil \frac{od * inD}{outD} \right\rceil \\ 219 \qquad iendD=\left\lfloor \frac{(od+1)* inD}{outD} \right\rfloor \\ 220 \qquad istartH=\left\lceil \frac{oh * inH}{outH} \right\rceil \\ 221 \qquad iendH=\left\lfloor \frac{(oh+1) * inH}{outH} \right\rfloor \\ 222 \qquad istartW=\left\lceil \frac{ow * inW}{outW} \right\rceil \\ 223 \qquad iendW=\left\lfloor \frac{(ow+1) * inW}{outW} \right\rfloor 224 \end{array} 225 226 Args: 227 input (Tensor): The input of adaptive_avg_pool3d, which is a 5D or 4D Tensor. 228 output_size (Union[int, tuple]): The target output size. `output_size` can be a tuple :math:`(D, H, W)`, 229 or an int D for :math:`(D, D, D)`. :math:`D`, :math:`H` and :math:`W` can be int or None 230 which means the output size is the same as that of the input. 231 232 Returns: 233 Tensor, with the same type as the `input`. 234 235 Raises: 236 TypeError: If `input` is not a Tensor. 237 TypeError: If dtype of `input` is not float16, float32 or float64. 238 ValueError: If the dimension of `input` is not 4D or 5D. 239 ValueError: If `output_size` value is not positive. 240 241 Supported Platforms: 242 ``Ascend`` ``GPU`` ``CPU`` 243 244 Examples: 245 >>> import mindspore 246 >>> import numpy as np 247 >>> from mindspore import Tensor, ops 248 >>> # case 1: output_size=(3, 3, 4) 249 >>> output_size=(3, 3, 4) 250 >>> input_val = np.random.randn(4, 3, 5, 6, 7) 251 >>> input = Tensor(input_val, mindspore.float32) 252 >>> output = ops.adaptive_avg_pool3d(input, output_size) 253 >>> print(output.shape) 254 (4, 3, 3, 3, 4) 255 >>> # case 2: output_size=4 256 >>> output_size=5 257 >>> input_val = np.random.randn(2, 3, 8, 6, 12) 258 >>> input = Tensor(input_val, mindspore.float32) 259 >>> output = ops.adaptive_avg_pool3d(input, output_size) 260 >>> print(output.shape) 261 (2, 3, 5, 5, 5) 262 >>> # case 3: output_size=(None, 4, 5) 263 >>> output_size=(None, 4, 5) 264 >>> input_val = np.random.randn(4, 1, 9, 10, 8) 265 >>> input = Tensor(input_val, mindspore.float32) 266 >>> output = ops.adaptive_avg_pool3d(input, output_size) 267 >>> print(output.shape) 268 (4, 1, 9, 4, 5) 269 """ 270 adaptive_avg_pool3d_ = _get_cache_prim(NN_OPS.AdaptiveAvgPool3D)(output_size) 271 return adaptive_avg_pool3d_(input) 272 273 274@constexpr 275def _check_avgpool_1d_type_and_int(kernel_size, stride, ceil_mode, count_include_pad): 276 """Checks the type of avgpool1d input""" 277 validator.check_value_type('kernel_size', kernel_size, [int], 'avg_pool1d') 278 validator.check_value_type('stride', stride, (int, tuple), 'avg_pool1d') 279 validator.check_value_type('ceil_mode', ceil_mode, bool, 'avg_pool1d') 280 validator.check_value_type('count_include_pad', count_include_pad, bool, 'avg_pool1d') 281 validator.check_int(kernel_size, 1, validator.GE, "kernel_size", 'avg_pool1d') 282 validator.check_int(stride, 1, validator.GE, "stride", 'avg_pool1d') 283 284 285@constexpr 286def check_non_negative_int(arg_value, arg_name=None, prim_name=None): 287 """Check argument is non-negative integer, which mean arg_value >= 0.""" 288 validator.check_non_negative_int(arg_value, arg_name, prim_name) 289 290 291def avg_pool1d(input_x, kernel_size=1, stride=1, padding=0, ceil_mode=False, count_include_pad=True): 292 r""" 293 Applies a 1D average pooling over an input Tensor which can be regarded as a composition of 1D input planes. 294 295 Typically the input is of shape :math:`(N_{in}, C_{in}, L_{in})`, avg_pool1d outputs regional average in the 296 :math:`(L_{in})`-dimension. Given kernel size :math:`ks = l_{ker}` and `stride` :math:`s = s_0`, the 297 operation is as follows. 298 299 .. math:: 300 \text{output}(N_i, C_j, l) = \frac{1}{l_{ker}} \sum_{n=0}^{l_{ker}-1} 301 \text{input}(N_i, C_j, s_0 \times l + n) 302 303 .. warning:: 304 `kernel_size` is in the range `[1, 255]`. `stride` is in the range `[1, 63]`. 305 306 Args: 307 input_x (Tensor): Tensor of shape :math:`(N, C_{in}, L_{in})`. 308 kernel_size (int): The size of kernel window used to take the average value. Default: ``1`` . 309 stride (Union(int, tuple[int])): The distance of kernel moving. `stride` can either be an int 310 number or a tuple of one int number. Default: ``1`` . 311 padding (Union(int, tuple[int])): The pad value to be filled. `padding` can either be an integer 312 or a tuple of one integer. Default: ``0`` . 313 ceil_mode (bool): If True, apply ceil instead of floor to compute the output shape. Default: ``False``. 314 count_include_pad (bool): If True, include the zero-padding in the averaging calculation. Default: ``True`` . 315 316 Returns: 317 Tensor of shape :math:`(N, C_{out}, L_{out})`. 318 319 Raises: 320 TypeError: If `input_x` is not a Tensor. 321 TypeError: If `kernel_size` or `stride` is not an int. 322 TypeError: If `ceil_mode` or `count_include_pad` is not a bool. 323 ValueError: If length of shape of `input_x` is not equal to `3`. 324 ValueError: If `kernel_size` or `stride` is less than `1`. 325 ValueError: If `padding` is not int nor a tuple whose length is equal to `2`. 326 ValueError: If value(s) of `padding` is less than `0`. 327 328 Supported Platforms: 329 ``Ascend`` ``GPU`` ``CPU`` 330 331 Examples: 332 >>> import mindspore 333 >>> import numpy as np 334 >>> from mindspore import Tensor, ops 335 >>> input_x = Tensor(np.random.randint(0, 10, [1, 3, 6]), mindspore.float32) 336 >>> output = ops.avg_pool1d(input_x, kernel_size=6, stride=1) 337 >>> print(output.shape) 338 (1, 3, 1) 339 """ 340 if not isinstance(input_x, (Tensor, Tensor_)): 341 raise TypeError("For avg_pool1d, the input input_x must be tensor") 342 343 _check_avgpool_1d_type_and_int(kernel_size, stride, ceil_mode, count_include_pad) 344 if isinstance(padding, int): 345 check_non_negative_int(padding, 'padding', 'avg_pool1d') 346 padding = (0, 0, 0, 0, padding, padding) 347 elif isinstance(padding, tuple): 348 if len(padding) != 1: 349 raise ValueError("For avg_pool1d, padding should be int or tuple of length 1.") 350 for item in padding: 351 check_non_negative_int(item, 'padding', 'avg_pool1d') 352 padding = (0, 0, 0, 0, padding[0], padding[0]) 353 else: 354 raise TypeError("For avg_pool1d, padding should be int or tuple of length 1.") 355 356 if isinstance(stride, tuple): 357 if len(stride) != 1: 358 raise ValueError("For avg_pool1d, stride should be int or tuple of length 1.") 359 stride = stride[0] 360 361 squeeze_op = _get_cache_prim(P.Squeeze)((2, 3)) 362 avg_pool_op = _get_cache_prim(P.AvgPool3D)(kernel_size=(1, 1, kernel_size), 363 strides=(1, 1, stride), 364 pad_mode='pad', 365 pad=padding, 366 ceil_mode=ceil_mode, 367 count_include_pad=count_include_pad) 368 input_x = expand_dims_(input_x, 2) 369 input_x = expand_dims_(input_x, 2) 370 input_x = avg_pool_op(input_x) 371 input_x = squeeze_op(input_x) 372 return input_x 373 374 375@_primexpr 376def _check_avgpool_2d_kernel_size(kernel_size): 377 """check and calculate the avgpool2d kernel_size""" 378 if isinstance(kernel_size, int): 379 validator.check_int(kernel_size, 1, validator.GE, "kernel_size", 'avg_pool2d') 380 kernel_size = (1, kernel_size, kernel_size) 381 elif isinstance(kernel_size, tuple): 382 if len(kernel_size) != 2: 383 raise ValueError("For avg_pool2d, kernel_size should be int or tuple of length 2.") 384 for item in kernel_size: 385 validator.check_int(item, 1, validator.GE, "kernel_size", 'avg_pool2d') 386 kernel_size = (1, kernel_size[0], kernel_size[1]) 387 else: 388 raise TypeError("For avg_pool2d, kernel_size should be int or tuple of length 2.") 389 return kernel_size 390 391 392@_primexpr 393def _check_avgpool_2d_stride(stride): 394 """check and calculate the avgpool2d stride""" 395 if isinstance(stride, int): 396 validator.check_int(stride, 1, validator.GE, "stride", 'avg_pool2d') 397 stride = (1, stride, stride) 398 elif isinstance(stride, tuple): 399 if len(stride) != 2: 400 raise ValueError("For avg_pool2d, stride should be int or tuple of length 2.") 401 for item in stride: 402 validator.check_int(item, 1, validator.GE, "stride", 'avg_pool2d') 403 stride = (1, stride[0], stride[1]) 404 else: 405 raise TypeError("For avg_pool2d, stride should be int or tuple of length 2.") 406 return stride 407 408 409@_primexpr 410def _check_avgpool_2d_padding(padding): 411 """check and calculate the avgpool2d padding""" 412 if isinstance(padding, int): 413 validator.check_non_negative_int(padding, 'padding', 'avg_pool2d') 414 padding = (0, 0, padding, padding, padding, padding) 415 elif isinstance(padding, tuple): 416 if len(padding) != 4: 417 raise ValueError("For avg_pool2d, padding should be int or tuple of length 4.") 418 for item in padding: 419 validator.check_non_negative_int(item, 'padding', 'avg_pool2d') 420 padding = (0, 0, padding[0], padding[1], padding[2], padding[3]) 421 else: 422 raise TypeError("For avg_pool2d, padding should be int or tuple of length 4.") 423 return padding 424 425 426@_primexpr 427def _check_avg_pool2d_type_and_value(ceil_mode, count_include_pad, divisor_override): 428 """check the type of avgpool2d input""" 429 validator.check_value_type('ceil_mode', ceil_mode, bool, 'avg_pool2d') 430 validator.check_value_type('count_include_pad', count_include_pad, bool, 'avg_pool2d') 431 validator.check_non_negative_int(divisor_override, 'divisor_override', 'avg_pool2d') 432 433 434def avg_pool2d(input_x, kernel_size=1, stride=1, padding=0, ceil_mode=False, count_include_pad=True, 435 divisor_override=0): 436 r""" 437 Applies a 2D average pooling over an input Tensor which can be regarded as a composition of 2D input planes. 438 Typically the input is of shape :math:`(N_{in}, C_{in}, H_{in}, W_{in})`, outputs regional average in the 439 :math:`(H_{in}, W_{in})`-dimension. Given kernel size :math:`(k_{h}, k_{w})` and `strides` , the operation 440 is as follows. 441 442 .. math:: 443 \text{output}(N_i, C_j, h, w) = \frac{1}{k_{h} * k_{w}} \sum_{m=0}^{k_{h}-1} \sum_{n=0}^{k_{w}-1} 444 \text{input}(N_i, C_j, stride[0] \times h + m, stride[1] \times w + n) 445 446 .. warning:: 447 `kernel_size` is in the range `[1, 255]`. `stride` is in the range `[1, 63]`. 448 449 Args: 450 input_x (Tensor): Tensor of shape :math:`(N, C_{in}, H_{in}, W_{in})`. 451 kernel_size (Union[int, tuple[int]]): The size of kernel used to take the average value. It is an int number 452 that represents height and width of the kernel, or a tuple of two int numbers that represent height and 453 width respectively. Default: ``1`` . 454 stride (Union[int, tuple[int]]): The distance of kernel moving, an int number that represents the height and 455 width of movement are both strides, or a tuple of two int numbers that represent height and width of 456 movement respectively. Default: ``1`` . 457 padding (Union(int, tuple[int])): The pad value to be filled. Default: 0. If `padding` is an integer, the 458 paddings of top, bottom, left and right are the same, equal to pad. If `padding` is a tuple of `4` integers, 459 the padding of top, bottom, left and right equal to `padding[0]`, `padding[1]`, `padding[2]` and 460 `padding[3]` correspondingly. Default: ``0`` . 461 ceil_mode (bool): If True, apply ceil instead of floor to compute the output shape. Default: ``False``. 462 count_include_pad (bool): If True, include the zero-padding in the averaging calculation. Default: ``True`` . 463 divisor_override (int): If specified, it will be used as divisor in the averaging calculation, otherwise 464 `kernel_size` will be used. Default: ``0``, which means not specified. 465 466 Returns: 467 Tensor, with shape :math:`(N, C_{out}, H_{out}, W_{out})`. 468 469 Raises: 470 TypeError: If `input_x` is not a Tensor. 471 TypeError: If `kernel_size` or `stride` is neither int nor tuple. 472 TypeError: If `ceil_mode` or `count_include_pad` is not a bool. 473 TypeError: If `divisor_override` is not an int. 474 ValueError: If length of shape of `input_x` is not equal to `4`. 475 ValueError: If `kernel_size` or `stride` is less than 1. 476 ValueError: If `kernel_size` or `stride` is a tuple whose length is not equal to `2`. 477 ValueError: If `padding` is not int nor a tuple whose length is equal to `4`. 478 ValueError: If value(s) of `padding` is less than `0`. 479 480 Supported Platforms: 481 ``Ascend`` ``GPU`` ``CPU`` 482 483 Examples: 484 >>> import mindspore 485 >>> import numpy as np 486 >>> from mindspore import Tensor, ops 487 >>> x = Tensor(np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4), mindspore.float32) 488 >>> output = ops.avg_pool2d(x, kernel_size=2, stride=1) 489 >>> print(output) 490 [[[[ 2.5 3.5 4.5] 491 [ 6.5 7.5 8.5]] 492 [[14.5 15.5 16.5] 493 [18.5 19.5 20.5]] 494 [[26.5 27.5 28.5] 495 [30.5 31.5 32.5]]]] 496 """ 497 if not isinstance(input_x, (Tensor, Tensor_)): 498 raise TypeError("For avg_pool2d, the input input_x must be tensor") 499 500 kernel_size = _check_avgpool_2d_kernel_size(kernel_size) 501 stride = _check_avgpool_2d_stride(stride) 502 padding = _check_avgpool_2d_padding(padding) 503 _check_avg_pool2d_type_and_value(ceil_mode, count_include_pad, divisor_override) 504 squeeze_op = _get_cache_prim(P.Squeeze)(2) 505 avg_pool_op = _get_cache_prim(P.AvgPool3D)(kernel_size=kernel_size, 506 strides=stride, 507 pad_mode='pad', 508 pad=padding, 509 ceil_mode=ceil_mode, 510 count_include_pad=count_include_pad, 511 divisor_override=divisor_override) 512 input_x = expand_dims_(input_x, 2) 513 input_x = avg_pool_op(input_x) 514 input_x = squeeze_op(input_x) 515 return input_x 516 517 518def avg_pool2d_ext(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True, 519 divisor_override=None): 520 r""" 521 Applies a 2D average pooling over an input Tensor which can be regarded as a composition of 2D input planes. 522 Typically the input is of shape :math:`(N, C, H_{in}, W_{in})`, outputs regional average in the 523 :math:`(H_{in}, W_{in})`-dimension. Given kernel size :math:`(k_{H}, k_{W})` and `stride` , the operation 524 is as follows. 525 526 .. math:: 527 \text{output}(N_i, C_j, h, w) = \frac{1}{k_{H} * k_{W}} \sum_{m=0}^{k_{H}-1} \sum_{n=0}^{k_{W}-1} 528 \text{input}(N_i, C_j, stride[0] \times h + m, stride[1] \times w + n) 529 530 Args: 531 input (Tensor): Tensor of shape :math:`(N, C, H_{in}, W_{in})`. 532 kernel_size (Union[int, tuple[int], list[int]]): The size of kernel used to take the average value. 533 Can be a single number or a tuple (kH, kW). 534 stride (Union[int, tuple[int], list[int]]): The distance of kernel moving. Can be a single number or 535 a tuple (sH, sW). Default value is `kernel_size` . 536 padding (Union(int, tuple[int], list[int])): Implicit zero padding to be added on both sides. 537 Can be a single number or a tuple (padH, padW). Default: 0. 538 ceil_mode (bool): If True, apply ceil instead of floor to compute the output shape. 539 Default: ``False``. 540 count_include_pad (bool): If True, include the zero-padding in the averaging calculation. 541 Default: ``True`` . 542 divisor_override (int): If specified, it will be used as divisor in the averaging calculation, 543 otherwise size of pooling region will be used. Default: ``None``. 544 545 Returns: 546 Tensor, with shape :math:`(N, C, H_{out}, W_{out})`. 547 548 .. math:: 549 \begin{array}{ll} \\ 550 H_{out} = \frac{H_{in} + 2 \times padding[0] - kernel_size[0]}{stride[0]} + 1 \\ 551 W_{out} = \frac{W_{in} + 2 \times padding[1] - kernel_size[1]}{stride[1]} + 1 552 \end{array} 553 554 Raises: 555 TypeError: If `input` is not a Tensor. 556 TypeError: If `kernel_size` or `stride` is neither int nor tuple. 557 TypeError: If `ceil_mode` or `count_include_pad` is not a bool. 558 TypeError: If `divisor_override` is not an int or None. 559 ValueError: If the dimension of `input` is not equal to `4` or `3`. 560 ValueError: If `kernel_size` or `stride` is less than 1. 561 ValueError: If `kernel_size` or `stride` is a tuple whose length is not equal to `2` or `1`. 562 ValueError: If `padding` is neither a int nor a tuple whose length is equal to `2` or `1`. 563 ValueError: If value of `padding` is less than `0`. 564 565 Supported Platforms: 566 ``Ascend`` 567 568 Examples: 569 >>> import mindspore 570 >>> import numpy as np 571 >>> from mindspore import Tensor, ops 572 >>> x = Tensor(np.arange(1 * 3 * 3 * 4).reshape(1, 3, 3, 4), mindspore.float32) 573 >>> output = ops.function.nn_func.avg_pool2d_ext(x, kernel_size=2, stride=1) 574 >>> print(output) 575 [[[[ 2.5 3.5 4.5] 576 [ 6.5 7.5 8.5]] 577 [[14.5 15.5 16.5] 578 [18.5 19.5 20.5]] 579 [[26.5 27.5 28.5] 580 [30.5 31.5 32.5]]]] 581 """ 582 if stride is None: 583 stride = kernel_size 584 return _get_cache_prim(ops.auto_generate.AvgPool2D)()(input, kernel_size, stride, padding, 585 ceil_mode, count_include_pad, divisor_override) 586 587 588def _check_avg_pool3d_padding(padding): 589 """Check the padding value in avg_pool3d op.""" 590 if isinstance(padding, int): 591 validator.check_non_negative_int(padding, 'padding', 'avg_pool3d') 592 elif isinstance(padding, tuple): 593 if len(padding) != 6: 594 raise ValueError("For avg_pool3d, padding should be int or tuple of length 6.") 595 for item in padding: 596 validator.check_non_negative_int(item, 'padding', 'avg_pool3d') 597 else: 598 raise TypeError("For avg_pool3d, padding should be int or tuple of length 6.") 599 600 601def avg_pool3d(input_x, kernel_size=1, stride=1, padding=0, ceil_mode=False, count_include_pad=True, 602 divisor_override=0): 603 r""" 604 Applies a 3D average pooling over an input Tensor which can be regarded as a composition of 3D input planes. 605 Typically the input is of shape :math:`(N, C, D_{in}, H_{in}, W_{in})`, avg_pool3d outputs regional average in the 606 :math:`(D_{in}, H_{in}, W_{in})`-dimension. Given kernel size :math:`ks = (d_{ker}, h_{ker}, w_{ker})` and stride 607 :math:`s = (s_0, s_1, s_2)`, the operation is as follows. 608 609 .. math:: 610 \text{output}(N_i, C_j, d, h, w) = 611 \frac{1}{d_{ker} * h_{ker} * w_{ker}} \sum_{l=0}^{d_{ker}-1} \sum_{m=0}^{h_{ker}-1} \sum_{n=0}^{w_{ker}-1} 612 613 \text{input}(N_i, C_j, s_0 \times d + l, s_1 \times h + m, s_2 \times w + n) 614 615 .. warning:: 616 `kernel_size` is in the range `[1, 255]`. `stride` is in the range `[1, 63]`. 617 618 Args: 619 input_x (Tensor): Tensor of shape :math:`(N, C, D_{in}, H_{in}, W_{in})`. Currently support float16 and 620 float32 data type. 621 kernel_size (Union[int, tuple[int]], optional): The size of kernel used to take the average value, is an int 622 number that represents depth, height and width are both `kernel_size`, or a tuple of three int numbers that 623 represent depth, height and width respectively. Default: ``1`` . 624 stride (Union[int, tuple[int]], optional): The distance of kernel moving, an int number that represents the 625 depth, height and width of movement are both stride, or a tuple of three int numbers that represent depth, 626 height and width of movement respectively. Default: ``1`` . 627 padding (Union(int, tuple[int]), optional): The pad value to be filled. If `padding` is an integer, the addings 628 of head, tail, top, bottom, left and right are the same, equal to pad. If `padding` is a tuple of six 629 integers, the padding of head, tail, top, bottom, left and right equal to padding[0], padding[1], 630 padding[2], padding[3], padding[4] and padding[5] correspondingly. Default: ``0`` . 631 ceil_mode (bool, optional): If ``True`` , ceil instead of floor to 632 compute the output shape. Default: ``False`` . 633 count_include_pad (bool, optional): If ``True`` , averaging calculation 634 will include the zero-padding. Default: ``True`` . 635 divisor_override (int, optional): If specified, it will be used as divisor in the averaging calculation, 636 otherwise `kernel_size` will be used. Default: ``0`` , which means not specified. 637 638 Returns: 639 Tensor, with shape :math:`(N, C, D_{out}, H_{out}, W_{out})`. Has the same data type with `input_x`. 640 641 Raises: 642 TypeError: If `input_x` is not a Tensor. 643 TypeError: If `kernel_size`, `stride` or `padding` is neither an int not a tuple. 644 TypeError: If `ceil_mode` or `count_include_pad` is not a bool. 645 TypeError: If `divisor_override` is not an int. 646 ValueError: If length of shape of `input_x` is not equal to `5`. 647 ValueError: If numbers in `kernel_size` or `stride` are not positive. 648 ValueError: If `kernel_size` or `stride` is a tuple whose length is not equal to `3`. 649 ValueError: If `padding` is a tuple whose length is not equal to `6`. 650 ValueError: If element of `padding` is less than `0`. 651 652 Supported Platforms: 653 ``Ascend`` ``GPU`` ``CPU`` 654 655 Examples: 656 >>> import mindspore 657 >>> import numpy as np 658 >>> from mindspore import Tensor, ops 659 >>> input_x = Tensor(np.arange(1 * 2 * 2 * 2 * 3).reshape((1, 2, 2, 2, 3)), mindspore.float16) 660 >>> output = ops.avg_pool3d(input_x, kernel_size=2, stride=1) 661 >>> print(output) 662 [[[[[ 5. 6.]]] 663 [[[17. 18.]]]]] 664 """ 665 if not isinstance(input_x, (Tensor, Tensor_)): 666 raise TypeError("For avg_pool3d, the input input_x must be tensor") 667 668 _check_avg_pool3d_padding(padding) 669 670 avg_pool_op = _get_cache_prim(P.AvgPool3D)(kernel_size=kernel_size, 671 strides=stride, 672 pad_mode='pad', 673 pad=padding, 674 ceil_mode=ceil_mode, 675 count_include_pad=count_include_pad, 676 divisor_override=divisor_override) 677 return avg_pool_op(input_x) 678 679 680@constexpr 681def is_ascend_backend(): 682 """Check if the Ascend is used""" 683 return context.get_context('device_target') == 'Ascend' 684 685 686@_primexpr 687def _check_adaptive_max_pool1d_output_size(output_size): 688 """Check the output_size value in adaptive_max_pool1d op.""" 689 validator.check_int(output_size, 1, validator.GE, "output_size", 'adaptive_max_pool1d') 690 validator.check_value_type('output_size', output_size, [int], 'adaptive_max_pool1d') 691 692 693def adaptive_max_pool1d(input, output_size): 694 r""" 695 Applies a 1D adaptive maximum pooling over an input Tensor which can be regarded as 696 a composition of 1D input planes. 697 698 Typically, the input is of shape :math:`(N, C, L_{in})`, 699 adaptive_max_pool1d outputs regional maximum in the :math:`L_{in}`-dimension. The output is of 700 shape :math:`(N, C, L_{out})`, where :math:`L_{out}` is defined by `output_size`. 701 702 Note: 703 - :math:`L_{in}` must be divisible by `output_size`. 704 - Ascend platform only supports float16 type for input. 705 706 Args: 707 input (Tensor): Tensor of shape :math:`(N, C, L_{in})`, with float16 or float32 data type. 708 output_size (int): the target output size :math:`L_{out}`. 709 710 Returns: 711 Tensor of shape :math:`(N, C, L_{out})`, has the same type as `input`. 712 713 Raises: 714 TypeError: If `input` is neither float16 nor float32. 715 TypeError: If `output_size` is not an int. 716 ValueError: If `output_size` is less than 1. 717 ValueError: If the last dimension of `input` is smaller than `output_size`. 718 ValueError: If the last dimension of `input` is not divisible by `output_size`. 719 ValueError: If length of shape of `input` is not equal to 3. 720 721 722 Supported Platforms: 723 ``Ascend`` ``GPU`` ``CPU`` 724 725 Examples: 726 >>> import mindspore 727 >>> import numpy as np 728 >>> from mindspore import Tensor, ops 729 >>> input = Tensor(np.random.randint(0, 10, [1, 3, 6]), mindspore.float32) 730 >>> output = ops.adaptive_max_pool1d(input, output_size=2) 731 >>> print(output.shape) 732 (1, 3, 2) 733 """ 734 if not isinstance(input, (Tensor, Tensor_)): 735 raise TypeError("For adaptive_max_pool1d, the input input must be tensor") 736 737 _check_adaptive_max_pool1d_output_size(output_size) 738 739 x_in_shape = input.shape 740 x_dtype = dtype_(input) 741 742 if len(x_in_shape) != 3: 743 raise ValueError(f"For adaptive_max_pool1d input must have 3 dim, but got {len(x_in_shape)}.") 744 if x_in_shape[2] < output_size: 745 raise ValueError(f"For adaptive_max_pool1d input's last dimension must be greater or equal to " 746 f"output size {output_size}, but got {x_in_shape[2]}.") 747 if x_in_shape[2] % output_size != 0: 748 raise ValueError(f"For adaptive_max_pool1d input's last dimension must be divisible by " 749 f"output size {output_size}, but got {x_in_shape[2]}.") 750 if is_ascend_backend(): 751 if x_dtype not in [mstype.float16]: 752 raise TypeError(f"For adaptive_max_pool1d in Ascend platform, the input dtype must be float16, " 753 f"but got {x_dtype}.") 754 else: 755 if x_dtype not in [mstype.float16, mstype.float32]: 756 raise TypeError(f"For adaptive_max_pool1d, the input dtype must be float16 or float32, " 757 f"but got {x_dtype}.") 758 759 squeeze_ = _get_cache_prim(P.Squeeze)(2) 760 width = x_in_shape[2] 761 stride = width // output_size 762 kernel_size = width - (output_size - 1) * stride 763 stride = (1, width // output_size) 764 kernel_size = (1, kernel_size) 765 max_pool_ = _get_cache_prim(NN_OPS.MaxPool)(kernel_size=kernel_size, strides=stride) 766 input = expand_dims_(input, 2) 767 input = max_pool_(input) 768 input = squeeze_(input) 769 770 return input 771 772 773@constexpr 774def _check_adaptive_max_pool2d(return_indices): 775 """check the type of return_indices""" 776 validator.check_value_type("return_indices", return_indices, bool, "adaptive_max_pool2d") 777 778 779def adaptive_max_pool2d(input, output_size, return_indices=False): 780 r""" 781 This operator applies a 2D adaptive max pooling to an input signal composed of multiple input planes. 782 That is, for any input size, the size of the specified output is H x W. 783 The number of output features is equal to the number of input planes. 784 785 The input and output data format can be "NCHW" and "CHW". N is the batch size, C is the number of channels, 786 H is the feature height, and W is the feature width. 787 788 .. math:: 789 790 \begin{align} 791 h_{start} &= floor(i * H_{in} / H_{out})\\ 792 h_{end} &= ceil((i + 1) * H_{in} / H_{out})\\ 793 w_{start} &= floor(j * W_{in} / W_{out})\\ 794 w_{end} &= ceil((j + 1) * W_{in} / W_{out})\\ 795 Output(i,j) &= {\max Input[h_{start}:h_{end}, w_{start}:w_{end}]} 796 \end{align} 797 798 Note: 799 Ascend platform only supports float16 type for input. 800 801 Args: 802 input (Tensor): A 3D or 4D tensor, 803 with float16, float32 or float64 data type. 804 output_size (Union[int, tuple]): The target output size. `output_size` can be a tuple :math:`(H, W)`, 805 or an int H for :math:`(H, H)`. :math:`H` and :math:`W` can be int or None. 806 If it is None, it means the output size is the same as the input size. 807 808 return_indices (bool): If `return_indices` is ``True`` , the indices of max value would be output. 809 Default: ``False`` . 810 811 Returns: 812 Tensor, with the same shape and dtype as the `input`. 813 814 Raises: 815 TypeError: If `output_size` is not int or tuple. 816 TypeError: If `input` is not a tensor. 817 TypeError: If `return_indices` is not a bool. 818 TypeError: If dtype of `input` is not float16, float32 or float64. 819 ValueError: If `output_size` is a tuple and the length of `output_size` is not 2. 820 ValueError: If the data format of `input` is not "NCHW" or "CHW". 821 822 Supported Platforms: 823 ``Ascend`` ``GPU`` ``CPU`` 824 825 Examples: 826 >>> import mindspore 827 >>> import numpy as np 828 >>> from mindspore import Tensor, ops 829 >>> # case 1: output_size=(None, 2) 830 >>> input = Tensor(np.array([[[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], 831 ... [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]], 832 ... [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]]), mindspore.float32) 833 >>> output = ops.adaptive_max_pool2d(input, (None, 2)) 834 >>> print(output) 835 [[[[2. 3.] 836 [5. 6.] 837 [8. 9.]] 838 [[2. 3.] 839 [5. 6.] 840 [8. 9.]] 841 [[2. 3.] 842 [5. 6.] 843 [8. 9.]]]] 844 >>> # case 2: output_size=2 845 >>> output = ops.adaptive_max_pool2d(input, 2) 846 >>> print(output) 847 [[[[5. 6.] 848 [8. 9.]] 849 [[5. 6.] 850 [8. 9.]] 851 [[5. 6.] 852 [8. 9.]]]] 853 >>> # case 3: output_size=(1, 2) 854 >>> output = ops.adaptive_max_pool2d(input, (1, 2)) 855 >>> print(output) 856 [[[[8. 9.]] 857 [[8. 9.]] 858 [[8. 9.]]]] 859 """ 860 _check_adaptive_max_pool2d(return_indices) 861 _adaptive_max_pool2d = _get_cache_prim(NN_OPS.AdaptiveMaxPool2D)(output_size) 862 out = _adaptive_max_pool2d(input) 863 output = out if return_indices else out[0] 864 return output 865 866 867def adaptive_max_pool3d(input, output_size, return_indices=False): 868 r""" 869 Calculates the 3D adaptive max pooling for an input Tensor. 870 871 Args: 872 input (Tensor): Tensor, with shape :math:`(C, D, H, W)` or :math:`(N, C, D, H, W)`. 873 output_size (Union[int, tuple]): The specified output size, which is an int or Tuple(int) that 874 represents depth, height and width, or a tuple of three int numbers that represent depth, height and 875 width respectively. The value must be a positive integer. If it is None, the output size and 876 input size of the corresponding dimension are the same. 877 return_indices (bool, optional): If `return_indices` is `True`, the indices of max value would be output, 878 Otherwise, it will not be output. Default: `False`. 879 880 Returns: 881 - **y** (Tensor) - Tensor, with the same number of dims and data type as the `input`. 882 - **argmax** (Tensor) - Tensor, the indices of max value, which has the same shape as the 883 `y` and it's data type is int32. It will output only when `return_indices` is True. 884 885 Raises: 886 TypeError: If `input` is not a Tensor. 887 ValueError: If the dimensions number of `input` is not 4 or 5. 888 TypeError: If dtype of `input` is not int or float. 889 ValueError: If `output_size` is neither an int nor a tuple with shape (3,). 890 891 Supported Platforms: 892 ``GPU`` ``CPU`` 893 894 Examples: 895 >>> import numpy as np 896 >>> from mindspore import Tensor, ops 897 >>> input = Tensor(np.arange(0,36).reshape((1, 3, 3, 4)).astype(np.float32)) 898 >>> output_size = (1, 1, 2) 899 >>> output = ops.adaptive_max_pool3d(input, output_size, True) 900 >>> print(output[0].asnumpy()) 901 [[[[33. 35.]]]] 902 >>> print(output[1].asnumpy()) 903 [[[[33 35]]]] 904 """ 905 if isinstance(output_size, int): 906 output_size = (output_size, output_size, output_size) 907 adaptive_max_pool3d_ = _get_cache_prim(NN_OPS.AdaptiveMaxPool3D)() 908 output_size_ = Tensor(output_size, dtype=mstype.int32) 909 out = adaptive_max_pool3d_(input, output_size_) 910 output = out if return_indices else out[0] 911 return output 912 913 914def max_unpool1d(x, indices, kernel_size, stride=None, padding=0, output_size=None): 915 r""" 916 Computes the inverse of `max_pool1d`. 917 918 `max_unpool1d` keeps the maximal value and set all position of non-maximal values to zero. 919 Typically the input is of shape :math:`(N, C, H_{in})` or :math:`(C, H_{in})`, and the output is of shape 920 :math:`(N, C, H_{out})` or :math:`(C, H_{out})`. The operation is as follows. 921 922 .. math:: 923 \begin{array}{ll} \\ 924 H_{out} = (H_{in} - 1) \times stride[0] - 2 \times padding[0] + kernel\_size[0] \\ 925 \end{array} 926 927 Args: 928 x (Tensor): The input Tensor to invert. Tensor of shape :math:`(N, C, H_{in})` or :math:`(C, H_{in})`. 929 indices (Tensor): Index of maximum value. 930 Tensor of shape must be same with input 'x'. 931 Values of indices must belong to :math:`[0, H_{in} - 1]`. 932 Data type must be in int32 or int64. 933 kernel_size (Union[int, tuple[int]]): The size of kernel used to take the maximum value. 934 stride (Union[int, tuple[int]]): The distance of kernel moving, 935 If stride is 0, (0) or ``None`` , then stride equal to kernel_size. 936 Default: ``None`` , which indicates the moving step is `kernel_size` . 937 padding (Union[int, tuple[int]]): The pad value to be filled. Default: ``0`` . 938 output_size (tuple[int], optional): The output shape. Default: ``None`` . 939 If output_size == (), then the shape of output computed by `kernel_size`, `stride` and `padding`. 940 If output_size != (), then output_size must be :math:`(N, C, H)` , :math:`(C, H)` or 941 :math:`(H)` and output_size must belong to 942 :math:`[(N, C, H_{out} - stride[0]), (N, C, H_{out} + stride[0])]`. 943 944 Returns: 945 Tensor, with shape :math:`(N, C, H_{out})` or :math:`(C, H_{out})`, 946 with the same data type with `x`. 947 948 Raises: 949 TypeError: If data type of `x` or `indices` is not supported. 950 TypeError: If `kernel_size`, `stride` or `padding` is neither an int nor a tuple. 951 ValueError: If numbers in `stride`, `padding` (also support 0 and (0)) or `kernel_size` is not positive. 952 ValueError: If the shapes of `x` and `indices` are not equal. 953 ValueError: If `x` whose length is not 2 or 3. 954 ValueError: If type of `output_size` is not tuple. 955 ValueError: If `output_size` whose length is not 0, 2 or 3. 956 ValueError: If `output_size` is not close to output size computed by attr `kernel_size`, `stride`, `padding`. 957 958 Supported Platforms: 959 ``Ascend`` ``GPU`` ``CPU`` 960 961 Examples: 962 >>> import numpy as np 963 >>> from mindspore import Tensor, ops 964 >>> x = Tensor(np.array([[2, 4, 6, 8]]).astype(np.float32)) 965 >>> indices = Tensor(np.array([[1, 3, 5, 7]]).astype(np.int64)) 966 >>> output = ops.max_unpool1d(x, indices, kernel_size =2, stride=2, padding=0) 967 >>> print(output.asnumpy()) 968 [[0. 2. 0. 4. 0. 6. 0. 8.]] 969 """ 970 if stride is None: 971 stride = kernel_size 972 973 x_shape = shape_(x) 974 x_dim = len(x_shape) 975 976 if output_size is None: 977 output_size = () 978 else: 979 if not isinstance(output_size, tuple): 980 raise ValueError(f"For max_unpool1d, output_size must be tuple, but type {type(output_size)}.") 981 if len(output_size) not in [0, 1, 2, 3]: 982 raise ValueError(f"For max_unpool1d, length of output_size with tuple must be 0, 1, 2, 3, " 983 f"but got type {len(output_size)}.") 984 if not output_size: 985 output_size = () 986 elif x_dim == 2: 987 output_size = (1,) + x_shape[:1] + output_size[-1:] + (1,) 988 else: 989 output_size = x_shape[:2] + output_size[-1:] + (1,) 990 if isinstance(kernel_size, tuple): 991 kernel_size = kernel_size + (1,) 992 elif isinstance(kernel_size, int): 993 kernel_size = (kernel_size, 1) 994 995 if isinstance(stride, tuple): 996 stride = stride + (1,) 997 elif isinstance(stride, int): 998 stride = (stride, 1) 999 1000 if isinstance(padding, tuple): 1001 padding = padding + (0,) 1002 elif isinstance(padding, int): 1003 padding = (padding, 0) 1004 1005 max_unpool_2d = _get_cache_prim(MaxUnpool2D)(ksize=kernel_size, strides=stride, 1006 pads=padding, output_shape=output_size, data_format="NCHW") 1007 if x_dim == 2: 1008 x = x.expand_dims(axis=0) 1009 indices = indices.expand_dims(axis=0) 1010 x = x.expand_dims(axis=3) 1011 indices = indices.expand_dims(axis=3) 1012 out = max_unpool_2d(x, indices) 1013 out = out.squeeze(-1) 1014 out = out.squeeze(0) 1015 else: 1016 x = x.expand_dims(axis=3) 1017 indices = indices.expand_dims(axis=3) 1018 out = max_unpool_2d(x, indices) 1019 out = out.squeeze(-1) 1020 return out 1021 1022 1023def max_unpool2d(x, indices, kernel_size, stride=None, padding=0, output_size=None): 1024 r""" 1025 Computes the inverse of `max_pool2d`. 1026 1027 `max_unpool2d` keeps the maximal value and set all position of non-maximal values to zero. Typically the input 1028 is of shape :math:`(N, C, H_{in}, W_{in})` or :math:`(C, H_{in}, W_{in})`, and the output is of 1029 shape :math:`(N, C, H_{out}, W_{out})` or :math:`(C, H_{out}, W_{out})`. The operation is as follows. 1030 1031 .. math:: 1032 \begin{array}{ll} \\ 1033 H_{out} = (H{in} - 1) \times stride[0] - 2 \times padding[0] + kernel\_size[0] \\ 1034 W_{out} = (W{in} - 1) \times stride[1] - 2 \times padding[1] + kernel\_size[1] \\ 1035 \end{array} 1036 1037 Args: 1038 x (Tensor): The input Tensor to invert. 1039 Tensor of shape :math:`(N, C, H_{in}, W_{in})` or :math:`(C, H_{in}, W_{in})`. 1040 indices (Tensor): Max values' index represented by the indices. 1041 Tensor of shape must be same with input 'x'. 1042 Values of indices must belong to :math:`[0, H_{in} \times W_{in} - 1]`. 1043 Data type must be in int32 or int64. 1044 kernel_size (Union[int, tuple[int]]): The size of kernel used to take the maximum value, 1045 an int number that represents height and width of the kernel, or a tuple 1046 of two int numbers that represent height and width respectively. 1047 stride (Union[int, tuple[int]]): The distance of kernel moving, an int number that represents 1048 the height and width of movement are both stride, or a tuple of two int numbers that 1049 represent height and width of movement respectively. 1050 Default: ``None`` , which indicates the moving step is `kernel_size` . 1051 padding (Union[int, tuple[int]]): The pad value to be filled. Default: ``0`` . If `padding` is an integer, 1052 the paddings of height and width are the same, equal to padding. If `padding` is a tuple of two 1053 integers, the padding of height and width equal to padding[0] and padding[1] correspondingly. 1054 output_size (tuple[int], optional): The target output size. Default: ``None`` . 1055 If output_size == (), then the shape of output computed by `kernel_size`, `stride` and `padding`. 1056 If output_size != (), then output_size must be :math:`(N, C, H, W)` , :math:`(C, H, W)` or :math:`(H, W)` 1057 and output_size must belong to 1058 :math:`[(N, C, H_{out} - stride[0], W_{out} - stride[1]), 1059 (N, C, H_{out} + stride[0], W_{out} + stride[1])]`. 1060 1061 Returns: 1062 Tensor, with shape :math:`(N, C, H_{out}, W_{out})` or :math:`(C, H_{out}, W_{out})`, 1063 with the same data type with `x`. 1064 1065 Raises: 1066 TypeError: If data type of `x` or `indices` is not supported. 1067 TypeError: If `kernel_size`, `stride` or `padding` is neither an int nor a tuple. 1068 ValueError: If numbers in `stride`, `padding` (also support 0 and (0, 0)) or `kernel_size` is not positive. 1069 ValueError: If the shape of `x` and `indices` are not equal. 1070 ValueError: If `kernel_size`, `stride` or `padding` is a tuple whose length is not equal to 2. 1071 ValueError: If `x` whose length is not 3 or 4. 1072 ValueError: If `output_size` whose type is not tuple. 1073 ValueError: If `output_size` whose length is not 0, 3 or 4. 1074 ValueError: If `output_size` is not close to output size computed by attr `kernel_size`, `stride`, `padding`. 1075 1076 Supported Platforms: 1077 ``Ascend`` ``GPU`` ``CPU`` 1078 1079 Examples: 1080 >>> import numpy as np 1081 >>> from mindspore import Tensor, ops 1082 >>> x = Tensor(np.array([[[[0, 1], [8, 9]]]]).astype(np.float32)) 1083 >>> indices = Tensor(np.array([[[[0, 1], [2, 3]]]]).astype(np.int64)) 1084 >>> output = ops.max_unpool2d(x, indices, kernel_size=1, stride=1, padding=0) 1085 >>> print(output.asnumpy()) 1086 [[[[0. 1.] 1087 [8. 9.]]]] 1088 """ 1089 if stride is None: 1090 stride = kernel_size 1091 1092 x_shape = shape_(x) 1093 x_dim = len(x_shape) 1094 1095 if output_size is None: 1096 output_size = () 1097 else: 1098 if not isinstance(output_size, tuple): 1099 raise ValueError(f"For max_unpool2d, output_size must be tuple, but type {type(output_size)}.") 1100 if len(output_size) not in [0, 2, 3, 4]: 1101 raise ValueError(f"For max_unpool2d, length of output_size with tuple must be 0, 2, 3, 4, " 1102 f"but got type {len(output_size)}.") 1103 if not output_size: 1104 output_size = () 1105 elif x_dim == 3: 1106 output_size = (1,) + x_shape[:1] + output_size[-2:] 1107 else: 1108 output_size = x_shape[:2] + output_size[-2:] 1109 1110 max_unpool_2d = MaxUnpool2D(ksize=kernel_size, strides=stride, pads=padding, output_shape=output_size, 1111 data_format="NCHW") 1112 if x_dim == 3: 1113 x = x.expand_dims(axis=0) 1114 indices = indices.expand_dims(axis=0) 1115 out = max_unpool_2d(x, indices) 1116 out = out.squeeze(0) 1117 else: 1118 out = max_unpool_2d(x, indices) 1119 return out 1120 1121 1122def max_unpool3d(x, indices, kernel_size, stride=None, padding=0, output_size=None): 1123 r""" 1124 Computes the inverse of :func:`mindspore.ops.max_pool3d`. 1125 1126 `max_unpool3d` keeps the maximal value and set all position of non-maximal values to zero. 1127 Typically the input is of shape :math:`(N, C, D_{in}, H_{in}, W_{in})` or :math:`(C, D_{in}, H_{in}, W_{in})`, 1128 and the output is of shape :math:`(N, C, D_{out}, H_{out}, W_{out})` or :math:`(C, D_{out}, H_{out}, W_{out})`. 1129 The operation is as follows. 1130 1131 .. math:: 1132 \begin{array}{ll} \\ 1133 D_{out} = (D{in} - 1) \times stride[0] - 2 \times padding[0] + kernel\_size[0] \\ 1134 H_{out} = (H{in} - 1) \times stride[1] - 2 \times padding[1] + kernel\_size[1] \\ 1135 W_{out} = (W{in} - 1) \times stride[2] - 2 \times padding[2] + kernel\_size[2] \\ 1136 \end{array} 1137 1138 Args: 1139 x (Tensor): The input Tensor to invert. 1140 Tensor of shape :math:`(N, C, D_{in}, H_{in}, W_{in})` or :math:`(C, D_{in}, H_{in}, W_{in})`. 1141 indices (Tensor): Max values' index represented by the indices. Tensor of shape must be same with input 'x'. 1142 Values of indices must belong to :math:`[0, D_{in} \times H_{in} \times W_{in} - 1]`. 1143 Data type must be in int32 or int64. 1144 kernel_size (Union[int, tuple[int]]): The size of kernel used to take the maximum value, 1145 an int number that represents depth, height and width of the kernel, or a tuple 1146 of three int numbers that represent depth, height and width respectively. 1147 stride (Union[int, tuple[int]]): The distance of kernel moving, an int number that represents 1148 the depth, height and width of movement are both stride, or a tuple of three int numbers that 1149 represent depth, height and width of movement respectively. 1150 Default: ``None`` , which indicates the moving step is `kernel_size` . 1151 padding (Union[int, tuple[int]]): The pad value to be filled. Default: ``0`` . If `padding` is an integer, 1152 the paddings of depth, height and width are the same, equal to padding. If `padding` is a tuple of three 1153 integers, the padding of depth, height and width equal to padding[0], padding[1] and padding[2] 1154 correspondingly. 1155 output_size (tuple[int], optional): The output size. Default: ``None`` . If output_size == (), then the shape of 1156 output computed by `kernel_size`, `stride` and `padding`. If output_size != (), then output_size must be 1157 :math:`(N, C, D, H, W)` or :math:`(C, D, H, W)` or :math:`(D, H, W)` and output_size must belong to 1158 :math:`[(N, C, D_{out} - stride[0], H_{out} - stride[1], W_{out} - stride[2]), 1159 (N, C, D_{out} + stride[0], H_{out} + stride[1], W_{out} + stride[2])]`. 1160 1161 Returns: 1162 Tensor, with shape :math:`(N, C, D_{out}, H_{out}, W_{out})` or :math:`(C, D_{out}, H_{out}, W_{out})`, 1163 with the same data type with `x`. 1164 1165 Raises: 1166 TypeError: If data type of `x` or `indices` is not supported. 1167 TypeError: If `kernel_size`, `stride` or `padding` is neither an int nor a tuple. 1168 ValueError: If numbers in `stride` or `padding` (also support 0 and (0, 0, 0)) or `kernel_size` is not positive. 1169 ValueError: If the shape of `x` and `indices` are not equal. 1170 ValueError: If `kernel_size`, `stride` or `padding` is a tuple whose length is not equal to 3. 1171 ValueError: If `x` whose length is not 4 or 5. 1172 ValueError: If `output_size` whose length is not 0, 4 or 5. 1173 ValueError: If `output_size` whose type is not tuple. 1174 ValueError: If `output_size` is not close to output size computed by attr `kernel_size`, `stride`, `padding`. 1175 1176 Supported Platforms: 1177 ``Ascend`` ``GPU`` ``CPU`` 1178 1179 Examples: 1180 >>> import numpy as np 1181 >>> from mindspore import Tensor, ops 1182 >>> x = Tensor(np.array([[[[[0, 1], [8, 9]]]]]).astype(np.float32)) 1183 >>> indices= Tensor(np.array([[[[[0, 1], [2, 3]]]]]).astype(np.int64)) 1184 >>> output = ops.max_unpool3d(x, indices, kernel_size=2, stride=1, padding=0) 1185 >>> print(output) 1186 [[[[[0. 1. 8.] 1187 [9. 0. 0.] 1188 [0. 0. 0.]] 1189 [[0. 0. 0.] 1190 [0. 0. 0.] 1191 [0. 0. 0.]]]]] 1192 """ 1193 if stride is None: 1194 stride = kernel_size 1195 1196 x_shape = shape_(x) 1197 x_dim = len(x_shape) 1198 1199 if output_size is None: 1200 output_size = () 1201 elif not isinstance(output_size, tuple): 1202 raise ValueError(f"For max_unpool3d, output_size must be tuple, but type {type(output_size)}.") 1203 elif len(output_size) not in [0, 3, 4, 5]: 1204 raise ValueError(f"For max_unpool3d, length of output_size with tuple must be 0, 3, 4, 5, " 1205 f"but got type {len(output_size)}.") 1206 if not output_size: 1207 output_size = () 1208 elif x_dim == 5: 1209 output_size = x_shape[:2] + output_size[-3:] 1210 else: 1211 output_size = (1,) + x_shape[:1] + output_size[-3:] 1212 max_unpool_3d = MaxUnpool3D(ksize=kernel_size, strides=stride, pads=padding, output_shape=output_size, 1213 data_format="NCDHW") 1214 1215 if x_dim == 4: 1216 x = x.expand_dims(axis=0) 1217 indices = indices.expand_dims(axis=0) 1218 out = max_unpool_3d(x, indices) 1219 out = out.squeeze(0) 1220 else: 1221 out = max_unpool_3d(x, indices) 1222 return out 1223 1224 1225def binary_cross_entropy_with_logits(input, target, weight=None, pos_weight=None, reduction='mean'): 1226 r""" 1227 Adds sigmoid activation function to input `input` as logits, and uses the given logits to compute binary cross 1228 entropy between the `input` and the `target`. 1229 1230 Sets input `input` as :math:`X`, input target as :math:`Y`, input weight as :math:`W`, output as :math:`L`. Then, 1231 1232 .. math:: 1233 1234 \begin{array}{ll} \\ 1235 p_{ij} = sigmoid(X_{ij}) = \frac{1}{1 + e^{-X_{ij}}} \\ 1236 L_{ij} = -[Y_{ij}log(p_{ij}) + (1 - Y_{ij})log(1 - p_{ij})] 1237 \end{array} 1238 1239 :math:`i` indicates the :math:`i^{th}` sample, :math:`j` indicates the category. Then, 1240 1241 .. math:: 1242 \ell(x, y) = \begin{cases} 1243 L, & \text{if reduction} = \text{'none';}\\ 1244 \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ 1245 \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} 1246 \end{cases} 1247 1248 :math:`\ell` indicates the method of calculating the loss. There are three methods: 1249 the first method is to provide the loss value directly, 1250 the second method is to calculate the average value of all losses, 1251 and the third method is to calculate the sum of all losses. 1252 1253 This operator will multiply the output by the corresponding weight. 1254 The tensor :math:`weight` assigns different weights to each piece of data in the batch, 1255 and the tensor :math:`pos\_weight` adds corresponding weights to the positive examples of each category. 1256 1257 In addition, it can trade off recall and precision by adding weights to positive examples. 1258 In the case of multi-label classification the loss can be described as: 1259 1260 .. math:: 1261 \begin{array}{ll} \\ 1262 p_{ij,c} = sigmoid(X_{ij,c}) = \frac{1}{1 + e^{-X_{ij,c}}} \\ 1263 L_{ij,c} = -[P_{c}Y_{ij,c} * log(p_{ij,c}) + (1 - Y_{ij,c})log(1 - p_{ij,c})] 1264 \end{array} 1265 1266 where c is the class number (c>1 for multi-label binary classification, c=1 for single-label binary classification), 1267 n is the number of the sample in the batch and :math:`P_c` is the weight of the positive answer for the class c. 1268 :math:`P_c>1` increases the recall, :math:`P_c<1` increases the precision. 1269 1270 Args: 1271 input (Tensor): Input `input`. Data type must be float16 or float32. 1272 target (Tensor): Ground truth label, has the same shape as `input`. 1273 Data type must be float16 or float32. 1274 weight (Tensor, optional): A rescaling weight applied to the loss of each batch element. It can be 1275 broadcast to a tensor with shape of `input`. Data type must be float16 or float32. 1276 Default: ``None``, `weight` is a Tensor whose value is ``1``. 1277 pos_weight (Tensor, optional): A weight of positive examples. Must be a vector with length equal to the 1278 number of classes. It can be broadcast to a tensor with shape of `input`. 1279 Data type must be float16 or float32. Default: ``None``, `pos_weight` is a Tensor whose value is ``1``. 1280 reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` , 1281 ``'sum'`` . Default: ``'mean'`` . 1282 1283 - ``'none'``: no reduction will be applied. 1284 - ``'mean'``: compute and return the weighted mean of elements in the output. 1285 - ``'sum'``: the output elements will be summed. 1286 1287 Returns: 1288 Tensor or Scalar, if `reduction` is ``'none'``, it's a tensor with the same shape and type as input `input`. 1289 Otherwise, the output is a scalar. 1290 1291 Raises: 1292 TypeError: If input `input`, `target`, `weight`, `pos_weight` is not Tensor. 1293 TypeError: If data type of input `input`, `target`, `weight`, `pos_weight` is neither float16 nor float32. 1294 TypeError: If data type of input `reduction` is not string. 1295 ValueError: If `weight` or `pos_weight` can not be broadcast to a tensor with shape of `input`. 1296 ValueError: If `reduction` is not one of ``'none'``, ``'mean'`` or ``'sum'``. 1297 1298 Supported Platforms: 1299 ``Ascend`` ``GPU`` ``CPU`` 1300 1301 Examples: 1302 >>> import mindspore 1303 >>> import numpy as np 1304 >>> from mindspore import Tensor, ops 1305 >>> input = Tensor(np.array([[-0.8, 1.2, 0.7], [-0.1, -0.4, 0.7]]), mindspore.float32) 1306 >>> target = Tensor(np.array([[0.3, 0.8, 1.2], [-0.6, 0.1, 2.2]]), mindspore.float32) 1307 >>> weight = Tensor(np.array([1.0, 1.0, 1.0]), mindspore.float32) 1308 >>> pos_weight = Tensor(np.array([1.0, 1.0, 1.0]), mindspore.float32) 1309 >>> output = ops.binary_cross_entropy_with_logits(input, target, weight, pos_weight) 1310 >>> print(output) 1311 0.3463612 1312 """ 1313 1314 bce_with_logits_loss_op = _get_cache_prim(NN_OPS.BCEWithLogitsLoss)(reduction) 1315 return bce_with_logits_loss_op(input, target, weight, pos_weight) 1316 1317 1318@_function_forbid_reuse 1319def dropout(input, p=0.5, training=True, seed=None): 1320 r""" 1321 During training, randomly zeroes some of the elements of the input tensor 1322 with probability `p` from a Bernoulli distribution. It plays the role of reducing neuron correlation and 1323 avoid overfitting. And the return will be multiplied by :math:`\frac{1}{1-p}` during training. 1324 During the reasoning, this operation returns the same Tensor as the `x`. 1325 1326 Args: 1327 input (Tensor): The input Tensor of shape :math:`(*, N)`, with data type of float16, float32 or float64. 1328 p (float, optional): The dropping rate, between 0 and 1, e.g. p = 0.1, 1329 means dropping out 10% of input units. Default: ``0.5`` . 1330 training (bool): Apply dropout if is True. Default: ``True``. 1331 seed (int, optional): Seed is used as entropy source for Random number engines generating pseudo-random numbers. 1332 Default: ``None`` , which will be treated as ``0`` . 1333 1334 Returns: 1335 - **output** (Tensor) - Zeroed tensor, with the same shape and data type as `input`. 1336 1337 Raises: 1338 TypeError: If `p` is not a float. 1339 TypeError: If dtype of `input` is not float16, float32 or float64. 1340 TypeError: If `input` is not a Tensor. 1341 1342 Supported Platforms: 1343 ``Ascend`` ``GPU`` ``CPU`` 1344 1345 Examples: 1346 >>> import mindspore 1347 >>> from mindspore import Tensor, ops 1348 >>> input = Tensor(((20, 16), (50, 50)), mindspore.float32) 1349 >>> output = ops.dropout(input, p=0.5) 1350 >>> print(output.shape) 1351 (2, 2) 1352 """ 1353 check_bool_const(training, "training", "dropout") 1354 if training is False: 1355 return input 1356 keep_prob = 1 - p 1357 seed0, seed1 = _get_seed(seed, "dropout") 1358 dropout_op = P.Dropout(keep_prob=keep_prob, Seed0=seed0, Seed1=seed1) 1359 dropout_op = _set_prim_op_user_data(dropout_op, "random_cache", False) 1360 out, _ = dropout_op(input) 1361 return out 1362 1363 1364@_function_forbid_reuse 1365def dropout_ext(input, p=0.5, training=True): 1366 r""" 1367 During training, randomly zeroes some of the elements of the input tensor 1368 with probability `p` from a Bernoulli distribution. It plays the role of reducing neuron correlation and 1369 avoid overfitting. And the return will be multiplied by :math:`\frac{1}{1-p}` during training. 1370 During the reasoning, this operation returns the same Tensor as the `input`. 1371 1372 Args: 1373 input (Tensor): The input Tensor of shape :math:`(*, N)`. 1374 p (float): The dropping rate of input neurons, between 0 and 1, e.g. `p` = 0.1, 1375 means dropping out 10% of input neurons. Default: ``0.5`` . 1376 training (bool): Apply dropout if it is ``True`` , if it is ``False`` , the input is returned directly, 1377 and `p` is invalid. Default: ``True``. 1378 1379 Returns: 1380 - **output** (Tensor) - Zeroed tensor, with the same shape and data type as `input`. 1381 1382 Raises: 1383 TypeError: If `p` is not a float. 1384 TypeError: If `input` is not a Tensor. 1385 1386 Supported Platforms: 1387 ``Ascend`` 1388 1389 Examples: 1390 >>> import mindspore 1391 >>> from mindspore import Tensor, ops 1392 >>> input = Tensor(((20, 16), (50, 50)), mindspore.float32) 1393 >>> output = ops.function.nn_func.dropout_ext(input, p=0.5) 1394 >>> print(output.shape) 1395 (2, 2) 1396 """ 1397 check_bool_const(training, "training", "dropout_ext") 1398 if training is False: 1399 return input 1400 seed, offset = default_generator._step(generator_step_) # pylint: disable=protected-access 1401 out, _ = dropout_ext_op(input, p, seed, offset) 1402 return out 1403 1404 1405def dropout1d(input, p=0.5, training=True): 1406 r""" 1407 During training, randomly zeroes some channels of the input tensor with probability `p` 1408 from a Bernoulli distribution(For a 3-dimensional tensor with a shape of :math:`NCL`, 1409 the channel feature map refers to a 1-dimensional feature map with the shape of :math:`L`). 1410 1411 For example, the :math:`j\_th` channel of the :math:`i\_th` sample in the batched input is a to-be-processed 1412 `1D` tensor input[i,j]. 1413 Each channel will be zeroed out independently on every forward call which based on Bernoulli distribution 1414 probability `p`. 1415 1416 The parper `Dropout: A Simple Way to Prevent Neural Networks from Overfitting 1417 <http://www.cs.toronto.edu/~rsalakhu/papers/srivastava14a.pdf>`_ mentioned this technology, And it is proved that 1418 it can effectively reduce over fitting and prevent neuronal coadaptation. 1419 For more details, refer to `Improving neural networks by preventing co-adaptation of feature detectors 1420 <https://arxiv.org/pdf/1207.0580.pdf>`_ . 1421 1422 `dropout1d` can improve the independence between channel feature maps. 1423 1424 Args: 1425 input (Tensor): A tensor with shape :math:`(N, C, L)` or :math:`(C, L)`, where `N` is the batch size, `C` is the 1426 number of channels, `L` is the feature length. The data type must be int8, int16, int32, int64, float16, 1427 float32 or float64. 1428 p (float, optional): The dropping probability of a channel, between 0 and 1, e.g. `p` = 0.8, 1429 which means an 80% chance of clearing. Default: ``0.5`` . 1430 training (bool, optional): Apply dropout if is True. Default: ``True`` . 1431 1432 Returns: 1433 Tensor, output, with the same shape and data type as `input`. 1434 1435 Raises: 1436 TypeError: If `input` is not a Tensor. 1437 TypeError: If the data type of `p` is not float. 1438 ValueError: If `p` is out of the range `[0.0, 1.0]`. 1439 ValueError: If `input` shape is not `2D` or `3D`. 1440 1441 Supported Platforms: 1442 ``Ascend`` ``GPU`` ``CPU`` 1443 1444 Examples: 1445 >>> import mindspore 1446 >>> import numpy as np 1447 >>> from mindspore import Tensor, ops 1448 >>> input_x = Tensor(np.random.randn(4, 3), mindspore.float32) 1449 >>> output = ops.dropout1d(input_x, 0.5) 1450 >>> print(output.shape) 1451 (4, 3) 1452 """ 1453 if not isinstance(p, float): 1454 raise TypeError(f"For dropout1d, 'p' must be float, but got type {type(p)}.") 1455 if p < 0 or p > 1: 1456 raise ValueError(f"For dropout1d, the 'p' must be a number in range [0, 1], but got {p}.") 1457 1458 if not isinstance(input, Tensor): 1459 raise TypeError(f"For dropout1d, 'input' must be Tensor, but got type {type(input)}.") 1460 check_bool_const(training, "training", "dropout1d") 1461 if training is False: 1462 return input 1463 dropout_2d_op = NN_OPS.Dropout2D(1.0 - p) 1464 1465 if len(input.shape) == 2: 1466 input = input.expand_dims(0) 1467 input = input.expand_dims(-1) 1468 out, _ = dropout_2d_op(input) 1469 out = out.squeeze(-1) 1470 out = out.squeeze(0) 1471 elif len(input.shape) == 3: 1472 input = input.expand_dims(-1) 1473 out, _ = dropout_2d_op(input) 1474 out = out.squeeze(-1) 1475 else: 1476 raise ValueError(f"For dropout1d, input shape should be 2D or 3D, but got {len(input.shape)}.") 1477 return out 1478 1479 1480def dropout2d(input, p=0.5, training=True): 1481 r""" 1482 During training, randomly zeroes some channels of the input tensor with probability `p` 1483 from a Bernoulli distribution(For a 4-dimensional tensor with a shape of :math:`NCHW`, 1484 the channel feature map refers to a 2-dimensional feature map with the shape of :math:`HW`). 1485 1486 For example, the :math:`j\_th` channel of the :math:`i\_th` sample in the batched input is a to-be-processed 1487 `2D` tensor input[i,j]. 1488 Each channel will be zeroed out independently on every forward call which based on Bernoulli distribution 1489 probability `p`. 1490 The parper `Dropout: A Simple Way to Prevent Neural Networks from Overfitting 1491 <http://www.cs.toronto.edu/~rsalakhu/papers/srivastava14a.pdf>`_ mentioned this technology, And it is proved that 1492 it can effectively reduce over fitting and prevent neuronal coadaptation. 1493 For more details, refer to `Improving neural networks by preventing co-adaptation of feature detectors 1494 <https://arxiv.org/pdf/1207.0580.pdf>`_ . 1495 1496 `dropout2d` can improve the independence between channel feature maps. 1497 1498 Args: 1499 input (Tensor): A `4D` tensor with shape :math:`(N, C, H, W)`, where `N` is the batch size, `C` is the number 1500 of channels, `H` is the feature height, and `W` is the feature width. The data type must be int8, 1501 int16, int32, int64, float16, float32 or float64. 1502 p (float): The dropping probability of a channel, between 0 and 1, e.g. `p` = 0.8, 1503 which means dropping out 80% of channels. Default: ``0.5`` . 1504 training(bool): If `training` is True, applying dropout, otherwise, not applying. Default: ``True`` . 1505 1506 Returns: 1507 Tensor, output, with the same shape and data type as `input`. 1508 1509 Raises: 1510 TypeError: If `input` is not a Tensor. 1511 TypeError: If dtype of `input` is not int8, int16, int32, int64, float16, float32 or float64. 1512 TypeError: If the data type of `p` is not float. 1513 ValueError: If `p` is out of the range `[0.0, 1.0]`. 1514 ValueError: If `input` shape is not `4D`. 1515 1516 Supported Platforms: 1517 ``Ascend`` ``GPU`` ``CPU`` 1518 1519 Examples: 1520 >>> import mindspore 1521 >>> import numpy as np 1522 >>> from mindspore import Tensor, ops 1523 >>> input = Tensor(np.ones([2, 1, 2, 3]), mindspore.float32) 1524 >>> output = ops.dropout2d(input, 0.5) 1525 >>> print(output.shape) 1526 (2, 1, 2, 3) 1527 """ 1528 check_bool_const(training, "training", "dropout2d") 1529 if training is False: 1530 return input 1531 dropout_2d_op = NN_OPS.Dropout2D(1.0 - p) 1532 out, _ = dropout_2d_op(input) 1533 return out 1534 1535 1536def dropout3d(input, p=0.5, training=True): 1537 r""" 1538 During training, randomly zeroes some channels of the input tensor 1539 with probability `p` from a Bernoulli distribution(For a 5-dimensional tensor 1540 with a shape of :math:`NCDHW`, the channel feature map refers to a 3-dimensional 1541 feature map with a shape of :math:`DHW`). 1542 1543 For example, the :math:`j\_th` channel of the :math:`i\_th` sample in the batched input is a to-be-processed 1544 `3D` tensor input[i,j]. 1545 Each channel will be zeroed out independently on every forward call which based on Bernoulli distribution 1546 probability `p`. 1547 1548 `dropout3d` can improve the independence between channel feature maps. 1549 1550 Args: 1551 input (Tensor): A `5D` tensor with shape :math:`(N, C, D, H, W)`, where `N` is the batch size, `C` is the number 1552 of channels, `D` is the feature depth, `H` is the feature height, and `W` is the feature width. 1553 The data type must be int8, int16, int32, int64, float16, float32 or float64. 1554 p (float): The dropping probability of a channel, between 0 and 1, e.g. `p` = 0.8, 1555 which means dropping out 80% of channels. Default: ``0.5`` . 1556 training(bool): If `training` is True, applying dropout, otherwise, not applying. Default: ``True`` . 1557 1558 Returns: 1559 Tensor, output, with the same shape and data type as `input`. 1560 1561 Raises: 1562 TypeError: If `input` is not a Tensor. 1563 TypeError: If dtype of `input` is not int8, int16, int32, int64, float16, float32 or float64. 1564 TypeError: If the data type of `p` is not float. 1565 ValueError: If `p` is out of the range `[0.0, 1.0]`. 1566 ValueError: If `input` shape is not 5D. 1567 1568 Supported Platforms: 1569 ``Ascend`` ``GPU`` ``CPU`` 1570 1571 Examples: 1572 >>> import mindspore 1573 >>> import numpy as np 1574 >>> from mindspore import Tensor, ops 1575 >>> input = Tensor(np.ones([2, 1, 2, 1, 2]), mindspore.float32) 1576 >>> output = ops.dropout3d(input, 0.5) 1577 >>> print(output.shape) 1578 (2, 1, 2, 1, 2) 1579 """ 1580 check_bool_const(training, "training", "dropout3d") 1581 if training is False: 1582 return input 1583 dropout_3d_op = NN_OPS.Dropout3D(1.0 - p) 1584 out, _ = dropout_3d_op(input) 1585 return out 1586 1587 1588@_primexpr 1589def _check_float_range_inc_neither(arg_value, lower_limit, upper_limit, arg_name=None, prim_name=None): 1590 """ 1591 Method for checking whether input value is in float range inc neither. 1592 """ 1593 return validator.check_float_range(arg_value, lower_limit, upper_limit, validator.INC_NEITHER, arg_name, prim_name) 1594 1595 1596def _check_fractional_output_size_ratio(output_size, output_ratio, cls_name): 1597 """Internal function, used to check whether fractional_max_pool can specify the output shape.""" 1598 if output_ratio is None and output_size is None: 1599 raise ValueError(f"For {cls_name}, 'output_size' and 'output_ratio' can not be None" 1600 f"at the same time, but got {output_ratio} and {output_size} .") 1601 1602 1603def fractional_max_pool2d(input, kernel_size, output_size=None, output_ratio=None, return_indices=False, 1604 _random_samples=None): 1605 r""" 1606 Applies the 2D FractionalMaxPool operation over `input`. The output Tensor shape can be determined by either 1607 `output_size` or `output_ratio`, and the step size is determined by `_random_samples`. `output_size` will take 1608 effect when `output_size` and `output_ratio` are set at the same time. 1609 And `output_size` and `output_ratio` can not be ``None`` at the same time. 1610 1611 Refer to the paper `Fractional MaxPooling by Ben Graham <https://arxiv.org/abs/1412.6071>`_ for more details. 1612 1613 Args: 1614 input (Tensor): Tensor of shape :math:`(N, C, H_{in}, W_{in})` or :math:`(C, H_{in}, W_{in})`, 1615 with float16, float32, float64, int32, int64 data type. 1616 kernel_size (Union[int, tuple[int]]): The size of kernel used to take the maximum value, 1617 is an int number that represents height and width of the kernel, or a tuple 1618 of two int numbers that represent height and width respectively. 1619 The value must be a positive integer. 1620 output_size (Union[int, tuple[int]], optional): The shape of the target `output_size`, 1621 is an int number that represents height and width, or a tuple 1622 of two int numbers that represent height and width respectively. 1623 The value must be a positive integer. 1624 Default: ``None``. 1625 output_ratio (Union[float, tuple[float]], optional): The ratio of target output shape to input shape. 1626 Specifying the size of the output tensor by using a ratio of the input size. 1627 Data type: float16, float32, double, and value is between (0, 1). 1628 Default: ``None``. 1629 return_indices (bool, optional): Whether to return the indices of max value. Default: ``False``. 1630 _random_samples (Tensor, optional): The random step of fractional_max_pool2d, which is a 3D tensor. 1631 Tensor of data type: float16, float32, double, and value is between [0, 1). 1632 Supported shape :math:`(N, C, 2)` or :math:`(1, C, 2)`. 1633 Default: ``None``, the values of `_random_samples` 1634 will be randomly distributed using uniform distribution over an interval [0,1). 1635 1636 Returns: 1637 - **y** (Tensor) - Has the same type as the `input`. 1638 Has the shape :math:`(N, C, H_{out}, W_{out})` or :math:`(C, H_{out}, W_{out})` , 1639 where :math:`(H_{out}, W_{out})` = `output_size` 1640 or :math:`(H_{out}, W_{out})` = `output_ratio` * :math:`(H_{in}, W_{in})`. 1641 1642 - **argmax** (Tensor) - The indices along with the outputs, which is a Tensor, with the same shape as the 1643 `y` and int64 data type. It will output only when `return_indices` is True. 1644 1645 Raises: 1646 TypeError: If data type of `input` is not one of the following: float16, float32, float64, int32, int64. 1647 TypeError: If data type of `_random_samples` is not one of the following: float16, float32, float64. 1648 ValueError: If `kernel_size` is not a number and `kernel_size` is not a tuple of length 2. 1649 ValueError: If `output_size` is not a number and `output_size` is not a tuple of length 2. 1650 ValueError: If the sum of `kernel_size` , `output_size` and -1 is larger than the corresponding 1651 dimension of `input`. 1652 ValueError: If the dimension of `_random_samples` is not 3. 1653 ValueError: if `output_size` and `output_ratio` are None at the same time. 1654 ValueError: If the first dimension size of `input` and `_random_samples` is not equal. 1655 ValueError: If the second dimension size of `input` and `_random_samples` is not equal. 1656 ValueError: If the third dimension size of `_random_samples` is not 2. 1657 1658 Supported Platforms: 1659 ``CPU`` 1660 1661 Examples: 1662 >>> import numpy as np 1663 >>> from mindspore import Tensor, ops 1664 >>> from mindspore import dtype as mstype 1665 >>> input = Tensor(np.array([0.3220, 0.9545, 0.7879, 0.0975, 0.3698, 1666 ... 0.5135, 0.5740, 0.3435, 0.1895, 0.8764, 1667 ... 0.9581, 0.4760, 0.9014, 0.8522, 0.3664, 1668 ... 0.4980, 0.9673, 0.9879, 0.6988, 0.9022, 1669 ... 0.9304, 0.1558, 0.0153, 0.1559, 0.9852]).reshape([1, 1, 5, 5]), mstype.float32) 1670 >>> _random_samples = Tensor(np.array([[[0.8, 0.8]]]), mstype.float32) 1671 >>> y, argmax = ops.fractional_max_pool2d(input, kernel_size=2, output_size=(2, 2), 1672 ... _random_samples=_random_samples, return_indices=True) 1673 >>> print(y) 1674 [[[[0.9545 0.8764] 1675 [0.9673 0.9852]]]] 1676 >>> print(argmax) 1677 [[[[ 1 9] 1678 [16 24]]]] 1679 >>> y, argmax = ops.fractional_max_pool2d(input, kernel_size=2, output_ratio=(0.5, 0.5), 1680 ... _random_samples=_random_samples, return_indices=True) 1681 >>> print(y) 1682 [[[[0.9545 0.8764] 1683 [0.9673 0.9852]]]] 1684 >>> print(argmax) 1685 [[[[ 1 9] 1686 [16 24]]]] 1687 """ 1688 _check_fractional_output_size_ratio(output_size, output_ratio, "fractional_max_pool2d") 1689 _check_value_type("return_indices", return_indices, [bool], "fractional_max_pool2d") 1690 dim_flag = False 1691 if input.ndim == 3: 1692 input = input.expand_dims(axis=0) 1693 dim_flag = True 1694 if _random_samples is None: 1695 if input.dtype in mstype.float_type: 1696 _random_samples = ops.rand(input.shape[0], input.shape[1], 2, dtype=input.dtype) 1697 else: 1698 _random_samples = ops.rand(input.shape[0], input.shape[1], 2) 1699 if output_size is None: 1700 if isinstance(output_ratio, (float, int)): 1701 _check_value_type("output_ratio", output_ratio, [float], "fractional_max_pool2d") 1702 output_ratio = (output_ratio, output_ratio) 1703 _check_float_range_inc_neither(output_ratio[0], 0.0, 1.0, "output_ratio[0]", "fractional_max_pool2d") 1704 _check_float_range_inc_neither(output_ratio[1], 0.0, 1.0, "output_ratio[1]", "fractional_max_pool2d") 1705 output_size = (int(input.shape[-2] * output_ratio[0]), int(input.shape[-1] * output_ratio[1])) 1706 fractional_max_pool = FractionalMaxPoolWithFixedKsize(kernel_size, output_size) 1707 output = fractional_max_pool(input, _random_samples) 1708 if dim_flag: 1709 output = output[0].squeeze(axis=0), output[1].squeeze(axis=0) 1710 if return_indices: 1711 return output 1712 return output[0] 1713 1714 1715def fractional_max_pool3d(input, kernel_size, output_size=None, output_ratio=None, return_indices=False, 1716 _random_samples=None): 1717 r""" 1718 Applies the 3D FractionalMaxPool operation over `input`. The output Tensor shape can be determined by either 1719 `output_size` or `output_ratio`, and the step size is determined by `_random_samples`. `output_size` will take 1720 effect when `output_size` and `output_ratio` are set at the same time. 1721 And `output_size` and `output_ratio` can not be ``None`` at the same time. 1722 1723 Refer to the paper `Fractional MaxPooling by Ben Graham <https://arxiv.org/abs/1412.6071>`_ for more details. 1724 1725 The input and output data format can be "NCDHW". N is the batch size, C is the number of channels, 1726 D the feature depth, H is the feature height, and W is the feature width. 1727 1728 .. warning:: 1729 This is an experimental API that is subject to change or deletion. 1730 1731 Args: 1732 input (Tensor): The input of FractionalMaxPool3d, which is a 4D or 5D tensor. 1733 Tensor of data type: float16, float32, double. 1734 Supported shape :math:`(N, C, D_{in}, H_{in}, W_{in})` or :math:`(C, D_{in}, H_{in}, W_{in})`. 1735 kernel_size (Union[int, tuple[int]]): The size of kernel used to take the maximum value, 1736 is an int number that represents depth, height and width of the kernel, or a tuple 1737 of three int numbers that represent depth, height and width respectively. 1738 The value must be a positive integer. 1739 output_size (Union[int, tuple[int]], optional): The shape of the target `output_size`, 1740 is an int number that represents depth, height and width, or a tuple 1741 of three int numbers that represent depth, height and width respectively. 1742 The value must be a positive integer. 1743 Default: ``None`` . 1744 output_ratio (Union[float, tuple[float]], optional): The ratio of target output shape to input shape. 1745 Specifying the size of the output tensor by using a ratio of the input size. 1746 Data type: float16, float32, double, and value is between (0, 1). 1747 Default: ``None`` . 1748 return_indices (bool, optional): Whether to return the indices of max value. Default: ``False`` . 1749 _random_samples (Tensor, optional): The random step of fractional_max_pool3d, which is a 3D tensor. 1750 Tensor of data type: float16, float32, double, and value is between [0, 1). 1751 Supported shape :math:`(N, C, 3)` or :math:`(1, C, 3)` . Default: ``None``, the values of `_random_samples` 1752 will be randomly distributed using uniform distribution over an interval [0,1). 1753 1754 Returns: 1755 - **y** (Tensor) - A tensor, the output of FractionalMaxPool3d. 1756 Has the same data type with `input`. 1757 Has the shape :math:`(N, C, D_{out}, H_{out}, W_{out})` or :math:`(C, D_{out}, H_{out}, W_{out})` , 1758 where :math:`(D_{out}, H_{out}, W_{out})` = `output_size` 1759 or :math:`(D_{out}, H_{out}, W_{out})` = `output_ratio` * :math:`(D_{in}, H_{in}, W_{in})` . 1760 1761 - **argmax** (Tensor) - The indices along with the outputs, which is a Tensor, with the same shape as the 1762 `y` and int32 data type. It will output only when `return_indices` is True. 1763 1764 Raises: 1765 TypeError: If `input` is not a 4D or 5D tensor. 1766 TypeError: If `_random_samples` is not a 3D tensor. 1767 TypeError: If data type of `input` is not float16, float32, double, int32, int64. 1768 TypeError: If dtype of `_random_samples` is not float16, float32, double. 1769 TypeError: If dtype of `argmax` is not int32, int64. 1770 TypeError: if _random_samples to have the different dtypes as input. 1771 ValueError: If `output_size` is a tuple and if `output_size` length is not 3. 1772 ValueError: If `kernel_size` is a tuple and if `kernel_size` length is not 3. 1773 ValueError: If numbers in `output_size` or `kernel_size` is not positive. 1774 ValueError: if `output_size` and `output_ratio` are None at the same time. 1775 ValueError: If the first dimension size of `input` and `_random_samples` is not equal. 1776 ValueError: If the second dimension size of `input` and `_random_samples` is not equal. 1777 ValueError: If the third dimension size of `_random_samples` is not 3. 1778 1779 Supported Platforms: 1780 ``GPU`` ``CPU`` 1781 1782 Examples: 1783 >>> import numpy as np 1784 >>> from mindspore import Tensor, ops 1785 >>> from mindspore import dtype as mstype 1786 >>> x = Tensor(np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]) 1787 ... .reshape([1, 1, 2, 2, 4]), mstype.float32) 1788 >>> _random_samples = Tensor(np.array([0.7, 0.7, 0.7]).reshape([1, 1, 3]), mstype.float32) 1789 >>> output, argmax = ops.fractional_max_pool3d(x, kernel_size=(1, 1, 1), output_size=(1, 1, 3), 1790 ... _random_samples=_random_samples, return_indices=True) 1791 >>> print(output) 1792 [[[[[13. 14. 16.]]]]] 1793 >>> print(argmax) 1794 [[[[[12 13 15]]]]] 1795 >>> output, argmax = ops.fractional_max_pool3d(x, kernel_size=(1, 1, 1), output_ratio=(0.5, 0.5, 0.5), 1796 ... _random_samples=_random_samples, return_indices=True) 1797 >>> print(output) 1798 [[[[[13. 16.]]]]] 1799 >>> print(argmax) 1800 [[[[[12 15]]]]] 1801 """ 1802 _check_fractional_output_size_ratio(output_size, output_ratio, "fractional_max_pool3d") 1803 _check_value_type("return_indices", return_indices, [bool], "fractional_max_pool3d") 1804 if _random_samples is None: 1805 n = 1 if input.ndim == 4 else input.shape[0] 1806 if input.dtype in mstype.float_type: 1807 _random_samples = ops.rand(n, input.shape[-4], 3, dtype=input.dtype) 1808 else: 1809 _random_samples = ops.rand(n, input.shape[-4], 3) 1810 if input.ndim == 4: 1811 _random_samples = _random_samples.transpose(1, 0, 2) 1812 if output_size is None: 1813 if isinstance(output_ratio, (float, int)): 1814 _check_value_type("output_ratio", output_ratio, [float], "fractional_max_pool3d") 1815 output_ratio = (output_ratio, output_ratio, output_ratio) 1816 _check_float_range_inc_neither(output_ratio[0], 0.0, 1.0, "output_ratio[0]", "fractional_max_pool3d") 1817 _check_float_range_inc_neither(output_ratio[1], 0.0, 1.0, "output_ratio[1]", "fractional_max_pool3d") 1818 _check_float_range_inc_neither(output_ratio[2], 0.0, 1.0, "output_ratio[2]", "fractional_max_pool3d") 1819 output_size = (int(input.shape[-3] * output_ratio[0]), int(input.shape[-2] * output_ratio[1]), 1820 int(input.shape[-1] * output_ratio[2])) 1821 if input.dtype != _random_samples.dtype: 1822 raise TypeError(f"For 'fractional_max_pool3d', 'input' and '_random_samples' must be same dtype, " 1823 f"but got Tensor[{input.dtype}] and Tensor[{_random_samples.dtype}].") 1824 fractional_max_pool = FractionalMaxPool3DWithFixedKsize(kernel_size, output_size) 1825 output = fractional_max_pool(input, _random_samples) 1826 if return_indices: 1827 return output 1828 return output[0] 1829 1830 1831def kl_div(logits, labels, reduction='mean'): 1832 r""" 1833 Computes the Kullback-Leibler divergence between the logits and the labels. 1834 1835 For input tensors :math:`x` and :math:`target` with the same shape, the updating formulas of KLDivLoss algorithm are 1836 as follows, 1837 1838 .. math:: 1839 L(x, target) = target \cdot (\log target - x) 1840 1841 Then, 1842 1843 .. math:: 1844 \ell(x, target) = \begin{cases} 1845 L(x, target), & \text{if reduction} = \text{'none';}\\ 1846 \operatorname{mean}(L(x, target)), & \text{if reduction} = \text{'mean';}\\ 1847 \operatorname{sum}(L(x, target)) / x.\operatorname{shape}[0], & \text{if reduction} = \text{'batchmean';}\\ 1848 \operatorname{sum}(L(x, target)), & \text{if reduction} = \text{'sum'.} 1849 \end{cases} 1850 1851 where :math:`x` represents `logits`. 1852 :math:`target` represents `labels`. 1853 :math:`\ell(x, target)` represents `output`. 1854 1855 Note: 1856 - Currently it does not support float64 input on `Ascend`. 1857 - The output aligns with the mathematical definition of Kullback-Leibler divergence 1858 only when `reduction` is set to ``'batchmean'``. 1859 1860 Args: 1861 logits (Tensor): The input Tensor. The data type must be float16, float32 or float64. 1862 labels (Tensor): The label Tensor which has the same shape and data type as `logits`. 1863 reduction (str): Specifies the reduction to be applied to the output. 1864 Its value must be one of ``'none'`` , ``'mean'`` , ``'batchmean'`` or ``'sum'`` . Default: ``'mean'`` . 1865 1866 - ``'none'``: no reduction will be applied. 1867 - ``'mean'``: compute and return the mean of elements in the output. 1868 - ``'sum'``: the output elements will be summed. 1869 - ``'batchmean'``: the summed output elements divided by batch size. 1870 1871 Returns: 1872 Tensor or Scalar, if `reduction` is ``'none'``, then output is a tensor and has the same shape as `logits`. 1873 Otherwise, it is a scalar. 1874 1875 Raises: 1876 TypeError: If `reduction` is not a str. 1877 TypeError: If neither `logits` nor `labels` is a Tensor. 1878 TypeError: If dtype of `logits` or `labels` is not the supported type. 1879 1880 Supported Platforms: 1881 ``Ascend`` ``GPU`` ``CPU`` 1882 1883 Examples: 1884 >>> import mindspore 1885 >>> import numpy as np 1886 >>> from mindspore import Tensor, ops 1887 >>> logits = Tensor(np.array([0.2, 0.7, 0.1]), mindspore.float32) 1888 >>> labels = Tensor(np.array([0., 1., 0.]), mindspore.float32) 1889 >>> output = mindspore.ops.kl_div(logits, labels, 'mean') 1890 >>> print(output) 1891 -0.23333333 1892 """ 1893 if not isinstance(reduction, str): 1894 raise ValueError("For 'kl_div', the 'reduction' must be str and must be in " 1895 f"'['none', 'mean', 'batchmean', 'sum']', but got '{reduction}'.") 1896 1897 if reduction == 'batchmean': 1898 kl_div_sum = _get_cache_prim(P.KLDivLoss)(reduction='sum')(logits, labels) 1899 shape = shape_(logits) 1900 batch_size = shape[0] 1901 return kl_div_sum / batch_size 1902 1903 if reduction == 'mean': 1904 kl_div_sum = _get_cache_prim(P.KLDivLoss)(reduction='sum')(logits, labels) 1905 shape = shape_(logits) 1906 total_size = 1 1907 for dim in shape: 1908 total_size = total_size * dim 1909 return kl_div_sum / total_size 1910 1911 return _get_cache_prim(P.KLDivLoss)(reduction=reduction)(logits, labels) 1912 1913 1914def hardshrink(x, lambd=0.5): 1915 r""" 1916 Hard Shrink activation function. Calculates the output according to the input elements. 1917 1918 The formula is defined as follows: 1919 1920 .. math:: 1921 \text{HardShrink}(x) = 1922 \begin{cases} 1923 x, & \text{ if } x > \lambda \\ 1924 x, & \text{ if } x < -\lambda \\ 1925 0, & \text{ otherwise } 1926 \end{cases} 1927 1928 HShrink Activation Function Graph: 1929 1930 .. image:: ../images/HShrink.png 1931 :align: center 1932 1933 Args: 1934 x (Tensor): The input of Hard Shrink with data type of float16 or float32. 1935 lambd (float, optional): The threshold :math:`\lambda` defined by the Hard Shrink formula. 1936 Default: ``0.5`` . 1937 1938 Returns: 1939 Tensor, has the same data type and shape as the input `x`. 1940 1941 Raises: 1942 TypeError: If `lambd` is not a float. 1943 TypeError: If `x` is not a tensor. 1944 TypeError: If dtype of `x` is neither float16 nor float32. 1945 1946 Supported Platforms: 1947 ``Ascend`` ``GPU`` ``CPU`` 1948 1949 Examples: 1950 >>> import mindspore 1951 >>> import numpy as np 1952 >>> from mindspore import Tensor, ops 1953 >>> x = Tensor(np.array([[ 0.5, 1, 2.0], [0.0533,0.0776,-2.1233]]), mindspore.float32) 1954 >>> output = ops.hardshrink(x) 1955 >>> print(output) 1956 [[ 0. 1. 2. ] 1957 [ 0. 0. -2.1233]] 1958 """ 1959 hshrink_op = _get_cache_prim(P.HShrink)(lambd) 1960 return hshrink_op(x) 1961 1962 1963@constexpr 1964def _check_axis_in_range(axis, ndim): 1965 """Checks axes are with the bounds of ndim""" 1966 if not isinstance(axis, int): 1967 raise TypeError(f'The dims must be integers, but got {type(axis)}') 1968 if not -ndim <= axis < ndim: 1969 raise ValueError(f"The 'axis' must be in the range of [-{ndim}, {ndim}), but got {axis}.") 1970 return axis % ndim 1971 1972 1973@constexpr 1974def _check_axis_valid(axes, ndim): 1975 """ 1976 Checks axes are valid given ndim, and returns axes that can be passed 1977 to the built-in operator (non-negative, int or tuple) 1978 """ 1979 if axes is None: 1980 raise ValueError(f"The parameter dims can not be None.") 1981 if isinstance(axes, (tuple, list)): 1982 axes = tuple(map(lambda x: _check_axis_in_range(x, ndim), axes)) 1983 if any(axes.count(el) > 1 for el in axes): 1984 raise ValueError(f"The element of parameter 'dims' can not be duplicate, but got {axes}.") 1985 return axes 1986 raise ValueError(f"The parameter dims must be tuple of ints, but got {type(axes)}") 1987 1988 1989def _get_flip_start(ndim, shape, axes): 1990 """Calculate the start index of flip""" 1991 return tuple([shape[i] - 1 if i in axes else 0 for i in range(ndim)]) 1992 1993 1994def _get_flip_end(ndim, shape, axes): 1995 """Calculate the end index of flip""" 1996 return tuple([-shape[i] - 1 if i in axes else shape[i] + 1 for i in range(ndim)]) 1997 1998 1999@constexpr 2000def _get_flip_strides(ndim, axes): 2001 """Calculate the strides of flip""" 2002 return tuple([-1 if i in axes else 1 for i in range(ndim)]) 2003 2004 2005def _is_shape_empty(shp): 2006 """Check whether shape contains zero""" 2007 if isinstance(shp, int): 2008 return shp == 0 2009 return ops.shape_mul(shp) == 0 2010 2011 2012def _check_input_tensor(arg_name, *tensors): 2013 """Check whether the input is tensor""" 2014 for tensor in tensors: 2015 if not isinstance(tensor, Tensor): 2016 raise TypeError(f"For '{arg_name}', the input must be Tensor, but got {ops.typeof(tensor)}") 2017 return True 2018 2019 2020def flip(input, dims): 2021 """ 2022 Reverses the order of elements in a tensor along the given axis. 2023 2024 The shape of the tensor is preserved, but the elements are reordered. 2025 2026 Args: 2027 input (Tensor): Input tensor. 2028 dims (Union[list[int], tuple[int]]): Axis or axes along which to flip over. 2029 Flipping is performed on all of the axes specified in the tuple, 2030 If `dims` is a tuple of integers contains negative, it counts from the last to the first axis. 2031 2032 Returns: 2033 Tensor, with the entries of `dims` reversed. 2034 2035 Raises: 2036 TypeError: If the input is not a tensor. 2037 ValueError: If `dims` is None. 2038 ValueError: If `dims` is not a list/tuple of ints. 2039 2040 Supported Platforms: 2041 ``Ascend`` ``GPU`` ``CPU`` 2042 2043 Examples: 2044 >>> import mindspore 2045 >>> from mindspore import ops 2046 >>> import numpy as np 2047 >>> input = mindspore.Tensor(np.arange(1, 9).reshape((2, 2, 2))) 2048 >>> output = ops.flip(input, (0, 2)) 2049 >>> print(output) 2050 [[[6 5] 2051 [8 7]] 2052 [[2 1] 2053 [4 3]]] 2054 """ 2055 res = reverse_v2_impl(input, dims) 2056 return res 2057 2058 2059def flipud(input): 2060 """ 2061 Flips the elements of each column in the up/down direction, while preserving the rows of the input tensor. 2062 2063 Args: 2064 input (Tensor): Input array. 2065 2066 Returns: 2067 Tensor after the flip. 2068 2069 Raises: 2070 TypeError: If the input is not a tensor. 2071 2072 Supported Platforms: 2073 ``Ascend`` ``GPU`` ``CPU`` 2074 2075 Examples: 2076 >>> import mindspore as ms 2077 >>> from mindspore import ops 2078 >>> import numpy as np 2079 >>> input = ms.Tensor(np.arange(1, 9).reshape((2, 2, 2))) 2080 >>> output = ops.flipud(input) 2081 >>> print(output) 2082 [[[5 6] 2083 [7 8]] 2084 [[1 2] 2085 [3 4]]] 2086 """ 2087 return flip(input, (0,)) 2088 2089 2090def fliplr(input): 2091 """ 2092 Flips the elements of each row in the left/right direction, while preserving the columns of the input tensor. 2093 2094 Args: 2095 input (Tensor): Input tensor. 2096 2097 Returns: 2098 Tensor after the flip. 2099 2100 Raises: 2101 TypeError: If the input is not a tensor. 2102 2103 Supported Platforms: 2104 ``Ascend`` ``GPU`` ``CPU`` 2105 2106 Examples: 2107 >>> import mindspore as ms 2108 >>> from mindspore import ops 2109 >>> import numpy as np 2110 >>> input = ms.Tensor(np.arange(1, 9).reshape((2, 2, 2))) 2111 >>> output = ops.fliplr(input) 2112 >>> print(output) 2113 [[[3 4] 2114 [1 2]] 2115 [[7 8] 2116 [5 6]]] 2117 """ 2118 return flip(input, (1,)) 2119 2120 2121def is_floating_point(input): 2122 """ 2123 Judge whether the data type of `input` is a floating point data type i.e., one of mindspore.float64, 2124 mindspore.float32, mindspore.float16. 2125 2126 Args: 2127 input (Tensor): The input Tensor. 2128 2129 Returns: 2130 Bool. If the dtype of `input` is a floating point data type, return ``True`` . Otherwise, return ``False`` . 2131 2132 Supported Platforms: 2133 ``Ascend`` ``GPU`` ``CPU`` 2134 2135 Examples: 2136 >>> import mindspore as ms 2137 >>> from mindspore import ops 2138 >>> from mindspore import Tensor 2139 >>> x = ms.Tensor([1, 2, 3], ms.float32) 2140 >>> y = ms.Tensor([1, 2, 3], ms.int64) 2141 >>> output = ops.is_floating_point(x) 2142 >>> output2 = ops.is_floating_point(y) 2143 >>> print(output) 2144 True 2145 >>> print(output2) 2146 False 2147 """ 2148 return input.dtype in [mstype.float32, mstype.bfloat16, mstype.float16, mstype.float64] 2149 2150 2151def hardswish(x): 2152 r""" 2153 Applies hswish-type activation element-wise. The input is a Tensor with any valid shape. 2154 2155 Hard swish is defined as: 2156 2157 .. math:: 2158 2159 \text{hswish}(x_{i}) = x_{i} * \frac{ReLU6(x_{i} + 3)}{6} 2160 2161 where :math:`x_i` is an element of the input Tensor. 2162 2163 HSwish Activation Function Graph: 2164 2165 .. image:: ../images/HSwish.png 2166 :align: center 2167 2168 Args: 2169 x (Tensor): The input to compute the Hard Swish. 2170 2171 Returns: 2172 Tensor, has the same data type and shape as the input. 2173 2174 Raises: 2175 TypeError: If `x` is not a Tensor. 2176 TypeError: If dtype of `x` is not int or float. 2177 2178 Supported Platforms: 2179 ``Ascend`` ``GPU`` ``CPU`` 2180 2181 Examples: 2182 >>> import mindspore 2183 >>> import numpy as np 2184 >>> from mindspore import Tensor, ops 2185 >>> x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16) 2186 >>> output = ops.hardswish(x) 2187 >>> print(output) 2188 [-0.3333 -0.3333 0 1.666 0.6665] 2189 """ 2190 return hardswish_(x) 2191 2192 2193def _is_dim_unknown(shape): 2194 return isinstance(shape, tuple) and -2 in shape 2195 2196 2197@_primexpr 2198def _interploate_make_tuple(rank, value): 2199 """ 2200 make tuple in dynamic scenarios 2201 """ 2202 s = tuple_to_tensor_((rank,), mstype.int32) 2203 v = None 2204 if isinstance(value, int): 2205 v = F.scalar_to_tensor(value, mstype.int64) 2206 else: 2207 v = F.scalar_to_tensor(value, mstype.float32) 2208 t = fillv2_(s, v) 2209 out = tensor_to_tuple_(t) 2210 return out 2211 2212 2213@_primexpr 2214def _interpolate_scale_factor_convert_size(shape, scale_factor): 2215 """ 2216 convert scale_factor to size 2217 """ 2218 x = tuple_to_tensor_(shape[2:], mstype.int64) 2219 y = tuple_to_tensor_(scale_factor, mstype.float32) 2220 t = x * y 2221 t = ops.TruncateDiv()(t, Tensor(1)) 2222 t = ops.cast(t, mstype.int64) 2223 return tensor_to_tuple_(t) 2224 2225 2226def _interpolate_size_check_with_rank(size, input_rank): 2227 """ 2228 size rank check 2229 """ 2230 if len(size) != input_rank - 2: 2231 raise ValueError( 2232 f"For 'interpolate', 'input' and 'size' must have the same spatial dimensions, " 2233 f"but got 'input' is {input_rank - 2}D, 'size' is {len(size)}D") 2234 2235 2236def _interpolate_scale_factor_check_with_rank(scale_factor, input_rank): 2237 """ 2238 scale_factor rank check 2239 """ 2240 if len(scale_factor) != input_rank - 2: 2241 raise ValueError( 2242 f"For 'interpolate', 'input' and 'scale_factor' must have the same spatial dimensions, " 2243 f"but got 'input' is {input_rank - 2}D, 'scale_factor' is {len(scale_factor)}D" 2244 ) 2245 2246 2247def _interpolate_mode_check(mode, supported_dict): 2248 """ 2249 mode check 2250 """ 2251 if isinstance(mode, list) or mode not in supported_dict: 2252 raise ValueError( 2253 f"For 'interpolate', 'mode' must be in '{list(supported_dict)}', but got {mode}" 2254 ) 2255 2256 2257def _interpolate_rank_check(input_rank, mode, supported_dict): 2258 """ 2259 rank check 2260 """ 2261 if input_rank not in supported_dict.get(mode): 2262 raise ValueError( 2263 f"For 'interpolate', {mode} only support '{list(supported_dict.get(mode, {}))}'D, but got {input_rank}D" 2264 ) 2265 2266 2267def _interpolate_scale_factor_check(scale_factor, mode, rank, supported_dict): 2268 """ 2269 scale_factor check 2270 """ 2271 if scale_factor is not None and "scale_factor" not in supported_dict.get( 2272 mode, {}).get(rank): 2273 raise ValueError( 2274 f"For 'interpolate', 'scale_factor' option cannot currently be set with the " 2275 f"mode = {mode} and dim = {rank}D.") 2276 2277 2278def _interpolate_align_corners_mode_check(rank, mode, supported_dict): 2279 """ 2280 align_corners check 2281 """ 2282 if "align_corners" not in supported_dict.get(mode, {}).get(rank): 2283 raise ValueError( 2284 f"For 'interpolate', 'align_corners' option cannot currently be set with the " 2285 f"mode = {mode}, and dim = {rank}D") 2286 2287 2288def interpolate(input, 2289 size=None, 2290 scale_factor=None, 2291 mode="nearest", 2292 align_corners=None, 2293 recompute_scale_factor=None): 2294 r""" 2295 Samples the input Tensor to the given size or scale_factor by using one of the interpolate algorithms. 2296 2297 Args: 2298 input (Tensor): Tensor to be resized. 2299 Input tensor must be a 3-D, 4-D, or 5-D tensor with shape 2300 :math:`(N, C, [optional D], [optional H], W)` , with data type of float. 2301 size (Union[int, tuple[int], list[int]], optional): The target size. 2302 If size is a tuple or list, its length should be the same as the number of dimensions in input 2303 after removing the first two dimensions N, C. 2304 One and only one of size and scale_factor can be set to None. Default: ``None`` . 2305 scale_factor (Union[float, tuple[float], list[float]], optional): The scale factor of new size of the tensor. 2306 If scale_factor is a tuple or list, its length should be the same as the number of dimensions in input 2307 after removing the first two dimensions N, C. 2308 One and only one of size and scale_factor can be set to None. Default: ``None`` . 2309 mode (str): The sampling algorithm. 2310 One of 'nearest', 'linear' (3D only), 'bilinear' (4D only), 'trilinear' (5D only), 'bicubic' (4D only), 2311 'area', 'nearest-exact'(matches Scikit-Image and PIL nearest neighbours interpolation algorithms and fixes 2312 knows issues with `nearest`, 3D and 4D). Default: ``"nearest"`` . 2313 2314 align_corners (bool): Whether to use corner alignment for coordinate mapping. Assuming a transformation is 2315 applied to the input Tensor along the x-axis, the specific calculation formula is as follows: 2316 2317 .. code-block:: 2318 2319 ori_i = new_length != 1 ? new_i * (ori_length - 1) / (new_length - 1) : 0 # 'align_corners' = True 2320 2321 ori_i = new_length > 1 ? (new_i + 0.5) * ori_length / new_length - 0.5 : 0 # 'align_corners' = False 2322 2323 Among them, :math:`ori\_length` and :math:`new\_length` represent the length of the Tensor before and after 2324 transformation along the x-axis respectively; :math:`new\_i` represents the coordinate of the i-th element 2325 along the x-axis after transformation; :math:`ori\_i` represents 2326 the corresponding coordinate of the original 2327 data along the x-axis. 2328 2329 This is only valid for ``'linear'``, ``'bilinear'``, or ``'bicubic'`` modes. Default: ``False`` . 2330 recompute_scale_factor (bool, optional): Recalculate `scale_factor`. 2331 If True, the parameter `size` will be calculated using the value of the `scale_factor`, 2332 and finally scaled using the value of `size`. 2333 If False, the value of `size` or `scale_factor` will be used for direct interpolation. Default: ``None`` . 2334 2335 .. note:: 2336 The 'nearest-exact' mode is the same as the nearest-neighbor interpolation algorithm used in 2337 scikit-image and PIL. The 'nearest' mode produces the same results as the INTER_NEAREST interpolation 2338 algorithm used in OpenCV. 2339 2340 Args Support List and Supported Platforms: 2341 2342 +---------------+-----------+---------------+--------------+----------------+ 2343 | mode | input.dim | align_corners | scale_factor | device | 2344 +===============+===========+===============+==============+================+ 2345 | nearest | 3 | \- | × | Ascend,GPU,CPU | 2346 +---------------+-----------+---------------+--------------+----------------+ 2347 | | 4 | \- | × | Ascend,GPU,CPU | 2348 +---------------+-----------+---------------+--------------+----------------+ 2349 | | 5 | \- | √ | Ascend,GPU,CPU | 2350 +---------------+-----------+---------------+--------------+----------------+ 2351 | linear | 3 | √ | × | Ascend,GPU,CPU | 2352 +---------------+-----------+---------------+--------------+----------------+ 2353 | bilinear | 4 | √ | × | Ascend,GPU,CPU | 2354 +---------------+-----------+---------------+--------------+----------------+ 2355 | bicubic | 4 | √ | × | Ascend,GPU,CPU | 2356 +---------------+-----------+---------------+--------------+----------------+ 2357 | area | 3 | \- | √ | Ascend,GPU,CPU | 2358 +---------------+-----------+---------------+--------------+----------------+ 2359 | | 4 | \- | √ | Ascend,GPU,CPU | 2360 +---------------+-----------+---------------+--------------+----------------+ 2361 | | 5 | \- | √ | Ascend,GPU,CPU | 2362 +---------------+-----------+---------------+--------------+----------------+ 2363 | nearest-exact | 3 | \- | × | Ascend,CPU | 2364 +---------------+-----------+---------------+--------------+----------------+ 2365 | | 4 | \- | × | Ascend,CPU | 2366 +---------------+-----------+---------------+--------------+----------------+ 2367 | trilinear | 5 | √ | √ | Ascend,GPU,CPU | 2368 +---------------+-----------+---------------+--------------+----------------+ 2369 2370 - `-` indicates that there is no such parameter. 2371 - `×` indicates that this parameter is not currently supported. 2372 - `√` indicates that this parameter is supported. 2373 2374 Returns: 2375 Tensor, resized, whose dimensions and dtype are the same as `input`. 2376 2377 Raises: 2378 TypeError: `input` is not a Tensor. 2379 ValueError: Both `size` and `scale_factor` are not empty. 2380 ValueError: Both `size` and `scale_factor` are empty. 2381 ValueError: When `size` is a tuple or list, its length is not equal to `input.ndim - 2`. 2382 ValueError: When `scale_factor` is a tuple or list, its length is not equal to `input.ndim - 2`. 2383 ValueError: `mode` is not in the list of supported modes. 2384 ValueError: `input.ndim` is not in the list of supported dimensions for the corresponding mode. 2385 ValueError: `size` is not empty, `recompute_scale_factor` is not empty. 2386 ValueError: `scale_factor` is not in the corresponding list of supported values. 2387 ValueError: `align_corners` is not in the corresponding list of supported values. 2388 2389 Supported Platforms: 2390 ``Ascend`` ``GPU`` ``CPU`` 2391 2392 Examples: 2393 >>> import mindspore 2394 >>> from mindspore import Tensor, ops 2395 >>> input = Tensor([[[1, 2, 3], [4, 5, 6]]], mindspore.float32) 2396 >>> output = ops.interpolate(input, size=(6,), mode='nearest') 2397 >>> print(output) 2398 [[[1. 1. 2. 2. 3. 3.] 2399 [4. 4. 5. 5. 6. 6.]]] 2400 """ 2401 2402 def run_nearest(x, size, align_corners=None, scale_factor=None): 2403 # 3D 4D use ResizeNearestNeighborV2, 5D use UpsampleNearest3D 2404 x_rank = F.rank(x) 2405 if size is not None and x_rank == 3: 2406 t1 = seq.TupleToTensor()(size[:1], mstype.int32) 2407 t2 = Tensor([1], mstype.int32) 2408 size = F.concat([t1, t2]) 2409 x = x.unsqueeze(-1) 2410 x = _get_cache_prim(P.ResizeNearestNeighborV2)()( 2411 x, size) 2412 x = _get_cache_prim(P.Squeeze)(-1)(x) 2413 elif size is not None and x_rank == 4: 2414 size = seq.TupleToTensor()(size[:2], mstype.int32) 2415 x = _get_cache_prim(P.ResizeNearestNeighborV2)()( 2416 x, size) 2417 else: 2418 x = _get_cache_prim(P.UpsampleNearest3D)()(x, size, scale_factor) 2419 return x 2420 2421 def run_linear(x, size, align_corners=None, scale_factor=None): 2422 coordinate_transformation_mode = "align_corners" if align_corners else "half_pixel" 2423 resize = _get_cache_prim( 2424 P.image_ops.ResizeLinear1D)(coordinate_transformation_mode) 2425 return resize(x, size) 2426 2427 def run_bilinear(x, size, align_corners=None, scale_factor=None): 2428 resize = _get_cache_prim(P.ResizeBilinearV2)(align_corners, 2429 not align_corners) 2430 return resize(x, size) 2431 2432 def run_trilinear(x, size, align_corners=None, scale_factor=None): 2433 resize = _get_cache_prim( 2434 P.nn_ops.UpsampleTrilinear3D)(align_corners=align_corners) 2435 return resize(x, size, scale_factor) 2436 2437 def run_bicubic(x, size, align_corners=None, scale_factor=None): 2438 resize = _get_cache_prim(P.image_ops.ResizeBicubic)( 2439 align_corners=align_corners, half_pixel_centers=not align_corners) 2440 size = seq.TupleToTensor()(size, mstype.int32) 2441 x = resize(x, size) 2442 return x 2443 2444 def run_area(x, size, align_corners=None, scale_factor=None): 2445 x_rank = F.rank(x) 2446 if x_rank == 3: 2447 x = ops.adaptive_avg_pool1d(x, size[0]) 2448 elif x_rank == 4: 2449 x = ops.adaptive_avg_pool2d(x, tuple(size)) 2450 else: 2451 x = ops.adaptive_avg_pool3d(x, tuple(size)) 2452 return x 2453 2454 def run_nearest_exact(x, size, align_corners=None, scale_factor=None): 2455 x_rank = F.rank(x) 2456 if x_rank == 3: 2457 size = seq.TupleToTensor()((size[0], 1), mstype.int32) 2458 # For impl of nearest 3D use 4D. 2459 x = x.unsqueeze(-1) 2460 resize = _get_cache_prim(P.ResizeNearestNeighborV2)( 2461 align_corners=False, 2462 half_pixel_centers=True) 2463 x = resize(x, size) 2464 x = _get_cache_prim(P.Squeeze)(-1)(x) 2465 if x_rank == 4: 2466 if isinstance(size, int): 2467 size = F.scalar_to_tensor(size, mstype.int32) 2468 elif isinstance(size, tuple): 2469 size = seq.TupleToTensor()(size, mstype.int32) 2470 else: 2471 size = seq.ListToTensor()(size, mstype.int32) 2472 resize = _get_cache_prim(P.ResizeNearestNeighborV2)( 2473 align_corners=False, 2474 half_pixel_centers=True) 2475 x = resize(x, size) 2476 return x 2477 2478 supported_dict = { 2479 "nearest": { 2480 3: (), 2481 4: (), 2482 5: ("scale_factor",) 2483 }, 2484 "linear": { 2485 3: ("align_corners",) 2486 }, 2487 "bilinear": { 2488 4: ("align_corners",) 2489 }, 2490 "bicubic": { 2491 4: ("align_corners",) 2492 }, 2493 "area": { 2494 3: ("scale_factor",), 2495 4: ("scale_factor",), 2496 5: ("scale_factor",) 2497 }, 2498 "nearest-exact": { 2499 3: (), 2500 4: () 2501 }, 2502 "trilinear": { 2503 5: ( 2504 "align_corners", 2505 "scale_factor", 2506 ) 2507 }, 2508 } 2509 resize_func = { 2510 "nearest": run_nearest, 2511 "linear": run_linear, 2512 "bilinear": run_bilinear, 2513 "bicubic": run_bicubic, 2514 "trilinear": run_trilinear, 2515 "area": run_area, 2516 "nearest-exact": run_nearest_exact, 2517 } 2518 2519 if not isinstance(input, Tensor): 2520 raise TypeError( 2521 f"For 'interpolate', 'input' must be a tensor, but got {type(input)}" 2522 ) 2523 2524 if isinstance(size, list): 2525 size = tuple(size) 2526 if isinstance(scale_factor, list): 2527 scale_factor = tuple(scale_factor) 2528 2529 rank = F.rank(input) 2530 shape = F.shape(input) 2531 dim_unknown = _is_dim_unknown(shape) 2532 2533 # check for size and scale_factor 2534 if size is not None and scale_factor is not None: 2535 raise ValueError( 2536 "For 'interpolate', 'size' and 'scale_factor' cannot be set simultaneously" 2537 ) 2538 if size is not None: 2539 if isinstance(size, (list, tuple)): 2540 check_positive_int_sequence_const(size, "size", "interpolate") 2541 if dim_unknown is False: 2542 _interpolate_size_check_with_rank(size, rank) 2543 else: 2544 check_positive_int_const(size, "size", "interpolate") 2545 if dim_unknown is False: 2546 size = tuple([size for _ in range(rank - 2)]) 2547 else: 2548 size = _interploate_make_tuple(rank - 2, size) 2549 elif scale_factor is not None: 2550 if isinstance(scale_factor, (list, tuple)): 2551 check_positive_float_sequence_const(scale_factor, "scale_factor", 2552 "interpolate") 2553 if dim_unknown is False: 2554 _interpolate_scale_factor_check_with_rank(scale_factor, rank) 2555 else: 2556 check_positive_float_const(scale_factor, "scale_factor", 2557 "interpolate") 2558 if dim_unknown is False: 2559 scale_factor = tuple([scale_factor for _ in range(rank - 2)]) 2560 else: 2561 scale_factor = _interploate_make_tuple(rank - 2, scale_factor) 2562 else: 2563 raise ValueError( 2564 "For 'interpolate', 'size' and 'scale_factor' cannot be both empty" 2565 ) 2566 2567 # rank check 2568 _interpolate_mode_check(mode, supported_dict) 2569 if dim_unknown is False: 2570 _interpolate_rank_check(rank, mode, supported_dict) 2571 2572 # "area" mode always requires an explicit size rather than scale factor. 2573 if mode == "area" and size is None: 2574 recompute_scale_factor = True 2575 2576 # recompute_scale_factor 2577 if recompute_scale_factor is not None and recompute_scale_factor: 2578 check_bool_const(recompute_scale_factor, "recompute_scale_factor", 2579 "interpolate") 2580 if size is not None: 2581 raise ValueError( 2582 "For 'interpolate', it is incorrect to set 'recompute_scale_factor' to True" 2583 " after specifying an explicit 'size'.") 2584 if F.isconstant(shape) and F.isconstant(scale_factor): 2585 tuple_len = min(len(shape) - 2, len(scale_factor)) 2586 size = tuple([floor(shape[i + 2] * scale_factor[i]) 2587 for i in range(tuple_len)]) 2588 else: 2589 size = _interpolate_scale_factor_convert_size(shape, scale_factor) 2590 scale_factor = None 2591 else: 2592 if dim_unknown is False: 2593 _interpolate_scale_factor_check(scale_factor, mode, rank, 2594 supported_dict) 2595 2596 # align_corners 2597 if align_corners is not None: 2598 check_bool_const(align_corners, "align_corners", "interpolate") 2599 if dim_unknown is False: 2600 _interpolate_align_corners_mode_check(rank, mode, supported_dict) 2601 else: 2602 align_corners = False 2603 2604 return resize_func.get(mode)(input, size, align_corners, scale_factor) 2605 2606 2607def _interploate_ext_make_tuple(input, value): 2608 """ 2609 make tuple 2610 """ 2611 if isinstance(value, (list, tuple)): 2612 return value 2613 2614 rank = F.rank(input) - 2 2615 out = None 2616 if F.isconstant(value) and F.isconstant(rank): 2617 out = tuple([value for _ in range(rank)]) 2618 else: 2619 s = tuple_to_tensor_((rank,), mstype.int32) 2620 v = None 2621 if isinstance(value, int): 2622 v = F.scalar_to_tensor(value, mstype.int64) 2623 else: 2624 v = F.scalar_to_tensor(value, mstype.float32) 2625 t = fillv2_(s, v) 2626 out = tensor_to_tuple_(t) 2627 return out 2628 2629 2630def _interpolate_ext_scale_factor_convert_size(input, scale_factor): 2631 """ 2632 convert scale_factor to size 2633 """ 2634 shape = F.shape(input) 2635 size = None 2636 if F.isconstant(shape) and F.isconstant(scale_factor): 2637 tuple_len = min(len(shape) - 2, len(scale_factor)) 2638 size = tuple([floor(shape[i + 2] * scale_factor[i]) 2639 for i in range(tuple_len)]) 2640 else: 2641 x = tuple_to_tensor_(shape[2:], mstype.int64) 2642 y = tuple_to_tensor_(scale_factor, mstype.float32) 2643 t = x * y 2644 t = ops.TruncateDiv()(t, Tensor(1)) 2645 t = ops.cast(t, mstype.int64) 2646 size = tensor_to_tuple_(t) 2647 return size 2648 2649 2650def interpolate_ext(input, 2651 size=None, 2652 scale_factor=None, 2653 mode="nearest", 2654 align_corners=None, 2655 recompute_scale_factor=None): 2656 r""" 2657 Samples the input Tensor to the given size or scale_factor by using one of the interpolate algorithms. 2658 2659 .. note:: 2660 - In 'linear' mode, backpropagation does not support scenarios where `scale_factor` is not None 2661 and `align_corners` is False. 2662 2663 Args: 2664 input (Tensor): Tensor to be resized. 2665 Input tensor must be a 3-D, 4-D, or 5-D tensor with shape 2666 :math:`(N, C, [optional D], [optional H], W)` , with data type of float. 2667 size (Union[int, tuple[int], list[int]], optional): The target size. 2668 If size is a tuple or list, its length should be the same as the number of dimensions in input 2669 after removing the first two dimensions N, C. 2670 One and only one of size and scale_factor can be set to None. Default: ``None`` . 2671 scale_factor (Union[float, tuple[float], list[float]], optional): The scale factor of new size of the tensor. 2672 If scale_factor is a tuple or list, its length should be the same as the number of dimensions in input 2673 after removing the first two dimensions N, C. 2674 One and only one of size and scale_factor can be set to None. Default: ``None`` . 2675 mode (str): The sampling algorithm. 2676 One of 'nearest', 'linear' (3D only), 'bilinear' (4D only), 'trilinear' (5D only), 'bicubic' (4D only), 2677 'area', 'nearest-exact'(matches Scikit-Image and PIL nearest neighbours interpolation algorithms and fixes 2678 knows issues with `nearest`, 3D and 4D). Default: ``"nearest"`` . 2679 2680 align_corners (bool): Whether to use corner alignment for coordinate mapping. Assuming a transformation is 2681 applied to the input Tensor along the x-axis, the specific calculation formula is as follows: 2682 2683 .. code-block:: 2684 2685 ori_i = new_length != 1 ? new_i * (ori_length - 1) / (new_length - 1) : 0 # 'align_corners' = True 2686 2687 ori_i = new_length > 1 ? (new_i + 0.5) * ori_length / new_length - 0.5 : 0 # 'align_corners' = False 2688 2689 Among them, :math:`ori\_length` and :math:`new\_length` represent the length of the Tensor before and after 2690 transformation along the x-axis respectively; :math:`new\_i` represents the coordinate of the i-th element 2691 along the x-axis after transformation; :math:`ori\_i` represents 2692 the corresponding coordinate of the original 2693 data along the x-axis. 2694 2695 This is only valid for ``'linear'``, ``'bilinear'``, or ``'bicubic'`` modes. Default: ``False`` . 2696 recompute_scale_factor (bool, optional): Recalculate `scale_factor`. 2697 If True, the parameter `size` will be calculated using the value of the `scale_factor`, 2698 and finally scaled using the value of `size`. 2699 If False, the value of `size` or `scale_factor` will be used for direct interpolation. Default: ``None`` . 2700 2701 .. note:: 2702 The 'nearest-exact' mode is the same as the nearest-neighbor interpolation algorithm used in 2703 scikit-image and PIL. The 'nearest' mode produces the same results as the INTER_NEAREST interpolation 2704 algorithm used in OpenCV. 2705 2706 Args Support List and Supported Platforms: 2707 2708 +---------------+-----------+---------------+--------------+----------------+ 2709 | mode | input.dim | align_corners | scale_factor | device | 2710 +===============+===========+===============+==============+================+ 2711 | nearest | 3 | \- | √ | Ascend,GPU,CPU | 2712 +---------------+-----------+---------------+--------------+----------------+ 2713 | | 4 | \- | √ | Ascend,GPU,CPU | 2714 +---------------+-----------+---------------+--------------+----------------+ 2715 | | 5 | \- | √ | Ascend,GPU,CPU | 2716 +---------------+-----------+---------------+--------------+----------------+ 2717 | linear | 3 | √ | √ | Ascend,GPU,CPU | 2718 +---------------+-----------+---------------+--------------+----------------+ 2719 | bilinear | 4 | √ | × | Ascend,GPU,CPU | 2720 +---------------+-----------+---------------+--------------+----------------+ 2721 | bicubic | 4 | √ | × | Ascend,GPU,CPU | 2722 +---------------+-----------+---------------+--------------+----------------+ 2723 | area | 3 | \- | √ | Ascend,GPU,CPU | 2724 +---------------+-----------+---------------+--------------+----------------+ 2725 | | 4 | \- | √ | Ascend,GPU,CPU | 2726 +---------------+-----------+---------------+--------------+----------------+ 2727 | | 5 | \- | √ | Ascend,GPU,CPU | 2728 +---------------+-----------+---------------+--------------+----------------+ 2729 | nearest-exact | 3 | \- | × | Ascend,CPU | 2730 +---------------+-----------+---------------+--------------+----------------+ 2731 | | 4 | \- | × | Ascend,CPU | 2732 +---------------+-----------+---------------+--------------+----------------+ 2733 | trilinear | 5 | √ | √ | Ascend,GPU,CPU | 2734 +---------------+-----------+---------------+--------------+----------------+ 2735 2736 - `-` indicates that there is no such parameter. 2737 - `×` indicates that this parameter is not currently supported. 2738 - `√` indicates that this parameter is supported. 2739 2740 Returns: 2741 Tensor, resized, whose dimensions and dtype are the same as `input`. 2742 2743 Raises: 2744 TypeError: `input` is not a Tensor. 2745 ValueError: Both `size` and `scale_factor` are not empty. 2746 ValueError: Both `size` and `scale_factor` are empty. 2747 ValueError: When `size` is a tuple or list, its length is not equal to `input.ndim - 2`. 2748 ValueError: When `scale_factor` is a tuple or list, its length is not equal to `input.ndim - 2`. 2749 ValueError: `mode` is not in the list of supported modes. 2750 ValueError: `input.ndim` is not in the list of supported dimensions for the corresponding mode. 2751 ValueError: `size` is not empty, `recompute_scale_factor` is not empty. 2752 ValueError: `scale_factor` is not in the corresponding list of supported values. 2753 ValueError: `align_corners` is not in the corresponding list of supported values. 2754 2755 Supported Platforms: 2756 ``Ascend`` ``GPU`` ``CPU`` 2757 2758 Examples: 2759 >>> import mindspore 2760 >>> from mindspore import Tensor, mint 2761 >>> input = Tensor([[[1, 2, 3], [4, 5, 6]]], mindspore.float32) 2762 >>> output = mint.interpolate(input, size=(6,), mode='nearest') 2763 >>> print(output) 2764 [[[1. 1. 2. 2. 3. 3.] 2765 [4. 4. 5. 5. 6. 6.]]] 2766 """ 2767 def run_nearest(x, size, align_corners=None, scale_factor=None): 2768 x_rank = F.rank(x) 2769 if x_rank == 3: 2770 x = _get_cache_prim(ops.auto_generate.UpsampleNearest1D)()( 2771 x, size, scale_factor) 2772 elif x_rank == 4: 2773 x = _get_cache_prim(ops.auto_generate.UpsampleNearest2D)()( 2774 x, size, scale_factor) 2775 else: 2776 x = _get_cache_prim(P.UpsampleNearest3D)()(x, size, scale_factor) 2777 return x 2778 2779 def run_linear(x, size, align_corners=None, scale_factor=None): 2780 out = _get_cache_prim( 2781 ops.auto_generate.UpsampleLinear1D)()(x, size, scale_factor, align_corners) 2782 return out 2783 2784 def run_bilinear(x, size, align_corners=None, scale_factor=None): 2785 out = _get_cache_prim( 2786 ops.auto_generate.UpsampleBilinear2D)()(x, size, scale_factor, align_corners) 2787 return out 2788 2789 def run_trilinear(x, size, align_corners=None, scale_factor=None): 2790 resize = _get_cache_prim(P.nn_ops.UpsampleTrilinear3D)(align_corners) 2791 return resize(x, size, scale_factor) 2792 2793 def run_bicubic(x, size, align_corners=None, scale_factor=None): 2794 resize = _get_cache_prim(P.image_ops.ResizeBicubic)( 2795 align_corners=align_corners, half_pixel_centers=not align_corners) 2796 x = resize(x, size) 2797 return x 2798 2799 def run_area(x, size, align_corners=None, scale_factor=None): 2800 x_rank = F.rank(x) 2801 if x_rank == 3: 2802 x = F.adaptive_avg_pool1d(x, size[0]) 2803 elif x_rank == 4: 2804 x = F.adaptive_avg_pool2d(x, tuple(size)) 2805 else: 2806 x = F.adaptive_avg_pool3d(x, tuple(size)) 2807 return x 2808 2809 def run_nearest_exact(x, size, align_corners=None, scale_factor=None): 2810 x_rank = F.rank(x) 2811 if x_rank == 3: 2812 size = size[:1] + (1,) 2813 # For impl of nearest 3D use 4D. 2814 x = x.unsqueeze(-1) 2815 resize = _get_cache_prim(P.ResizeNearestNeighborV2)( 2816 align_corners=False, 2817 half_pixel_centers=True) 2818 x = resize(x, size) 2819 x = _get_cache_prim(P.Squeeze)(-1)(x) 2820 if x_rank == 4: 2821 resize = _get_cache_prim(P.ResizeNearestNeighborV2)( 2822 align_corners=False, 2823 half_pixel_centers=True) 2824 x = resize(x, size) 2825 return x 2826 2827 2828 resize_funcs = { 2829 "nearest": run_nearest, 2830 "linear": run_linear, 2831 "bilinear": run_bilinear, 2832 "bicubic": run_bicubic, 2833 "trilinear": run_trilinear, 2834 "area": run_area, 2835 "nearest-exact": run_nearest_exact, 2836 } 2837 2838 # mode check 2839 if mode not in resize_funcs: 2840 raise ValueError( 2841 f"For 'interpolate', 'mode' must be in '{list(resize_funcs)}', but got {mode}" 2842 ) 2843 if mode in ("nearest", "area", "nearest-exact"): 2844 if align_corners is not None: 2845 raise ValueError("align_corners option can only be set with the " 2846 "interpolating modes: linear | bilinear | bicubic | trilinear" 2847 ) 2848 else: 2849 if align_corners is None: 2850 align_corners = False 2851 2852 # check for size and scale_factor 2853 if size is not None and scale_factor is not None: 2854 raise ValueError( 2855 "For 'interpolate', 'size' and 'scale_factor' cannot be set simultaneously" 2856 ) 2857 if size is not None: 2858 size = _interploate_ext_make_tuple(input, size) 2859 elif scale_factor is not None: 2860 scale_factor = _interploate_ext_make_tuple(input, scale_factor) 2861 else: 2862 raise ValueError( 2863 "For 'interpolate', 'size' and 'scale_factor' cannot be both empty" 2864 ) 2865 2866 # "area" mode always requires an explicit size rather than scale factor. 2867 if mode == "area" and size is None: 2868 recompute_scale_factor = True 2869 2870 # recompute_scale_factor 2871 if recompute_scale_factor is not None and recompute_scale_factor: 2872 if size is not None: 2873 raise ValueError( 2874 "For 'interpolate', it is incorrect to set 'recompute_scale_factor' to True" 2875 " after specifying an explicit 'size'.") 2876 size = _interpolate_ext_scale_factor_convert_size(input, scale_factor) 2877 scale_factor = None 2878 2879 # scale_factor 2880 if mode in ("bilinear", "bicubic", "nearest-exact"): 2881 if scale_factor is not None: 2882 raise ValueError("scale_factor option can only be set with the " 2883 "interpolating modes: nearest | linear | area | trilinear" 2884 ) 2885 2886 return resize_funcs.get(mode)(input, size, align_corners, scale_factor) 2887 2888 2889def upsample(input, size=None, scale_factor=None, mode="nearest", align_corners=None, recompute_scale_factor=None): 2890 r""" 2891 Alias for :func:`mindspore.ops.interpolate` . 2892 2893 Supported Platforms: 2894 ``Ascend`` ``GPU`` ``CPU`` 2895 """ 2896 return interpolate(input, size, scale_factor, mode, align_corners, recompute_scale_factor) 2897 2898 2899def softsign(x): 2900 r""" 2901 SoftSign activation function. 2902 2903 The function is shown as follows: 2904 2905 .. math:: 2906 \text{SoftSign}(x) = \frac{x}{1 + |x|} 2907 2908 Softsign Activation Function Graph: 2909 2910 .. image:: ../images/Softsign.png 2911 :align: center 2912 2913 Args: 2914 x (Tensor): Tensor of shape :math:`(N, *)`, where :math:`*` means, any number of 2915 additional dimensions, with float16 or float32 data type. 2916 2917 Returns: 2918 Tensor, with the same type and shape as the `x`. 2919 2920 Raises: 2921 TypeError: If `x` is not a Tensor. 2922 TypeError: If dtype of `x` is neither float16 nor float32. 2923 2924 Supported Platforms: 2925 ``Ascend`` ``GPU`` ``CPU`` 2926 2927 Examples: 2928 >>> import mindspore 2929 >>> import numpy as np 2930 >>> from mindspore import Tensor, ops 2931 >>> x = Tensor(np.array([0, -1, 2, 30, -30]), mindspore.float32) 2932 >>> output = ops.softsign(x) 2933 >>> print(output) 2934 [ 0. -0.5 0.6666667 0.9677419 -0.9677419] 2935 """ 2936 return softsign_(x) 2937 2938 2939def soft_margin_loss(input, target, reduction='mean'): 2940 r""" 2941 Calculate the soft margin loss of input and target. 2942 2943 Creates a criterion that optimizes a two-class classification 2944 logistic loss between input tensor :math:`x` and target tensor :math:`y` 2945 (containing 1 or -1). 2946 2947 .. math:: 2948 \text{loss}(x, y) = \sum_i \frac{\log(1 + \exp(-y[i]*x[i]))}{\text{x.nelement}()} 2949 2950 where :math:`x.nelement()` is the number of elements of :math:`x`. 2951 2952 .. warning:: 2953 This is an experimental API that is subject to change or deletion. 2954 2955 Args: 2956 input (Tensor): Predict data. Data type must be float16 or float32. 2957 target (Tensor): Ground truth data, with the same type and shape as `input`. 2958 reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` , 2959 ``'sum'`` . Default: ``'mean'`` . 2960 2961 - ``'none'``: no reduction will be applied. 2962 - ``'mean'``: compute and return the mean of elements in the output. 2963 - ``'sum'``: the output elements will be summed. 2964 2965 Outputs: 2966 Tensor or Scalar. If `reduction` is ``'none'``, its shape is the same as `input`. 2967 Otherwise, a scalar value will be returned. 2968 2969 Raises: 2970 TypeError: If `input` or `target` is not a Tensor. 2971 TypeError: If dtype of `input` or `target` is neither float16 nor float32. 2972 ValueError: If shape of `input` is not the same as that of `target`. 2973 ValueError: If `reduction` is not one of ``'none'``, ``'mean'`` or ``'sum'``. 2974 2975 Supported Platforms: 2976 ``Ascend`` ``GPU`` 2977 2978 Examples: 2979 >>> import mindspore 2980 >>> import numpy as np 2981 >>> from mindspore import Tensor, ops 2982 >>> logits = Tensor(np.array([[0.3, 0.7], [0.5, 0.5]]), mindspore.float32) 2983 >>> labels = Tensor(np.array([[-1, 1], [1, -1]]), mindspore.float32) 2984 >>> output = ops.soft_margin_loss(logits, labels) 2985 >>> print(output) 2986 0.6764238 2987 """ 2988 soft_margin_loss_op = _get_cache_prim(P.SoftMarginLoss)(reduction=reduction) 2989 output = soft_margin_loss_op(input, target) 2990 return output 2991 2992 2993def softmax(input, axis=-1, *, dtype=None): 2994 r""" 2995 Applies the Softmax operation to the input tensor on the specified axis. 2996 Suppose a slice in the given axis :math:`axis`, then for each element :math:`input_i`, 2997 the Softmax function is shown as follows: 2998 2999 .. math:: 3000 \text{output}(input_i) = \frac{\exp(input_i)}{\sum_{j = 0}^{N-1}\exp(input_j)}, 3001 3002 where :math:`N` is the length of the tensor. 3003 3004 Args: 3005 input (Tensor): Tensor of shape :math:`(N, *)`, where :math:`*` means, any number of 3006 additional dimensions, with float16 or float32 data type. 3007 axis (int, optional): The axis to perform the Softmax operation. Default: ``-1`` . 3008 3009 Keyword Args: 3010 dtype (:class:`mindspore.dtype`, optional): When set, `input` will be converted to the specified type, 3011 `dtype`, before execution, and dtype of returned Tensor will also be `dtype`. Default: ``None`` . 3012 3013 Returns: 3014 Tensor, with the same type and shape as the `input`. 3015 3016 Raises: 3017 TypeError: If `axis` is not an int. 3018 3019 Supported Platforms: 3020 ``Ascend`` ``GPU`` ``CPU`` 3021 3022 Examples: 3023 >>> import mindspore 3024 >>> import numpy as np 3025 >>> from mindspore import Tensor, ops 3026 >>> input = Tensor(np.array([1, 2, 3, 4, 5]), mindspore.float32) 3027 >>> output = ops.softmax(input) 3028 >>> print(output) 3029 [0.01165623 0.03168492 0.08612854 0.23412167 0.6364086 ] 3030 """ 3031 3032 if not isinstance(axis, int): 3033 type_axis = type(axis).__name__ 3034 raise TypeError(f" the type of 'axis' must be 'int', but got '{axis}' with type '{type_axis}'.") 3035 if dtype is not None: 3036 input = ops.cast(input, dtype) 3037 softmax_ = _get_cache_prim(P.Softmax)(axis) 3038 return softmax_(input) 3039 3040 3041def softmax_ext(input, dim=None, dtype=None): 3042 r""" 3043 Applies the Softmax operation to the input tensor on the specified axis. 3044 Suppose a slice in the given axis :math:`dim`, then for each element :math:`input_i`, 3045 the Softmax function is shown as follows: 3046 3047 .. math:: 3048 \text{output}(input_i) = \frac{\exp(input_i)}{\sum_{j = 0}^{N-1}\exp(input_j)}, 3049 3050 where :math:`N` is the length of the tensor. 3051 3052 Args: 3053 input (Tensor): Tensor of shape :math:`(N, *)`, where :math:`*` means, any number of 3054 additional dimensions. 3055 dim (int, optional): The dim to perform the Softmax operation. Default: ``None`` . 3056 3057 Keyword Args: 3058 dtype (:class:`mindspore.dtype`, optional): When set, `input` will be converted to the specified type, 3059 `dtype`, before execution, and dtype of returned Tensor will also be `dtype`. Default: ``None`` . 3060 3061 Returns: 3062 Tensor, with the same type and shape as the `input`. 3063 3064 Raises: 3065 TypeError: If `dim` is not an int. 3066 3067 Supported Platforms: 3068 ``Ascend`` ``GPU`` ``CPU`` 3069 3070 Examples: 3071 >>> import mindspore 3072 >>> import numpy as np 3073 >>> from mindspore import Tensor, ops 3074 >>> input = Tensor(np.array([1, 2, 3, 4, 5]), mindspore.float32) 3075 >>> output = ops.function.nn_func.softmax_ext(input) 3076 >>> print(output) 3077 [0.01165623 0.03168492 0.08612854 0.23412167 0.6364086 ] 3078 """ 3079 dim = -1 if dim is None else dim 3080 if not isinstance(dim, int): 3081 type_dim = type(dim).__name__ 3082 raise TypeError(f" the type of 'dim' must be 'int', but got '{dim}' with type '{type_dim}'.") 3083 if dtype is not None: 3084 input = ops.cast(input, dtype) 3085 softmax_ = _get_cache_prim(P.Softmax)(dim) 3086 return softmax_(input) 3087 3088 3089def softmin(x, axis=-1, *, dtype=None): 3090 r""" 3091 Applies the Softmin operation to the input tensor on the specified axis. 3092 Suppose a slice in the given axis :math:`x`, then for each element :math:`x_i`, 3093 the Softmin function is shown as follows: 3094 3095 .. math:: 3096 \text{output}(x_i) = \frac{\exp(-x_i)}{\sum_{j = 0}^{N-1}\exp(-x_j)}, 3097 3098 where :math:`N` is the length of the tensor. 3099 3100 Args: 3101 axis (Union[int, tuple[int]], optional): The axis to perform the Softmin operation. Default: ``-1`` . 3102 x (Tensor): Tensor of shape :math:`(N, *)`, where :math:`*` means, any number of 3103 additional dimensions, with float16 or float32 data type. 3104 3105 Keyword Args: 3106 dtype (:class:`mindspore.dtype`, optional): When set, `x` will be converted to the specified type, 3107 `dtype`, before execution, and dtype of returned Tensor will also be `dtype`. Default: ``None`` . 3108 3109 Returns: 3110 Tensor, with the same type and shape as `x`. 3111 3112 Raises: 3113 TypeError: If `axis` is not an int or a tuple. 3114 TypeError: If dtype of `x` is neither float16 nor float32. 3115 ValueError: If `axis` is a tuple whose length is less than 1. 3116 ValueError: If `axis` is a tuple whose elements are not all in range [-len(logits.shape), len(logits.shape)). 3117 3118 Supported Platforms: 3119 ``Ascend`` ``GPU`` ``CPU`` 3120 3121 Examples: 3122 >>> import mindspore 3123 >>> import numpy as np 3124 >>> from mindspore import Tensor, ops 3125 >>> x = Tensor(np.array([-1, -2, 0, 2, 1]), mindspore.float16) 3126 >>> output = ops.softmin(x) 3127 >>> print(output) 3128 [0.2341 0.636 0.0862 0.01165 0.03168 ] 3129 """ 3130 3131 if dtype is not None: 3132 x = ops.cast(x, dtype) 3133 softmax_ = _get_cache_prim(P.Softmax)(axis) 3134 return softmax_(-1*x) 3135 3136 3137def softshrink(x, lambd=0.5): 3138 r""" 3139 Applies the Softshrink function element-wise. 3140 3141 .. math:: 3142 \text{SoftShrink}(x) = 3143 \begin{cases} 3144 x - \lambda, & \text{ if } x > \lambda \\ 3145 x + \lambda, & \text{ if } x < -\lambda \\ 3146 0, & \text{ otherwise } 3147 \end{cases} 3148 3149 SoftShrink Activation Function Graph: 3150 3151 .. image:: ../images/Softshrink.png 3152 :align: center 3153 3154 Args: 3155 x (Tensor): The input of soft shrink with data type of float16 or float32. 3156 lambd (float): The :math:`\lambda` must be no less than zero. Default: ``0.5`` . 3157 3158 Returns: 3159 Tensor, has the same shape and data type as `x`. 3160 3161 Raises: 3162 TypeError: If `lambd` is not a float. 3163 TypeError: If `x` is not a Tensor. 3164 TypeError: If dtype of `x` is neither float16 nor float32. 3165 ValueError: If `lambd` is less than 0. 3166 3167 Supported Platforms: 3168 ``Ascend`` ``GPU`` ``CPU`` 3169 3170 Examples: 3171 >>> import mindspore 3172 >>> from mindspore import Tensor 3173 >>> from mindspore import ops 3174 >>> import numpy as np 3175 >>> x = Tensor(np.array([[ 0.5297, 0.7871, 1.1754], [ 0.7836, 0.6218, -1.1542]]), mindspore.float32) 3176 >>> output = ops.softshrink(x) 3177 >>> print(output) 3178 [[ 0.02979 0.287 0.676 ] 3179 [ 0.2837 0.1216 -0.6543 ]] 3180 """ 3181 soft_shrink_op = _get_cache_prim(P.SoftShrink)(lambd) 3182 return soft_shrink_op(x) 3183 3184 3185def soft_shrink(input, lambd=0.5): 3186 r""" 3187 `soft_shrink` is deprecated, please use `softshrink` instead. 3188 """ 3189 logger.warning("`soft_shrink` is deprecated, please use `softshrink` instead.") 3190 soft_shrink_op = _get_cache_prim(P.SoftShrink)(lambd) 3191 return soft_shrink_op(input) 3192 3193 3194def softplus(input, beta=1, threshold=20): # pylint:disable=redefined-outer-name 3195 r""" 3196 Applies softplus function to `input` element-wise. 3197 3198 The softplus function is shown as follows, x is the element of `input` : 3199 3200 .. math:: 3201 3202 \text{output} = \frac{1}{beta}\log(1 + \exp(\text{beta * x})) 3203 3204 When :math:`input * beta > threshold`, the implementation converts to the linear function 3205 to ensure numerical stability. 3206 3207 Args: 3208 input (Tensor) - Tensor of any dimension. 3209 Supported dtypes: 3210 3211 - GPU/CPU: float16, float32, float64. 3212 - Ascend: float16, float32. 3213 3214 beta (int, optional) - The :math:`\beta` value in softplus function. Default: ``1`` . 3215 threshold (int, optional) - When :math:`input * beta > threshold`, converting softplus to a linear function. 3216 Default: ``20`` . 3217 3218 Returns: 3219 Tensor, with the same type and shape as the `input` . 3220 3221 Raises: 3222 TypeError: If `input` is not a Tensor. 3223 TypeError: If the dtype of `input` is not float16, float32 or float64. 3224 3225 Supported Platforms: 3226 ``Ascend`` ``GPU`` ``CPU`` 3227 3228 Examples: 3229 >>> import mindspore 3230 >>> import numpy as np 3231 >>> from mindspore import Tensor, ops 3232 >>> input = Tensor(np.array([0.1, 0.2, 30, 25]), mindspore.float32) 3233 >>> output = ops.softplus(input) 3234 >>> print(output) 3235 [0.7443967 0.79813886 30. 25.] 3236 """ 3237 scaling_input = beta * input 3238 op_output = (1 / beta) * softplus_(scaling_input) 3239 return ops.select(input * beta > threshold, input, op_output) 3240 3241 3242def selu(input_x): 3243 r""" 3244 Activation function SeLU (Scaled exponential Linear Unit). 3245 3246 The activation function is defined as: 3247 3248 .. math:: 3249 E_{i} = 3250 scale * 3251 \begin{cases} 3252 x_{i}, &\text{if } x_{i} \geq 0; \cr 3253 \text{alpha} * (\exp(x_i) - 1), &\text{otherwise.} 3254 \end{cases} 3255 3256 where :math:`alpha` and :math:`scale` are pre-defined constants(:math:`alpha=1.67326324` 3257 and :math:`scale=1.05070098`). 3258 3259 See more details in `Self-Normalizing Neural Networks <https://arxiv.org/abs/1706.02515>`_. 3260 3261 SeLU Activation Function Graph: 3262 3263 .. image:: ../images/SeLU.png 3264 :align: center 3265 3266 Args: 3267 input_x (Tensor): Tensor of any dimension, 3268 the data type is int8, int32, float16, float32, or float64 (CPU, GPU only). 3269 3270 Returns: 3271 Tensor, with the same type and shape as the `input_x`. 3272 3273 Raises: 3274 TypeError: If dtype of `input_x` is not int8, int32, float16, float32, or float64. 3275 3276 Supported Platforms: 3277 ``Ascend`` ``GPU`` ``CPU`` 3278 3279 Examples: 3280 >>> import mindspore 3281 >>> import numpy as np 3282 >>> from mindspore import Tensor, ops 3283 >>> input_x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32) 3284 >>> output = ops.selu(input_x) 3285 >>> print(output) 3286 [[-1.1113307 4.202804 -1.7575096] 3287 [ 2.101402 -1.7462534 9.456309 ]] 3288 """ 3289 return selu_(input_x) 3290 3291 3292def logsigmoid(x): 3293 r""" 3294 Applies logsigmoid activation element-wise. The input is a Tensor with any valid shape. 3295 3296 Logsigmoid is defined as: 3297 3298 .. math:: 3299 \text{logsigmoid}(x_{i}) = \log(\frac{1}{1 + \exp(-x_i)}), 3300 3301 where :math:`x_{i}` is the element of the input. 3302 3303 LogSigmoid Activation Function Graph: 3304 3305 .. image:: ../images/LogSigmoid.png 3306 :align: center 3307 3308 Args: 3309 x (Tensor): The input of LogSigmoid with data type of float16 or float32. 3310 The shape is :math:`(N,*)` where :math:`*` means, any number of additional dimensions. 3311 3312 Returns: 3313 Tensor, with the same type and shape as the `x`. 3314 3315 Raises: 3316 TypeError: If dtype of `x` is neither float16 nor float32. 3317 3318 Supported Platforms: 3319 ``Ascend`` ``GPU`` ``CPU`` 3320 3321 Examples: 3322 >>> import mindspore 3323 >>> import numpy as np 3324 >>> from mindspore import Tensor, ops 3325 >>> x = Tensor(np.array([1.0, 2.0, 3.0]), mindspore.float32) 3326 >>> output = ops.logsigmoid(x) 3327 >>> print(output) 3328 [-0.31326166 -0.12692806 -0.04858734] 3329 """ 3330 output = sigmoid_(x) 3331 ret = log_(output) 3332 return ret 3333 3334 3335def _check_dense_add_bias_shape(input_shape, output_shape, bias_shape): 3336 """Check that the output has the correct shape after adding bias.""" 3337 if input_shape != output_shape: 3338 raise ValueError(f"For dense, the bias shape {bias_shape} does not match the input shape {input_shape}.") 3339 3340 3341@_primexpr 3342def check_dense_inputs_same_shape(input1_shape, input2_shape, prim_name=None): 3343 """check bidense input Tensors' shape""" 3344 msg_prefix = f"For '{prim_name}', the" if prim_name else "The" 3345 if input1_shape[:-1] != input2_shape[:-1]: 3346 raise ValueError(f"{msg_prefix} dimensions except the last of 'input1' must be same as 'input2', but got " 3347 f"{input1_shape} of 'input1' and {input2_shape} of 'input2'") 3348 3349 3350def bidense(input1, input2, weight, bias=None): 3351 r""" 3352 Applies bilinear dense connected layer for `input1` and `input2`. The bilinear dense function is defined as: 3353 3354 .. math:: 3355 output = x_{1}^{T}Ax_{2} + b 3356 3357 :math:`x_{1}` represents `input1` , :math:`x_{2}` represents `input2` , :math:`A` represents `weight` , 3358 :math:`b` represents `bias` . 3359 3360 .. warning:: 3361 This is an experimental API that is subject to change or deletion. 3362 3363 Args: 3364 input1 (Tensor): Input Tensor of shape :math:`(*, in1\_channels)`, 3365 where :math:`*` means any number of additional dimensions. All but the last dimension 3366 should be the same with `input2`. 3367 input2 (Tensor): Input Tensor of shape :math:`(*, in2\_channels)`, 3368 where :math:`*` means any number of additional dimensions. All but the last dimension 3369 should be the same with `input1`. 3370 weight (Tensor): The weight applied to the input1 and input2. 3371 The shape is :math:`(out\_channels, in1\_channels, in2\_channels)`. 3372 bias (Tensor, optional): Additive biases to the output. 3373 The shape is :math:`(out\_channels)` or :math:`()`. Defaults: ``None`` , the `bias` is 0. 3374 3375 Returns: 3376 Tensor, shape :math:`(*, out\_channels)`, where :math:`*` means any number of additional dimensions. 3377 All but the last dimension should be the same with the input Tensors. 3378 3379 Raises: 3380 TypeError: If `input1` is not Tensor. 3381 TypeError: If `input2` is not Tensor. 3382 TypeError: If `weight` is not Tensor. 3383 TypeError: If `bias` is not Tensor. 3384 ValueError: If dimensions except the last of 'input1' are different from 'input2' . 3385 3386 3387 Supported Platforms: 3388 ``Ascend`` ``GPU`` ``CPU`` 3389 3390 Examples: 3391 >>> import mindspore 3392 >>> from mindspore import Tensor, ops 3393 >>> input1 = mindspore.Tensor([[-1.1283, 1.2603], 3394 ... [0.0214, 0.7801], 3395 ... [-1.2086, 1.2849]], mindspore.float32) 3396 >>> input2 = mindspore.Tensor([[-0.4631, 0.3238, 0.4201], 3397 ... [0.6215, -1.0910, -0.5757], 3398 ... [-0.7788, -0.0706, -0.7942]], mindspore.float32) 3399 >>> weight = mindspore.Tensor([[[-0.3132, 0.9271, 1.1010], 3400 ... [0.6555, -1.2162, -0.2987]], 3401 ... [[1.0458, 0.5886, 0.2523], 3402 ... [-1.3486, -0.8103, -0.2080]], 3403 ... [[1.1685, 0.5569, -0.3987], 3404 ... [-0.4265, -2.6295, 0.8535]], 3405 ... [[0.6948, -1.1288, -0.6978], 3406 ... [0.3511, 0.0609, -0.1122]]], mindspore.float32) 3407 >>> output = ops.bidense(input1, input2, weight) 3408 >>> print(output) 3409 [[-2.0612743 0.5581219 0.22383511 0.8667302] 3410 [1.4476739 0.12626505 1.6552988 0.21297503] 3411 [0.6003161 2.912046 0.5590313 -0.35449564]] 3412 """ 3413 _check_is_tensor("input1", input1, "bidense") 3414 _check_is_tensor("input2", input2, "bidense") 3415 _check_is_tensor("weight", weight, "bidense") 3416 _check_is_tensor("bias", bias, "bidense") 3417 input1_shape = input1.shape 3418 input2_shape = input2.shape 3419 check_dense_inputs_same_shape(input1_shape, input2_shape, "bidense") 3420 3421 if len(input1_shape) != 2: 3422 input1 = input1.reshape((-1, input1_shape[-1])) 3423 input2 = input2.reshape((-1, input2_shape[-1])) 3424 batch_size = input1.shape[0] 3425 output = matmul_(input1, weight.transpose(1, 2, 0).view(input1_shape[-1], -1)) 3426 output = output.view(batch_size, input2_shape[-1], weight.shape[0]) 3427 output = output.transpose(2, 0, 1) * input2 3428 output = output.sum(2).swapaxes(0, 1) 3429 if bias is not None: 3430 if input1.dtype != bias.dtype or input2.dtype != bias.dtype: 3431 raise TypeError(f"For 'bidense', the dtype of 'bias', 'input1' and 'input2' must be the same," 3432 f" but got {bias.dtype}, {input1.dtype} and {input2.dtype}.") 3433 output = bias_add_(output.astype(bias.dtype), bias) 3434 if len(input1_shape) != 2: 3435 output_shape = input1_shape[:-1] + (-1,) 3436 output = output.reshape(output_shape) 3437 return output 3438 3439 3440def deformable_conv2d(x, weight, offsets, kernel_size, strides, padding, bias=None, dilations=(1, 1, 1, 1), groups=1, 3441 deformable_groups=1, modulated=True): 3442 r""" 3443 Given 4D tensor inputs `x`, `weight` and `offsets`, compute a 2D deformable convolution. The deformable convolution 3444 operation can be expressed as follow: 3445 3446 Deformable Convolution v1: 3447 3448 .. math:: 3449 y(p)=\sum_{k=1}^{K}w_{k}\cdot x(p+p_{k}+\Delta{p_{k}}) 3450 3451 Deformable Convolution v2: 3452 3453 .. math:: 3454 y(p)=\sum_{k=1}^{K}w_{k}\cdot x(p+p_{k}+\Delta{p_{k}})\cdot \Delta{m_{k}} 3455 3456 Where :math:`\Delta{p_{k}}` and :math:`\Delta{m_{k}}` are the learnable offset and modulation scalar for the k-th 3457 location. For details, please refer to `Deformable ConvNets v2: More Deformable, Better Results 3458 <https://arxiv.org/abs/1811.11168>`_ and `Deformable Convolutional Networks <https://arxiv.org/abs/1703.06211>`_. 3459 3460 Args: 3461 x (Tensor): A 4D tensor of input image. With the format "NCHW", 3462 the shape is :math:`(N, C_{in}, H_{in}, W_{in})`. Dtype: float16 or float32. 3463 weight (Tensor): A 4D tensor of learnable filters. Must have the same type as `x`. 3464 The shape is :math:`(C_{out}, C_{in} / groups, H_{f}, W_{f})`. 3465 offsets (Tensor): A 4D tensor of x-y coordinates offset and mask. With the format "NCHW", 3466 the shape is :math:`(batch, 3 * deformable\_groups * H_{f} * W_{f}, H_{out}, W_{out})`. Note the C dimension 3467 is stored in the order of (offset_x, offset_y, mask). Must have the same type as `x`. 3468 kernel_size (tuple[int]): A tuple of 2 integers. The size of kernel. 3469 strides (tuple[int]): A tuple of 4 integers. The stride of the sliding window for each dimension of 3470 input. The dimension order is interpreted according to the data format of `x`. The N and C dimensions must 3471 be set to 1. 3472 padding (tuple[int]): A tuple of 4 integers. The number of pixels to add to each (top, bottom, left, 3473 right) side of the input. 3474 bias (Tensor, optional): An 1D tensor of additive biases to the filter outputs. 3475 The shape is :math:`(C_{out})`. Default: ``None`` . 3476 dilations (tuple[int], optional): A tuple of 4 integers. The dilation factor for each dimension of input. The 3477 dimension order is interpreted according to the data format of `x`. The N and C dimensions must be set 3478 to 1. Default: ``(1, 1, 1, 1)`` . 3479 groups (int, optional): An integer of type int32. The number of blocked connections from input channels 3480 to output channels. In_channels and out_channels must both be divisible by `groups`. Default: ``1`` . 3481 deformable_groups (int, optional): An integer of type int32. The number of deformable group partitions. 3482 In_channels must be divisible by `deformable_groups`. Default: ``1`` . 3483 modulated (bool, optional): Specifies version of DeformableConv2D, True means v2, False means v1, currently 3484 only supports v2. Default: ``True`` . 3485 3486 Returns: 3487 Tensor, A 4D Tensor of output feature map. With the same type as `x`. With the format "NCHW", 3488 the shape is :math:`(N, C_{out}, H_{out}, W_{out})`. 3489 3490 .. math:: 3491 \begin{array}{ll} \\ 3492 H_{out} = \left \lfloor{\frac{H_{in} + padding[0] + padding[1] - (H_{f} - 1) \times 3493 \text{dilations[2]} - 1 }{\text{stride[0]}} + 1} \right \rfloor \\ 3494 W_{out} = \left \lfloor{\frac{W_{in} + padding[2] + padding[3] - (W_{f} - 1) \times 3495 \text{dilations[3]} - 1 }{\text{stride[1]}} + 1} \right \rfloor \\ 3496 \end{array} 3497 3498 Raises: 3499 TypeError: If `strides`, `padding`, `kernel_size` or `dilations` is not a tuple with integer elements. 3500 TypeError: If `modulated` is not a bool. 3501 ValueError: If the tuple size of `strides`, `padding`, `kernel_size` or `dilations` is not expected. 3502 ValueError: The N or C dimensions of `strides` or `dilations` is not set to 1. 3503 ValueError: If `modulated` is not set to True. 3504 3505 .. warning:: 3506 This is an experimental API that is subject to change or deletion. 3507 3508 Supported Platforms: 3509 ``Ascend`` ``GPU`` ``CPU`` 3510 3511 Examples: 3512 >>> import numpy as np 3513 >>> from mindspore import Tensor, ops 3514 >>> from mindspore import dtype as mstype 3515 >>> x = Tensor(np.ones((4, 3, 10, 10)), mstype.float32) 3516 >>> kh, kw = 3, 3 3517 >>> weight = Tensor(np.ones((5, 3, kh, kw)), mstype.float32) 3518 >>> offsets = Tensor(np.ones((4, 3 * kh * kw, 8, 8)), mstype.float32) 3519 >>> output = ops.deformable_conv2d(x, weight, offsets, (kh, kw), (1, 1, 1, 1), (0, 0, 0, 0)) 3520 >>> print(output.shape) 3521 (4, 5, 8, 8) 3522 """ 3523 deformable_offsets = _get_cache_prim(NN_OPS.DeformableOffsets)(strides, padding, kernel_size, dilations, "NCHW", 3524 deformable_groups, 3525 modulated) 3526 fm_offset = deformable_offsets(x, offsets) 3527 weight_shape = weight.shape 3528 out_channel = weight_shape[0] 3529 strides_conv = (kernel_size[0], kernel_size[1]) 3530 conv = _get_cache_prim(P.Conv2D)(out_channel, kernel_size, 1, "valid", 0, strides_conv, 1, groups) 3531 output = conv(fm_offset, weight) 3532 if bias is not None: 3533 output = bias_add_(output, bias) 3534 return output 3535 3536 3537def pdist(input, p=2.0): 3538 r""" 3539 Calculates the distance between every pair of row vectors in 3540 the input using the p-norm. If the input `input` is a 2D Tensor with shape :math:`(N, M)`, 3541 the `output` must be a 1D Tensor with shape :math:`(N * (N - 1) / 2,)`. 3542 3543 .. math:: 3544 y[n] = \sqrt[p]{{\mid x_{i} - x_{j} \mid}^p} 3545 3546 where :math:`x_{i}, x_{j}` are two different row vectors in the input. 3547 3548 Args: 3549 input (Tensor): Input tensor. dtype: float16, float32 or float64. 3550 p (float): The order of norm distance, :math:`p∈[0, ∞)`. Default: ``2.0`` . 3551 3552 Returns: 3553 Tensor, has the same dtype as `input`. 3554 3555 Raises: 3556 TypeError: If `input` is not a Tensor. 3557 TypeError: If dtype of `input` is not float16, float32 or float64. 3558 TypeError: If `p` is not a float. 3559 ValueError: If `p` is a negative float. 3560 ValueError: If dimension of `input` is less than 2. 3561 3562 Supported Platforms: 3563 ``GPU`` ``CPU`` 3564 3565 Examples: 3566 >>> import numpy as np 3567 >>> from mindspore import Tensor, ops 3568 >>> x = Tensor(np.array([[1.0, 1.0], [2.0, 2.0], [3.0, 3.0]]).astype(np.float32)) 3569 >>> y = ops.pdist(x, p=2.0) 3570 >>> print(y) 3571 [1.4142135 2.828427 1.4142135] 3572 """ 3573 pdist_ = _get_cache_prim(NN_OPS.Pdist)(p=p) 3574 return pdist_(input) 3575 3576 3577def _circular_pad(input_x, padding): 3578 """circular pad""" 3579 if isinstance(padding, tuple): 3580 padding = tuple_to_tensor_(padding, mstype.int64) 3581 elif isinstance(padding, list): 3582 padding = list_to_tensor_(padding, mstype.int64) 3583 is_expand = False 3584 if padding.shape[0] // 2 + 1 == input_x.ndim: 3585 input_x = input_x.expand_dims(0) 3586 is_expand = True 3587 out = PadV3(mode="circular", paddings_contiguous=True)(input_x, padding, None) 3588 if is_expand: 3589 out = out.squeeze(0) 3590 return out 3591 3592 3593def _reflection_pad(input, pad): 3594 """reflection pad""" 3595 out = input 3596 if len(pad) == 2: 3597 out = reflection_pad_1d_op(input, pad) 3598 elif len(pad) == 4: 3599 out = reflection_pad_2d_op(input, pad) 3600 else: 3601 out = reflection_pad_3d_op(input, pad) 3602 return out 3603 3604 3605def _replication_pad(input, pad): 3606 """replication pad""" 3607 out = input 3608 if len(pad) == 2: 3609 out = replication_pad_1d_op(input, pad) 3610 elif len(pad) == 4: 3611 out = replication_pad_2d_op(input, pad) 3612 else: 3613 out = replication_pad_3d_op(input, pad) 3614 return out 3615 3616 3617def pad_ext(input, pad, mode='constant', value=0.0): 3618 r""" 3619 Pads the input tensor according to the pad. 3620 3621 .. warning:: 3622 `circular` mode has poor performance and is not recommended. 3623 3624 Args: 3625 input (Tensor): Tensor of shape :math:`(N, *)`, where :math:`*` means, any number of additional dimensions. 3626 pad (Union[tuple[int], list[int], Tensor]): Filling position of pad. 3627 :math:`\left\lfloor\frac{\text{len(pad)}}{2}\right\rfloor` dimensions 3628 of `input` will be padded. 3629 3630 Example: to pad only the last dimension of the input tensor, then 3631 :attr:`pad` has the form 3632 :math:`(\text{padding_left}, \text{padding_right})`; 3633 3634 Example: to pad the last 2 dimensions of the input tensor, then use 3635 :math:`(\text{padding_left}, \text{padding_right}, \text{padding_top}, \text{padding_bottom})`; 3636 3637 Example: to pad the last 3 dimensions, use 3638 :math:`(\text{padding_left}, \text{padding_right}, \text{padding_top}, \text{padding_bottom}, 3639 \text{padding_front}, \text{padding_back})` and so on. 3640 3641 mode (str, optional): Pad filling mode, ``'constant'`` , ``'reflect'`` , ``'replicate'`` or ``'circular'`` . 3642 Default: ``'constant'`` . 3643 3644 For ``'constant'`` mode, please refer to :class:`mindspore.nn.ConstantPad1d` as an example to understand 3645 this filling pattern and extend the padding pattern to n dimensions. 3646 3647 For ``'reflect'`` mode, please refer to :class:`mindspore.nn.ReflectionPad1d` as an example to understand 3648 this filling pattern. 3649 The reflect mode is used to pad the last three dimensions of 4D or 5D input, the last two dimensions of 3D 3650 or 4D input, or the last dimension of 2D or 3D input. 3651 3652 For ``'replicate'`` mode, please refer to :class:`mindspore.nn.ReplicationPad1d` as an example to understand 3653 this filling pattern. 3654 The replicate mode is used to pad the last three dimensions of 4D or 5D input, the last two dimensions of 3D 3655 or 4D input, or the last dimension of 2D or 3D input. 3656 3657 For ``'circular'`` mode, the pixels from one edge of the image are wrapped around to the opposite edge, 3658 such that the pixel on the right edge of the image is replaced with the pixel on the left edge, 3659 and the pixel on the bottom edge is replaced with the pixel on the top edge. 3660 The circular mode is used to pad the last three dimensions of 4D or 5D input, the last two dimensions of 3D 3661 or 4D input, or the last dimension of 2D or 3D input. 3662 3663 value (Union[int, float, None], optional): Valid only in ``'constant'`` mode. 3664 Set the padding value in ``'constant'`` mode. If the value is None, 0 is used as the default padding value. 3665 Default: ``0.0`` . 3666 3667 Returns: 3668 Tensor, the tensor after padding. 3669 3670 Raises: 3671 TypeError: If `pad` is not an int of tuple or int of list. 3672 TypeError: If `input` is not a Tensor. 3673 ValueError: If length of `pad` is not even. 3674 ValueError: If length of `pad` is greater than 6. 3675 ValueError: If `mode` is not ``'constant'`` and `value` not ``None``. 3676 3677 Supported Platforms: 3678 ``Ascend`` 3679 3680 Examples: 3681 >>> from mindspore import ops 3682 >>> import numpy as np 3683 >>> x = ms.Tensor(np.arange(1 * 2 * 2 * 2).reshape((1, 2, 2, 2)), dtype=ms.float64) 3684 >>> output = ops.function.nn_func.pad_ext(x, [1, 0, 0, 1], mode='constant', value=6.0) 3685 >>> print(output) 3686 [[[[6. 0. 1.] 3687 [6. 2. 3.] 3688 [6. 6. 6.]] 3689 [[6. 4. 5.] 3690 [6. 6. 7.] 3691 [6. 6. 6.]]]] 3692 """ 3693 if not isinstance(input, Tensor): 3694 raise TypeError(f"For 'pad', the type of 'input' must be Tensor, but got {type(input)}.") 3695 out = input 3696 if (isinstance(pad, tuple) and not pad): 3697 return out 3698 if mode == "constant": 3699 value = 0 if value is None else value 3700 out = constant_pad_nd_op(input, pad, value) 3701 else: 3702 if value != 0.0: 3703 raise ValueError(f"Padding mode {mode} doesn\'t take in value argument.") 3704 if mode == "circular": 3705 out = _circular_pad(input, pad) 3706 elif mode == "reflect": 3707 out = _reflection_pad(input, pad) 3708 elif mode == "replicate": 3709 out = _replication_pad(input, pad) 3710 else: 3711 raise ValueError(f"Pad filling mode must be 'constant' 'circular' 'reflect' or 'replicate'.") 3712 return out 3713 3714 3715def _check_pad_inputs(padding): 3716 """check the input of pad""" 3717 if len(padding) % 2 != 0: 3718 raise ValueError(f"For 'pad', the size of padding must be divisible by 2, but got {len(padding)}") 3719 if not isinstance(padding, (tuple, list)): 3720 raise TypeError(f"For 'pad', the type of 'paddings' must be a tuple of int or list of int or a Tensor," 3721 f" but got {type(padding)}.") 3722 for pd in padding: 3723 if not isinstance(pd, int): 3724 raise TypeError(f"For 'pad', the paddings value must be tuple of int or list of int, but got {padding}") 3725 3726 3727def pad(input_x, padding, mode='constant', value=None): 3728 r""" 3729 Pads the input tensor according to the padding. 3730 3731 Args: 3732 input_x (Tensor): Tensor of shape :math:`(N, *)`, where :math:`*` means, any number of additional dimensions 3733 which is required to be no more than 5 in Ascend. 3734 padding (Union[tuple[int], list[int], Tensor]): Filling position of pad where the negative value is not 3735 supported while running in Ascend. 3736 :math:`\left\lfloor\frac{\text{len(padding)}}{2}\right\rfloor` dimensions 3737 of `input_x` will be padded. 3738 3739 Example: to pad only the last dimension of the input tensor, then 3740 :attr:`padding` has the form 3741 :math:`(\text{padding_left}, \text{padding_right})`; 3742 3743 Example: to pad the last 2 dimensions of the input tensor, then use 3744 :math:`(\text{padding_left}, \text{padding_right}, \text{padding_top}, \text{padding_bottom})`; 3745 3746 Example: to pad the last 3 dimensions, use 3747 :math:`(\text{padding_left}, \text{padding_right}, \text{padding_top}, \text{padding_bottom}, 3748 \text{padding_front}, \text{padding_back})` and so on. 3749 3750 mode (str, optional): Pad filling mode, ``'constant'`` , ``'reflect'`` , ``'replicate'`` or ``'circular'`` . 3751 Default: ``'constant'`` . 3752 3753 For ``'constant'`` mode, please refer to :class:`mindspore.nn.ConstantPad1d` as an example to understand 3754 this filling pattern and extend the padding pattern to n dimensions. 3755 3756 For ``'reflect'`` mode, please refer to :class:`mindspore.nn.ReflectionPad1d` as an example to understand 3757 this filling pattern. 3758 The reflect mode is used to pad the last two dimensions of 3D or 4D input, or the last dimension of 2D or 3759 3D input. 3760 3761 For ``'replicate'`` mode, please refer to :class:`mindspore.nn.ReplicationPad1d` as an example to understand 3762 this filling pattern. 3763 The replicate mode is used to pad the last three dimensions of 4D or 5D input, the last two dimensions of 3D 3764 or 4D input, or the last dimension of 2D or 3D input. 3765 3766 For ``'circular'`` mode, the pixels from one edge of the image are wrapped around to the opposite edge, 3767 such that the pixel on the right edge of the image is replaced with the pixel on the left edge, 3768 and the pixel on the bottom edge is replaced with the pixel on the top edge. 3769 The circular mode is used to pad the last three dimensions of 4D or 5D input, the last two dimensions of 3D 3770 or 4D input, or the last dimension of 2D or 3D input. 3771 3772 value (Union[int, float, None], optional): Valid only in ``'constant'`` mode. 3773 Set the padding value in ``'constant'`` mode. If the value is None, 0 is used as the default padding value. 3774 Default: ``None`` . 3775 3776 Returns: 3777 Tensor, the tensor after padding. 3778 3779 Raises: 3780 TypeError: If `padding` is not an int of tuple or int of list. 3781 TypeError: If `input_x` is not a Tensor. 3782 ValueError: If length of `padding` is not even. 3783 ValueError: If length of `padding` is greater than 6. 3784 ValueError: If `mode` is not ``'constant'`` and `value` not ``None``. 3785 ValueError: If rank of `input_x` is more than 5 while running in Ascend. 3786 ValueError: If `paddings` contains negative value while running in Ascend. 3787 3788 Supported Platforms: 3789 ``Ascend`` ``GPU`` ``CPU`` 3790 3791 Examples: 3792 >>> import mindspore as ms 3793 >>> from mindspore import ops 3794 >>> import numpy as np 3795 >>> x = ms.Tensor(np.arange(1 * 2 * 2 * 2).reshape((1, 2, 2, 2)), dtype=ms.float64) 3796 >>> output = ops.pad(x, [1, 0, 0, 1], mode='constant', value=6.0) 3797 >>> print(output) 3798 [[[[6. 0. 1.] 3799 [6. 2. 3.] 3800 [6. 6. 6.]] 3801 [[6. 4. 5.] 3802 [6. 6. 7.] 3803 [6. 6. 6.]]]] 3804 >>> output1 = ops.pad(x, (1, 0, 0, 1), mode='reflect') 3805 >>> print(output1) 3806 [[[[1. 0. 1.] 3807 [3. 2. 3.] 3808 [1. 0. 1.]] 3809 [[5. 4. 5.] 3810 [7. 6. 7.] 3811 [5. 4. 5.]]]] 3812 >>> output2 = ops.pad(x, (1, 1, 2, 1), mode='replicate') 3813 >>> print(output2) 3814 [[[[0. 0. 1. 1.] 3815 [0. 0. 1. 1.] 3816 [0. 0. 1. 1.] 3817 [2. 2. 3. 3.] 3818 [2. 2. 3. 3.]] 3819 [[4. 4. 5. 5.] 3820 [4. 4. 5. 5.] 3821 [4. 4. 5. 5.] 3822 [6. 6. 7. 7.] 3823 [6. 6. 7. 7.]]]] 3824 >>> output3 = ops.pad(x, (1, 1, 2, 1), mode='circular') 3825 >>> print(output3) 3826 [[[[1. 0. 1. 0.] 3827 [3. 2. 3. 2.] 3828 [1. 0. 1. 0.] 3829 [3. 2. 3. 2.] 3830 [1. 0. 1. 0.]] 3831 [[5. 4. 5. 4.] 3832 [7. 6. 7. 6.] 3833 [5. 4. 5. 4.] 3834 [7. 6. 7. 6.] 3835 [5. 4. 5. 4.]]]] 3836 """ 3837 if not isinstance(input_x, Tensor): 3838 raise TypeError(f"For 'pad', the type of 'input_x' must be Tensor, but got {type(input_x)}.") 3839 if (isinstance(padding, (tuple, list)) and not padding) or (isinstance(padding, Tensor) and padding.shape == (0,)): 3840 return input_x 3841 if not isinstance(padding, Tensor): 3842 _check_pad_inputs(padding) 3843 padding = tuple(padding) 3844 is_expand = False 3845 if mode == "constant": 3846 value = 0 if value is None else value 3847 if isinstance(value, (float, int)): 3848 value = scalar_to_tensor_(value, input_x.dtype) 3849 else: 3850 if len(padding) > 6: 3851 raise ValueError(f"For 'pad', the padding must be less than or equal to 6, but got {len(padding)}.") 3852 if value is not None: 3853 raise ValueError(f"For 'pad', the padding mode '{mode}' can not set value, but got value {value}.") 3854 if mode == "replicate": 3855 mode = "edge" 3856 if len(padding) // 2 + 1 == input_x.ndim: 3857 input_x = input_x.expand_dims(0) 3858 is_expand = True 3859 out = PadV3(mode=mode, paddings_contiguous=True)(input_x, padding, value) 3860 if is_expand: 3861 out = out.squeeze(0) 3862 return out 3863 3864 3865def rrelu(input, lower=1.0 / 8, upper=1.0 / 3): 3866 r""" 3867 3868 Randomized Leaky ReLU activation function. 3869 3870 The activation function is defined as: 3871 3872 .. math:: 3873 \text{rrelu}(input_{ji}) = \begin{cases}input_{ji}, &\text{if } input_{ji} \geq 0; \cr 3874 {\alpha_{ji}} * input_{ji}, &\text{otherwise.}\end{cases} 3875 3876 where :math:`\alpha_{ji}` ~ :math:`U(l, u)`, :math:`l \le u`. 3877 3878 Applies the rrelu function elementally, as described in the paper: 3879 `Empirical Evaluation of Rectified Activations in Convolution Network <https://arxiv.org/pdf/1505.00853.pdf>`_ . 3880 3881 Args: 3882 input (Tensor): The input of rrelu is a Tensor of any dimension. 3883 lower (Union[int, float]): Slope of the activation function at x < 0. Default: ``1.0 / 8`` . 3884 upper (Union[int, float]): Slope of the activation function at x < 0. Default: ``1.0 / 3`` . 3885 3886 Returns: 3887 Tensor, after rrelu, has the same type and shape as the `input`. 3888 3889 Raises: 3890 TypeError: If `lower` is not a float or an int. 3891 TypeError: If `upper` is not a float or an int. 3892 TypeError: If `input` is not a Tensor. 3893 TypeError: If `input` is not a Tensor of mindspore.float16 or mindspore.float32. 3894 ValueError: If `lower` is greater than upper. 3895 3896 Supported Platforms: 3897 ``Ascend`` ``GPU`` ``CPU`` 3898 3899 Examples: 3900 >>> import mindspore 3901 >>> import numpy as np 3902 >>> from mindspore import Tensor, ops 3903 >>> x = Tensor(np.array([[-1.0, 4.0], [2.0, 0]]), mindspore.float32) 3904 >>> output = ops.rrelu(x) 3905 >>> print(output) 3906 [[-0.31465699 4. ] 3907 [ 2. 0. ]] 3908 """ 3909 if not isinstance(upper, (float, int)): 3910 raise TypeError(f"For 'rrelu', 'upper' must be an int or a float, but got {type(upper)}") 3911 if not isinstance(lower, (float, int)): 3912 raise TypeError(f"For 'rrelu', 'lower' must be an int or a float, but got {type(lower)}") 3913 if lower > upper: 3914 raise ValueError(f"For 'rrelu', the value of 'upper' must be greater than or equal to 'lower', " 3915 f"but got upper: {upper}, lower: {lower}. ") 3916 if not isinstance(input, Tensor): 3917 raise TypeError(f"For 'rrelu', the 'input' must be a Tensor but got {type(input)}.") 3918 _lower = Tensor(lower, mstype.float32) 3919 _upper = Tensor(upper, mstype.float32) 3920 _size = input.shape 3921 if ops.is_sequence_value_unknown(_size): 3922 _size = tensor_shape_(input) 3923 sign_matrix = sign_(input) 3924 negative_filter = sign_matrix.clip(None, 0) 3925 positive_filter = sign_matrix.clip(0, None) 3926 input_dtype = dtype_(input) 3927 mask = ops.uniform(_size, _lower, _upper).astype(input_dtype) 3928 negative_mask = negative_filter * mask * -1 3929 total_mask = negative_mask + positive_filter 3930 out = total_mask * input 3931 return out 3932 3933 3934def mirror_pad(input_x, paddings, mode): 3935 """ 3936 Pads the input tensor according to the paddings and mode. 3937 3938 Args: 3939 input_x (Tensor): Tensor of shape :math:`(N, *)`, where :math:`*` means, any number of 3940 additional dimensions. 3941 paddings (Tensor): Paddings requires constant tensor. The value of `paddings` is a 3942 matrix(list), and its shape is (N, 2). N is the rank of input data. All elements of paddings 3943 are int type. For the input in the `D` th dimension, paddings[D, 0] indicates how many sizes 3944 to be extended ahead of the input tensor in the `D` th dimension, and paddings[D, 1] 3945 indicates how many sizes to be extended behind the input tensor in the `D` th dimension. Both 3946 paddings[D, 0] and paddings[D, 1] must be no greater than input_x.dim_size(D) 3947 (or input_x.dim_size(D) - 1) if mode is SYMMETRIC (if REFLECT, respectively). 3948 mode (str): Specifies the padding mode. The optional values are "REFLECT" and "SYMMETRIC". 3949 Default: "REFLECT". 3950 3951 Returns: 3952 Tensor, the tensor after padding. 3953 3954 - If `mode` is "REFLECT", it uses a way of symmetrical copying through the axis of symmetry to fill in. 3955 If the `input_x` is [[1,2,3], [4,5,6], [7,8,9]] and `paddings` is [[1,1], [2,2]], then the 3956 `Outputs` is [[6,5,4,5,6,5,4], [3,2,1,2,3,2,1], [6,5,4,5,6,5,4], [9,8,7,8,9,8,7], [6,5,4,5,6,5,4]]. 3957 For a more intuitive understanding, please see the example below. 3958 - If `mode` is "SYMMETRIC", the filling method is similar to the "REFLECT". It is also copied 3959 according to the symmetry axis, except that it includes the symmetry axis. If the `input_x` 3960 is [[1,2,3], [4,5,6], [7,8,9]] and `paddings` is [[1,1], [2,2]], then the `Outputs` is 3961 [[2,1,1,2,3,3,2], [2,1,1,2,3,3,2], [5,4,4,5,6,6,5], [8,7,7,8,9,9,8], [8,7,7,8,9,9,8]]. 3962 For a more intuitive understanding, please see the example below. 3963 3964 Raises: 3965 TypeError: If `input_x` or `paddings` is not a Tensor. 3966 TypeError: If `mode` is not a str. 3967 ValueError: If paddings.size is not equal to 2 * rank of input_x. 3968 3969 Supported Platforms: 3970 ``Ascend`` ``GPU`` ``CPU`` 3971 3972 Examples: 3973 >>> from mindspore import Tensor, ops 3974 >>> input_x = Tensor([[1,2,3], [4,5,6], [7,8,9]]) 3975 >>> mode = "REFLECT" 3976 >>> paddings = Tensor([[1, 1], [2, 2]]) 3977 >>> output = ops.mirror_pad(input_x, paddings, mode) 3978 >>> print(output) 3979 [[6 5 4 5 6 5 4] 3980 [3 2 1 2 3 2 1] 3981 [6 5 4 5 6 5 4] 3982 [9 8 7 8 9 8 7] 3983 [6 5 4 5 6 5 4]] 3984 """ 3985 3986 _mirror_pad = _get_cache_prim(P.MirrorPad)(mode) 3987 return _mirror_pad(input_x, paddings) 3988 3989 3990def _innner_log_softmax(inputs, axis): 3991 """inner implementation of log_softmax, since the LogSoftmaxGrad op do not support inputs > 2d""" 3992 return inputs - logsumexp(inputs, axis, True) 3993 3994 3995def _check_cross_entropy_inputs(input, target, weight, ignore_index, reduction, label_smoothing): 3996 """ 3997 Check inputs for cross_entropy(). 3998 """ 3999 _check_is_tensor('input', input, "cross_entropy_loss") 4000 _check_is_tensor('target', target, "cross_entropy_loss") 4001 _check_is_tensor('weight', weight, "cross_entropy_loss") 4002 check_int_const(ignore_index, 'ignore_index', "cross_entropy_loss") 4003 check_non_negative_float_const(label_smoothing, 'label_smoothing', "cross_entropy_loss") 4004 check_string_const(reduction, ['none', 'mean', 'sum'], 'reduction', "cross_entropy_loss") 4005 if input.dtype not in [mstype.float64, mstype.float32, mstype.float16]: 4006 raise TypeError(f'For cross_entropy, the input dtype should be mstype.float64, mstype.float32 or' 4007 f'mstype.float16, but got dtype:{input.dtype}.') 4008 4009 4010def cross_entropy(input, target, weight=None, ignore_index=-100, reduction='mean', label_smoothing=0.0): 4011 r""" 4012 The cross entropy loss between input and target. 4013 4014 The cross entropy support two kind of targets: 4015 4016 - Class indices (int) in the range :math:`[0, C)` where :math:`C` is the number of classes, 4017 the loss with reduction=none can be described as: 4018 4019 .. math:: 4020 4021 \ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad 4022 l_n = - w_{y_n} \log \frac{\exp(x_{n,y_n})}{\sum_{c=1}^C \exp(x_{n,c})} 4023 \cdot \mathbb{1}\{y_n \not= \text{ignore_index}\} 4024 4025 where :math:`x` is the inputs, :math:`y` is the target, :math:`w` is the weight, N is the batch size, 4026 :math:`c` belonging to :math:`[0, C-1]` is class index, where :math:`C` is the number of classes. 4027 4028 If `reduction` is not ``None`` (default ``'mean'`` ), then 4029 4030 .. math:: 4031 4032 \ell(x, y) = \begin{cases} 4033 \sum_{n=1}^N \frac{1}{\sum_{n=1}^N w_{y_n} \cdot \mathbb{1}\{y_n \not= \text{ignore_index}\}} l_n, & 4034 \text{if reduction} = \text{'mean',}\\ 4035 \sum_{n=1}^N l_n, & 4036 \text{if reduction} = \text{'sum'.} 4037 \end{cases} 4038 4039 - Probabilities (float) for each class, useful when labels beyond a single class per minibatch item 4040 are required, the loss with reduction=none can be described as: 4041 4042 .. math:: 4043 4044 \ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad 4045 l_n = - \sum_{c=1}^C w_c \log \frac{\exp(x_{n,c})}{\sum_{i=1}^C \exp(x_{n,i})} y_{n,c} 4046 4047 where :math:`x` is the inputs, :math:`y` is the target, :math:`w` is the weight, N is the batch size, 4048 :math:`c` belonging to :math:`[0, C-1]` is class index, where :math:`C` is the number of classes. 4049 4050 If `reduction` is not ``None`` (default ``'mean'`` ), then 4051 4052 .. math:: 4053 4054 \ell(x, y) = \begin{cases} 4055 \frac{\sum_{n=1}^N l_n}{N}, & 4056 \text{if reduction} = \text{'mean',}\\ 4057 \sum_{n=1}^N l_n, & 4058 \text{if reduction} = \text{'sum'.} 4059 \end{cases} 4060 4061 Args: 4062 input (Tensor): :math:`(N)` or :math:`(N, C)` where `C = number of classes` or :math:`(N, C, H, W)` 4063 in case of 2D Loss, or :math:`(N, C, d_1, d_2, ..., d_K)`. 4064 `input` is expected to be log-probabilities, data type must be float16 or float32. 4065 target (Tensor): For class indices, tensor of shape :math:`()`, :math:`(N)` or 4066 :math:`(N, d_1, d_2, ..., d_K)` , data type must be int32. For probabilities, tensor of shape :math:`(C,)` , 4067 :math:`(N, C)` or :math:`(N, C, d_1, d_2, ..., d_K)` , data type must be float16 or float32 or float64. 4068 weight (Tensor): A rescaling weight applied to the loss of each batch element. 4069 If not None, the shape is :math:`(C,)`, data type must be float16 or float32. Default: ``None`` . 4070 ignore_index (int): Specifies a target value that is ignored 4071 and does not contribute to the input gradient. Default: ``-100`` . 4072 reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` , 4073 ``'sum'`` . Default: ``'mean'`` . 4074 4075 - ``'none'``: no reduction will be applied. 4076 - ``'mean'``: compute and return the weighted mean of elements in the output. 4077 - ``'sum'``: the output elements will be summed. 4078 4079 label_smoothing (float): Label smoothing values, a regularization tool used to prevent the model 4080 from overfitting when calculating Loss. The value range is [0.0, 1.0]. Default value: ``0.0`` . 4081 4082 Returns: 4083 Tensor, the computed loss value. 4084 4085 Supported Platforms: 4086 ``Ascend`` ``GPU`` ``CPU`` 4087 4088 Examples: 4089 >>> import mindspore as ms 4090 >>> import numpy as np 4091 >>> # Case 1: Indices labels 4092 >>> inputs = ms.Tensor(np.random.randn(3, 5), ms.float32) 4093 >>> target = ms.Tensor(np.array([1, 0, 4]), ms.int32) 4094 >>> output = ms.ops.cross_entropy(inputs, target) 4095 >>> # Case 2: Probability labels 4096 >>> inputs = ms.Tensor(np.random.randn(3, 5), ms.float32) 4097 >>> target = ms.Tensor(np.random.randn(3, 5), ms.float32) 4098 >>> output = ms.ops.cross_entropy(inputs, target) 4099 """ 4100 _check_cross_entropy_inputs(input, target, weight, ignore_index, reduction, label_smoothing) 4101 class_dim = 0 if input.ndim == 1 else 1 4102 if target.dtype in [mstype.float32, mstype.float16]: 4103 return _cross_entropy(input, target, class_dim, weight, reduction, label_smoothing) 4104 return nll_loss(_innner_log_softmax(input, class_dim), target, weight, ignore_index, reduction, label_smoothing) 4105 4106 4107def _cross_entropy(inputs, target, target_dim, weight=None, reduction='mean', label_smoothing=0.0): 4108 """cross entropy inner function""" 4109 class_dim = 0 if inputs.ndim == 1 else 1 4110 n_classes = inputs.shape[class_dim] 4111 inputs = _innner_log_softmax(inputs, class_dim) 4112 if label_smoothing > 0.0: 4113 target = target * (1 - label_smoothing) + label_smoothing / n_classes 4114 4115 if weight is None: 4116 weight = ones_like_(inputs) 4117 elif inputs.ndim != 1: 4118 broadcast_shape = [1 for _ in range(inputs.ndim)] 4119 broadcast_shape[1] = weight.shape[0] 4120 weight = weight.reshape(broadcast_shape) 4121 4122 if reduction == 'mean': 4123 return -(inputs * target * weight).sum() / (inputs.size / n_classes) 4124 if reduction == 'sum': 4125 return -(inputs * target * weight).sum() 4126 return -(inputs * target * weight).sum(class_dim) 4127 4128 4129def nll_loss(inputs, target, weight=None, ignore_index=-100, reduction='mean', label_smoothing=0.0): 4130 r""" 4131 Gets the negative log likelihood loss between inputs and target. 4132 4133 The nll loss with reduction=none can be described as: 4134 4135 .. math:: 4136 4137 \ell(x, t)=L=\left\{l_{1}, \ldots, l_{N}\right\}^{\top}, 4138 \quad l_{n}=-w_{t_{n}} x_{n, t_{n}}, 4139 \quad w_{c}=\text { weight }[c] \cdot \mathbb{1} 4140 \{c \not= \text{ignore_index}\}, 4141 4142 where :math:`x` is the inputs, :math:`t` is the target, :math:`w` is the weight, 4143 N is the batch size, :math:`c` belonging to :math:`[0, C-1]` is class index, where :math:`C` is the number of 4144 classes. 4145 4146 If `reduction` is not ``None`` (default ``'mean'``), then 4147 4148 .. math:: 4149 4150 \ell(x, t)=\left\{\begin{array}{ll} 4151 \sum_{n=1}^{N} \frac{1}{\sum_{n=1}^{N} w_{t n}} l_{n}, & \text { if reduction }=\text { 'mean', } \\ 4152 \sum_{n=1}^{N} l_{n}, & \text { if reduction }=\text { 'sum' } 4153 \end{array}\right. 4154 4155 Args: 4156 inputs (Tensor): :math:`(N, C)` where `C = number of classes` or :math:`(N, C, H, W)` 4157 in case of 2D Loss, or :math:`(N, C, d_1, d_2, ..., d_K)`. 4158 `inputs` is expected to be log-probabilities, data type must be float16 or float32. 4159 target (Tensor): :math:`(N)` or :math:`(N, d_1, d_2, ..., d_K)` for 4160 high-dimensional loss, data type must be int32. 4161 weight (Tensor): A rescaling weight applied to the loss of each batch element. 4162 If not None, the shape is :math:`(C,)`. 4163 The data type must be float16 or float32. Default: ``None`` . 4164 ignore_index (int): Specifies a target value that is ignored 4165 and does not contribute to the input gradient. Default: ``-100`` . 4166 reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` , 4167 ``'sum'`` . Default: ``'mean'`` . 4168 4169 - ``'none'``: no reduction will be applied. 4170 - ``'mean'``: compute and return the weighted mean of elements in the output. 4171 - ``'sum'``: the output elements will be summed. 4172 4173 label_smoothing (float): Label smoothing values, a regularization tool used to prevent the model 4174 from overfitting when calculating Loss. The value range is [0.0, 1.0]. Default value: ``0.0`` . 4175 4176 Returns: 4177 Tensor, the computed loss value. 4178 4179 Supported Platforms: 4180 ``Ascend`` ``GPU`` ``CPU`` 4181 4182 Examples: 4183 >>> import mindspore 4184 >>> import numpy as np 4185 >>> from mindspore import Tensor, ops 4186 >>> inputs = mindspore.Tensor(np.random.randn(3, 5), mindspore.float32) 4187 >>> target = mindspore.Tensor(np.array([1, 0, 4]), mindspore.int32) 4188 >>> output = ops.nll_loss(inputs, target) 4189 4190 """ 4191 ndim = inputs.ndim 4192 if ndim == 2: 4193 ret = _nll_loss(inputs, target, -1, weight, ignore_index, reduction, label_smoothing) 4194 elif ndim == 4: 4195 ret = _nll_loss(inputs, target, 1, weight, ignore_index, reduction, label_smoothing) 4196 elif ndim == 1: 4197 ret = _nll_loss(inputs, target, 0, weight, ignore_index, reduction, label_smoothing) 4198 else: 4199 n = inputs.shape[0] 4200 c = inputs.shape[1] 4201 out_size = (n,) + inputs.shape[2:] 4202 inputs = inputs.view((n, c, 1, -1)) 4203 target = target.view((n, 1, -1)) 4204 if reduction != 'none': 4205 ret = _nll_loss(inputs, target, 1, weight, ignore_index, reduction, label_smoothing) 4206 else: 4207 ret = _nll_loss(inputs, target, 1, weight, ignore_index, label_smoothing=label_smoothing) 4208 ret = ret.view(out_size) 4209 return ret 4210 4211 4212def _nll_loss(inputs, target, target_dim=-1, weight=None, ignore_index=None, reduction='none', label_smoothing=0.0): 4213 """nll loss inner function""" 4214 if target.ndim == inputs.ndim - 1: 4215 target = target.expand_dims(target_dim) 4216 if ignore_index is not None: 4217 non_pad_mask = equal_(target, ignore_index) 4218 target = target.masked_fill(non_pad_mask, ops.cast(0, target.dtype)) 4219 else: 4220 non_pad_mask = target 4221 if weight is not None: 4222 loss_weights = gather_(weight, target, 0) 4223 orig_shape = inputs.shape 4224 if inputs.ndim != 2: 4225 inputs = inputs.view(orig_shape[:2] + (-1,)) 4226 weight = weight.view(weight.shape + (1,)) 4227 weighted_inputs = inputs * weight 4228 weighted_inputs = weighted_inputs.view(orig_shape) 4229 loss = neg_(gather_d_(weighted_inputs, target_dim, target)) 4230 smooth_loss = neg_(weighted_inputs.sum(axis=target_dim, keepdims=True)) 4231 else: 4232 loss = neg_(gather_d_(inputs, target_dim, target)) 4233 smooth_loss = neg_(inputs.sum(axis=target_dim, keepdims=True)) 4234 loss_weights = ones_like_(loss) 4235 if ignore_index is not None: 4236 loss = loss.masked_fill(non_pad_mask, ops.cast(0, loss.dtype)) 4237 loss_weights = loss_weights.masked_fill(non_pad_mask, ops.cast(0, loss_weights.dtype)) 4238 smooth_loss = smooth_loss.masked_fill(non_pad_mask, ops.cast(0, smooth_loss.dtype)) 4239 4240 loss = loss.squeeze(target_dim) 4241 smooth_loss = smooth_loss.squeeze(target_dim) 4242 4243 if reduction == 'sum': 4244 loss = loss.sum() 4245 smooth_loss = smooth_loss.sum() 4246 if reduction == 'mean': 4247 loss = loss.sum() / loss_weights.sum() 4248 smooth_loss = smooth_loss.sum() / loss_weights.sum() 4249 4250 eps_i = label_smoothing / inputs.shape[target_dim] 4251 loss = (1. - label_smoothing) * loss + eps_i * smooth_loss 4252 4253 return loss 4254 4255 4256def l1_loss(input, target, reduction='mean'): 4257 r""" 4258 Calculate the mean absolute error between the `input` value and the `target` value. 4259 4260 Assuming that the :math:`x` and :math:`y` (predicted and target value) are 1-D Tensor, 4261 length :math:`N`, `reduction` is set to ``'none'``, then calculate the loss of 4262 :math:`x` and :math:`y` without dimensionality reduction. 4263 4264 The formula is as follows: 4265 4266 .. math:: 4267 \ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad \text{with } l_n = \left| x_n - y_n \right|, 4268 4269 where :math:`N` is the batch size. 4270 4271 If `reduction` is ``'mean'`` or ``'sum'`` , then: 4272 4273 .. math:: 4274 \ell(x, y) = 4275 \begin{cases} 4276 \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ 4277 \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} 4278 \end{cases} 4279 4280 Args: 4281 input (Tensor): Predicted value, Tensor of any dimension. 4282 target (Tensor): Target value, usually has the same shape as the `input`. 4283 If `input` and `target` have different shape, make sure they can broadcast to each other. 4284 reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` , 4285 ``'sum'`` . Default: ``'mean'`` . 4286 4287 - ``'none'``: no reduction will be applied. 4288 - ``'mean'``: compute and return the mean of elements in the output. 4289 - ``'sum'``: the output elements will be summed. 4290 4291 Returns: 4292 Tensor or Scalar, if `reduction` is ``'none'``, return a Tensor with same shape and dtype as `input`. 4293 Otherwise, a scalar value will be returned. 4294 4295 Raises: 4296 TypeError: If `input` is not a Tensor. 4297 TypeError: If `target` is not a Tensor. 4298 ValueError: If `reduction` is not one of ``'none'``, ``'mean'`` or ``'sum'``. 4299 4300 Supported Platforms: 4301 ``Ascend`` ``GPU`` ``CPU`` 4302 4303 Examples: 4304 >>> from mindspore import Tensor, ops 4305 >>> from mindspore import dtype as mstype 4306 >>> x = Tensor([[1, 2, 3], [4, 5, 6]], mstype.float32) 4307 >>> target = Tensor([[6, 5, 4], [3, 2, 1]], mstype.float32) 4308 >>> output = ops.l1_loss(x, target, reduction="mean") 4309 >>> print(output) 4310 3.0 4311 """ 4312 _check_is_tensor('input', input, "l1_loss") 4313 _check_is_tensor('target', target, "l1_loss") 4314 if reduction not in ('mean', 'sum', 'none'): 4315 raise ValueError(f"For l1_loss, the 'reduction' must be in ['mean', 'sum', 'none'], but got {reduction}.") 4316 loss = abs_(input - target) 4317 return _get_loss(loss, reduction, "l1_loss") 4318 4319 4320def smooth_l1_loss(input, target, beta=1.0, reduction='none'): 4321 r""" 4322 Computes smooth L1 loss, a robust L1 loss. 4323 4324 SmoothL1Loss is a Loss similar to MSELoss but less sensitive to outliers as described in the 4325 `Fast R-CNN <https://arxiv.org/abs/1504.08083>`_ by Ross Girshick. 4326 4327 Given two input :math:`x,\ y` of length :math:`N`, the unreduced SmoothL1Loss can be described 4328 as follows: 4329 4330 .. math:: 4331 L_{i} = 4332 \begin{cases} 4333 \frac{0.5 (x_i - y_i)^{2}}{\beta}, & \text{if } |x_i - y_i| < \beta \\ 4334 |x_i - y_i| - 0.5 * \beta, & \text{otherwise. } 4335 \end{cases} 4336 4337 If `reduction` is not `none`, then: 4338 4339 .. math:: 4340 L = 4341 \begin{cases} 4342 \operatorname{mean}(L_{i}), & \text{if reduction} = \text{'mean';}\\ 4343 \operatorname{sum}(L_{i}), & \text{if reduction} = \text{'sum'.} 4344 \end{cases} 4345 4346 Here :math:`\text{beta}` controls the point where the loss function changes from quadratic to linear. 4347 :math:`\text{beta}>0` , its default value is ``1.0`` . :math:`N` is the batch size. 4348 4349 Args: 4350 input (Tensor): Tensor of shape :math:`(N, *)` where :math:`*` means, any number of additional dimensions. 4351 Data type is float16, float32 or float64. 4352 target (Tensor): Ground truth data, tensor of shape :math:`(N, *)`, same shape and dtype as the `input`. 4353 beta (float): A parameter used to control the point where the function will change between 4354 L1 to L2 loss. The value should be greater than zero. Default: ``1.0`` . 4355 reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` , 4356 ``'sum'`` . Default: ``'none'`` . 4357 4358 - ``'none'``: no reduction will be applied. 4359 - ``'mean'``: compute and return the mean of elements in the output. 4360 - ``'sum'``: the output elements will be summed. 4361 4362 Returns: 4363 Tensor, if `reduction` is ``'none'``, then output is a tensor with the same shape as `input`. 4364 Otherwise, the shape of output tensor is :math:`(1,)`. 4365 4366 Raises: 4367 TypeError: If `beta` is not a float. 4368 ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``. 4369 TypeError: If dtype of `input` or `target` is not one of float16, float32, float64. 4370 ValueError: If `beta` is less than or equal to 0. 4371 ValueError: If shape of `input` is not the same as `target`. 4372 4373 Supported Platforms: 4374 ``Ascend`` ``GPU`` ``CPU`` 4375 4376 Examples: 4377 >>> import mindspore 4378 >>> import numpy as np 4379 >>> from mindspore import Tensor, ops 4380 >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32) 4381 >>> labels = Tensor(np.array([1, 2, 2]), mindspore.float32) 4382 >>> output = ops.smooth_l1_loss(logits, labels) 4383 >>> print(output) 4384 [0. 0. 0.5] 4385 """ 4386 _smooth_l1_loss = _get_cache_prim(P.SmoothL1Loss)(beta, reduction) 4387 return _smooth_l1_loss(input, target) 4388 4389 4390def threshold(input, thr, value): 4391 r""" 4392 Returns each element of `input` after thresholding by `thr` as a Tensor. 4393 4394 The formula is defined as follows: 4395 4396 .. math:: 4397 y = 4398 \begin{cases} 4399 input, &\text{ if } input > \text{thr} \\ 4400 \text{value}, &\text{ otherwise } 4401 \end{cases} 4402 4403 Args: 4404 input (Tensor): The input of threshold with data type of float16 or float32. 4405 thr (Union[int, float]): The value of the threshold. 4406 value (Union[int, float]): The value to replace with when element is less than threshold. 4407 4408 Returns: 4409 Tensor, the same shape and data type as the input. 4410 4411 Raises: 4412 TypeError: If `input` is not a Tensor. 4413 TypeError: If `thr` is not a float or an int. 4414 TypeError: If `value` is not a float or an int. 4415 4416 Supported Platforms: 4417 ``Ascend`` ``GPU`` ``CPU`` 4418 4419 Examples: 4420 >>> import mindspore 4421 >>> from mindspore import Tensor, ops 4422 >>> inputs = mindspore.Tensor([0.0, 2, 3], mindspore.float32) 4423 >>> outputs = ops.threshold(inputs, 1, 100) 4424 >>> print(outputs) 4425 [100. 2. 3.] 4426 """ 4427 _check_is_tensor('input', input, "threshold") 4428 _check_value_type("thr", thr, [float, int], "threshold") 4429 _check_value_type("value", value, [float, int], "threshold") 4430 cond = greater_(input, thr) 4431 input_type = input.dtype 4432 value = Tensor(value, input_type) 4433 input_shape = input.shape 4434 shape_tensor = tuple_to_tensor_(input_shape, mstype.int64) 4435 value = fillv2_(shape_tensor, value) 4436 return select_(cond, input, value) 4437 4438 4439def leaky_relu(input, alpha=0.2): 4440 r""" 4441 leaky_relu activation function. The element of `input` less than 0 times `alpha` . 4442 4443 The activation function is defined as: 4444 4445 .. math:: 4446 \text{leaky_relu}(input) = \begin{cases}input, &\text{if } input \geq 0; \cr 4447 {\alpha} * input, &\text{otherwise.}\end{cases} 4448 4449 where :math:`\alpha` represents the `alpha` parameter. 4450 4451 For more details, see `Rectifier Nonlinearities Improve Neural Network Acoustic Models 4452 <https://ai.stanford.edu/~amaas/papers/relu_hybrid_icml2013_final.pdf>`_. 4453 4454 LeakyReLU Activation Function Graph: 4455 4456 .. image:: ../images/LeakyReLU.png 4457 :align: center 4458 4459 Args: 4460 input (Tensor): The input of leaky_relu is a Tensor of any dimension. 4461 alpha (Union[int, float]): Slope of the activation function when the element of `input` is less than 0. 4462 Default: ``0.2`` . 4463 4464 Returns: 4465 Tensor, has the same type and shape as the `input`. 4466 4467 Raises: 4468 TypeError: If `input` is not a Tensor. 4469 TypeError: If `alpha` is not a float or an int. 4470 4471 Supported Platforms: 4472 ``Ascend`` ``GPU`` ``CPU`` 4473 4474 Examples: 4475 >>> import mindspore 4476 >>> import numpy as np 4477 >>> from mindspore import Tensor, ops 4478 >>> x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32) 4479 >>> print(ops.leaky_relu(x, alpha=0.2)) 4480 [[-0.2 4. -1.6] 4481 [ 2. -1. 9. ]] 4482 """ 4483 _check_is_tensor('input', input, "leaky_relu") 4484 _check_value_type("alpha", alpha, [float, int], "leaky_relu") 4485 select_op = maximum_ 4486 if alpha > 1: 4487 select_op = minimum_ 4488 alpha = cast_(F.scalar_to_tensor(alpha), input.dtype) 4489 return select_op(alpha * input, input) 4490 4491 4492def intopk(x1, x2, k): 4493 r""" 4494 Determines whether the targets are in the top `k` predictions. 4495 4496 Args: 4497 x1 (Tensor): A 2D Tensor defines the predictions of a batch of samples with float16 or float32 4498 data type. 4499 x2 (Tensor): A 1D Tensor defines the labels of a batch of samples with int32 data type. The size of `x2` 4500 must be equal to the first dimension of `x1`. The values of `x2` can not be negative and 4501 must be equal to or less than index of x1's second dimension. 4502 k (int): Specifies the number of top elements to be used for computing precision along the last dimension. 4503 4504 Returns: 4505 Tensor has 1 dimension of type bool and the same shape with `x2`. For labeling sample `i` in `x2`, 4506 if the label in the first `k` predictions for sample `i` is in `x1`, then the value is True, otherwise False. 4507 4508 Raises: 4509 TypeError: If `k` is not an int. 4510 TypeError: If `x1` or `x2` is not a Tensor. 4511 TypeError: If dtype of `x1` is neither float16 nor float32. 4512 4513 Supported Platforms: 4514 ``Ascend`` ``GPU`` ``CPU`` 4515 4516 Examples: 4517 >>> import mindspore 4518 >>> import numpy as np 4519 >>> from mindspore import Tensor, ops 4520 >>> x1 = Tensor(np.array([[1, 8, 5, 2, 7], [4, 9, 1, 3, 5]]), mindspore.float32) 4521 >>> x2 = Tensor(np.array([1, 3]), mindspore.int32) 4522 >>> output = ops.intopk(x1, x2, 3) 4523 >>> print(output) 4524 [ True False] 4525 """ 4526 _in_topk = _get_cache_prim(P.InTopK)(k) 4527 return _in_topk(x1, x2) 4528 4529def lrn(x, depth_radius=5, bias=1.0, alpha=1.0, beta=0.5, norm_region="ACROSS_CHANNELS"): 4530 r""" 4531 Local Response Normalization. 4532 4533 .. warning:: 4534 lrn is deprecated on Ascend due to potential accuracy problem. It's recommended to use other 4535 normalization methods, e.g. :class:`mindspore.ops.batch_norm`. 4536 4537 .. math:: 4538 4539 b_{c} = a_{c}\left(k + \frac{\alpha}{n} 4540 \sum_{c'=\max(0, c-n/2)}^{\min(N-1,c+n/2)}a_{c'}^2\right)^{-\beta} 4541 4542 where the :math:`a_{c}` indicates the specific value of the pixel corresponding to :math:`c` in feature map; 4543 where the :math:`n/2` indicates the `depth_radius`; where the :math:`k` indicates the `bias`; 4544 where the :math:`\alpha` indicates the `alpha`; where the :math:`\beta` indicates the `beta`. 4545 4546 Args: 4547 depth_radius (int): Half-width of the 1-D normalization window with the shape of 0-D. Default: ``5`` . 4548 bias (float): An offset (usually positive to avoid dividing by 0). Default: ``1.0`` . 4549 alpha (float): A scale factor, usually positive. Default: ``1.0`` . 4550 beta (float): An exponent. Default: ``0.5`` . 4551 norm_region (str): Specifies normalization region. Options: ``"ACROSS_CHANNELS"`` . 4552 Default: ``"ACROSS_CHANNELS"`` . 4553 x (Tensor): A 4-D Tensor with float16 or float32 data type. 4554 4555 Returns: 4556 Tensor, with the same shape and data type as `x`. 4557 4558 Raises: 4559 TypeError: If `depth_radius` is not an int. 4560 TypeError: If `bias`, `alpha` or `beta` is not a float. 4561 TypeError: If `norm_region` is not a str. 4562 TypeError: If `x` is not a Tensor. 4563 4564 Supported Platforms: 4565 ``GPU`` ``CPU`` 4566 4567 Examples: 4568 >>> import mindspore 4569 >>> import numpy as np 4570 >>> from mindspore import Tensor, ops 4571 >>> input_x = Tensor(np.array([[[[0.1], [0.2]], 4572 ... [[0.3], [0.4]]]]), mindspore.float32) 4573 >>> output = ops.lrn(input_x) 4574 >>> print(output) 4575 [[[[0.09534626] 4576 [0.1825742 ]] 4577 [[0.2860388 ] 4578 [0.3651484 ]]]] 4579 """ 4580 lrn_op = NN_OPS.LRN(depth_radius, bias, alpha, beta, norm_region) 4581 return lrn_op(x) 4582 4583 4584def mish(x): 4585 r""" 4586 Computes MISH(A Self Regularized Non-Monotonic Neural Activation Function) of input tensors element-wise. 4587 4588 The function is shown as follows: 4589 4590 .. math:: 4591 4592 \text{output} = x * \tanh(\log(1 + \exp(\text{x}))) 4593 4594 See more details in `A Self Regularized Non-Monotonic Neural Activation Function 4595 <https://arxiv.org/abs/1908.08681>`_. 4596 4597 Mish Activation Function Graph: 4598 4599 .. image:: ../images/Mish.png 4600 :align: center 4601 4602 Args: 4603 x (Tensor): The input Tensor. 4604 Supported dtypes: 4605 4606 - GPU/CPU: float16, float32, float64. 4607 - Ascend: float16, float32. 4608 4609 Returns: 4610 Tensor, with the same type and shape as the `x`. 4611 4612 Raises: 4613 TypeError: If dtype of `x` is not float16, float32 or float64. 4614 4615 Supported Platforms: 4616 ``Ascend`` ``GPU`` ``CPU`` 4617 4618 Examples: 4619 >>> import mindspore 4620 >>> import numpy as np 4621 >>> from mindspore import Tensor, ops 4622 >>> input_x = Tensor(np.array([[-1.0, 4.0, -8.0], [2.0, -5.0, 9.0]]), mindspore.float32) 4623 >>> output = ops.mish(input_x) 4624 >>> print(output) 4625 [[-3.0340147e-01 3.9974129e+00 -2.68311895e-03] 4626 [ 1.9439590e+00 -3.3576239e-02 8.99999990e+00]] 4627 >>> input_x = Tensor(2.1, mindspore.float32) 4628 >>> output = ops.mish(input_x) 4629 >>> print(output) 4630 2.050599 4631 """ 4632 return mish_(x) 4633 4634 4635@_primexpr 4636def _check_value_type(arg_name, arg_value, valid_types, prim_name=None): 4637 """Checks whether a value is instance of some types.""" 4638 return validator.check_value_type(arg_name, arg_value, valid_types, prim_name) 4639 4640 4641@constexpr(check=False) 4642def _check_is_tensor(param_name, input_data, cls_name): 4643 """Internal function, used to check whether the input data is Tensor.""" 4644 if input_data is not None and not isinstance(ops.typeof(input_data), mstype.TensorType): 4645 raise TypeError(f"For '{cls_name}', the '{param_name}' must be a Tensor, " 4646 f"but got '{ops.typeof(input_data)}'") 4647 4648 4649@constexpr 4650def _check_number_gt_value(arg_name, arg_value, value, cls_name): 4651 """Internal function, used to judge whether arg_value is greater than or equal to value.""" 4652 return validator.check_number(arg_name, arg_value, value, validator.GT, cls_name) 4653 4654 4655def _get_axis(x): 4656 """Get a range of axis for input.""" 4657 shape = ops.shape(x) 4658 length = ops.tuple_len(shape) 4659 perm = ops.make_range(0, length) 4660 return perm 4661 4662 4663def _get_loss(x, reduction, cls_name, weights=1.0): 4664 """Calculate the loss with reduction and weights.""" 4665 if reduction not in ('mean', 'sum', 'none'): 4666 raise ValueError(f"For '{cls_name}', the 'reduction' must be in ['mean', 'sum', 'none'], " 4667 f"but got {reduction}.") 4668 input_dtype = x.dtype 4669 x = cast_(x, mstype.float32) 4670 weights = cast_(weights, mstype.float32) 4671 x = mul_(weights, x) 4672 if reduction == 'mean': 4673 x = reduce_mean_(x, _get_axis(x)) 4674 if reduction == 'sum': 4675 x = reduce_sum_(x, _get_axis(x)) 4676 x = cast_(x, input_dtype) 4677 return x 4678 4679 4680def check_input_dtype(param_name1, input_data1, param_name2, input_data2, cls_name): 4681 """Check the type of input1 and input2.""" 4682 if input_data1.dtype != input_data2.dtype: 4683 raise TypeError(f'For {cls_name}, the {param_name1} dtype should be equal to {param_name2} dtype, ' 4684 f'but got {param_name1} dtype:{input_data1.dtype}, {param_name2} dtype:{input_data2.dtype}.') 4685 4686 4687def margin_ranking_loss(input1, input2, target, margin=0.0, reduction='mean'): 4688 r""" 4689 MarginRankingLoss creates a criterion that measures the loss. 4690 4691 Given two tensors :math:`input1`, :math:`input2` and a Tensor label :math:`target` with values 1 or -1, 4692 the operation is as follows: 4693 4694 .. math:: 4695 \text{loss}(input1, input2, target) = \max(0, -target * (input1 - input2) + \text{margin}) 4696 4697 Args: 4698 input1 (Tensor): Tensor of shape :math:`(N, *)` where :math:`*` means, any number of additional dimensions. 4699 input2 (Tensor): Tensor of shape :math:`(N, *)`, same shape and dtype as `input1`. 4700 target (Tensor): Contains value 1 or -1. Suppose the shape of `input1` is 4701 :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)`. 4702 margin (float, optional): Specify the adjustment factor of the operation. Default: ``0.0`` . 4703 reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` , 4704 ``'sum'`` . Default: ``'mean'`` . 4705 4706 - ``'none'``: no reduction will be applied. 4707 - ``'mean'``: compute and return the mean of elements in the output. 4708 - ``'sum'``: the output elements will be summed. 4709 4710 Returns: 4711 Tensor or Scalar. if `reduction` is ``'none'``, its shape is the same as `input1`. 4712 Otherwise, a scalar value will be returned. 4713 4714 Raises: 4715 TypeError: If `margin` is not a float. 4716 TypeError: If `input1`, `input2` or `target` is not a Tensor. 4717 TypeError: If the types of `input1` and `input2` are inconsistent. 4718 TypeError: If the types of `input1` and `target` are inconsistent. 4719 ValueError: If the shape of `input1` and `input2` are inconsistent. 4720 ValueError: If the shape of `input1` and `target` are inconsistent. 4721 ValueError: If `reduction` is not one of ``'none'``, ``'mean'`` , ``'sum'``. 4722 4723 Supported Platforms: 4724 ``Ascend`` ``GPU`` ``CPU`` 4725 4726 Examples: 4727 >>> import mindspore as ms 4728 >>> from mindspore import Tensor, ops 4729 >>> import numpy as np 4730 >>> input1 = Tensor(np.array([0.3864, -2.4093, -1.4076]), ms.float32) 4731 >>> input2 = Tensor(np.array([-0.6012, -1.6681, 1.2928]), ms.float32) 4732 >>> target = ops.Sign()(Tensor(np.array([-2, -2, 3]), ms.float32)) 4733 >>> output = ops.margin_ranking_loss(input1, input2, target) 4734 >>> print(output) 4735 1.2293333 4736 """ 4737 margin = _check_value_type("margin", margin, [float], "margin_ranking_loss") 4738 _check_is_tensor('input1', input1, "margin_ranking_loss") 4739 _check_is_tensor('input2', input2, "margin_ranking_loss") 4740 _check_is_tensor('target', target, "margin_ranking_loss") 4741 check_input_dtype('input1', input1, 'input2', input2, 'margin_ranking_loss') 4742 check_input_dtype('target', target, 'input1', input1, 'margin_ranking_loss') 4743 x = maximum_(-target * (input1 - input2) + margin, 0) 4744 return _get_loss(x, reduction, "margin_ranking_loss") 4745 4746 4747@_primexpr 4748def _check_reduced_shape_valid(ori_shape, reduced_shape, axis, cls_name, arg_name1, arg_name2): 4749 """Internal function, used to check whether the reduced shape meets the requirements.""" 4750 validator.check_reduce_shape(ori_shape, reduced_shape, axis, cls_name, arg_name1, arg_name2) 4751 4752 4753def cosine_embedding_loss(input1, input2, target, margin=0.0, reduction="mean"): 4754 r""" 4755 CosineEmbeddingLoss creates a criterion to measure the similarity between two tensors using cosine distance. 4756 4757 Given two tensors :math:`input1`, :math:`input2`, and a Tensor label :math:`target` with values 1 or -1: 4758 4759 .. math:: 4760 loss(input1, input2, target) = \begin{cases} 4761 1-cos(input1, input2), & \text{if } target = 1\\ 4762 max(0, cos(input1, input2)-margin), & \text{if } target = -1\\ 4763 \end{cases} 4764 4765 Args: 4766 input1 (Tensor): Tensor of shape :math:`(N, *)` where :math:`*` means, any number 4767 of additional dimensions. 4768 input2 (Tensor): Tensor of shape :math:`(N, *)`, same shape and dtype as `input1`. 4769 target (Tensor): Contains value 1 or -1. Suppose the shape of `input1` is 4770 :math:`(x_1, x_2, x_3, ..., x_R)`, then the shape of `target` must be :math:`(x_1, x_3, x_4, ..., x_R)`. 4771 margin (float, optional): Should be in [-1.0, 1.0]. Default: ``0.0``. 4772 reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` , 4773 ``'sum'`` . Default: ``'mean'`` . 4774 4775 - ``'none'``: no reduction will be applied. 4776 - ``'mean'``: compute and return the mean of elements in the output. 4777 - ``'sum'``: the output elements will be summed. 4778 4779 Returns: 4780 Tensor or Scalar, if `reduction` is ``"none"``, its shape is the same as `target`. 4781 Otherwise, a scalar value will be returned. 4782 4783 Raises: 4784 TypeError: If `margin` is not a float. 4785 ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``. 4786 ValueError: If `margin` is not in range [-1.0, 1.0]. 4787 4788 Supported Platforms: 4789 ``Ascend`` ``GPU`` ``CPU`` 4790 4791 Examples: 4792 >>> import mindspore 4793 >>> import numpy as np 4794 >>> from mindspore import Tensor, ops 4795 >>> intput1 = Tensor(np.array([[0.3, 0.8], [0.4, 0.3]]), mindspore.float32) 4796 >>> intput2 = Tensor(np.array([[0.4, 1.2], [-0.4, -0.9]]), mindspore.float32) 4797 >>> target = Tensor(np.array([1, -1]), mindspore.int32) 4798 >>> output = ops.cosine_embedding_loss(intput1, intput2, target) 4799 >>> print(output) 4800 0.0003425479 4801 """ 4802 4803 _check_is_tensor('input1', input1, "ops.cosine_embedding_loss") 4804 _check_is_tensor('input2', input2, "ops.cosine_embedding_loss") 4805 _check_is_tensor('target', target, "ops.cosine_embedding_loss") 4806 check_input_dtype('input1', input1, 'input2', input2, 'ops.cosine_embedding_loss') 4807 _check_reduced_shape_valid(ops.shape(input1), ops.shape(target), (1,), 4808 "ops.cosine_embedding_loss", "input1", "target") 4809 if input1.dtype in (mstype.int32, mstype.int64): 4810 input1 = input1.astype(mstype.float32) 4811 if input2.dtype in (mstype.int32, mstype.int64): 4812 input2 = input2.astype(mstype.float32) 4813 margin_f = float(margin) if isinstance(margin, int) else margin 4814 _check_value_type("margin", margin_f, [float], "ops.cosine_embedding_loss") 4815 if not isinstance(margin_f, float): 4816 raise TypeError(f"For ops.cosine_embedding_loss, 'margin' must be float, but got {type(margin_f)}") 4817 if margin_f > 1.0 or margin_f < -1.0: 4818 raise ValueError(f"For ops.cosine_embedding_loss, the value of 'margin' should be in [-1, 1]," 4819 f"but got {margin_f}.") 4820 prod_sum = reduce_sum_(input1 * input2, (1,)) 4821 square1 = reduce_sum_(ops.square(input1), (1,)) 4822 square2 = reduce_sum_(ops.square(input2), (1,)) 4823 denom = ops.sqrt(square1) * ops.sqrt(square2) 4824 cosine = prod_sum / denom 4825 4826 pos_value = 1.0 - cosine 4827 neg_value = maximum_(cosine - margin_f, 0.0) 4828 zeros = ops.zeros_like(cosine) 4829 pos_part = ops.select(target == 1, pos_value, zeros) 4830 neg_part = ops.select(target == -1, neg_value, zeros) 4831 output_unreduced = pos_part + neg_part 4832 4833 return _get_loss(output_unreduced, reduction, "cosine_embedding_loss") 4834 4835 4836def max_pool3d(x, kernel_size, stride=None, padding=0, dilation=1, ceil_mode=False, return_indices=False): 4837 r""" 4838 Performs a 3D max pooling on the input Tensor. 4839 4840 Typically the input is a Tensor with shape :math:`(N_{in}, C_{in}, D_{in}, H_{in}, W_{in})`, outputs 4841 regional maximum in the :math:`(D_{in}, H_{in}, W_{in})`-dimension. Given `kernel_size` 4842 :math:`ks = (d_{ker}, h_{ker}, w_{ker})` and `stride` :math:`s = (s_0, s_1, s_2)`, the operation is as follows: 4843 4844 .. math:: 4845 \text{output}(N_i, C_j, d, h, w) = 4846 \max_{l=0, \ldots, d_{ker}-1} \max_{m=0, \ldots, h_{ker}-1} \max_{n=0, \ldots, w_{ker}-1} 4847 \text{input}(N_i, C_j, s_0 \times d + l, s_1 \times h + m, s_2 \times w + n) 4848 4849 Args: 4850 x (Tensor): Tensor of shape :math:`(N_{in}, C_{in}, D_{in}, H_{in}, W_{in})` with data type of int8, 4851 int16, int32, int64, uint8, uint16, uint32, uint64, float16, float32 or float64. 4852 kernel_size (Union[int, tuple[int]]): The size of kernel used to take the maximum value and arg 4853 value, is an int number that represents depth, height and width of the kernel, or a tuple of 4854 three int numbers that represent depth, height and width respectively. 4855 stride (Union[int, tuple[int]]): The distance of kernel moving, an int number that represents 4856 the depth, height and width of movement are both stride, or a tuple of three int numbers that 4857 represent depth, height and width of movement respectively. 4858 Default: ``None`` , which indicates the moving step is `kernel_size` . 4859 padding (Union[int, tuple[int]]): An int number that represents the depth, height and width of movement are both 4860 strides, or a tuple of three int numbers that represent depth, height and width of movement respectively. 4861 Default: ``0`` . 4862 dilation (Union[int, tuple[int]]): Control the stride of elements in the kernel. Default: ``1`` . 4863 ceil_mode (bool): Whether to use ceil instead of floor to calculate output shape. Default: ``False`` . 4864 return_indices (bool): Whether to output the indices of max value. Default: ``False`` . 4865 4866 Returns: 4867 If `return_indices` is False, return a Tensor `output`, else return a tuple (`output`, `argmax`). 4868 4869 - **output** (Tensor) - Maxpooling result, with shape :math:`(N_{out}, C_{out}, D_{out}, H_{out}, W_{out})`. 4870 It has the same data type as `x`. 4871 4872 .. math:: 4873 D_{out} = \left\lfloor\frac{D_{in} + 2 \times \text{padding}[0] - \text{dilation}[0] \times 4874 (\text{kernel_size}[0] - 1) - 1}{\text{stride}[0]} + 1\right\rfloor 4875 4876 .. math:: 4877 H_{out} = \left\lfloor\frac{H_{in} + 2 \times \text{padding}[1] - \text{dilation}[1] \times 4878 (\text{kernel_size}[1] - 1) - 1}{\text{stride}[1]} + 1\right\rfloor 4879 4880 .. math:: 4881 W_{out} = \left\lfloor\frac{W_{in} + 2 \times \text{padding}[2] - \text{dilation}[2] \times 4882 (\text{kernel_size}[2] - 1) - 1}{\text{stride}[2]} + 1\right\rfloor 4883 4884 - **argmax** (Tensor) - Index corresponding to the maximum value. Data type is int64. It will be returned 4885 only when `return_indices` is ``True`` . 4886 4887 Raises: 4888 TypeError: If `x` is not a Tensor. 4889 ValueError: If length of shape of `x` is not equal to 5. 4890 TypeError: If `kernel_size` , `stride` , `padding` or `dilation` is not int or tuple. 4891 ValueError: If `kernel_size` or `stride` is less than 1. 4892 ValueError: If `padding` is less than 0. 4893 4894 Supported Platforms: 4895 ``Ascend`` ``GPU`` ``CPU`` 4896 4897 Examples: 4898 >>> import mindspore 4899 >>> import numpy as np 4900 >>> from mindspore import Tensor, ops 4901 >>> x = Tensor(np.arange(2 * 1 * 2 * 2 * 2).reshape((2, 1, 2, 2, 2)), mindspore.float32) 4902 >>> output_tensor, argmax = ops.max_pool3d(x, kernel_size=2, stride=1, padding=1, return_indices=True) 4903 >>> print(output_tensor.shape) 4904 (2, 1, 3, 3, 3) 4905 >>> print(argmax.shape) 4906 (2, 1, 3, 3, 3) 4907 """ 4908 strides = stride if (stride is not None) else kernel_size 4909 max_pool3d_with_argmax_ = _get_cache_prim(NN_OPS.MaxPool3DWithArgmax)( 4910 kernel_size, strides, padding, dilation, ceil_mode) 4911 out, indices = max_pool3d_with_argmax_(x) 4912 if return_indices: 4913 return out, indices 4914 return out 4915 4916 4917def grid_sample(input, grid, mode='bilinear', padding_mode='zeros', align_corners=False): 4918 """ 4919 Given an `input` and a flow-field `grid`, computes the `output` using `input` values and pixel locations from 4920 `grid`. Only spatial (4-D) and volumetric (5-D) `input` is supported. 4921 4922 In the spatial (4-D) case, for `input` with shape :math:`(N, C, H_{in}, W_{in})` and `grid` with shape 4923 :math:`(N, H_{out}, W_{out}, 2)`, the `output` will have shape :math:`(N, C, H_{out}, W_{out})`. 4924 4925 For each output location `output[n, :, h, w]`, the size-2 vector `grid[n, h, w]` specifies `input` pixel 4926 locations `x` and `y`, which are used to interpolate the output value `output[n, :, h, w]`. In the case of 5D 4927 inputs, `grid[n, d, h, w]`, specifies the `x`, `y`, `z` pixel locations for interpolating 4928 `output[n, :, d, h, w]`. And `mode` argument specifies "nearest" or "bilinear" ("bicubic" is not supported yet) 4929 interpolation method to sample the input pixels. 4930 4931 `grid` specifies the sampling pixel locations normalized by the `input` spatial dimensions. Therefore, it should 4932 have most values in the range of :math:`[-1, 1]`. 4933 4934 If `grid` has values outside the range of :math:`[-1, 1]`, the corresponding outputs are handled as defined by 4935 `padding_mode`. If `padding_mode` is set to be "zeros", use :math:`0` for out-of-bound grid locations. If 4936 `padding_mode` is set to be "border", use border values for out-of-bound grid locations. If `padding_mode` is set 4937 to be "reflection", use values at locations reflected by the border for out-of-bound grid locations. For location 4938 far away from the border, it will keep being reflected until becoming in bound. 4939 4940 Args: 4941 input (Tensor): input with shape of :math:`(N, C, H_{in}, W_{in})` (4-D case) or :math:`(N, C, D_{in}, 4942 H_{in}, W_{in})` (5-D case) and dtype of float32 or float64. 4943 grid (Tensor): flow-field with shape of :math:`(N, H_{out}, W_{out}, 2)` (4-D case) or :math:`(N, D_{out}, 4944 H_{out}, W_{out}, 3)` (5-D case) and same dtype as `input`. 4945 mode (str): An optional string specifying the interpolation method. The optional values are 4946 ``'bilinear'``, ``'nearest'``. Default: ``'bilinear'`` . Note: `bicubic` is not supported yet. When 4947 `mode="bilinear"` and the input is 5-D, the interpolation mode used internally will actually 4948 be trilinear. However, when the input is 4-D, the interpolation mode will legistimately be bilinear. 4949 Default: ``'bilinear'`` . 4950 4951 - ``'nearest'``: Nearest neighbor interpolation. Each output pixel is assigned the value of the 4952 nearest input pixel. This method is simple and fast but can result in blocky or pixelated outputs. 4953 - ``'bilinear'``: Bilinear interpolation. Each output pixel is a weighted average of the four nearest input 4954 pixels, computed using bilinear interpolation. This method produces smoother results compared 4955 to nearest neighbor interpolation. 4956 - ``'trilinear'``: Trilinear interpolation. This is an extension of bilinear interpolation to 3D data. 4957 It performs bilinear interpolation in the two spatial dimensions and linear interpolation along 4958 the third dimension. It is commonly used for volume or 3D image interpolation. 4959 4960 padding_mode (str): An optional string specifying the pad method. The optional values are "zeros", "border" or 4961 "reflection". Default: ``'zeros'`` . 4962 align_corners (bool): If set to `True`, the extrema (-1 and 1) are considered as referring to 4963 the center points of the input's corner pixels. If set to `False`, they are instead considered as referring 4964 to the corner points of the input's corner pixels, making the sampling more resolution agnostic. Default: 4965 ``False`` . 4966 4967 Returns: 4968 Tensor, dtype is the same as `input` and whose shape is :math:`(N, C, H_{out}, W_{out})` (4-D) and 4969 :math:`(N, C, D_{out}, H_{out}, W_{out})` (5-D). 4970 4971 Raises: 4972 TypeError: If `input` or `grid` is not a Tensor. 4973 TypeError: If the dtypes of `input` and `grid` are inconsistent. 4974 TypeError: If the dtype of `input` or `grid` is not a valid type. 4975 TypeError: If `align_corners` is not a boolean value. 4976 ValueError: If the rank of `input` or `grid` is not equal to 4(4-D case) or 5(5-D case). 4977 ValueError: If the first dimension of `input` is not equal to that of `grid`. 4978 ValueError: If the last dimension of `grid` is not equal to 2(4-D case) or 3(5-D case). 4979 ValueError: If `mode` is not "bilinear", "nearest" or a string value. 4980 ValueError: If `padding_mode` is not "zeros", "border", "reflection" or a string value. 4981 4982 Supported Platforms: 4983 ``Ascend`` ``GPU`` ``CPU`` 4984 4985 Examples: 4986 >>> import numpy as np 4987 >>> from mindspore import Tensor, ops 4988 >>> input_x = Tensor(np.arange(16).reshape((2, 2, 2, 2)).astype(np.float32)) 4989 >>> grid = Tensor(np.arange(0.2, 1, 0.1).reshape((2, 2, 1, 2)).astype(np.float32)) 4990 >>> output = ops.grid_sample(input_x, grid, mode='bilinear', padding_mode='zeros', 4991 ... align_corners=True) 4992 >>> print(output) 4993 [[[[ 1.9 ] 4994 [ 2.1999998]] 4995 [[ 5.9 ] 4996 [ 6.2 ]]] 4997 [[[10.5 ] 4998 [10.8 ]] 4999 [[14.5 ] 5000 [14.8 ]]]] 5001 """ 5002 if input.ndim == 4: 5003 _grid_sampler_2d = _get_cache_prim(NN_OPS.GridSampler2D)(mode, padding_mode, align_corners) 5004 return _grid_sampler_2d(input, grid) 5005 _grid_sampler_3d = _get_cache_prim(NN_OPS.GridSampler3D)(mode, padding_mode, align_corners) 5006 return _grid_sampler_3d(input, grid) 5007 5008 5009@constexpr 5010def _check_ctc_loss_inputs(blank, reduction, zero_infinity, prim_name): 5011 validator.check_value_type("blank", blank, [int], prim_name) 5012 validator.check_value_type('reduction', reduction, [str], prim_name) 5013 validator.check_string(reduction, ['none', 'sum', 'mean'], 'reduction', prim_name) 5014 validator.check_value_type("zero_infinity", zero_infinity, [bool], prim_name) 5015 5016 5017def ctc_loss(log_probs, targets, input_lengths, target_lengths, blank=0, reduction="mean", zero_infinity=False): 5018 """ 5019 Calculates the CTC (Connectionist Temporal Classification) loss and the gradient. 5020 5021 CTC is a loss function in sequence labeling problems, which is mainly used to deal with the alignment of input 5022 and output labels in sequence labeling problems. 5023 While traditional sequence labeling algorithms require the input and output symbols to be perfectly aligned at 5024 each moment, CTC expands the label collection and adds empty elements. 5025 After labeling the sequence using the extended label set, all the prediction sequences that can be converted 5026 into real sequences by the mapping function are correct prediction results, that is, the predicted sequence 5027 can be obtained without data alignment processing. 5028 Its objective function is to maximize the sum of probabilities of all correct prediction sequences. 5029 5030 The CTC algorithm is proposed in `Connectionist Temporal Classification: Labeling Unsegmented Sequence Data with 5031 Recurrent Neural Networks <http://www.cs.toronto.edu/~graves/icml_2006.pdf>`_. 5032 5033 Args: 5034 log_probs (Tensor): A tensor of shape :math:`(T, N, C)`, where T is input length, N is batch size and C is 5035 number of classes (including blank). 5036 targets (Tensor): Target sequences. A tensor of shape :math:`(N, S)`, where S is max target length. 5037 input_lengths (Union(tuple, Tensor)): Lengths of the input. A tuple or Tensor of shape :math:`(N)`. 5038 target_lengths (Union(tuple, Tensor)): Lengths of the target. A tuple or Tensor of shape :math:`(N)`. 5039 blank (int, optional): The blank label. Default: ``0`` . 5040 reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` , 5041 ``'sum'`` . Default: ``'mean'`` . 5042 5043 - ``'none'``: no reduction will be applied. 5044 - ``'mean'``: compute and return the mean of elements in the output. 5045 - ``'sum'``: the output elements will be summed. 5046 5047 zero_infinity (bool, optional): Whether to set infinite loss and correlation gradient to 0. Default: ``False`` . 5048 5049 Returns: 5050 neg_log_likelihood (Tensor), A loss value with shape :math:`(N)` , which is differentiable with respect to 5051 each input node. 5052 5053 log_alpha (Tensor), The probability of possible trace of input to target with shape :math:`(N, T, 2 * S + 1)` . 5054 5055 Raises: 5056 TypeError: If `zero_infinity` is not a bool, `reduction` is not string. 5057 TypeError: If the dtype of `log_probs` is not float or double. 5058 TypeError: If the dtype of `targets`, `input_lengths` or `target_lengths` is not int32 or int64. 5059 ValueError: If the rank of `log_probs` is not 3. 5060 ValueError: If the rank of `targets` is not 2. 5061 ValueError: If the shape of `input_lengths` does not match N. N is batch size of `log_probs` . 5062 ValueError: If the shape of `target_lengths` does not match N. N is batch size of `log_probs` . 5063 ValueError: If the value of `blank` is not in range [0, num_labels|C). C is number of classes of `log_probs` . 5064 RuntimeError: If any value of `input_lengths` is larger than T. T is the length of `log_probs`. 5065 RuntimeError: If any target_lengths[i] is not in range [0, input_length[i]]. 5066 5067 Supported Platforms: 5068 ``Ascend`` ``GPU`` ``CPU`` 5069 5070 Examples: 5071 >>> import numpy as np 5072 >>> from mindspore import Tensor, ops 5073 >>> from mindspore import dtype as mstype 5074 >>> log_probs = Tensor(np.array([[[0.3, 0.6, 0.6]], 5075 ... [[0.9, 0.4, 0.2]]]).astype(np.float32)) 5076 >>> targets = Tensor(np.array([[0, 1]]), mstype.int32) 5077 >>> input_lengths = Tensor(np.array([2]), mstype.int32) 5078 >>> target_lengths = Tensor(np.array([1]), mstype.int32) 5079 >>> loss, log_alpha = ops.ctc_loss(log_probs, targets, input_lengths, 5080 ... target_lengths, 0, 'mean', True) 5081 >>> print(loss) 5082 -2.2986124 5083 >>> print(log_alpha) 5084 [[[0.3 0.3 -inf -inf -inf] 5085 [1.2 1.8931472 1.2 -inf -inf]]] 5086 """ 5087 _check_ctc_loss_inputs(blank, reduction, zero_infinity, 'ctc_loss') 5088 ctc_loss_op = NN_OPS.CTCLossV2(blank=blank, reduction="none", zero_infinity=zero_infinity) 5089 loss, log_alpha = ctc_loss_op(log_probs, targets, input_lengths, target_lengths) 5090 if reduction == 'sum': 5091 loss = loss.sum() 5092 if reduction == 'mean': 5093 input_type = loss.dtype 5094 target_length_t = target_lengths.clip(1., None) 5095 loss = loss.astype("float32") 5096 loss = loss / target_length_t 5097 loss = loss.mean() 5098 loss = loss.astype(input_type) 5099 return (loss, log_alpha) 5100 5101 5102def gaussian_nll_loss(x, target, var, full=False, eps=1e-6, reduction='mean'): 5103 r""" 5104 Gaussian negative log likelihood loss. 5105 5106 The target values are considered to be samples from a Gaussian distribution, where the expectation and variance are 5107 predicted by a neural network. For `labels` modeled on a Gaussian distribution, `logits` to record expectations, 5108 and the variance `var` (elements are all positive), the calculated loss is: 5109 5110 .. math:: 5111 \text{loss} = \frac{1}{2}\left(\log\left(\text{max}\left(\text{var}, 5112 \ \text{eps}\right)\right) + \frac{\left(\text{x} - \text{target}\right)^2} 5113 {\text{max}\left(\text{var}, \ \text{eps}\right)}\right) + \text{const.} 5114 5115 where :math:`eps` is used for stability of :math:`log`. When :math:`full=True`, a constant will be added to the 5116 loss. If the shape of :math:`var` and :math:`logits` are not the same (due to a homoscedastic assumption), 5117 their shapes must allow correct broadcasting. 5118 5119 Args: 5120 x (Tensor): Tensor of shape :math:`(N, *)` or :math:`(*)` where :math:`*` means any number of 5121 additional dimensions. 5122 target (Tensor): Tensor of shape :math:`(N, *)` or :math:`(*)`, same shape as the x, or same shape 5123 as the x but with one dimension equal to 1 (to allow broadcasting). 5124 var (Tensor): Tensor of shape :math:`(N, *)` or :math:`(*)`, same shape as x, or same shape as the x 5125 but with one dimension equal to 1, or same shape as the x but with one fewer dimension 5126 (to allow for broadcasting). 5127 full (bool, optional): Include the constant term in the loss calculation. When :math:`full=True`, 5128 the constant term will be :math:`const = 0.5*log(2\pi)`. Default: ``False``. 5129 eps (float, optional): Used to improve the stability of log function must be greater than 0. Default: ``1e-6`` . 5130 reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` , 5131 ``'sum'`` . Default: ``'mean'`` . 5132 5133 - ``'none'``: no reduction will be applied. 5134 - ``'mean'``: compute and return the mean of elements in the output. 5135 - ``'sum'``: the output elements will be summed. 5136 5137 Returns: 5138 Tensor or Tensor scalar, the computed loss depending on :math:`reduction`. 5139 5140 Raises: 5141 TypeError: If `x`, `target` or `var` is not a Tensor. 5142 TypeError: If `full` is not a bool. 5143 TypeError: If `eps` is not a float. 5144 ValueError: If `eps` is not a float within (0, inf). 5145 ValueError: If `reduction` is not one of ``"none"`` , ``"mean"`` , ``"sum"`` . 5146 5147 Supported Platforms: 5148 ``Ascend`` ``GPU`` ``CPU`` 5149 5150 Examples: 5151 >>> import numpy as np 5152 >>> from mindspore import Tensor, ops 5153 >>> import mindspore.common.dtype as mstype 5154 >>> arr1 = np.arange(8).reshape((4, 2)) 5155 >>> arr2 = np.array([2, 3, 1, 4, 6, 4, 4, 9]).reshape((4, 2)) 5156 >>> x = Tensor(arr1, mstype.float32) 5157 >>> var = Tensor(np.ones((4, 1)), mstype.float32) 5158 >>> target = Tensor(arr2, mstype.float32) 5159 >>> output = ops.gaussian_nll_loss(x, target, var) 5160 >>> print(output) 5161 1.4374993 5162 5163 Reference: 5164 Nix, D. A. and Weigend, A. S., "Estimating the mean and variance of the 5165 target probability distribution", Proceedings of 1994 IEEE International 5166 Conference on Neural Networks (ICNN'94), Orlando, FL, USA, 1994, pp. 55-60 5167 vol.1, doi: 10.1109/ICNN.1994.374138. 5168 """ 5169 if not isinstance(x, Tensor): 5170 raise TypeError(f"For 'gaussian_nll_loss', 'x' must be a tensor, but got {type(x)}.") 5171 if not isinstance(target, Tensor): 5172 raise TypeError(f"For 'gaussian_nll_loss', 'target' must be a tensor, but got {type(target)}.") 5173 if not isinstance(var, Tensor): 5174 raise TypeError(f"For 'gaussian_nll_loss', 'var' must be a tensor, but got {type(var)}.") 5175 if not isinstance(full, bool): 5176 raise TypeError(f"For 'gaussian_nll_loss', 'full' must be a bool, but got {type(full)}.") 5177 if not isinstance(eps, float) or eps <= 0: 5178 raise ValueError(f"For 'gaussian_nll_loss', 'eps' must be a positive float, but got {eps}.") 5179 if reduction not in ('none', 'mean', 'sum'): 5180 raise ValueError(f"For 'gaussian_nll_loss', 'reduction' must be one of 'none', 'mean', or 'sum',\ 5181 but got {reduction}.") 5182 if not x.shape == var.shape: 5183 if x.shape[:-1] == var.shape: 5184 var = var.unsqueeze(dim=-1) 5185 5186 maxima = maximum_(var, eps) 5187 logarithm = log_(maxima) 5188 squared_loss = square_(x - target) 5189 c = 0 if not full else 0.5 * log(2 * pi) 5190 loss = 0.5 * (logarithm + squared_loss / maxima) + c 5191 if reduction == 'mean': 5192 loss = loss.mean() 5193 elif reduction == 'sum': 5194 loss = loss.sum() 5195 return loss 5196 5197 5198@_primexpr 5199def _check_hinge_embedding_loss_type(inputs_dtype, targets_dtype, inputs, targets, margin, reduction): 5200 """Check hinge embedding loss type.""" 5201 if not isinstance(margin, (float, int)): 5202 raise TypeError(f"For 'HingeEmbeddingLoss', 'margin' must be a float or int, but got {type(margin)}.") 5203 if reduction not in ['none', 'mean', 'sum']: 5204 raise ValueError(f"For 'HingeEmbeddingLoss', 'reduction' must be one of 'none', 'mean', 'sum'," 5205 f"but got {reduction}.") 5206 if not isinstance(inputs, Tensor): 5207 raise TypeError(f"For 'HingeEmbeddingLoss', the first input must be a Tensor, but got {type(inputs)}.") 5208 if not isinstance(targets, Tensor): 5209 raise TypeError(f"For 'HingeEmbeddingLoss', the second input must be a Tensor, but got {type(targets)}.") 5210 5211 if inputs_dtype not in mstype.float_type: 5212 raise TypeError(f"For 'HingeEmbeddingLoss', the dtype of the first input must be float, but got " 5213 f"{inputs_dtype}.") 5214 if targets_dtype not in mstype.float_type: 5215 raise TypeError(f"For 'HingeEmbeddingLoss', the dtype of the second input must be float, but got " 5216 f"{targets_dtype}.") 5217 5218 5219def hinge_embedding_loss(inputs, targets, margin=1.0, reduction='mean'): 5220 r""" 5221 Measures Hinge Embedding Loss given an input Tensor `intputs` and a labels Tensor `targets` (containing 1 or -1). 5222 5223 The loss function for :math:`n`-th sample in the mini-batch is 5224 5225 .. math:: 5226 l_n = \begin{cases} 5227 x_n, & \text{if}\; y_n = 1,\\ 5228 \max \{0, \Delta - x_n\}, & \text{if}\; y_n = -1, 5229 \end{cases} 5230 5231 and the total loss functions is 5232 5233 .. math:: 5234 \ell(x, y) = \begin{cases} 5235 \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ 5236 \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} 5237 \end{cases} 5238 5239 where :math:`L = \{l_1,\dots,l_N\}^\top`. 5240 5241 Args: 5242 inputs (Tensor): Predicted values, represented as :math:`x` in the formula. 5243 targets (Tensor): Label values, represented as :math:`y` in the formula. 5244 Has the same shape as `inputs`, contains -1 or 1. 5245 margin (float, int): Threshold defined by Hinge Embedding Loss `margin`. 5246 Represented as :math:`\Delta` in the formula. Default: ``1.0`` . 5247 reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` , 5248 ``'sum'`` . Default: ``'mean'`` . 5249 5250 - ``'none'``: no reduction will be applied. 5251 - ``'mean'``: compute and return the mean of elements in the output. 5252 - ``'sum'``: the output elements will be summed. 5253 5254 Returns: 5255 Tensor or Tensor scalar, the computed loss depending on `reduction`. 5256 5257 Raises: 5258 TypeError: If `inputs` is not a Tensor. 5259 TypeError: If `targets` is not a Tensor. 5260 TypeError: If `margin` is not a float or int. 5261 ValueError: If `targets` does not have the same shape as `inputs` or they could not broadcast to each other. 5262 ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``. 5263 5264 Supported Platforms: 5265 ``Ascend`` ``GPU`` ``CPU`` 5266 5267 Examples: 5268 >>> import numpy as np 5269 >>> import mindspore.common.dtype as mstype 5270 >>> from mindspore import ops 5271 >>> from mindspore import Tensor 5272 >>> arr1 = np.array([0.9, -1.2, 2, 0.8, 3.9, 2, 1, 0, -1]).reshape((3, 3)) 5273 >>> arr2 = np.array([1, 1, -1, 1, -1, 1, -1, 1, 1]).reshape((3, 3)) 5274 >>> logits = Tensor(arr1, mstype.float32) 5275 >>> labels = Tensor(arr2, mstype.float32) 5276 >>> loss = ops.hinge_embedding_loss(logits, labels, margin=1.0, reduction='mean') 5277 >>> print(loss) 5278 0.16666666 5279 """ 5280 inputs_dtype = inputs.dtype 5281 targets_dtype = targets.dtype 5282 _check_hinge_embedding_loss_type(inputs_dtype, targets_dtype, inputs, targets, margin, reduction) 5283 5284 min_val = Tensor(0, inputs_dtype) 5285 pos_index = targets > 0 5286 neg_index = targets < 0 5287 pos = pos_index * inputs 5288 neg = neg_index * inputs 5289 m = ops.cast(margin, inputs_dtype) 5290 margin_matrix = m * neg_index 5291 neg = margin_matrix - neg 5292 neg = ops.clip_by_value(neg, min_val) 5293 loss = pos + neg 5294 if reduction == 'mean': 5295 loss = loss.mean() 5296 elif reduction == 'sum': 5297 loss = loss.sum() 5298 return loss 5299 5300 5301def ctc_greedy_decoder(inputs, sequence_length, merge_repeated=True): 5302 r""" 5303 Performs greedy decoding on the logits given in inputs. 5304 5305 Note: 5306 On Ascend, 'merge_repeated' can not be set to false. 5307 5308 Args: 5309 inputs (Tensor): The input Tensor must be a 3-D tensor whose shape is 5310 :math:`(max\_time, batch\_size, num\_classes)`. `num_classes` must be `num_labels + 1` classes, 5311 `num_labels` indicates the number of actual labels. Blank labels are reserved. 5312 Default blank label is `num_classes - 1`. Data type must be float32 or float64. 5313 sequence_length (Tensor): A tensor containing sequence lengths with the shape of :math:`(batch\_size, )`. 5314 The type must be int32. Each value in the tensor must be equal to or less than `max_time`. 5315 merge_repeated (bool): If ``true`` , merge repeated classes in output. Default: ``True`` . 5316 5317 Returns: 5318 decoded_indices (Tensor), A tensor with shape of :math:`(total\_decoded\_outputs, 2)`. 5319 Data type is int64. 5320 5321 decoded_values (Tensor), A tensor with shape of :math:`(total\_decoded\_outputs, )`, 5322 it stores the decoded classes. Data type is int64. 5323 5324 decoded_shape (Tensor), A tensor with shape of :math:`(batch\_size, max\_decoded\_length)`. 5325 Data type is int64. 5326 5327 log_probability (Tensor), A tensor with shape of :math:`(batch\_size, 1)`, 5328 containing sequence log-probability, has the same type as `inputs`. 5329 5330 Raises: 5331 TypeError: If `merge_repeated` is not a bool. 5332 ValueError: If length of shape of `inputs` is not equal to 3. 5333 ValueError: If length of shape of `sequence_length` is not equal to 1. 5334 ValueError: If value in the `sequence_length` is larger than `max_time`. 5335 5336 Supported Platforms: 5337 ``Ascend`` ``GPU`` ``CPU`` 5338 5339 Examples: 5340 >>> import mindspore 5341 >>> import numpy as np 5342 >>> from mindspore import Tensor, ops 5343 >>> inputs = Tensor(np.array([[[0.6, 0.4, 0.2], [0.8, 0.6, 0.3]], 5344 ... [[0.0, 0.6, 0.0], [0.5, 0.4, 0.5]]]), mindspore.float32) 5345 >>> sequence_length = Tensor(np.array([2, 2]), mindspore.int32) 5346 >>> decoded_indices, decoded_values, decoded_shape, log_probability = ops.ctc_greedy_decoder(inputs, 5347 ... sequence_length) 5348 >>> print(decoded_indices) 5349 [[0 0] 5350 [0 1] 5351 [1 0]] 5352 >>> print(decoded_values) 5353 [0 1 0] 5354 >>> print(decoded_shape) 5355 [2 2] 5356 >>> print(log_probability) 5357 [[-1.2] 5358 [-1.3]] 5359 """ 5360 _ctc_greedy_decoder = _get_cache_prim(NN_OPS.CTCGreedyDecoder)(merge_repeated) 5361 return _ctc_greedy_decoder(inputs, sequence_length) 5362 5363 5364def conv3d_transpose(inputs, weight, pad_mode='valid', padding=0, stride=1, dilation=1, group=1, 5365 output_padding=0): 5366 r""" 5367 Computes a 3D transposed convolution, which is also known as a deconvolution 5368 (although it is not an actual deconvolution). 5369 5370 Args: 5371 inputs (Tensor): The gradients with respect to the output of the convolution. 5372 The shape conforms to the default. 5373 data_format :math:`(N, C_{in}, D_{out}, H_{out}, W_{out})`. Currently dout data type only supports float16 5374 and float32. 5375 weight (Tensor): Set size of kernel is :math:`(K_d, K_h, K_w)`, then the shape is 5376 :math:`(C_{in}, C_{out}//group, K_d, K_h, K_w)`. Where :math:`group` is the Args parameter, 5377 :math:`//` is the symbol for integer division. 5378 Currently weight data type only supports float16 and float32. 5379 pad_mode (str): Specifies padding mode. The optional values are 5380 "same", "valid", "pad". Default: "valid". 5381 5382 - same: Adopts the way of completion. The depth, height and width of the output will be equal to 5383 the input `x` divided by stride. The padding will be evenly calculated in head and tail, top and bottom, 5384 left and right directions possibility. 5385 Otherwise, the last extra padding will be calculated from the tail, bottom and the right side. 5386 If this mode is set, `pad` must be 0. 5387 5388 - valid: Adopts the way of discarding. The possible largest depth, height and width of output 5389 will be returned without padding. Extra pixels will be discarded. If this mode is set, `pad` 5390 and `output_padding` must be 0. 5391 5392 - pad: Implicit paddings on both sides of the input in depth, height and width. The number of `pad` will 5393 be padded to the input Tensor borders. `pad` must be greater than or equal to 0. 5394 5395 padding (Union(int, tuple[int])): The padding value to be filled. Default: 0. If `padding` is an integer, the 5396 paddings of head, tail, top, bottom, left and right are the same, equal to pad. If `padding` is a tuple of 5397 six integers, the padding of head, tail, top, bottom, left and right equal to padding[0], padding[1], 5398 padding[2], padding[3], padding[4] and padding[5] correspondingly. 5399 stride (Union(int, tuple[int])): The distance of kernel moving, an int number that represents 5400 the depth, height and width of movement are both strides, or a tuple of three int numbers that 5401 represent depth, height and width of movement respectively. Default: 1. 5402 dilation (Union(int, tuple[int])): Specifies the space to use between kernel elements. Default: 1. 5403 group (int): Splits input into groups. Default: 1. Only 1 is currently supported. 5404 output_padding (Union(int, tuple[int])): Add extra size to each dimension of the output. Default: 0. 5405 5406 5407 Outputs: 5408 Tensor, the gradients with respect to the input of convolution 3D. 5409 Tensor of shape :math:`(N, C_{out}//group, D_{out}, H_{out}, W_{out})`, 5410 where :math:`group` is the Args parameter. 5411 5412 Supported Platforms: 5413 ``Ascend`` ``GPU`` ``CPU`` 5414 5415 Raises: 5416 TypeError: If `group` is not an int. 5417 TypeError: If `stride`, `padding` , `dilation` or `output_padding` is neither an int not a tuple. 5418 ValueError: If the rank of `inputs`, `weight` is not equal to 5. 5419 ValueError: If `stride` or `dilation` is less than 1. 5420 ValueError: if inputs[1], weight[1] and weight[2:5] i.e. `in_channel`, `out_channel` and `kernel_size` is less 5421 than 1. 5422 ValueError: If `padding` is less than 0. 5423 ValueError: If `pad_mode` is not one of 'same', 'valid' nor 'pad'. 5424 ValueError: If `padding` is a tuple whose length is not equal to 6. 5425 ValueError: If `pad_mode` is not equal to 'padding' and `padding` is not equal to (0, 0, 0, 0, 0, 0). 5426 ValueError: If `data_format` is not 'NCDHW'. 5427 TypeError: If data type of dout and weight is not float16. 5428 5429 Examples: 5430 >>> import mindspore 5431 >>> import numpy as np 5432 >>> from mindspore import Tensor 5433 >>> dout = Tensor(np.ones([32, 16, 10, 32, 32]), mindspore.float16) 5434 >>> weight = Tensor(np.ones([16, 3, 4, 6, 2]), mindspore.float16) 5435 >>> output = conv3d_transpose(dout, weight) 5436 >>> print(output.shape) 5437 (32, 3, 13, 37, 33) 5438 """ 5439 if len(inputs.shape) != 5: 5440 raise_value_error("the rank of inputs tensor should be 5.") 5441 if len(weight.shape) != 5: 5442 raise_value_error("the rank of weight tensor should be 5.") 5443 in_channel = inputs.shape[1] 5444 out_channel = weight.shape[1] 5445 kernel_size = weight.shape[2:5] 5446 _conv_3d_transpose = _get_cache_prim(NN_OPS.Conv3DTranspose)(in_channel, out_channel, kernel_size, 1, pad_mode, 5447 padding, stride, dilation, group, output_padding) 5448 return _conv_3d_transpose(inputs, weight) 5449 5450 5451def _manipulate_padding(padding, dim): 5452 """convert padding to Conv2D padding""" 5453 ms_padding = () 5454 if not isinstance(padding, (tuple, list)): 5455 raise TypeError(f"For 'conv{dim}d', 'padding' must be a tuple, list or int, but got {type(padding)}.") 5456 if len(padding) != dim: 5457 raise ValueError(f"For 'conv{dim}d', 'padding' must be a tuple or list of {dim} integers, but got {padding}.") 5458 for i in range(dim): 5459 ms_padding += (padding[i], padding[i]) 5460 return ms_padding 5461 5462 5463def _dim_manipulation(x, name): 5464 """convert 1d dilation, stride, etc. to 2d""" 5465 if isinstance(x, int): 5466 if x <= 0: 5467 raise ValueError(f"For 'conv1d', {name} must be a positive int, but got {x}.") 5468 return 1, x 5469 if isinstance(x, (tuple, list)): 5470 if len(x) != 1: 5471 raise ValueError(f"For 'conv1d', {name} must be a tuple/list with 1 element or int, but got {x}.") 5472 if x[0] <= 0: 5473 raise ValueError(f"For 'conv1d', elements in {name} must be positive int, but got {x}.") 5474 return 1, x[0] 5475 raise ValueError(f"For 'conv1d', {name} must be an int or a tuple/list with 1 element, but got {x}.") 5476 5477 5478def _check_conv_iterable_lengths(iterable, dim, iter_name): 5479 """check iterables lengths used in conv functions""" 5480 if len(iterable) != dim: 5481 raise ValueError(f"For 'conv{dim}d', the {iter_name} must be a int or a tuple/list with length {dim}, " 5482 f"but got {iterable}.") 5483 5484 5485def conv1d(input, weight, bias=None, stride=1, pad_mode="valid", padding=0, dilation=1, groups=1): 5486 r""" 5487 Applies a 1D convolution over an input tensor. The input Tensor is typically 5488 of shape :math:`(N, C_{in}, L_{in})`, 5489 where :math:`N` is batch size, :math:`C` is channel number, :math:`L` is input sequence width. 5490 5491 The output is calculated based on formula: 5492 5493 .. math:: 5494 5495 \text{out}(N_i, C_{\text{out}_j}) = \text{bias}(C_{\text{out}_j}) + 5496 \sum_{k = 0}^{C_{in} - 1} \text{ccor}({\text{weight}(C_{\text{out}_j}, k), \text{X}(N_i, k)}) 5497 5498 where :math:`bias` is the output channel bias, :math:`ccor` is 5499 the `cross-correlation <https://en.wikipedia.org/wiki/Cross-correlation>`_, 5500 , :math:`weight` is the convolution kernel value and :math:`X` represents the input feature map. 5501 5502 Here are the indices' meanings: 5503 5504 - :math:`i` corresponds to the batch number, the range is :math:`[0, N-1]`, 5505 where :math:`N` is the batch size of the input. 5506 5507 - :math:`j` corresponds to the output channel, ranging from :math:`[0, C_{out}-1]`, 5508 where :math:`C_{out}` is the number of 5509 output channels, which is also equal to the number of kernels. 5510 5511 - :math:`k` corresponds to the input channel, ranging from :math:`[0, C_{in}-1]`, 5512 where :math:`C_{in}` is the number of 5513 input channels, which is also equal to the number of channels in the convolutional kernels. 5514 5515 Therefore, in the above formula, :math:`{bias}(C_{\text{out}_j})` represents the bias of the :math:`j`-th 5516 output channel, :math:`{weight}(C_{\text{out}_j}, k)` represents the slice of the :math:`j`-th convolutional 5517 kernel in the :math:`k`-th channel, and :math:`{X}(N_i, k)` represents the slice of the :math:`k`-th input 5518 channel in the :math:`i`-th batch of the input feature map. 5519 5520 The shape of the convolutional kernel is given by :math:`(\text{kernel_size})`, 5521 where :math:`\text{kernel_size}` is the width of the kernel. 5522 If we consider the input and output channels as well as the `group` parameter, the complete kernel shape 5523 will be :math:`(C_{out}, C_{in} / \text{group}, \text{kernel_size})`, 5524 where `group` is the number of groups dividing `x`'s input channel when applying group convolution. 5525 5526 For more details about convolution layer, please refer to `Gradient Based Learning Applied to Document Recognition 5527 <http://vision.stanford.edu/cs598_spring07/papers/Lecun98.pdf>`_ 5528 and `ConvNets <http://cs231n.github.io/convolutional-networks/>`_ . 5529 5530 Note: 5531 On Ascend platform, only group convolution in depthwise convolution scenarios is supported. 5532 That is, when `groups>1`, condition :math:`C_{in}` = :math:`C_{out}` = `groups` must be satisfied. 5533 5534 Args: 5535 input (Tensor): Input Tensor of shape :math:`(N, C_{in}, L_{in})`. 5536 weight (Tensor): The convolutional kernel value, it should has shape 5537 :math:`(N, C_{in} / \text{groups}, \text{kernel_size})`. 5538 bias (Tensor, optional): Bias Tensor with shape :math:`(C_{out})`. 5539 When bias is None, zeros will be used. Default: ``None`` . 5540 stride (Union(int, tuple[int]), optional): The distance of kernel moving, an int number or a tuple of one int 5541 that represents width of movement. Default: ``1``. 5542 pad_mode (str, optional): Specifies padding mode. The optional values are 5543 ``"same"`` , ``"valid"`` and ``"pad"`` . Default: ``"valid"`` . 5544 5545 - ``"same"``: Adopts the way of completion. The height and width of the output will be equal to 5546 the input `x` divided by stride. The padding will be evenly calculated in left and right possiblily. 5547 Otherwise, the last extra padding will be calculated from the right side. 5548 If this mode is set, `padding` must be 0. 5549 5550 - ``"valid"``: Adopts the way of discarding. The possible largest width of output will be returned 5551 without padding. Extra pixels will be discarded. If this mode is set, `padding` must be 0. 5552 5553 - ``"pad"``: Implicit paddings on both sides of the input `x`. 5554 The number of `padding` will be padded to the input 5555 Tensor borders. `padding` must be greater than or equal to 0. 5556 padding (Union(int, tuple[int], list[int]), optional): Specifies the amount of padding to apply on 5557 both side of `input` when `pad_mode` is set to ``"pad"``. The 5558 paddings of left and right are the same, equal to padding or padding[0] when padding is a tuple of 5559 1 integer. Default: ``0`` . 5560 dilation (Union(int, tuple[int]), optional): Specifies the dilation rate to use for dilated convolution. 5561 It can be a single int or a tuple of 1 integer. 5562 Assuming :math:`dilation=(d0,)`, the convolutional kernel samples the input with a 5563 spacing of :math:`d0-1` elements in the width direction. 5564 The value should be in the ranges [1, L]. 5565 Default: ``1`` . 5566 groups (int, optional): Splits `input` into groups. Default: ``1`` . 5567 5568 Returns: 5569 Tensor, the value that applied 1D convolution. The shape is :math:`(N, C_{out}, L_{out})`. 5570 To see how different pad modes affect the output shape, please refer to 5571 :class:`mindspore.nn.Conv1d` for more details. 5572 5573 Raises: 5574 TypeError: If `stride`, `padding` or `dilation` is neither an int nor a tuple. 5575 TypeError: `groups` is not an int. 5576 TypeError: If `bias` is not a Tensor. 5577 ValueError: If the shape of `bias` is not :math:`(C_{out})` . 5578 ValueError: If `stride` or `dilation` is less than 1. 5579 ValueError: If `pad_mode` is not one of 'same', 'valid' or 'pad'. 5580 ValueError: If `padding` is a tuple whose length is not equal to 1. 5581 ValueError: If `pad_mode` is not equal to 'pad' and `padding` is greater than 0. 5582 5583 Supported Platforms: 5584 ``Ascend`` ``GPU`` 5585 5586 Examples: 5587 >>> import mindspore 5588 >>> import numpy as np 5589 >>> from mindspore import Tensor, ops 5590 >>> x = Tensor(np.arange(64).reshape((4, 4, 4)), mindspore.float32) 5591 >>> weight = Tensor(np.arange(8).reshape((2, 2, 2)), mindspore.float32) 5592 >>> bias = Tensor([-0.12345, 2.7683], mindspore.float32) 5593 >>> output = ops.conv1d(x, weight, pad_mode='pad', padding=(1,), bias=bias, groups=2) 5594 >>> print(output.shape) 5595 (4, 2, 5) 5596 """ 5597 if input.ndim != 3: 5598 raise ValueError(f"For 'conv1d', the input must be a 3D Tensor, but got input of {input.ndim}D.") 5599 if weight.ndim != 3: 5600 raise ValueError(f"For 'conv1d', the weight must be a 3D Tensor, but got input of {weight.ndim}D.") 5601 expanded_input = expand_dims_(input, 2) 5602 sqz = _get_cache_prim(P.Squeeze)(2) 5603 weight_shape = weight.shape 5604 out_channel = weight_shape[0] 5605 kernel_size = (1, weight_shape[2]) 5606 expanded_weight = expand_dims_(weight, 2) 5607 if isinstance(padding, int): 5608 padding = (0, 0, padding, padding) 5609 elif isinstance(padding, (tuple, list)): 5610 if len(padding) != 1: 5611 raise ValueError(f"For 'conv1d', padding must be a tuple or list with 1 element or int, but got {padding}.") 5612 padding = (0, 0, padding[0], padding[0]) 5613 else: 5614 raise TypeError(f"For 'conv1d', padding must be a tuple, list or int, but got {type(padding)}.") 5615 input_shape = input.shape 5616 in_channel = input_shape[1] 5617 if not (in_channel % groups == 0 and out_channel % groups == 0): 5618 raise ValueError(f"The argument 'groups' should be divisible by 'in_channel' " \ 5619 f"and 'out_channel', but got group:{groups}, in_channel:{in_channel}, " \ 5620 f"out_channel:{out_channel}.") 5621 dilation = _dim_manipulation(dilation, name='dilation') 5622 stride = _dim_manipulation(stride, name='stride') 5623 conv = _get_cache_prim(P.Conv2D)(out_channel, kernel_size, 1, pad_mode, padding, stride, dilation, groups, "NCHW") 5624 conv_res = conv(expanded_input, expanded_weight) 5625 squeezed_conv_res = sqz(conv_res) 5626 if bias is None: 5627 return squeezed_conv_res 5628 if not isinstance(bias, Tensor): 5629 raise TypeError(f"For 'conv1d', the 'bias' must be a Tensor, but got {type(bias)}.") 5630 if bias.shape[0] != out_channel: 5631 raise ValueError(f"For 'conv1d', given weight of size {weight_shape}, expected bias to be 1-dimensional with " \ 5632 f"{out_channel} elements, but got bias of size {bias.shape[0]} instead.") 5633 output = bias_add(squeezed_conv_res, bias) 5634 return output 5635 5636 5637def conv2d(input, weight, bias=None, stride=1, pad_mode="valid", padding=0, dilation=1, groups=1): 5638 r""" 5639 Applies a 2D convolution over an input tensor. The input tenor is typically of 5640 shape :math:`(N, C_{in}, H_{in}, W_{in})`, where :math:`N` is batch size, :math:`C` is 5641 channel number, :math:`H` is feature height, :math:`W` is feature width. 5642 5643 The output is calculated based on formula: 5644 5645 .. math:: 5646 5647 \text{out}(N_i, C_{\text{out}_j}) = \text{bias}(C_{\text{out}_j}) + 5648 \sum_{k = 0}^{C_{in} - 1} \text{ccor}({\text{weight}(C_{\text{out}_j}, k), \text{X}(N_i, k)}) 5649 5650 where :math:`bias` is the output channel bias, :math:`ccor` is 5651 the `cross-correlation <https://en.wikipedia.org/wiki/Cross-correlation>`_, 5652 , :math:`weight` is the convolution kernel value and :math:`X` represents the input feature map. 5653 5654 Here are the indices' meanings: 5655 5656 - :math:`i` corresponds to the batch number, the range is :math:`[0, N-1]`, 5657 where :math:`N` is the batch size of the input. 5658 5659 - :math:`j` corresponds to the output channel, the range is :math:`[0, C_{out}-1]`, 5660 where :math:`C_{out}` is the number of output channels, which is also equal to the number of kernels. 5661 5662 - :math:`k` corresponds to the input channel, the range is :math:`[0, C_{in}-1]`, 5663 where :math:`C_{in}` is the number of 5664 input channels, which is also equal to the number of channels in the convolutional kernels. 5665 5666 Therefore, in the above formula, :math:`{bias}(C_{out_j})` represents the bias of the :math:`j`-th 5667 output channel, :math:`{weight}(C_{out_j}, k)` represents the slice of the :math:`j`-th convolutional 5668 kernel in the :math:`k`-th channel, and :math:`{X}(N_i, k)` represents the slice of the :math:`k`-th input 5669 channel in the :math:`i`-th batch of the input feature map. 5670 5671 The shape of the convolutional kernel is given by :math:`(\text{kernel_size[0]}, \text{kernel_size[1]})`, 5672 where :math:`\text{kernel_size[0]}` and :math:`\text{kernel_size[1]}` are the height and width of the kernel, 5673 respectively. 5674 If we consider the input and output channels as well as the `group` parameter, the complete kernel shape 5675 will be :math:`(C_{out}, C_{in} / \text{group}, \text{kernel_size[0]}, \text{kernel_size[1]})`, 5676 where `group` is the number of groups dividing `x`'s input channel when applying group convolution. 5677 5678 For more details about convolution layer, please refer to `Gradient Based Learning Applied to Document Recognition 5679 <http://vision.stanford.edu/cs598_spring07/papers/Lecun98.pdf>`_ and 5680 `ConvNets <http://cs231n.github.io/convolutional-networks/>`_. 5681 5682 Note: 5683 On Ascend platform, only group convolution in depthwise convolution scenarios is supported. 5684 That is, when `groups>1`, condition :math:`C_{in}` = :math:`C_{out}` = `groups` must be satisfied. 5685 5686 Args: 5687 input (Tensor): Tensor of shape :math:`(N, C_{in}, H_{in}, W_{in})`. 5688 weight (Tensor): Tensor of shape 5689 :math:`(N, C_{in} / \text{groups}, \text{kernel_size[0]}, \text{kernel_size[1]})`, then the size of kernel 5690 is :math:`(\text{kernel_size[0]}, \text{kernel_size[1]})`. 5691 bias (Tensor, optional): Bias Tensor with shape :math:`(C_{out})`. 5692 When bias is ``None`` , zeros will be used. Default: ``None`` . 5693 stride (Union(int, tuple[int]), optional): The distance of kernel moving, an int number that represents 5694 the height and width of movement are both strides, or a tuple of two int numbers that 5695 represent height and width of movement respectively. Default: ``1`` . 5696 pad_mode (str, optional): Specifies padding mode. The optional values are 5697 ``"same"`` , ``"valid"`` and ``"pad"`` . Default: ``"valid"`` . 5698 5699 - same: Adopts the way of completion. The height and width of the output will be equal to 5700 the input `x` divided by stride. The padding will be evenly calculated in top and bottom, 5701 left and right possiblily. Otherwise, the last extra padding will be calculated from the bottom 5702 and the right side. If this mode is set, `padding` must be 0. 5703 5704 - valid: Adopts the way of discarding. The possible largest height and width of output will be returned 5705 without padding. Extra pixels will be discarded. If this mode is set, `padding` must be 0. 5706 5707 - pad: Implicit paddings on both sides of the input `x`. The number of `padding` will be padded to the input 5708 Tensor borders. `padding` must be greater than or equal to 0. 5709 padding (Union(int, tuple[int], list[int]), optional): Implicit paddings on both sides of the input `x`. 5710 If `padding` is one integer, the paddings of top, bottom, left and right are the same, equal to padding. 5711 If `padding` is a tuple/list with 2 integers, the padding of top adn bottom is padding[0], 5712 and the padding of left and right is padding[1]. Default: ``0`` . 5713 dilation (Union(int, tuple[int]), optional): Gaps between kernel elements.The data type is int or a tuple of 5714 2 integers. Specifies the dilation rate to use for dilated convolution. If set to be :math:`k > 1`, 5715 there will be :math:`k - 1` pixels skipped for each sampling location. Its value must 5716 be greater than or equal to 1 and bounded by the height and width of the input `x`. Default: ``1`` . 5717 groups (int, optional): Splits `input` into groups. Default: ``1`` . 5718 5719 Returns: 5720 Tensor, the value that applied 2D convolution. The shape is :math:`(N, C_{out}, H_{out}, W_{out})`. 5721 To see how different pad modes affect the output shape, please refer to 5722 :class:`mindspore.nn.Conv2d` for more details. 5723 5724 5725 Raises: 5726 TypeError: If `stride`, `padding` or `dilation` is neither an int nor a tuple. 5727 TypeError: `groups` is not an int. 5728 TypeError: If `bias` is not a Tensor. 5729 ValueError: If the shape of `bias` is not :math:`(C_{out})` . 5730 ValueError: If `stride` or `dilation` is less than 1. 5731 ValueError: If `pad_mode` is not one of 'same', 'valid' or 'pad'. 5732 ValueError: If `padding` is a tuple/list whose length is not equal to 2. 5733 ValueError: If `pad_mode` is not equal to 'pad' and `padding` is greater than 0. 5734 5735 Supported Platforms: 5736 ``Ascend`` ``GPU`` 5737 5738 Examples: 5739 >>> import mindspore 5740 >>> import numpy as np 5741 >>> from mindspore import Tensor, ops 5742 >>> x = Tensor(np.ones([10, 32, 32, 32]), mindspore.float32) 5743 >>> weight = Tensor(np.ones([32, 32, 3, 3]), mindspore.float32) 5744 >>> output = ops.conv2d(x, weight) 5745 >>> print(output.shape) 5746 (10, 32, 30, 30) 5747 """ 5748 if isinstance(stride, (tuple, list)): 5749 _check_conv_iterable_lengths(stride, dim=2, iter_name='stride') 5750 if isinstance(dilation, (tuple, list)): 5751 _check_conv_iterable_lengths(dilation, dim=2, iter_name='dilation') 5752 if isinstance(padding, (tuple, list)): 5753 padding = _manipulate_padding(padding, dim=2) 5754 weight_shape = weight.shape 5755 out_channel = weight_shape[0] 5756 kernel_size = weight_shape[2:4] 5757 input_shape = input.shape 5758 in_channel = input_shape[1] 5759 if not (in_channel % groups == 0 and out_channel % groups == 0): 5760 raise ValueError(f"The argument 'groups' should be divisible by 'in_channel' " \ 5761 f"and 'out_channel', but got group:{groups}, in_channel:{in_channel}, " \ 5762 f"out_channel:{out_channel}.") 5763 conv = _get_cache_prim(P.Conv2D)(out_channel, kernel_size, 1, pad_mode, padding, stride, dilation, groups, "NCHW") 5764 if bias is None: 5765 return conv(input, weight) 5766 if not isinstance(bias, Tensor): 5767 raise TypeError(f"For 'conv2d', the 'bias' must be a Tensor, but got {type(bias)}.") 5768 if bias.shape[0] != out_channel: 5769 raise ValueError(f"For 'conv2d', Given weight of size {weight_shape}, expected bias to be 1-dimensional with " \ 5770 f"{out_channel} elements, but got bias of size {bias.shape[0]} instead.") 5771 conv_result = conv(input, weight) 5772 output = bias_add(conv_result, bias) 5773 return output 5774 5775 5776def conv_transpose2d(input, weight, bias=None, stride=1, padding=0, output_padding=0, groups=1, dilation=1): 5777 r""" 5778 Calculates a 2D transposed convolution, which can be regarded as Conv2d for the gradient of the input, 5779 also called deconvolution (although it is not an actual deconvolution). 5780 5781 The input is typically of shape :math:`(N, C_{in}, H_{in}, W_{in})`, 5782 where :math:`N` is batch size, :math:`C_{in}` is space dimension, 5783 :math:`H_{in}, W_{in}` are the height and width of the feature layer respectively. 5784 5785 When Conv2d and Conv2dTranspose are initialized with the same parameters, and `pad_mode` is set to 'pad', 5786 :math:`dilation * (kernel\_size - 1) - padding` amount of zero will be paded to the height and width 5787 directions of the input, they are inverses of each other in regard to the input and output shapes in this case. 5788 However, when `stride` > 1, Conv2d maps multiple input shapes to the same output shape. Deconvolutional network 5789 can refer to `Deconvolutional Networks <https://www.matthewzeiler.com/mattzeiler/deconvolutionalnetworks.pdf>`_. 5790 5791 Args: 5792 input (Tensor): Tensor of shape :math:`(N, C_{in}, H_{in}, W_{in})`. 5793 weight (Tensor): Tensor of shape 5794 :math:`(N, C_{in} / \text{groups}, \text{kernel_size[0]}, \text{kernel_size[1]})`, then the size of kernel 5795 is :math:`(\text{kernel_size[0]}, \text{kernel_size[1]})`. 5796 bias (Tensor, optional): Bias Tensor with shape :math:`(C_{out})`. 5797 When bias is ``None`` , zeros will be used. Default: ``None`` . 5798 stride (Union(int, tuple[int]), optional): The distance of kernel moving, an int number that represents 5799 the height and width of movement are both strides, or a tuple of two int numbers that 5800 represent height and width of movement respectively. Default: ``1`` . 5801 padding (Union(int, tuple[int], list[int]), optional): Implicit paddings on both sides of the input `x`. 5802 Can be an integer or a tuple/list with 2 integers. 5803 output_padding (Union[int, tuple[int]]): The number of padding on the height and width directions of the output. 5804 The data type is an integer or a tuple of two integers. If `output_padding` is an integer, 5805 then the bottom and right padding are all equal to `output_padding`. If `output_padding` is a tuple of 5806 2 integers, then the bottom and right padding is equal to `output_padding[0]`, `output_padding[1]` 5807 respectively. 5808 groups (int, optional): Splits `input` into groups. Default: ``1`` . 5809 dilation (Union(int, tuple[int]), optional): Gaps between kernel elements.The data type is int or a tuple of 5810 2 integers. Specifies the dilation rate to use for dilated convolution. If set to be :math:`k > 1`, 5811 there will be :math:`k - 1` pixels skipped for each sampling location. Its value must 5812 be greater than or equal to 1 and bounded by the height and width of the input `x`. Default: ``1`` . 5813 5814 Returns: 5815 Tensor, the value that applied 2D convolution. The shape is :math:`(N, C_{out}, H_{out}, W_{out})`. 5816 To see how different pad modes affect the output shape, please refer to 5817 :class:`mindspore.nn.Conv2dTranspose` for more details. 5818 5819 5820 Raises: 5821 TypeError: If `stride`, `padding` or `dilation` is neither an int nor a tuple. 5822 TypeError: `groups` is not an int. 5823 TypeError: If `bias` is not a Tensor. 5824 ValueError: If the shape of `bias` is not :math:`(C_{out})` . 5825 ValueError: If `stride` or `dilation` is less than 1. 5826 ValueError: If `padding` is a tuple/list whose length is not equal to 2. 5827 5828 Supported Platforms: 5829 ``Ascend`` 5830 5831 Examples: 5832 >>> import mindspore 5833 >>> import numpy as np 5834 >>> from mindspore import Tensor, ops 5835 >>> x = Tensor(np.ones([1, 6, 32, 32]), mindspore.float32) 5836 >>> weight = Tensor(np.ones([6, 3, 5, 5]), mindspore.float32) 5837 >>> output = ops.conv_transpose2d(x, weight) 5838 >>> print(output.shape) 5839 (1, 3, 36, 36) 5840 """ 5841 conv = _get_cache_prim(Convolution)(stride, padding, dilation, True, output_padding, groups) 5842 return conv(input, weight, bias) 5843 5844 5845def hardsigmoid(input): 5846 r""" 5847 Hard sigmoid activation function. 5848 5849 Applies hard sigmoid activation element-wise. The input is a Tensor with any valid shape. 5850 5851 Hard sigmoid is defined as: 5852 5853 .. math:: 5854 5855 \text{hsigmoid}(x_{i}) = \max(0, \min(1, \frac{x_{i} + 3}{6})) 5856 5857 where :math:`x_i` is an element of the input Tensor. 5858 5859 HSigmoid Activation Function Graph: 5860 5861 .. image:: ../images/HSigmoid.png 5862 :align: center 5863 5864 Args: 5865 input (Tensor): The input Tensor. 5866 5867 Returns: 5868 A Tensor whose dtype and shape are the same as `input`. 5869 5870 Raises: 5871 TypeError: If `input` is not a Tensor. 5872 TypeError: If dtype of `input` is not int or float. 5873 5874 Supported Platforms: 5875 ``Ascend`` ``GPU`` ``CPU`` 5876 5877 Examples: 5878 >>> import mindspore 5879 >>> import numpy as np 5880 >>> from mindspore import Tensor, ops 5881 >>> x = Tensor(np.array([ -3.5, 0, 4.3]), mindspore.float32) 5882 >>> output = ops.hardsigmoid(x) 5883 >>> print(output) 5884 [0. 0.5 1. ] 5885 """ 5886 hardsigmoid_ = NN_OPS.HSigmoid() 5887 return hardsigmoid_(input) 5888 5889 5890def hardtanh(input, min_val=-1.0, max_val=1.0): 5891 r""" 5892 Applies the hardtanh activation function element-wise. The activation function is defined as: 5893 5894 .. math:: 5895 \text{hardtanh}(input) = \begin{cases} 5896 max\_val, & \text{ if } input > max\_val \\ 5897 min\_val, & \text{ if } input < min\_val \\ 5898 input, & \text{ otherwise. } 5899 \end{cases} 5900 5901 Linear region range :math:`[min\_val, max\_val]` can be adjusted using `min_val` and `max_val`. 5902 5903 Hardtanh Activation Function Graph: 5904 5905 .. image:: ../images/Hardtanh.png 5906 :align: center 5907 5908 Args: 5909 input (Tensor): Input Tensor. 5910 min_val (Union[int, float], optional): Minimum value of the linear region range. Default: ``-1.0`` . 5911 max_val (Union[int, float], optional): Maximum value of the linear region range. Default: ``1.0`` . 5912 5913 Returns: 5914 Tensor, with the same dtype and shape as `input`. 5915 5916 Raises: 5917 TypeError: If `input` is not a Tensor. 5918 TypeError: If dtype of `min_val` is neither float nor int. 5919 TypeError: If dtype of `max_val` is neither float nor int. 5920 5921 Supported Platforms: 5922 ``Ascend`` ``GPU`` ``CPU`` 5923 5924 Examples: 5925 >>> import mindspore 5926 >>> from mindspore import Tensor, ops 5927 >>> x = Tensor([-1, -2, 0, 2, 1], mindspore.float16) 5928 >>> output = ops.hardtanh(x, min_val=-1.0, max_val=1.0) 5929 >>> print(output) 5930 [-1. -1. 0. 1. 1.] 5931 """ 5932 _check_is_tensor('input', input, "hardtanh") 5933 _check_value_type("min_val", min_val, [int, float], "hardtanh") 5934 _check_value_type("max_val", max_val, [int, float], "hardtanh") 5935 input_dtype = input.dtype 5936 input = maximum_(input, min_val) 5937 input = minimum_(input, max_val) 5938 return input.astype(input_dtype) 5939 5940 5941def huber_loss(input, target, reduction='mean', delta=1.0): 5942 r""" 5943 Calculates the error between the predicted value and the target value, 5944 which has the best of both the loss of :func:`mindspore.ops.l1_loss` and the loss of :func:`mindspore.ops.mse_loss`. 5945 5946 Assuming that the :math:`x` and :math:`y` are 1-D Tensor, length :math:`N`, the `reduction` parameter 5947 is set to ``'none'`` then calculate the loss of :math:`x` and :math:`y` without dimensionality reduction. 5948 The formula is as follows: 5949 5950 .. math:: 5951 \ell(x, y) = L = \{l_1,\dots,l_N\}^\top 5952 5953 with 5954 5955 .. math:: 5956 l_n = \begin{cases} 5957 0.5 * (x_n - y_n)^2, & \text{if } |x_n - y_n| < delta; \\ 5958 delta * (|x_n - y_n| - 0.5 * delta), & \text{otherwise. } 5959 \end{cases} 5960 5961 where :math:`N` is the batch size. 5962 5963 If `reduction` is "mean" or "sum", then: 5964 5965 .. math:: 5966 \ell(x, y) = 5967 \begin{cases} 5968 \operatorname{mean}(L), & \text{if reduction} = \text{"mean";}\\ 5969 \operatorname{sum}(L), & \text{if reduction} = \text{"sum".} 5970 \end{cases} 5971 5972 Args: 5973 input (Tensor): Predicted value, Tensor of any dimension. 5974 target (Tensor): Target value, has same dtype and shape as the `input` in common cases. 5975 However, when the shape of `target` is different from the shape of `input`, 5976 and they should be broadcasted to each other. 5977 reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` , 5978 ``'sum'`` . Default: ``'mean'`` . 5979 5980 - ``'none'``: no reduction will be applied. 5981 - ``'mean'``: compute and return the mean of elements in the output. 5982 - ``'sum'``: the output elements will be summed. 5983 5984 delta (Union[int, float]): The threshold to change between two type of loss. 5985 The value must be greater than zero. Default: ``1.0`` . 5986 5987 Returns: 5988 Tensor or Scalar, if `reduction` is ``'none'``, return a Tensor with same shape and dtype as `input`. 5989 Otherwise, a scalar value will be returned. 5990 5991 Raises: 5992 TypeError: If `input` or `target` is not a Tensor. 5993 TypeError: If dtype of `delta` is neither float nor int. 5994 ValueError: If `delta` is less than or equal to 0. 5995 ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``. 5996 ValueError: If `input` and `target` have different shapes and cannot be broadcasted to each other. 5997 5998 Supported Platforms: 5999 ``Ascend`` ``GPU`` ``CPU`` 6000 6001 Examples: 6002 >>> import mindspore 6003 >>> from mindspore import Tensor, ops 6004 >>> x = Tensor([1, 2, 10, 2], mindspore.float32) 6005 >>> target = Tensor([1, 5, 1, 20], mindspore.float32) 6006 >>> output = ops.huber_loss(x, target, reduction="mean", delta=2) 6007 >>> print(output) 6008 13.5 6009 """ 6010 _check_is_tensor('input', input, "huber_loss") 6011 _check_is_tensor('target', target, "huber_loss") 6012 _check_value_type("delta", delta, [int, float], "huber_loss") 6013 _check_number_gt_value("delta", delta, 0.0, "huber_loss") 6014 z = sub_(input, target) 6015 z = abs_(z) 6016 cond = less_(z, delta) 6017 l1 = mul_(0.5, square_(z)) 6018 l2 = mul_(delta, sub_(z, 0.5 * delta)) 6019 loss = select_(cond, l1, l2) 6020 return _get_loss(loss, reduction, "huber_loss") 6021 6022 6023@_primexpr 6024def _check_adaptive_avg_pool1d_output_size(output_size): 6025 """Check the output_size value in adaptive_avg_pool1d op.""" 6026 validator.check_int(output_size, 1, validator.GE, "output_size", 'adaptive_avg_pool1d') 6027 validator.check_value_type('output_size', output_size, [int], 'adaptive_avg_pool1d') 6028 6029 6030def adaptive_avg_pool1d(input, output_size): 6031 r""" 6032 Applies a 1D adaptive average pooling over an input Tensor which can be regarded as a composition of 1D input 6033 planes. 6034 6035 Typically, the input is of shape :math:`(N, C, L_{in})`, adaptive_avg_pool1d outputs regional average 6036 in the :math:`L_{in}`-dimension. The output is of shape :math:`(N, C, L_{out})`, where :math:`L_{out}` 6037 is defined by `output_size`. 6038 6039 Note: 6040 :math:`L_{in}` must be divisible by `output_size`. 6041 6042 Args: 6043 input (Tensor): Tensor of shape :math:`(N, C, L_{in})`, with float16 or float32 data type. 6044 output_size (int): the target output size :math:`L_{out}`. 6045 6046 Returns: 6047 Tensor of shape :math:`(N, C, L_{out})`, has the same type as `input`. 6048 6049 Raises: 6050 TypeError: If `output_size` is not an int. 6051 TypeError: If `input` is neither float16 nor float32. 6052 ValueError: If `output_size` is less than 1. 6053 ValueError: If length of shape of `input` is not equal to 3. 6054 ValueError: If the last dimension of `input` is smaller than `output_size`. 6055 ValueError: If the last dimension of `input` is not divisible by `output_size`. 6056 6057 Supported Platforms: 6058 ``Ascend`` ``GPU`` ``CPU`` 6059 6060 Examples: 6061 >>> import mindspore 6062 >>> import numpy as np 6063 >>> from mindspore import Tensor, ops 6064 >>> input = Tensor(np.random.randint(0, 10, [1, 3, 6]), mindspore.float32) 6065 >>> output = ops.adaptive_avg_pool1d(input, output_size=2) 6066 >>> print(output.shape) 6067 (1, 3, 2) 6068 """ 6069 def _check(x, output_size): 6070 x_in_shape = x.shape 6071 x_dtype = dtype_(x) 6072 if not isinstance(x, (Tensor, Tensor_)): 6073 raise TypeError("For adaptive_avg_pool1d, the input input must be tensor") 6074 6075 _check_adaptive_avg_pool1d_output_size(output_size) 6076 6077 if len(x_in_shape) != 3: 6078 raise ValueError(f"For adaptive_avg_pool1d input must have 3 dim, but got {len(x_in_shape)}.") 6079 if x_in_shape[2] < output_size: 6080 raise ValueError(f"For adaptive_avg_pool1d input's last dimension must be greater or equal to " \ 6081 f"output size {output_size}, but got {x_in_shape[2]}.") 6082 if x_in_shape[2] % output_size != 0: 6083 raise ValueError(f"For adaptive_avg_pool1d input's last dimension must be divisible by " 6084 f"output size {output_size}, but got {x_in_shape[2]}.") 6085 if x_dtype not in [mstype.float16, mstype.float32]: 6086 raise TypeError(f"For adaptive_avg_pool1d, the input dtype must be float16 or float32, " \ 6087 f"but got {x_dtype}.") 6088 6089 _check(input, output_size) 6090 x_in_shape = input.shape 6091 squeeze_ = _get_cache_prim(P.Squeeze)(2) 6092 width = x_in_shape[2] 6093 stride = width // output_size 6094 kernel_size = width - (output_size - 1) * stride 6095 stride = (1, width // output_size) 6096 kernel_size = (1, kernel_size) 6097 avg_pool_ = _get_cache_prim(P.AvgPool)(kernel_size=kernel_size, strides=stride) 6098 input = expand_dims_(input, 2) 6099 input = avg_pool_(input) 6100 input = squeeze_(input) 6101 return input 6102 6103 6104def layer_norm(input, normalized_shape, weight=None, bias=None, eps=1e-5): 6105 r"""Applies the Layer Normalization on the mini-batch input. 6106 6107 Layer normalization is widely used in recurrent neural networks. Apply normalization to the mini-batch 6108 input of a single training case. LayerNorm is described in the paper 6109 `Layer Normalization <https://arxiv.org/abs/1607.06450>`_. 6110 6111 Unlike batch normalization, layer normalization performs the exact same calculations at training and 6112 test time. Applies to all channels and pixels, even batch_size=1. The formula is as follows: 6113 6114 .. math:: 6115 y = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta 6116 6117 where :math:`\gamma` is the weight value learned through training, :math:`\beta` is the bias value 6118 learned through training. 6119 6120 Args: 6121 input (Tensor): The shape of input is `(N, *)`, where `*` represents any additional dimension. 6122 normalized_shape (Union(int, tuple[int], list[int])): The normalized shape of `input` for LayerNorm. 6123 `normalized_shape` equal to `input_shape[begin_norm_axis:]`, where `begin_norm_axis` represents the axis 6124 where normalization begins. 6125 weight (Tensor, optional): Learnable parameter :math:`\gamma` . Tensor of shape `normalized_shape`. 6126 Default: ``None``, has the same data type with `input`. Initialized to ``1`` when `weight` is None. 6127 bias (Tensor, optional): Learnable parameter :math:`\beta` . Tensor of shape `normalized_shape`. 6128 Default: ``None``, has the same data type with `input`. Initialized to ``0`` when `bias` is None. 6129 eps (float, optional): A value added to the denominator for numerical stability(:math:`\epsilon`). 6130 Default: ``1e-5`` . 6131 6132 Returns: 6133 Tensor. The normalized tensor, has the same type and shape as the `input`. 6134 6135 Raises: 6136 TypeError: If `input` is not a Tensor. 6137 TypeError: If `normalized_shape` is not an integer, a list or a tuple. 6138 TypeError: If `eps` is not a float. 6139 6140 Supported Platforms: 6141 ``Ascend`` 6142 6143 Examples: 6144 >>> import mindspore 6145 >>> import numpy as np 6146 >>> from mindspore import Tensor, ops 6147 >>> input_x = Tensor(np.array([[1, 2, 3], [1, 2, 3]]), mindspore.float32) 6148 >>> normalized_shape = (3,) 6149 >>> gamma = Tensor(np.ones(normalized_shape), mindspore.float32) 6150 >>> beta = Tensor(np.zeros(normalized_shape), mindspore.float32) 6151 >>> eps = 1e-7 6152 >>> output = ops.layer_norm(input_x, normalized_shape, gamma, beta, eps) 6153 >>> print(output) 6154 [[-1.2247448 0. 1.2247448] 6155 [-1.2247448 0. 1.2247448]] 6156 """ 6157 if weight is None: 6158 weight = ops.ones(normalized_shape, dtype=input.dtype) 6159 if bias is None: 6160 bias = ops.zeros(normalized_shape, dtype=input.dtype) 6161 return layer_norm_ext_op(input, normalized_shape, weight, bias, eps)[0] 6162 6163 6164def group_norm(input, num_groups, weight=None, bias=None, eps=1e-5): 6165 r"""Group Normalization over a mini-batch of inputs. 6166 6167 Group Normalization is widely used in recurrent neural networks. It applies 6168 normalization on a mini-batch of inputs for each single training case as described 6169 in the paper `Group Normalization <https://arxiv.org/pdf/1803.08494.pdf>`_. Group Normalization 6170 divides the channels into groups and computes within each group the mean and variance for normalization, 6171 and it performs very stable over a wide range of batch size. :math:`\gamma` and :math:`\beta` are trainable scale 6172 and shift. 6173 It can be described using the following formula: 6174 6175 .. math:: 6176 y = \frac{x - \mathrm{E}[x]}{\sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta 6177 6178 where :math:`\gamma` is `weight`, :math:`\beta` is `bias`, :math:`\epsilon` is `eps`. 6179 6180 Args: 6181 input (Tensor): The input feature with shape :math:`(N, C, *)` where :math:`*` means, any number of 6182 additional dimensions. 6183 num_groups (int): The number of groups to be divided along the channel dimension. 6184 weight (Tensor, optional): The shape :math:`(C,)`, Default: ``None``, has the same data type with `input`. 6185 bias (Tensor, optional): The shape :math:`(C,)`, Default: ``None``, has the same data type with `input`. 6186 eps (float, optional): A value added to the denominator for numerical stability. Default: ``1e-5`` . 6187 6188 Returns: 6189 Tensor, the normalized and scaled offset tensor, has the same shape and data type as the `input`. 6190 6191 Raises: 6192 TypeError: If `num_groups` is not an int. 6193 TypeError: If `eps` is not a float. 6194 ValueError: If `num_groups` is less than 1. 6195 ValueError: If `C` (the second parameter of dimensions of `input`) is not divided by `num_groups`. 6196 6197 Supported Platforms: 6198 ``Ascend`` ``GPU`` ``CPU`` 6199 6200 Examples: 6201 >>> import mindspore as ms 6202 >>> import numpy as np 6203 >>> from mindspore import ops 6204 >>> x = ms.Tensor(np.ones([1, 2, 4, 4], np.float32)) 6205 >>> output = ops.group_norm(x, 2) 6206 >>> print(output) 6207 [[[[0. 0. 0. 0.] 6208 [0. 0. 0. 0.] 6209 [0. 0. 0. 0.] 6210 [0. 0. 0. 0.]] 6211 [[0. 0. 0. 0.] 6212 [0. 0. 0. 0.] 6213 [0. 0. 0. 0.] 6214 [0. 0. 0. 0.]]]] 6215 """ 6216 if weight is None: 6217 weight = ops.ones([input.shape[1]], dtype=input.dtype) 6218 if bias is None: 6219 bias = ops.zeros([input.shape[1]], dtype=input.dtype) 6220 return group_norm_op(input, num_groups, weight, bias, eps)[0] 6221 6222 6223def batch_norm_ext(input, running_mean, running_var, weight=None, bias=None, training=False, momentum=0.1, eps=1e-5): 6224 r""" 6225 Batch Normalization for input data and updated parameters. 6226 6227 Batch Normalization is widely used in convolutional neural networks. This operation 6228 applies Batch Normalization over inputs to avoid internal covariate shift as described 6229 in the paper `Batch Normalization: Accelerating Deep Network Training by Reducing Internal 6230 Covariate Shift <https://arxiv.org/abs/1502.03167>`_. It rescales and recenters the 6231 features using a mini-batch of data and the learned parameters can be described 6232 in the following formula, 6233 6234 .. math:: 6235 6236 y = \frac{x - mean}{\sqrt{variance + \epsilon}} * \gamma + \beta 6237 6238 where :math:`\gamma` is `weight`, :math:`\beta` is `bias`, :math:`\epsilon` is `eps`, :math:`mean` is the 6239 mean of :math:`x`, :math:`variance` is the variance of :math:`x`. 6240 6241 Args: 6242 input (Tensor): Tensor of shape :math:`(N, C, *)`, with bfloat16, float16 or float32 data type. 6243 For Atlas training products, the shape must be 2-4 dimensions currently. 6244 running_mean (Tensor): The shape :math:`(C,)`, with bfloat, float16 or float32 data type. 6245 running_var (Tensor): The shape :math:`(C,)`, with bfloat, float16 or float32 data type. 6246 weight (Tensor, optional): The shape :math:`(C,)`, with bfloat, float16 or float32 data type, Default: ``None``. 6247 Initialized to ``1`` when `weight` is None. 6248 bias (Tensor, optional): The shape :math:`(C,)`, with bfloat, float16 or float32 data type. Default: ``None``. 6249 Initialized to ``0`` when `weight` is None. 6250 training (bool, optional): If `training` is `True`, `mean` and `variance` are computed during training. 6251 If `training` is `False`, they're loaded from checkpoint during inference. Default: ``False`` . 6252 momentum (float, optional): The hyper parameter to compute moving average for `running_mean` and `running_var` 6253 (e.g. :math:`new\_running\_mean = (1 - momentum) * running\_mean + momentum * current\_mean`). 6254 Default: ``0.1`` . 6255 eps (float, optional): A small value added for numerical stability. Default: ``1e-5``. 6256 6257 Returns: 6258 Tensor, has the same type and shape as `input`. The shape is :math:`(N, C, *)`. 6259 6260 Raises: 6261 TypeError: If `training` is not a bool. 6262 TypeError: If dtype of `eps` or `momentum` is not float. 6263 TypeError: If `input`, `weight`, `bias`, `running_mean` or `running_var` is not a Tensor. 6264 6265 Supported Platforms: 6266 ``Ascend`` 6267 6268 Examples: 6269 >>> import mindspore 6270 >>> from mindspore import Tensor, ops 6271 >>> input_x = Tensor([[1.0, 2.0], [3.0, 4.0]], mindspore.float32) 6272 >>> running_mean = Tensor([0.5, 1.5], mindspore.float32) 6273 >>> running_var = Tensor([0.1, 0.2], mindspore.float32) 6274 >>> weight = Tensor([2.0, 2.0], mindspore.float32) 6275 >>> bias = Tensor([-1.0, -1.0], mindspore.float32) 6276 >>> output = ops.function.nn_func.batch_norm_ext(input_x, running_mean, running_var, weight, bias) 6277 >>> print(output) 6278 [[ 2.1621194 1.2360122] 6279 [14.810596 10.180061 ]] 6280 """ 6281 if weight is None: 6282 weight = ops.ones([input.shape[1]], dtype=input.dtype) 6283 if bias is None: 6284 bias = ops.zeros([input.shape[1]], dtype=input.dtype) 6285 output = batch_norm_ext_op(input, weight, bias, running_mean, running_var, training, momentum, eps) 6286 return output[0] 6287 6288def batch_norm(input_x, running_mean, running_var, weight, bias, training=False, momentum=0.1, eps=1e-5): 6289 r""" 6290 Batch Normalization for input data and updated parameters. 6291 6292 Batch Normalization is widely used in convolutional neural networks. This operation 6293 applies Batch Normalization over inputs to avoid internal covariate shift as described 6294 in the paper `Batch Normalization: Accelerating Deep Network Training by Reducing Internal 6295 Covariate Shift <https://arxiv.org/abs/1502.03167>`_. It rescales and recenters the 6296 features using a mini-batch of data and the learned parameters can be described 6297 in the following formula, 6298 6299 .. math:: 6300 6301 y = \frac{x - mean}{\sqrt{variance + \epsilon}} * \gamma + \beta 6302 6303 where :math:`\gamma` is `weight`, :math:`\beta` is `bias`, :math:`\epsilon` is `eps`, :math:`mean` is the 6304 mean of :math:`x`, :math:`variance` is the variance of :math:`x`. 6305 6306 .. warning:: 6307 - For Atlas 200/300/500 inference product, 6308 the result accuracy fails to reach 1‰ due to the square root instruction. 6309 6310 Note: 6311 - If `training` is `False`, `weight`, `bias`, `running_mean` and `running_var` are Tensors. 6312 - If `training` is `True`, `weight`, `bias`, `running_mean` and `running_var` are Parameters. 6313 6314 Args: 6315 input_x (Tensor): Tensor of shape :math:`(N, C)`, with float16 or float32 data type. 6316 running_mean (Union[Tensor, Parameter]): The shape :math:`(C,)`, has the same data type with `weight`. 6317 running_var (Union[Tensor, Parameter]): The shape :math:`(C,)`, has the same data type with `weight`. 6318 weight (Union[Tensor, Parameter]): The shape :math:`(C,)`, with float16 or float32 data type. 6319 bias (Union[Tensor, Parameter]): The shape :math:`(C,)`, has the same data type with `weight`. 6320 training (bool, optional): If `training` is `True`, `mean` and `variance` are computed during training. 6321 If `training` is `False`, they're loaded from checkpoint during inference. Default: ``False`` . 6322 momentum (float, optional): The hyper parameter to compute moving average for `running_mean` and `running_var` 6323 (e.g. :math:`new\_running\_mean = (1 - momentum) * running\_mean + momentum * current\_mean`). 6324 Momentum value must be `[0, 1]`. Default: ``0.1`` . 6325 eps (float, optional): A small value added for numerical stability. Default: ``1e-5``, value must be `(0, 1]` . 6326 6327 Returns: 6328 output_x (Tensor) - The same type and shape as the `input_x`. The shape is :math:`(N, C)`. 6329 6330 Raises: 6331 TypeError: If `training` is not a bool. 6332 TypeError: If dtype of `eps` or `momentum` is not float. 6333 TypeError: If `input_x`, `weight`, `bias`, `running_mean` or `running_var` is not a Tensor. 6334 TypeError: If dtype of `input_x`, `weight` is neither float16 nor float32. 6335 6336 Supported Platforms: 6337 ``Ascend`` ``GPU`` ``CPU`` 6338 6339 Examples: 6340 >>> import mindspore 6341 >>> from mindspore import Tensor, ops 6342 >>> input_x = Tensor([[1.0, 2.0], [3.0, 4.0]], mindspore.float32) 6343 >>> running_mean = Tensor([0.5, 1.5], mindspore.float32) 6344 >>> running_var = Tensor([0.1, 0.2], mindspore.float32) 6345 >>> weight = Tensor([2.0, 2.0], mindspore.float32) 6346 >>> bias = Tensor([-1.0, -1.0], mindspore.float32) 6347 >>> output = ops.batch_norm(input_x, running_mean, running_var, weight, bias) 6348 >>> print(output) 6349 [[ 2.1621194 1.2360122] 6350 [14.810596 10.180061 ]] 6351 """ 6352 batch_norm_op = _get_cache_prim(P.BatchNorm)(is_training=training, epsilon=eps, momentum=momentum) 6353 output = batch_norm_op(input_x, weight, bias, running_mean, running_var) 6354 return output[0] 6355 6356 6357def bias_add(input_x, bias): 6358 r""" 6359 Returns the sum of the `input_x` and the `bias` Tensor. Before adding, the `bias` Tensor will be broadcasted to be 6360 consistent with the shape of the `input_x` Tensor. 6361 6362 Args: 6363 input_x (Tensor): The input tensor. The shape can be 2-5 dimensions. Supported dtypes: 6364 6365 - Ascend/CPU: all Number type. 6366 - GPU: float16, float32, int8. 6367 6368 bias (Tensor): The bias tensor, with shape :math:`(C)`. C must be the same as channel dimension C of 6369 `input_x`. It has the same type as `input_x`. 6370 6371 Returns: 6372 Tensor, with the same shape and data type as `input_x`. 6373 6374 Raises: 6375 TypeError: If `input_x` or `bias` is not a Tensor. 6376 TypeError: If dtype of `input_x` and `bias` is inconsistent. 6377 TypeError: If dimension of `input_x` is not in the range [2, 5]. 6378 6379 Supported Platforms: 6380 ``Ascend`` ``GPU`` ``CPU`` 6381 6382 Examples: 6383 >>> import mindspore 6384 >>> import numpy as np 6385 >>> from mindspore import Tensor, ops 6386 >>> input_x = Tensor(np.arange(6).reshape((2, 3)), mindspore.float32) 6387 >>> bias = Tensor(np.random.random(3).reshape((3)), mindspore.float32) 6388 >>> output = ops.bias_add(input_x, bias) 6389 >>> print(output.shape) 6390 (2, 3) 6391 """ 6392 bias_add_op = _get_cache_prim(P.BiasAdd)(data_format="NCHW") 6393 return bias_add_op(input_x, bias) 6394 6395 6396def binary_cross_entropy(logits, labels, weight=None, reduction='mean'): 6397 r""" 6398 Computes the binary cross entropy(Measure the difference information between two probability distributions) between 6399 predictive value `logits` and target value `labels`. 6400 6401 Set `logits` as :math:`x`, `labels` as :math:`y`, output as :math:`\ell(x, y)`, the 6402 weight of nth batch of binary cross entropy is :math:`w_n`. 6403 Let, 6404 6405 .. math:: 6406 L = \{l_1,\dots,l_N\}^\top, \quad 6407 l_n = - w_n \left[ y_n \cdot \log x_n + (1 - y_n) \cdot \log (1 - x_n) \right] 6408 6409 In which, :math:`L` indicates the loss of all `batch_size`, :math:`l` indicates the loss of one `batch_size`, 6410 and :math:`n` indicates one `batch_size` in the :math:`1-N` range. Then, 6411 6412 .. math:: 6413 \ell(x, y) = \begin{cases} 6414 L, & \text{if reduction} = \text{'none';}\\ 6415 \operatorname{mean}(L), & \text{if reduction} = \text{'mean';}\\ 6416 \operatorname{sum}(L), & \text{if reduction} = \text{'sum'.} 6417 \end{cases} 6418 6419 .. warning:: 6420 - The value of `logits` must range from `0` to `l`. 6421 6422 Args: 6423 logits (Tensor): The predictive value whose data type must be float16 or float32. 6424 labels (Tensor): The target value which has the same shape and data type as `logits`. 6425 And the data type is float16 or float32. 6426 weight (Tensor, optional): A rescaling weight applied to the loss of each batch element. 6427 Its shape must be able to broadcast to that of `logits` and `labels`. 6428 And it must have the same shape and data type as `logits`. Default: ``None`` . If set to ``None`` , 6429 the loss function 6430 will not consider any sample weights, and each sample will be treated as having equal importance 6431 when calculating the loss. 6432 reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` , 6433 ``'sum'`` . Default: ``'mean'`` . 6434 6435 - ``'none'``: no reduction will be applied. 6436 - ``'mean'``: compute and return the weighted mean of elements in the output. 6437 - ``'sum'``: the output elements will be summed. 6438 6439 Returns: 6440 Tensor or Scalar. Returns Tensor that has the same dtype and shape as `logits` if `reduction` is 'none'. 6441 Otherwise, returns a scalar Tensor. 6442 6443 Raises: 6444 TypeError: If `logits`, `labels` or `weight` is not a Tensor. 6445 TypeError: If dtype of `logits`, `labels` or `weight` (if given) is neither float16 nor float32. 6446 ValueError: If `reduction` is not one of ``'none'``, ``'mean'`` or ``'sum'``. 6447 ValueError: If shape of `labels` is not the same as `logits` or `weight` (if given). 6448 6449 Supported Platforms: 6450 ``Ascend`` ``GPU`` ``CPU`` 6451 6452 Examples: 6453 >>> import mindspore 6454 >>> import numpy as np 6455 >>> from mindspore import Tensor, ops 6456 >>> logits = Tensor(np.array([0.2, 0.7, 0.1]), mindspore.float32) 6457 >>> labels = Tensor(np.array([0., 1., 0.]), mindspore.float32) 6458 >>> weight = Tensor(np.array([1, 2, 2]), mindspore.float32) 6459 >>> output = ops.binary_cross_entropy(logits, labels, weight) 6460 >>> print(output) 6461 0.38240486 6462 """ 6463 binary_cross_entropy_op = _get_cache_prim(P.BinaryCrossEntropy)(reduction=reduction) 6464 return binary_cross_entropy_op(logits, labels, weight) 6465 6466 6467def conv3d(input, weight, bias=None, stride=1, pad_mode="valid", padding=0, dilation=1, groups=1): 6468 r""" 6469 Applies a 3D convolution over an input tensor. The input tensor is typically of 6470 shape :math:`(N, C_{in}, D_{in}, H_{in}, W_{in})`, where :math:`N` is batch size, :math:`C` 6471 is channel number, :math:`D, H, W` are the depth, height and width of the feature graph, respectively. 6472 6473 The output is calculated based on formula: 6474 6475 .. math:: 6476 6477 \text{out}(N_i, C_{\text{out}_j}) = \text{bias}(C_{\text{out}_j}) + 6478 \sum_{k = 0}^{C_{in} - 1} \text{ccor}({\text{weight}(C_{\text{out}_j}, k), \text{X}(N_i, k)}) 6479 6480 where :math:`bias` is the output channel bias, :math:`ccor` is 6481 the `cross-correlation <https://en.wikipedia.org/wiki/Cross-correlation>`_ 6482 , :math:`weight` is the convolution kernel value and :math:`X` represents the input feature map. 6483 6484 Here are the indices' meanings: 6485 6486 - :math:`i` corresponds to the batch number, the range is :math:`[0, N-1]`, 6487 where :math:`N` is the batch size of the input. 6488 6489 - :math:`j` corresponds to the output channel, the range is :math:`[0, C_{out}-1]`, 6490 where :math:`C_{out}` is the number of 6491 output channels, which is also equal to the number of kernels. 6492 6493 - :math:`k` corresponds to the input channel, the range is :math:`[0, C_{in}-1]`, 6494 where :math:`C_{in}` is the number of 6495 input channels, which is also equal to the number of channels in the convolutional kernels. 6496 6497 Therefore, in the above formula, :math:`{bias}(C_{\text{out}_j})` represents the bias of the :math:`j`-th 6498 output channel, :math:`{weight}(C_{\text{out}_j}, k)` represents the slice of the :math:`j`-th convolutional 6499 kernel in the :math:`k`-th channel, and :math:`{X}(N_i, k)` represents the slice of the :math:`k`-th input 6500 channel in the :math:`i`-th batch of the input feature map. 6501 6502 The shape of the convolutional kernel is given by 6503 :math:`(\text{kernel_size[0]}, \text{kernel_size[1]}, \text{kernel_size[2]})` 6504 where :math:`\text{kernel_size[0]}` , :math:`\text{kernel_size[1]}` and :math:`\text{kernel_size[2]}` are the depth, 6505 height and width of the kernel, respectively. 6506 If we consider the input and output channels as well as the `group` parameter, the complete kernel shape 6507 will be :math:`(C_{out}, C_{in} / \text{group}, \text{kernel_size[0]}, 6508 \text{kernel_size[1]}, \text{kernel_size[2]})`, 6509 where `group` is the number of groups dividing `x`'s input channel when applying group convolution. 6510 6511 For more details about convolution layer, please refer to `Gradient Based Learning Applied to Document Recognition 6512 <http://vision.stanford.edu/cs598_spring07/papers/Lecun98.pdf>`_. 6513 6514 Note: 6515 1. On Ascend platform, :math:`groups = 1` must be satisfied. 6516 2. On Ascend platform, :math:`dilation=1` must be satisfied. 6517 6518 Args: 6519 input (Tensor): Tensor of shape :math:`(N, C_{in}, D_{in}, H_{in}, W_{in})`. 6520 weight (Tensor): Set size of kernel is :math:`(\text{kernel_size[0]}, \text{kernel_size[1]}, 6521 \text{kernel_size[2]})`, then the shape is :math:`(C_{out}, C_{in}, \text{kernel_size[0]}, 6522 \text{kernel_size[1]}, \text{kernel_size[1]})`. 6523 bias (Tensor, optional): Bias Tensor with shape :math:`(C_{out})`. 6524 When bias is None, zeros will be used. Default: ``None`` . 6525 stride (Union[int, tuple[int]], optional): The distance of kernel moving, 6526 it can be an int number that represents 6527 the depth, height and width of movement or a tuple of three int numbers that 6528 represent depth, height and width movement respectively. Default: ``1`` . 6529 pad_mode (str, optional): Specifies padding mode. The optional values are 6530 ``"same"`` , ``"valid"`` and ``"pad"`` . Default: ``"valid"`` . 6531 6532 - ``"same"``: Adopts the way of completion. The depth, height and width of the output will be equal to 6533 the input `x` divided by stride. The padding will be evenly calculated in head and tail, top and bottom, 6534 left and right directions possiblily. 6535 Otherwise, the last extra padding will be calculated from the tail, bottom and the right side. 6536 If this mode is set, `pad` must be 0. 6537 6538 - ``"valid"``: Adopts the way of discarding. The possible largest depth, height and width of output 6539 will be returned without padding. Extra pixels will be discarded. If this mode is set, `pad` 6540 must be 0. 6541 6542 - ``"pad"``: Implicit paddings on both sides of the input in depth, height and width. 6543 The number of `pad` will be padded to the input Tensor borders. `pad` must be greater than or equal to 0. 6544 6545 padding (Union[int, tuple[int], list[int]], optional): The pad value to be filled. If `pad` is an integer, 6546 the paddings of head, tail, top, bottom, left and right are the same, equal to pad. 6547 If `pad` is a tuple/list of 3 integers, the padding of head, tail, top, bottom, 6548 left and right equal to pad[0], pad[0], pad[1], pad[1], pad[2] and pad[2] correspondingly. Default: ``0`` . 6549 dilation (Union[int, tuple[int]], optional): The data type is int or a tuple of 3 integers 6550 :math:`(dilation_d, dilation_h, dilation_w)`. Currently, dilation on depth only supports the case of 1 6551 on Ascend backend. Specifies the dilation rate to use for dilated convolution. If set :math:`k > 1`, 6552 there will be :math:`k - 1` pixels skipped for each sampling location. 6553 The value ranges for the depth, height, and width dimensions are [1, D], [1, H], and [1, W], 6554 respectively. Default: ``1`` . 6555 groups (int, optional):The number of groups into which the filter is divided. Default: ``1`` . 6556 6557 Returns: 6558 Tensor, the value that applied 3D convolution. The shape is :math:`(N, C_{out}, D_{out}, H_{out}, W_{out})`. 6559 6560 `pad_mode` is ``"same"``: 6561 6562 .. math:: 6563 \begin{array}{ll} \\ 6564 D_{out} = \left \lceil{\frac{D_{in}}{\text{stride[0]}}} \right \rceil \\ 6565 H_{out} = \left \lceil{\frac{H_{in}}{\text{stride[1]}}} \right \rceil \\ 6566 W_{out} = \left \lceil{\frac{W_{in}}{\text{stride[2]}}} \right \rceil \\ 6567 \end{array} 6568 6569 `pad_mode` is ``"valid"``: 6570 6571 .. math:: 6572 \begin{array}{ll} \\ 6573 D_{out} = \left \lfloor{\frac{D_{in} - \text{dilation[0]} \times (\text{kernel_size[0]} - 1) } 6574 {\text{stride[0]}} + 1} \right \rfloor \\ 6575 H_{out} = \left \lfloor{\frac{H_{in} - \text{dilation[1]} \times (\text{kernel_size[1]} - 1) } 6576 {\text{stride[1]}} + 1} \right \rfloor \\ 6577 W_{out} = \left \lfloor{\frac{W_{in} - \text{dilation[2]} \times (\text{kernel_size[2]} - 1) } 6578 {\text{stride[2]}} + 1} \right \rfloor \\ 6579 \end{array} 6580 6581 `pad_mode` is ``"pad"``: 6582 6583 .. math:: 6584 \begin{array}{ll} \\ 6585 D_{out} = \left \lfloor{\frac{D_{in} + padding[0] + padding[1] - (\text{dilation[0]} - 1) \times 6586 \text{kernel_size[0]} - 1 }{\text{stride[0]}} + 1} \right \rfloor \\ 6587 H_{out} = \left \lfloor{\frac{H_{in} + padding[2] + padding[3] - (\text{dilation[1]} - 1) \times 6588 \text{kernel_size[1]} - 1 }{\text{stride[1]}} + 1} \right \rfloor \\ 6589 W_{out} = \left \lfloor{\frac{W_{in} + padding[4] + padding[5] - (\text{dilation[2]} - 1) \times 6590 \text{kernel_size[2]} - 1 }{\text{stride[2]}} + 1} \right \rfloor \\ 6591 \end{array} 6592 6593 Raises: 6594 TypeError: If `out_channel` or `groups` is not an int. 6595 TypeError: If `stride`, `padding` or `dilation` is neither an int nor a tuple. 6596 TypeError: If `bias` is not a Tensor. 6597 ValueError: If the shape of `bias` is not :math:`(C_{out})`. 6598 ValueError: If `stride` or `dilation` is less than 1. 6599 ValueError: If `pad_mode` is not one of 'same', 'valid' or 'pad'. 6600 ValueError: If `padding` is a tuple or list whose length is not equal to 3. 6601 ValueError: If `pad_mode` is not equal to 'pad' and `pad` is greater than 0. 6602 6603 Supported Platforms: 6604 ``Ascend`` ``GPU`` 6605 6606 Examples: 6607 >>> import mindspore 6608 >>> import numpy as np 6609 >>> from mindspore import Tensor, ops 6610 >>> x = Tensor(np.ones([16, 3, 10, 32, 32]), mindspore.float16) 6611 >>> weight = Tensor(np.ones([32, 3, 4, 3, 3]), mindspore.float16) 6612 >>> output = ops.conv3d(x, weight, pad_mode="same", padding=0, stride=1, dilation=1, groups=1) 6613 >>> print(output.shape) 6614 (16, 32, 10, 32, 32) 6615 >>> output = ops.conv3d(x, weight, pad_mode="valid", padding=0, stride=1, dilation=1, groups=1) 6616 >>> print(output.shape) 6617 (16, 32, 7, 30, 30) 6618 >>> output = ops.conv3d(x, weight, pad_mode="pad", padding=(2, 1, 1), stride=1, dilation=1, groups=1) 6619 >>> print(output.shape) 6620 (16, 32, 11, 32, 32) 6621 """ 6622 weight_shape = weight.shape 6623 out_channel = weight_shape[0] 6624 kernel_size = weight_shape[2:5] 6625 if isinstance(stride, (tuple, list)): 6626 _check_conv_iterable_lengths(stride, dim=3, iter_name='stride') 6627 if isinstance(dilation, (tuple, list)): 6628 _check_conv_iterable_lengths(dilation, dim=3, iter_name='dilation') 6629 input_shape = input.shape 6630 in_channel = input_shape[1] 6631 if not (in_channel % groups == 0 and out_channel % groups == 0): 6632 raise ValueError("The argument 'groups' should be divisible by 'in_channel' " \ 6633 "and 'out_channel'") 6634 if isinstance(padding, (list, tuple)): 6635 padding = _manipulate_padding(padding, dim=3) 6636 conv = _get_cache_prim(P.Conv3D)(out_channel, kernel_size, 1, pad_mode, padding, stride, dilation, groups, "NCDHW") 6637 if bias is None: 6638 return conv(input, weight) 6639 if not isinstance(bias, Tensor): 6640 raise TypeError(f"For 'conv3d', the 'bias' must be a Tensor, but got {type(bias)}.") 6641 conv_result = conv(input, weight) 6642 output = bias_add(conv_result, bias) 6643 return output 6644 6645 6646@_primexpr 6647def _check_positive_int(arg_value, arg_name=None, prim_name=None): 6648 validator.check_positive_int(arg_value, arg_name=arg_name, prim_name=prim_name) 6649 6650 6651@_primexpr 6652def _check_pxiel_shuffle_valid(num, factor): 6653 if num % factor ** 2 != 0: 6654 raise ValueError("For 'pixel_shuffle', the length of third to last dimension is not divisible" 6655 "by `upscale_factor` squared.") 6656 6657 6658def _check_pixel_shuffle_unshuffle_input_shape(input, cls_name): 6659 """Internal function, used to check whether the shape of pixel shuffle or unshuffle input meets the requirements.""" 6660 if input.ndim < 3: 6661 raise ValueError(f"For {cls_name}, the dimension of `input` should be larger than 2, but got {input.ndim}.") 6662 6663 6664def pixel_shuffle(input, upscale_factor): 6665 r""" 6666 Applies the PixelShuffle operation over input `input` which implements sub-pixel convolutions 6667 with stride :math:`1/r` . For more details, refer to 6668 `Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network 6669 <https://arxiv.org/abs/1609.05158>`_ . 6670 6671 Typically, the `input` is of shape :math:`(*, C \times r^2, H, W)` , and the output is of shape 6672 :math:`(*, C, H \times r, W \times r)`, where `r` is an upscale factor and `*` is zero or more batch dimensions. 6673 6674 Args: 6675 input (Tensor): Tensor of shape :math:`(*, C \times r^2, H, W)` . The dimension of `input` is larger than 2, 6676 and the length of third to last dimension can be divisible by `upscale_factor` squared. 6677 upscale_factor (int): factor to shuffle the input Tensor, and is a positive integer. 6678 `upscale_factor` is the above-mentioned :math:`r`. 6679 6680 Returns: 6681 - **output** (Tensor) - Tensor of shape :math:`(*, C, H \times r, W \times r)` . 6682 6683 Raises: 6684 ValueError: If `upscale_factor` is not a positive integer. 6685 ValueError: If the length of third to last dimension is not divisible by `upscale_factor` squared. 6686 ValueError: If the dimension of `input` is less than 3. 6687 TypeError: If `input` is not a Tensor. 6688 6689 Supported Platforms: 6690 ``Ascend`` ``GPU`` ``CPU`` 6691 6692 Examples: 6693 >>> import mindspore 6694 >>> import numpy as np 6695 >>> from mindspore import ops 6696 >>> input_x = np.arange(3 * 2 * 9 * 4 * 4).reshape((3, 2, 9, 4, 4)) 6697 >>> input_x = mindspore.Tensor(input_x, mindspore.dtype.int32) 6698 >>> output = ops.pixel_shuffle(input_x, 3) 6699 >>> print(output.shape) 6700 (3, 2, 1, 12, 12) 6701 """ 6702 _check_positive_int(upscale_factor, "upscale_factor") 6703 _check_is_tensor("input", input, "pixel_shuffle") 6704 _check_pixel_shuffle_unshuffle_input_shape(input, "pixel_shuffle") 6705 idx = shape_(input) 6706 length = input.ndim 6707 pre = idx[:-3] 6708 c, h, w = idx[-3:] 6709 _check_pxiel_shuffle_valid(c, upscale_factor) 6710 c = c // upscale_factor ** 2 6711 input_perm = (pre + (c, upscale_factor, upscale_factor, h, w)) 6712 input = reshape_(input, input_perm) 6713 input_perm = [i for i in range(length - 2)] 6714 input_perm = input_perm + [length, length - 2, length + 1, length - 1] 6715 input_perm = tuple(input_perm) 6716 input = transpose_(input, input_perm) 6717 input = reshape_(input, (pre + (c, upscale_factor * h, upscale_factor * w))) 6718 return input 6719 6720 6721@_primexpr 6722def _check_pxiel_unshuffle_valid(num1, num2, factor): 6723 if num1 % factor != 0 or num2 % factor != 0: 6724 raise ValueError("For 'pixel_unshuffle', the length of second to last 2 dimension should be divisible " 6725 "by downscale_factor.") 6726 6727 6728def pixel_unshuffle(input, downscale_factor): 6729 r""" 6730 Applies the PixelUnshuffle operation over input `input` which is the inverse of PixelShuffle. For more details, 6731 refer to `Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural 6732 Network <https://arxiv.org/abs/1609.05158>`_ . 6733 6734 Typically, the input is of shape :math:`(*, C, H \times r, W \times r)` , and the output is of shape 6735 :math:`(*, C \times r^2, H, W)` , where `r` is a downscale factor and `*` is zero or more batch dimensions. 6736 6737 Args: 6738 input (Tensor): Tensor of shape :math:`(*, C, H \times r, W \times r)` . The dimension of `input` is larger than 6739 2, and the length of second to last dimension or last dimension can be divisible by `downscale_factor` . 6740 downscale_factor (int): factor to unshuffle the input Tensor, and is a positive integer. 6741 `downscale_factor` is the above-mentioned :math:`r`. 6742 6743 Returns: 6744 - **output** (Tensor) - Tensor of shape :math:`(*, C \times r^2, H, W)` . 6745 6746 Raises: 6747 ValueError: If `downscale_factor` is not a positive integer. 6748 ValueError: If the length of second to last dimension or last dimension is not divisible by `downscale_factor` . 6749 ValueError: If the dimension of `input` is less than 3. 6750 TypeError: If `input` is not a Tensor. 6751 6752 Supported Platforms: 6753 ``Ascend`` ``GPU`` ``CPU`` 6754 6755 Examples: 6756 >>> import mindspore 6757 >>> import numpy as np 6758 >>> from mindspore import Tensor, ops 6759 >>> input_x = np.arange(8 * 8).reshape((1, 1, 8, 8)) 6760 >>> input_x = mindspore.Tensor(input_x, mindspore.dtype.int32) 6761 >>> output = ops.pixel_unshuffle(input_x, 2) 6762 >>> print(output.shape) 6763 (1, 4, 4, 4) 6764 """ 6765 _check_positive_int(downscale_factor, "downscale_factor") 6766 _check_is_tensor("input", input, "pixel_unshuffle") 6767 _check_pixel_shuffle_unshuffle_input_shape(input, "pixel_unshuffle") 6768 idx = shape_(input) 6769 length = input.ndim 6770 pre = idx[:-3] 6771 c, h, w = idx[-3:] 6772 _check_pxiel_unshuffle_valid(h, w, downscale_factor) 6773 h = h // downscale_factor 6774 w = w // downscale_factor 6775 input_perm = (pre + (c, h, downscale_factor, w, downscale_factor)) 6776 input = reshape_(input, input_perm) 6777 input_perm = [i for i in range(length - 2)] 6778 input_perm = input_perm + [length - 1, length + 1, length - 2, length] 6779 input_perm = tuple(input_perm) 6780 input = transpose_(input, input_perm) 6781 input = reshape_(input, (pre + (c * downscale_factor * downscale_factor, h, w))) 6782 return input 6783 6784 6785def glu(x, axis=-1): 6786 r""" 6787 Computes GLU (Gated Linear Unit activation function) of input tensors. 6788 6789 .. math:: 6790 {GLU}(a, b)= a \otimes \sigma(b) 6791 6792 where :math:`a` is the first half of the input matrices and :math:`b` is the second half. 6793 6794 Here :math:`\sigma` is the sigmoid function, and :math:`\otimes` is the Hadamard product. 6795 See `Language Modeling with Gated Convluational Networks <https://arxiv.org/abs/1612.08083>`_. 6796 6797 Args: 6798 x (Tensor): Tensor to be split. Its dtype is Number, and shape is :math:`(\ast_1, N, \ast_2)` 6799 where `*` means, any number of additional dimensions. 6800 axis (int, optional): the axis to split the input. It must be int. Default: ``-1`` , the last axis of `x`. 6801 6802 Returns: 6803 Tensor, the same dtype as the `x`, with the shape :math:`(\ast_1, M, \ast_2)` where :math:`M=N/2`. 6804 6805 Raises: 6806 TypeError: If dtype of `x` is not Number. 6807 TypeError: If `x` is not a Tensor. 6808 6809 Supported Platforms: 6810 ``Ascend`` ``GPU`` ``CPU`` 6811 6812 Examples: 6813 >>> from mindspore import Tensor, ops 6814 >>> input = Tensor([[0.1,0.2,0.3,0.4],[0.5,0.6,0.7,0.8]]) 6815 >>> output = ops.glu(input) 6816 >>> print(output) 6817 [[0.05744425 0.11973753] 6818 [0.33409387 0.41398472]] 6819 """ 6820 spilt = _get_cache_prim(P.Split)(axis=axis, output_num=2) 6821 x, y = spilt(x) 6822 y = sigmoid_(y) 6823 return x * y 6824 6825 6826def multi_margin_loss(input, target, p=1, margin=1, weight=None, reduction='mean'): 6827 r""" 6828 Hinge loss for optimizing a multi-class classification. 6829 6830 Optimizes a multi-class classification hinge 6831 loss (margin-based loss) between input and 6832 output. 6833 6834 For each mini-batch sample, the loss in terms of the 1D input :math:`x` and scalar output :math:`y` is: 6835 6836 .. math:: 6837 \text{loss}(x, y) = \frac{\sum_i \max(0, \text{margin} - x[y] + x[i])^p}{\text{x.size}(0)} 6838 6839 where :math:`i\in \{0,⋯,x.size(0)-1\}` and :math:`i \ne y`. 6840 6841 Args: 6842 input (Tensor): Input , with shape :math:`(N, C)`. Data type only support float32, float16 or float64. 6843 It is :math:`x` in the above formula. 6844 target (Tensor): Ground truth labels, with shape :math:`(N,)`. Data type only support int64. The 6845 value of target should be non-negative, less than C. It is :math:`y` in the above formula. 6846 p (int, optional): The norm degree for pairwise distance. Should be 1 or 2. Default: ``1`` . 6847 margin (int, optional): A parameter to change pairwise distance. Default: ``1`` . 6848 weight (Tensor, optional): The rescaling weight to each class with shape :math:`(C,)`. Data type only 6849 support float16, float32 or float64. Default: ``None`` . 6850 reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` , 6851 ``'sum'`` . Default: ``'mean'`` . 6852 6853 - ``'none'``: no reduction will be applied. 6854 - ``'mean'``: compute and return the weighted mean of elements in the output. 6855 - ``'sum'``: the output elements will be summed. 6856 6857 Returns: 6858 - **outputs** - Tensor. If `reduction` is ``'none'``, returns a Tensor with the same shape as `target`. 6859 Otherwise, it is a scalar. 6860 6861 Raises: 6862 TypeError: If dtype of `p` or `target` is not int. 6863 TypeError: If dtype of `margin` is not int. 6864 TypeError: If dtype of `reduction` is not str. 6865 TypeError: If dtype of `input` is not float16, float or float64. 6866 TypeError: If dtype of `weight` and `input` is not the same. 6867 ValueError: If `p` is not 1 or 2. 6868 ValueError: If `reduction` is not one of {'none','sum','mean'}. 6869 ValueError: If shape[0] of `input` is not equal to shape[0] of `target`. 6870 ValueError: If shape[1] of `input` is not equal to shape[0] of `weight`. 6871 ValueError: If rank of `weight` is not 1 or rank of `target` is not 1 or `input` is not 2. 6872 6873 Supported Platforms: 6874 ``Ascend`` ``GPU`` ``CPU`` 6875 6876 Examples: 6877 >>> import mindspore 6878 >>> import numpy as np 6879 >>> from mindspore import Tensor, ops 6880 >>> inputs = Tensor(np.ones(shape=[3, 3]), mindspore.float32) 6881 >>> target = Tensor(np.array([1, 2, 1]), mindspore.int64) 6882 >>> weight = Tensor(np.array([1, 1, 1]), mindspore.float32) 6883 >>> output = ops.multi_margin_loss(inputs, target, weight=weight) 6884 >>> print(output) 6885 0.6666667 6886 """ 6887 6888 if not isinstance(margin, int): 6889 raise TypeError(f"For 'multi_margin_loss', the type of 'margin' must be int, but got {type(margin)}.") 6890 margin_ = float(margin) 6891 loss = _get_cache_prim(P.MultiMarginLoss)(p, margin_, reduction) 6892 outputs = loss(input, target, weight) 6893 return outputs 6894 6895 6896def multilabel_margin_loss(input, target, reduction='mean'): 6897 r""" 6898 Hinge loss for optimizing a multi-label classification. 6899 6900 Creates a criterion that optimizes a multi-label multi-classification 6901 hinge loss (margin-based loss) between input :math:`x` (a 2D mini-batch `Tensor`) 6902 and output :math:`y` (which is a 2D `Tensor` of target class indices). 6903 For each sample in the mini-batch: 6904 6905 .. math:: 6906 \text{loss}(x, y) = \sum_{ij}\frac{\max(0, 1 - (x[y[j]] - x[i]))}{\text{x.size}(0)} 6907 6908 where :math:`x \in \left\{0, \; \cdots , \; \text{x.size}(0) - 1\right\}`, \ 6909 :math:`y \in \left\{0, \; \cdots , \; \text{y.size}(0) - 1\right\}`, \ 6910 :math:`0 \leq y[j] \leq \text{x.size}(0)-1`, \ 6911 and :math:`i \neq y[j]` for all :math:`i` and :math:`j`. 6912 :math:`y` and :math:`x` must have the same size. 6913 The criterion only considers a contiguous block of non-negative targets that 6914 starts at the front. 6915 This allows for different samples to have variable amounts of target classes. 6916 6917 Args: 6918 input (Tensor): Predict data, :math:`x` in the formula above. Tensor of shape :math:`(C)` 6919 or :math:`(N, C)`, where :math:`N` is the batch size and :math:`C` is the number of classes. 6920 Data type must be float16 or float32. 6921 target (Tensor): Ground truth data, :math:`y` in the formula above, with the same shape as `input`, 6922 data type must be int32 and label targets padded by -1. 6923 reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` , 6924 ``'sum'`` . Default: ``'mean'`` . 6925 6926 - ``'none'``: no reduction will be applied. 6927 - ``'mean'``: compute and return the mean of elements in the output. 6928 - ``'sum'``: the output elements will be summed. 6929 6930 Returns: 6931 - **outputs** (Union[Tensor, Scalar]) - The loss of MultilabelMarginLoss. 6932 If `reduction` is ``"none"``, its shape is :math:`(N)`. 6933 Otherwise, a scalar value will be returned. 6934 6935 Raises: 6936 TypeError: If `input` or `target` is not a Tensor. 6937 TypeError: If dtype of `input` is neither float16 nor float32. 6938 TypeError: If dtype of `target` is not int32. 6939 ValueError: If length of shape of `input` is neither 1 nor 2. 6940 ValueError: If shape of `input` is not the same as `target`. 6941 ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``. 6942 6943 Supported Platforms: 6944 ``Ascend`` ``GPU`` 6945 6946 Examples: 6947 >>> import mindspore 6948 >>> import numpy as np 6949 >>> from mindspore import Tensor, ops 6950 >>> inputs = Tensor(np.array([[0.1, 0.2, 0.4, 0.8], [0.2, 0.3, 0.5, 0.7]]), mindspore.float32) 6951 >>> target = Tensor(np.array([[1, 2, 0, 3], [2, 3, -1, 1]]), mindspore.int32) 6952 >>> output = ops.multilabel_margin_loss(inputs, target) 6953 >>> print(output) 6954 0.325 6955 """ 6956 6957 loss = _get_cache_prim(P.MultilabelMarginLoss)(reduction) 6958 outputs, _ = loss(input, target) 6959 return outputs 6960 6961 6962def multilabel_soft_margin_loss(input, target, weight=None, reduction='mean'): 6963 r""" 6964 Calculates the MultiLabelSoftMarginLoss. 6965 The multi-label soft margin loss is a commonly used loss function in multi-label classification tasks 6966 where an input sample can belong to multiple classes. 6967 Given an input :math:`input` and binary labels :math:`output` of size :math:`(N,C)`, 6968 where :math:`N` denotes the number of samples 6969 and :math:`C` denotes the number of classes. 6970 6971 .. math:: 6972 \mathcal{loss\left( input , output \right)} = - \frac{1}{N}\frac{1}{C}\sum_{i = 1}^{N} 6973 \sum_{j = 1}^{C}\left(output_{ij}\log\frac{1}{1 + e^{- input_{ij}}} + \left( 1 - output_{ij} 6974 \right)\log\frac{e^{-input_{ij}}}{1 + e^{-input_{ij}}} \right) 6975 6976 where :math:`input_{ij}` represents the predicted score of sample :math:`i` for class :math:`j`. 6977 :math:`output_{ij}` represents the binary label of sample :math:`i` for class :math:`j`, where 6978 sample :math:`i` belongs to class :math:`j` if :math:`output_{ij}=1` , and sample :math:`i` does 6979 not belong to class :math:`j` if :math:`output_{ij}=0`. For a multi-label classification task, each 6980 sample may have multiple labels with a value of 1 in the binary label :math:`output`. `weight` will 6981 multiply to the loss of each class if given. 6982 6983 Args: 6984 input (Tensor): A tensor of shape :math:`(N, C)` , where N is batch size and C is number of classes. 6985 target (Tensor): The label target Tensor which has the same shape as `input`. 6986 weight (Union[Tensor, int, float]): The manual rescaling weight given to each class. Default: ``None``. 6987 reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` , 6988 ``'sum'`` . Default: ``'mean'`` . 6989 6990 - ``'none'``: no reduction will be applied. 6991 - ``'mean'``: compute and return the weighted mean of elements in the output. 6992 - ``'sum'``: the output elements will be summed. 6993 6994 Returns: 6995 Tensor, the data type is the same as input, if the `reduction` is ``'none'``, 6996 its shape is :math:`(N)` , otherwise it is zero. 6997 6998 Supported Platforms: 6999 ``Ascend`` ``GPU`` ``CPU`` 7000 7001 Examples: 7002 >>> from mindspore import Tensor, ops 7003 >>> input = Tensor([[0.3, 0.6, 0.6], [0.9, 0.4, 0.2]]) 7004 >>> target = Tensor([[0.0, 0.0, 1.0], [0.0, 0.0, 1.0]]) 7005 >>> loss = ops.multilabel_soft_margin_loss(input, target, reduction='mean') 7006 >>> print(loss.asnumpy()) 7007 0.84693956 7008 """ 7009 cls_name = "multilabel_soft_margin_loss" 7010 _check_is_tensor('input', input, cls_name) 7011 _check_is_tensor('target', target, cls_name) 7012 7013 input_shape = input.shape 7014 if ops.is_sequence_value_unknown(input_shape): 7015 input_shape = tensor_shape_(input) 7016 7017 pos = log_(add_(exp_(-input), 1)) 7018 neg = log_(add_(exp_(input), 1)) 7019 loss = mul_(target, pos) + mul_(1 - target, neg) 7020 if weight is not None: 7021 loss = mul_(loss, weight) 7022 class_dim = input.ndim - 1 7023 loss = loss.sum(axis=class_dim) / input_shape[class_dim] 7024 return _get_loss(loss, reduction, cls_name) 7025 7026 7027def gelu(input, approximate='none'): 7028 r""" 7029 Gaussian Error Linear Units activation function. 7030 7031 GeLU is described in the paper `Gaussian Error Linear Units (GELUs) <https://arxiv.org/abs/1606.08415>`_. 7032 And also please refer to `BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding 7033 <https://arxiv.org/abs/1810.04805>`_. 7034 7035 When `approximate` argument is `none`, GeLU is defined as follows: 7036 7037 .. math:: 7038 GELU(x_i) = x_i*P(X < x_i), 7039 7040 where :math:`P` is the cumulative distribution function of the standard Gaussian distribution, 7041 :math:`x_i` is the input element. 7042 7043 When `approximate` argument is `tanh`, GeLU is estimated with: 7044 7045 .. math:: 7046 GELU(x_i) = 0.5 * x_i * (1 + \tanh(\sqrt(2 / \pi) * (x_i + 0.044715 * x_i^3))) 7047 7048 GELU Activation Function Graph: 7049 7050 .. image:: ../images/GELU.png 7051 :align: center 7052 7053 Args: 7054 input (Tensor): The input of the activation function GeLU, the data type is float16, float32 or float64. 7055 approximate (str): the gelu approximation algorithm to use. Acceptable vaslues are ``'none'`` and ``'tanh'`` . 7056 Default: ``'none'`` . 7057 7058 Returns: 7059 Tensor, with the same type and shape as `input`. 7060 7061 Raises: 7062 TypeError: If `input` is not a Tensor. 7063 TypeError: If dtype of `input` is not bfloat16, float16, float32 or float64. 7064 ValueError: If `approximate` value is neither `none` nor `tanh`. 7065 7066 Supported Platforms: 7067 ``Ascend`` ``GPU`` ``CPU`` 7068 7069 Examples: 7070 >>> import mindspore 7071 >>> from mindspore import Tensor, ops 7072 >>> x = Tensor([1.0, 2.0, 3.0], mindspore.float32) 7073 >>> result = ops.gelu(x) 7074 >>> print(result) 7075 [0.8413447 1.9544997 2.9959505] 7076 """ 7077 if approximate not in ['none', 'tanh']: 7078 raise ValueError("For ops.gelu, approximate value should be either 'none' or 'tanh'.") 7079 7080 x_dtype = dtype_(input) 7081 if x_dtype not in [mstype.float16, mstype.float32, mstype.float64, mstype.bfloat16]: 7082 raise TypeError(f"For gelu, the input dtype must be float16, float32 or float64, " 7083 f"but got {x_dtype}.") 7084 if approximate == 'tanh': 7085 output = gelu_(input) 7086 else: 7087 output = sqrt_(Tensor(2.0, x_dtype)) 7088 output = div_(input, output) 7089 output = erf_(output) + Tensor(1.0, x_dtype) 7090 output = input * output * Tensor(0.5, x_dtype) 7091 7092 return output 7093 7094 7095def channel_shuffle(x, groups): 7096 r""" 7097 Divide the channels in a tensor of shape :math:`(*, C, H, W)` into :math:`g` groups and 7098 rearrange them as :math:`(*, \frac{C}{g}, g, H*W)`, while keeping the original tensor shapes. 7099 7100 Args: 7101 x (Tensor): Tensor to be divided, it has shape :math:`(*, C, H, W)`, 7102 with float16, float32, int8, int16, int32, int64, uint8, uint16, uint32, uint64 data type. 7103 groups (int): Number of groups to divide channels in. 7104 7105 Returns: 7106 A Tensor, has the same type as the `x`, and has the shape :math:`(*, C, H, W)`. 7107 7108 Raises: 7109 TypeError: If data type of `x` is not one of the following: 7110 float16, float32, int8, int16, int32, int64, uint8, uint16, uint32, uint64. 7111 TypeError: If dim of `x` is < 4. 7112 TypeError: If `groups` is not a positive number. 7113 ValueError: If channel number of `x` is not divisible by `groups`. 7114 7115 Supported Platforms: 7116 ``Ascend`` ``CPU`` 7117 7118 Examples: 7119 >>> import mindspore 7120 >>> import numpy as np 7121 >>> from mindspore import Tensor, ops 7122 >>> group = 2 7123 >>> x = Tensor(np.arange(1* 4 * 2 * 2).reshape(1, 4, 2, 2).astype(np.int16)) 7124 >>> y = mindspore.ops.channel_shuffle(x, group) 7125 >>> print(y) 7126 [[[[ 0 1] 7127 [ 2 3]] 7128 [[ 8 9] 7129 [10 11]] 7130 [[ 4 5] 7131 [ 6 7]] 7132 [[12 13] 7133 [14 15]]]] 7134 """ 7135 channel_shuffle_func = ChannelShuffle(group=groups) 7136 y = channel_shuffle_func(x) 7137 return y 7138 7139 7140def lp_pool1d(x, norm_type, kernel_size, stride=None, ceil_mode=False): 7141 r""" 7142 Applying 1D LPPooling operation on an input Tensor can be regarded as forming a 1D input plane. 7143 7144 Typically the input is of shape :math:`(N, C, L_{in})` or :math:`(C, L_{in})`, the output is of shape 7145 :math:`(N, C, L_{out})` or :math:`(C, L_{out})`. 7146 7147 .. math:: 7148 L_{out} = \left\lfloor\frac{L_{in} - \text{kernel_size}}{\text{stride}} + 1\right\rfloor 7149 7150 The operation is as follows. 7151 7152 .. math:: 7153 f(X) = \sqrt[p]{\sum_{x \in X} x^{p}} 7154 7155 Args: 7156 x (Tensor): Tensor of shape :math:`(N, C, L_{in})` or :math:`(C, L_{in})`. 7157 norm_type (Union[int, float]): Type of normalization, represents p in the formula, can not be 0, 7158 7159 - if p = 1, the result obtained is the sum of elements in the pool nucleus(Proportional to average pooling). 7160 - if p = :math:`\infty`, the result is the result of maximum pooling. 7161 7162 kernel_size (int): The size of kernel window. 7163 stride (int): The distance of kernel moving, an int number that represents 7164 the width of movement is stride. Default: ``None`` , which indicates the moving step is `kernel_size` . 7165 ceil_mode (bool): Whether to use ceil or floor to calculate output shape. Default: ``False`` . 7166 7167 Returns: 7168 - **output** (Tensor) - LPPool1d result, with shape :math:`(N, C, L_{out})` or :math:`(C, L_{out})`, 7169 It has the same data type as `x`, where 7170 7171 .. math:: 7172 L_{out} = \left\lfloor\frac{L_{in} - \text{kernel_size}}{\text{stride}} + 1\right\rfloor 7173 7174 Raises: 7175 TypeError: If `x` is not a Tensor. 7176 TypeError: If `kernel_size` or `stride` is not an int. 7177 TypeError: If `ceil_mode` is not a bool. 7178 TypeError: If `norm_type` is neither float nor int. 7179 ValueError: If `norm_type` is equal to 0. 7180 ValueError: If `kernel_size` or `stride` is less than 1. 7181 ValueError: If length of shape of `x` is not equal to 2 or 3. 7182 7183 Supported Platforms: 7184 ``Ascend`` ``GPU`` ``CPU`` 7185 7186 Examples: 7187 >>> import mindspore as ms 7188 >>> from mindspore import ops 7189 >>> from mindspore import Tensor 7190 >>> import numpy as np 7191 >>> x = Tensor(np.arange(2 * 3 * 4).reshape((2, 3, 4)), dtype=ms.float32) 7192 >>> out = ops.lp_pool1d(x, norm_type=1, kernel_size=3, stride=1, ceil_mode=False) 7193 >>> print(out) 7194 [[[ 3. 6.] 7195 [15. 18.] 7196 [27. 30.]] 7197 [[39. 42.] 7198 [51. 54.] 7199 [63. 66.]]] 7200 """ 7201 if isinstance(norm_type, (float, int)): 7202 norm_type = float(norm_type) 7203 else: 7204 raise TypeError(f"For lp_pool1d, the type of 'norm_type' must be float or int, but got {type(norm_type)}") 7205 if norm_type == 0: 7206 raise ValueError(f"For lp_pool1d, the value of 'norm_type' can not be 0.") 7207 sign = _get_cache_prim(ops.Sign)() 7208 squeeze = _get_cache_prim(ops.Squeeze)(0) 7209 expand_dims = _get_cache_prim(ops.ExpandDims)() 7210 _is_squeeze = False 7211 if len(x.shape) == 2: 7212 x = expand_dims(x, 0) 7213 _is_squeeze = True 7214 if stride is None: 7215 stride = kernel_size 7216 out = ops.avg_pool1d(x.pow(norm_type), kernel_size=kernel_size, stride=stride, padding=0, ceil_mode=ceil_mode) 7217 if _is_squeeze: 7218 out = squeeze(out) 7219 return ((sign(out) * ops.relu(ops.abs(out))) * kernel_size).pow(1.0 / norm_type) 7220 7221 7222def lp_pool2d(x, norm_type, kernel_size, stride=None, ceil_mode=False): 7223 r""" 7224 Applying 2D LPPooling operation on an input Tensor can be regarded as forming a 2D input plane. 7225 7226 Typically the input is of shape :math:`(N, C, H_{in}, W_{in})`, the output is of shape 7227 :math:`(N, C, H_{in}, W_{in})`, with the same shape as input, the operation is as follows. 7228 7229 .. math:: 7230 f(X) = \sqrt[p]{\sum_{x \in X} x^{p}} 7231 7232 Args: 7233 x (Tensor): Tensor of shape :math:`(N, C, H_{in}, W_{in})`. 7234 norm_type (Union[int, float]): Type of normalization, represents p in the formula, can not be 0, 7235 7236 - if p = 1, the result obtained is the sum of elements in the pool nucleus(Proportional to average pooling). 7237 - if p = :math:`\infty`, the result is the result of maximum pooling. 7238 7239 kernel_size (Union[int, tuple[int]]): The size of kernel window. 7240 The data type of kernel_size must be int and the value represents the height and width, 7241 or a tuple of two int numbers that represent height and width respectively. 7242 stride (Union[int, tuple[int]]): The distance of kernel moving, an int number that represents 7243 the height and width of movement are both strides, or a tuple of two int numbers that 7244 represent height and width of movement respectively. 7245 Default: ``None`` , which indicates the moving step is `kernel_size` . 7246 ceil_mode (bool): Whether to use ceil or floor to calculate output shape. Default: ``False`` . 7247 7248 Returns: 7249 - **output** (Tensor) - LPPool2d result, with shape :math:`(N, C, H_{in}, W_{in})`, 7250 It has the same data type as `x`, where 7251 7252 .. math:: 7253 H_{out} = \left\lfloor\frac{H_{in} - \text{kernel_size}[0]}{\text{stride}[0]} + 1\right\rfloor 7254 7255 .. math:: 7256 W_{out} = \left\lfloor\frac{W_{in} - \text{kernel_size}[1]}{\text{stride}[1]} + 1\right\rfloor 7257 7258 Raises: 7259 TypeError: If `x` is not a Tensor. 7260 TypeError: If `kernel_size` or `stride` is neither int nor tuple. 7261 TypeError: If `ceil_mode` is not a bool. 7262 TypeError: If `norm_type` is neither float nor int. 7263 ValueError: If `norm_type` is equal to 0. 7264 ValueError: If `kernel_size` or `stride` is less than 1. 7265 ValueError: If `kernel_size` or `stride` is a tuple whose length is not equal to `2`. 7266 ValueError: If length of shape of `x` is not equal to 4. 7267 7268 Supported Platforms: 7269 ``Ascend`` ``GPU`` ``CPU`` 7270 7271 Examples: 7272 >>> import mindspore as ms 7273 >>> from mindspore import ops 7274 >>> from mindspore import Tensor 7275 >>> import numpy as np 7276 >>> x = Tensor(np.arange(2 * 3 * 4 * 5).reshape((2, 3, 4, 5)), dtype=ms.float32) 7277 >>> out = ops.lp_pool2d(x, norm_type=1, kernel_size=3, stride=1, ceil_mode=False) 7278 >>> print(out) 7279 [[[[ 54. 63. 72.] 7280 [ 99. 108. 117.]] 7281 [[ 234. 243. 252.] 7282 [ 279. 288. 297.]] 7283 [[ 414. 423. 432.] 7284 [ 459. 468. 477.]]] 7285 [[[ 594. 603. 612.] 7286 [ 639. 648. 657.]] 7287 [[ 774. 783. 792.] 7288 [ 819. 828. 837.]] 7289 [[ 954. 963. 972.] 7290 [ 999. 1008. 1017.]]]] 7291 7292 """ 7293 if isinstance(norm_type, (float, int)): 7294 norm_type = float(norm_type) 7295 else: 7296 raise TypeError(f"For lp_pool2d, the type of 'norm_type' must be float or int, but got {type(norm_type)}") 7297 if norm_type == 0: 7298 raise ValueError(f"For lp_pool2d, the value of 'norm_type' can not be 0.") 7299 sign = _get_cache_prim(ops.Sign)() 7300 if not isinstance(kernel_size, tuple): 7301 kernel_size = (kernel_size, kernel_size) 7302 if stride is None: 7303 stride = kernel_size 7304 out = ops.avg_pool2d(x.pow(norm_type), kernel_size=kernel_size, stride=stride, padding=0, ceil_mode=ceil_mode) 7305 return ((sign(out) * ops.relu(ops.abs(out))) * (kernel_size[0] * kernel_size[1])).pow(1.0 / norm_type) 7306 7307 7308def mse_loss(input, target, reduction='mean'): 7309 r""" 7310 Calculates the mean squared error between the predicted value and the label value. 7311 7312 For detailed information, please refer to :class:`mindspore.nn.MSELoss`. 7313 7314 Args: 7315 input (Tensor): Tensor of any dimension. 7316 target (Tensor): The input label. Tensor of any dimension, same shape as the `input` in common cases. 7317 However, it supports that the shape of `input` is different from the shape of `target` 7318 and they should be broadcasted to each other. 7319 reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` , 7320 ``'sum'`` . Default: ``'mean'`` . 7321 7322 - ``'none'``: no reduction will be applied. 7323 - ``'mean'``: compute and return the mean of elements in the output. 7324 - ``'sum'``: the output elements will be summed. 7325 7326 Returns: 7327 Tensor, loss of type float, the shape is zero if `reduction` is ``'mean'`` or ``'sum'`` , 7328 while the shape of output is the broadcasted shape if `reduction` is ``'none'`` . 7329 7330 Raises: 7331 ValueError: If `reduction` is not one of ``'none'`` , ``'mean'`` or ``'sum'``. 7332 ValueError: If `input` and `target` have different shapes and cannot be broadcasted. 7333 7334 Supported Platforms: 7335 ``Ascend`` ``GPU`` ``CPU`` 7336 7337 Examples: 7338 >>> import mindspore 7339 >>> import numpy as np 7340 >>> from mindspore import Tensor, ops 7341 >>> logits = Tensor(np.array([1, 2, 3]), mindspore.float32) 7342 >>> labels = Tensor(np.array([[1, 1, 1], [1, 2, 2]]), mindspore.float32) 7343 >>> output = ops.mse_loss(logits, labels, reduction='none') 7344 >>> print(output) 7345 [[0. 1. 4.] 7346 [0. 0. 1.]] 7347 """ 7348 if not isinstance(input, (Tensor, Tensor_)): 7349 raise TypeError("For ops.mse_loss, the `input` must be tensor") 7350 if not isinstance(target, (Tensor, Tensor_)): 7351 raise TypeError("For ops.mse_loss, the `target` must be tensor") 7352 if reduction not in ['mean', 'none', 'sum']: 7353 raise ValueError("For ops.mse_loss, `reduction` value should be either 'mean', 'none' or 'sum'.") 7354 7355 x = square_(input - target) 7356 float_type = (mstype.float16, mstype.float32, mstype.float64) 7357 if x.dtype not in float_type: 7358 input_dtype = mstype.float32 7359 else: 7360 input_dtype = x.dtype 7361 x = cast_(x, mstype.float32) 7362 7363 average_flag = True 7364 reduce_flag = True 7365 if reduction == 'sum': 7366 average_flag = False 7367 if reduction == 'none': 7368 reduce_flag = False 7369 7370 if reduce_flag and average_flag: 7371 x = reduce_mean_(x, _get_axis(x)) 7372 7373 if reduce_flag and not average_flag: 7374 x = reduce_sum_(x, _get_axis(x)) 7375 7376 return cast_(x, input_dtype) 7377 7378 7379def msort(input): 7380 r""" 7381 Sorts the elements in Tensor in ascending order of value along its first dimension. 7382 7383 ops.msort(t) is equivalent to ops.Sort(axis=0)(t)[0]. See also :class:`mindspore.ops.Sort()`. 7384 7385 Args: 7386 input (Tensor): The input to sort, with float16 or float32 data type. 7387 7388 Returns: 7389 A tensor whose values are the sorted values, with the same shape and data type as input. 7390 7391 Raises: 7392 TypeError: If dtype of `input` is neither float16 nor float32. 7393 7394 Supported Platforms: 7395 ``Ascend`` ``GPU`` ``CPU`` 7396 7397 Examples: 7398 >>> import mindspore as ms 7399 >>> from mindspore import ops 7400 >>> import numpy as np 7401 >>> input = ms.Tensor(np.array([[8, 2, 1], [5, 9, 3], [4, 6, 7]]), ms.float16) 7402 >>> output = ops.msort(input) 7403 >>> print(output) 7404 [[4. 2. 1.] 7405 [5. 6. 3.] 7406 [8. 9. 7.]] 7407 """ 7408 return ops.Sort(axis=0)(input)[0] 7409 7410 7411def triplet_margin_loss(anchor, positive, negative, margin=1.0, p=2, eps=1e-06, swap=False, reduction='mean'): 7412 """ 7413 TripletMarginLoss operation. 7414 See :class:`mindspore.nn.TripletMarginLoss` for details. 7415 7416 Args: 7417 anchor (Tensor): A sample randomly selected from the training set. Data type must be BasicType. 7418 positive (Tensor): A sample belonging to the same category as `anchor`, with the same type and shape 7419 as `anchor`. 7420 negative (Tensor): A sample belonging to the different class from `anchor`, with the same type and shape 7421 as `anchor`. 7422 margin (float, optional): Make a margin between the positive pair and the negative pair. Default: ``1.0`` . 7423 p (int, optional): The degree of norm for pairwise distance. Default: ``2`` . 7424 eps (float, optional): Add small value to avoid division by zero. Default: ``1e-06``. 7425 swap (bool, optional): The distance swap change the negative distance to the distance between positive 7426 sample and negative sample. Default: ``False`` . 7427 reduction (str, optional): Apply specific reduction method to the output: ``'none'`` , ``'mean'`` , 7428 ``'sum'`` . Default: ``'mean'`` . 7429 7430 - ``'none'``: no reduction will be applied. 7431 - ``'mean'``: compute and return the mean of elements in the output. 7432 - ``'sum'``: the output elements will be summed. 7433 7434 Returns: 7435 Tensor. If `reduction` is ``"none"``, its shape is :math:`(N)`. Otherwise, a scalar value will be returned. 7436 7437 Raises: 7438 TypeError: If `anchor` or `positive` or `negative` is not a Tensor. 7439 TypeError: If dtype of `anchor`, `positive` and `negative` is not the same. 7440 TypeError: If `margin` is not a float. 7441 TypeError: If `p` is not an int. 7442 TypeError: If `eps` is not a float. 7443 TypeError: If `swap` is not a bool. 7444 ValueError: If dimensions of input `anchor`, `positive` and `negative` are less than or equal to 1 at the 7445 same time. 7446 ValueError: If the dimension of input `anchor` or `positive` or `negative` is bigger than or equal to 8. 7447 ValueError: If shape of `anchor`, `positive` and `negative` cannot broadcast. 7448 ValueError: If `reduction` is not one of ``'none'``, ``'mean'``, ``'sum'``. 7449 7450 Supported Platforms: 7451 ``GPU`` 7452 7453 Examples: 7454 >>> import mindspore 7455 >>> import numpy as np 7456 >>> from mindspore import Tensor, ops 7457 >>> anchor = Tensor(np.array([[0.3, 0.7], [0.5, 0.5]]), mindspore.float32) 7458 >>> positive = Tensor(np.array([[0.4, 0.6], [0.4, 0.6]]), mindspore.float32) 7459 >>> negative = Tensor(np.array([[0.2, 0.9], [0.3, 0.7]]), mindspore.float32) 7460 >>> output = ops.triplet_margin_loss(anchor, positive, negative) 7461 >>> print(output) 7462 0.8881968 7463 """ 7464 if not isinstance(margin, Tensor): 7465 margin_tensor = Tensor(margin, mstype.float32) 7466 else: 7467 margin_tensor = margin 7468 triplet_margin_loss_op = _get_cache_prim(TripletMarginLoss)(p=p, eps=eps, swap=swap, reduction=reduction) 7469 return triplet_margin_loss_op(anchor, positive, negative, margin_tensor) 7470 7471 7472def linear(x, w, b): 7473 """inner linear""" 7474 out = ops.matmul(x, w.swapaxes(-1, -2)) 7475 if b is not None: 7476 out = out + b 7477 return out 7478 7479 7480def _inner_dropout(x, p, training): 7481 """inner dropout""" 7482 _dropout = _get_cache_prim(P.Dropout)(1 - p) 7483 if 0. < p <= 1. and training: 7484 return _dropout(x)[0] 7485 return x 7486 7487 7488def _in_projection(q, k, v, w_q, w_k, w_v, b_q=None, b_k=None, b_v=None): 7489 """in projection function""" 7490 Eq, Ek, Ev = q.shape[-1], k.shape[-1], v.shape[-1] 7491 w_q_shape, w_k_shape, w_v_shape = w_q.shape, w_k.shape, w_v.shape 7492 7493 if w_q_shape != (Eq, Eq): 7494 raise ValueError(f"Expecting query weights shape of {(Eq, Eq)}, but got {w_q_shape}") 7495 if w_k_shape != (Eq, Ek): 7496 raise ValueError(f"Expecting key weights shape of {(Eq, Ek)}, but got {w_k_shape}") 7497 if w_v_shape != (Eq, Ev): 7498 raise ValueError(f"Expecting value weights shape of {(Eq, Ev)}, but got {w_v_shape}") 7499 if b_q is not None: 7500 b_q_shape = b_q.shape 7501 if b_q_shape != (Eq,): 7502 raise ValueError(f"Expecting query bias shape of {(Eq,)}, but got {b_q_shape}") 7503 if b_k is not None: 7504 b_k_shape = b_k.shape 7505 if b_k_shape != (Eq,): 7506 raise ValueError(f"Expecting key bias shape of {(Eq,)}, but got {b_k_shape}") 7507 if b_v is not None: 7508 b_v_shape = b_v.shape 7509 if b_v_shape != (Eq,): 7510 raise ValueError(f"Expecting value bias shape of {(Eq,)}, but got {b_v_shape}") 7511 return linear(q, w_q, b_q), linear(k, w_k, b_k), linear(v, w_v, b_v) 7512 7513 7514def _in_projection_packed(q, k, v, w, b, k_is_v, q_is_k): 7515 """in projecktion packed function""" 7516 E = q.shape[-1] 7517 if k_is_v: 7518 if q_is_k: 7519 # self-attention 7520 return linear(q, w, b).tensor_split(3, axis=-1) 7521 # encoder-decoder attention 7522 w_q, w_kv = w.split([E, E * 2]) 7523 if b is None: 7524 b_q = b_kv = None 7525 else: 7526 b_q, b_kv = b.split([E, E * 2]) 7527 return (linear(q, w_q, b_q),) + linear(k, w_kv, b_kv).tensor_split(2, axis=-1) 7528 w_q, w_k, w_v = w.tensor_split(3) 7529 if b is None: 7530 b_q = b_k = b_v = None 7531 else: 7532 b_q, b_k, b_v = b.tensor_split(3) 7533 return linear(q, w_q, b_q), linear(k, w_k, b_k), linear(v, w_v, b_v) 7534 7535 7536def _scaled_dot_product_attention(query, key, value, attn_mask, dropout_p, is_causal, is_training, dtype): 7537 """scaled dot product attention""" 7538 embed_size = query.shape[-1] 7539 embed_size_tensor = scalar_to_tensor_(embed_size, dtype) 7540 scaling_factor = embed_size_tensor.sqrt().sqrt() 7541 query = query / scaling_factor 7542 7543 if is_causal: 7544 L = query.shape[-2] 7545 S = key.shape[-2] 7546 attn_mask = ops.ones((L, S), mstype.bool_).tril() 7547 7548 attn = ops.matmul(query, key.swapaxes(-2, -1) / scaling_factor) 7549 if attn_mask is not None: 7550 attn = attn + attn_mask 7551 attn = ops.softmax(attn, -1) 7552 attn = _inner_dropout(attn, dropout_p, is_training) 7553 output = ops.matmul(attn, value) 7554 7555 return (output, attn) 7556 7557 7558@_primexpr 7559def _check_qkv_shape(query_ndim, key_ndim, value_ndim): 7560 """Check the expected shape for `query, `key`, `value` and returns whether the input is batched.""" 7561 # Shape check. 7562 if query_ndim == 3: 7563 # Batched Inputs 7564 is_batched = True 7565 if key_ndim != 3 or value_ndim != 3: 7566 raise ValueError(f"For batched `query`, the `key` and `value` must be 3D tensor, " 7567 f"but got `key` with {key_ndim}D and `value` with {value_ndim}D.") 7568 elif query_ndim == 2: 7569 # Unbatched Inputs 7570 is_batched = False 7571 if key_ndim != 2 or value_ndim != 2: 7572 raise ValueError(f"For unbatched `query`, the `key` and `value` must be 2D tensor, " 7573 f"but got `key` with {key_ndim}D and `value` with {value_ndim}D.") 7574 else: 7575 raise ValueError(f"The `query` should be unbatched 2D or batched 3D tensor, " 7576 f"but got `query` with {query_ndim}D.") 7577 7578 return is_batched 7579 7580 7581@_primexpr 7582def _check_kpm_shape(query_ndim, kmp_ndim): 7583 """check key_padding_mask shape""" 7584 if query_ndim == 3: 7585 if kmp_ndim != 2: 7586 raise ValueError(f"For batched `query`, the `key_padding_mask` must be `None` or 2D, " 7587 f"but got `key_padding_mask` with {kmp_ndim}D.") 7588 7589 elif query_ndim == 2: 7590 if kmp_ndim != 1: 7591 raise ValueError(f"For unbatched `query`, the `key_padding_mask` must be `None` or 1D, " 7592 f"but got `key_padding_mask` with {kmp_ndim}D.") 7593 7594 7595@_primexpr 7596def _check_attn_mask_shape(query_ndim, query_shape, key_shape, attn_mask_ndim, 7597 attn_mask_shape, num_heads): 7598 """ 7599 Check the expected shape for `attn_mask`. 7600 """ 7601 # Shape check. 7602 if query_ndim == 3: 7603 if attn_mask_ndim not in (2, 3): 7604 raise ValueError(f"For batched `query`, the `attn_mask` must be `None`, 2-D or 3-D, " 7605 f"but got `attn_mask` with{attn_mask_ndim}D.") 7606 elif query_ndim == 2: 7607 if attn_mask_ndim not in (2, 3): 7608 raise ValueError(f"For unbatched `query`, the `attn_mask` must be `None`, 2-D or 3-D, " 7609 f"but got `attn_mask` with{attn_mask_ndim}D.") 7610 7611 if attn_mask_ndim == 3: 7612 expected_shape = (num_heads, query_shape[0], key_shape[0]) 7613 if attn_mask_shape != expected_shape: 7614 raise ValueError(f"The shape of `attn_mask` must to be {expected_shape}, " 7615 f"but got {attn_mask_shape}.") 7616 7617 7618def _inner_pad(x, padding, value=None): 7619 """inner pad function for bool type.""" 7620 x_dtype = x.dtype 7621 if x_dtype == mstype.bool_: 7622 x = x.astype(mstype.int32) 7623 x = pad(x, padding, value=value) 7624 x = x.astype(x_dtype) 7625 return x 7626 7627 7628def multi_head_attention_forward(query, key, value, embed_dim_to_check, num_heads, in_proj_weight, 7629 in_proj_bias, bias_k, bias_v, add_zero_attn, dropout_p, out_proj_weight, 7630 out_proj_bias, training=True, key_padding_mask=None, attn_mask=None, 7631 use_separate_proj_weight=False, q_proj_weight=None, k_proj_weight=None, 7632 v_proj_weight=None, static_k=None, static_v=None, average_attn_weights=True, 7633 is_causal=False, k_is_v=False, q_is_k=False, dtype=mstype.float32): 7634 """multi head attetion forward function""" 7635 is_batched = _check_qkv_shape(query.ndim, key.ndim, value.ndim) 7636 if key_padding_mask is not None: 7637 _check_kpm_shape(query.ndim, key_padding_mask.ndim) 7638 if attn_mask is not None: 7639 _check_attn_mask_shape(query.ndim, query.shape, key.shape, attn_mask.ndim, 7640 attn_mask.shape, num_heads) 7641 7642 if not is_batched: 7643 query = query.expand_dims(1) 7644 key = key.expand_dims(1) 7645 value = value.expand_dims(1) 7646 if key_padding_mask is not None: 7647 key_padding_mask = key_padding_mask.expand_dims(0) 7648 7649 tgt_len, bsz, embed_dim = query.shape 7650 src_len, _, _ = key.shape 7651 if key_padding_mask is not None: 7652 _kpm_dtype = key_padding_mask.dtype 7653 if _kpm_dtype != mstype.bool_ and not ops.is_floating_point(key_padding_mask): 7654 raise ValueError("The `key_padding_mask` only supports bool and floating dtypes.") 7655 if embed_dim != embed_dim_to_check: 7656 raise ValueError(f"The `embed_dim` should be {embed_dim_to_check}, but got {embed_dim}.") 7657 7658 head_dim = embed_dim // num_heads 7659 if head_dim * num_heads != embed_dim: 7660 raise ValueError(f"The `embed_dim` {embed_dim} can not be divisible by `num_heads` {num_heads}.") 7661 if use_separate_proj_weight: 7662 # allow MHA to have different embedding dims when separate projection weights are used 7663 if key.shape[:2] != value.shape[:2]: 7664 raise ValueError(f"The sequence length and batch dims of `key`: {key.shape[:2]} do not match " 7665 f"`value`: {value.shape[:2]}.") 7666 else: 7667 if key.shape != value.shape: 7668 raise ValueError(f"The shape of `key` {key.shape} does not match `value` {value.shape}.") 7669 7670 # compute in-projection 7671 if not use_separate_proj_weight: 7672 if in_proj_weight is None: 7673 raise ValueError("`use_separate_proj_weight` is ``False`` but `in_proj_weight` got ``None``.") 7674 q, k, v = _in_projection_packed(query, key, value, in_proj_weight, in_proj_bias, k_is_v, q_is_k) 7675 else: 7676 if q_proj_weight is None: 7677 raise ValueError("`use_separate_proj_weight` is ``True`` but `q_proj_weight` got ``None``.") 7678 if k_proj_weight is None: 7679 raise ValueError("`use_separate_proj_weight` is ``True`` but `k_proj_weight` got ``None``.") 7680 if v_proj_weight is None: 7681 raise ValueError("`use_separate_proj_weight` is ``True`` but `v_proj_weight` got ``None``.") 7682 if in_proj_bias is None: 7683 b_q = b_k = b_v = None 7684 else: 7685 b_q, b_k, b_v = in_proj_bias.tensor_split(3) 7686 q, k, v = _in_projection(query, key, value, q_proj_weight, k_proj_weight, v_proj_weight, b_q, b_k, b_v) 7687 7688 # prep attention mask 7689 if attn_mask is not None: 7690 if attn_mask.dtype == mstype.uint8: 7691 attn_mask = attn_mask.astype(mstype.bool_) 7692 else: 7693 if not ops.is_floating_point(attn_mask) and attn_mask.dtype != mstype.bool_: 7694 raise ValueError(f"`attn_mask` only support float, byte, and bool types, " 7695 f"but got not {attn_mask.dtype}.") 7696 # ensure attn_mask's ndim is 3 7697 if attn_mask.ndim == 2: 7698 correct_2d_size = (tgt_len, src_len) 7699 if attn_mask.shape != correct_2d_size: 7700 raise ValueError(f"The shape of the `attn_mask` should be {correct_2d_size}, " 7701 f"but got {attn_mask.shape}.") 7702 attn_mask = attn_mask.expand_dims(0) 7703 elif attn_mask.ndim == 3: 7704 correct_3d_size = (bsz * num_heads, tgt_len, src_len) 7705 if attn_mask.shape != correct_3d_size: 7706 raise ValueError(f"The shape of the `attn_mask` should be {correct_3d_size}, " 7707 f"but got {attn_mask.shape}.") 7708 else: 7709 raise ValueError(f"The ndim of `attn_mask` only support 2 or 3, " 7710 f"but got {attn_mask.ndim}.") 7711 7712 if bias_k is not None and bias_v is not None: 7713 if static_k is not None: 7714 raise ValueError("The bias_k cannot be added to static_k.") 7715 if static_v is not None: 7716 raise ValueError("The bias_v cannot be added to static_v.") 7717 k = ops.cat([k, bias_k.tile((1, bsz, 1))]) 7718 v = ops.cat([v, bias_v.tile((1, bsz, 1))]) 7719 if attn_mask is not None: 7720 attn_mask = _inner_pad(attn_mask, (0, 1)) 7721 if key_padding_mask is not None: 7722 key_padding_mask = _inner_pad(key_padding_mask, (0, 1)) 7723 else: 7724 if bias_k is not None or bias_v is not None: 7725 raise ValueError("The bias_k and bias_v should be ``None``" 7726 "at the same time.") 7727 7728 q = q.view((tgt_len, bsz * num_heads, head_dim)).swapaxes(0, 1) 7729 if static_k is None: 7730 k = k.view((k.shape[0], bsz * num_heads, head_dim)).swapaxes(0, 1) 7731 else: 7732 if static_k.shape[0] != bsz * num_heads: 7733 raise ValueError(f"The shape[0] of `static_k` should be {bsz * num_heads}, " 7734 f"but got {static_k.shape[0]}") 7735 if static_k.shape[2] != head_dim: 7736 raise ValueError(f"The shape[2] of `static_k` should be {head_dim}, " 7737 f"but got {static_k.shape[2]}") 7738 k = static_k 7739 if static_v is None: 7740 v = v.view((v.shape[0], bsz * num_heads, head_dim)).swapaxes(0, 1) 7741 else: 7742 if static_v.shape[0] != bsz * num_heads: 7743 raise ValueError(f"The shape[0] of `static_v` should be {bsz * num_heads}, " 7744 f"but got {static_v.shape[0]}") 7745 if static_v.shape[2] != head_dim: 7746 raise ValueError(f"The shape[2] of `static_v` should be {head_dim}, " 7747 f"but got {static_v.shape[2]}") 7748 v = static_v 7749 7750 if add_zero_attn: 7751 zero_attn_shape = (bsz * num_heads, 1, head_dim) 7752 k = ops.cat([k, ops.zeros(zero_attn_shape, dtype=k.dtype)], axis=1) 7753 v = ops.cat([v, ops.zeros(zero_attn_shape, dtype=v.dtype)], axis=1) 7754 if attn_mask is not None: 7755 attn_mask = _inner_pad(attn_mask, (0, 1)) 7756 if key_padding_mask is not None: 7757 key_padding_mask = _inner_pad(key_padding_mask, (0, 1)) 7758 7759 src_len = k.shape[1] 7760 7761 if key_padding_mask is not None: 7762 if key_padding_mask.shape != (bsz, src_len): 7763 raise ValueError(f"The shape of `key_padding_mask` should be {(bsz, src_len)}, " 7764 f"but got {key_padding_mask.shape}.") 7765 7766 key_padding_mask = key_padding_mask.view((bsz, 1, 1, src_len)). \ 7767 tile((1, num_heads, 1, 1)).reshape(bsz * num_heads, 1, src_len) 7768 if attn_mask is None: 7769 attn_mask = key_padding_mask 7770 elif attn_mask.dtype == mstype.bool_: 7771 attn_mask = attn_mask.logical_or(key_padding_mask) 7772 else: 7773 attn_mask = attn_mask + key_padding_mask 7774 7775 if attn_mask is not None and attn_mask.dtype == mstype.bool_: 7776 new_attn_mask = ops.zeros_like(attn_mask, dtype=q.dtype) 7777 attn_mask = new_attn_mask.masked_fill(attn_mask, ops.cast(float("-inf"), new_attn_mask.dtype)) 7778 7779 if attn_mask is not None: 7780 if attn_mask.shape[0] == 1: 7781 attn_mask = attn_mask.expand_dims(0) 7782 else: 7783 attn_mask = attn_mask.view((bsz, num_heads, -1, src_len)) 7784 7785 q = q.view((bsz, num_heads, tgt_len, head_dim)) 7786 k = k.view((bsz, num_heads, src_len, head_dim)) 7787 v = v.view((bsz, num_heads, src_len, head_dim)) 7788 7789 attn_output, attn_output_weights = _scaled_dot_product_attention( 7790 q, k, v, attn_mask, dropout_p, is_causal, training, dtype) 7791 attn_output = attn_output.transpose(2, 0, 1, 3).view((bsz * tgt_len, embed_dim)) 7792 7793 attn_output = linear(attn_output, out_proj_weight, out_proj_bias) 7794 attn_output = attn_output.view((tgt_len, bsz, attn_output.shape[1])) 7795 7796 attn_output_weights = attn_output_weights.view((bsz, num_heads, tgt_len, src_len)) 7797 if average_attn_weights: 7798 attn_output_weights = attn_output_weights.sum(axis=1) / num_heads 7799 7800 if not is_batched: 7801 attn_output = attn_output.squeeze(1) 7802 attn_output_weights = attn_output_weights.squeeze(0) 7803 return attn_output, attn_output_weights 7804 7805 7806def max_pool2d(x, kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False): 7807 r""" 7808 Performs a 2D max pooling on the input Tensor. 7809 7810 Typically, the input is a Tensor with shape :math:`(N_{in}, C_{in}, H_{in}, W_{in})`, outputs 7811 regional maximum in the :math:`(H_{in}, W_{in})`-dimension. Given `kernel_size` 7812 :math:`ks = (h_{ker}, w_{ker})` and `stride` :math:`s = (s_0, s_1)`, the operation is as follows: 7813 7814 .. math:: 7815 \text{output}(N_i, C_j, h, w) = 7816 \max_{m=0, \ldots, h_{ker}-1} \max_{n=0, \ldots, w_{ker}-1} 7817 \text{input}(N_i, C_j, s_0 \times h + m, s_1 \times w + n) 7818 7819 Args: 7820 x (Tensor): Tensor of shape :math:`(N_{in}, C_{in}, H_{in}, W_{in})` with data type of int8, 7821 int16, int32, int64, uint8, uint16, uint32, uint64, float16, float32 or float64 in CPU or GPU 7822 while that of uint16 in Ascend. 7823 kernel_size (Union[int, tuple[int]]): The size of kernel used to take the maximum value and arg 7824 value, is an int number that represents height and width of the kernel, or a tuple of 7825 two int numbers that represent height and width respectively. 7826 stride (Union[int, tuple[int]]): The distance of kernel moving, an int number that represents 7827 the height and width of movement are both stride, or a tuple of two int numbers that 7828 represent height and width of movement respectively. 7829 Default: ``None`` , which indicates the moving step is `kernel_size` . 7830 padding (Union[int, tuple[int]]): An int number that represents the height and width of movement are both 7831 strides, or a tuple of two int numbers that represent height and width of movement respectively. 7832 Default: ``0`` . 7833 dilation (Union[int, tuple[int]]): Control the stride of elements in the kernel. Default: ``1`` . 7834 return_indices (bool): Whether to output the indices of max value. Default: ``False`` . 7835 ceil_mode (bool): Whether to use ceil instead of floor to calculate output shape. Default: ``False`` . 7836 7837 Returns: 7838 If `return_indices` is ``False`` , return a Tensor `output`, else return a tuple (`output`, `argmax`). 7839 7840 - **output** (Tensor) - Maxpooling result, with shape :math:`(N_{out}, C_{out}, H_{out}, W_{out})`. 7841 It has the same data type as `x`. 7842 7843 .. math:: 7844 H_{out} = \left\lfloor\frac{H_{in} + 2 * \text{padding[0]} - \text{dilation[0]} 7845 \times (\text{kernel_size[0]} - 1) - 1}{\text{stride[0]}} + 1\right\rfloor 7846 7847 .. math:: 7848 W_{out} = \left\lfloor\frac{W_{in} + 2 * \text{padding[1]} - \text{dilation[1]} 7849 \times (\text{kernel_size[1]} - 1) - 1}{\text{stride[1]}} + 1\right\rfloor 7850 7851 - **argmax** (Tensor) - Index corresponding to the maximum value. In CPU and GPU, data type is int64 7852 while that is uint16 in Ascend. It will be return only when `return_indices` is True. 7853 7854 Raises: 7855 TypeError: If `x` is not a Tensor. 7856 ValueError: If length of shape of `x` is not equal to 4. 7857 TypeError: If `kernel_size` , `stride` , `padding` or `dilation` is not int or tuple. 7858 ValueError: If `kernel_size`, `stride` or `dilation` is less than 1. 7859 ValueError: If `padding` is less than 0. 7860 ValueError: If `padding` is more than half of `kernel_size`. 7861 TypeError: If `ceil_mode` is not bool. 7862 7863 Supported Platforms: 7864 ``Ascend`` ``GPU`` ``CPU`` 7865 7866 Examples: 7867 >>> import mindspore 7868 >>> import numpy as np 7869 >>> from mindspore import Tensor, ops 7870 >>> x = Tensor(np.arange(20 * 16 * 50 * 32).reshape((20, 16, 50, 32)), mindspore.float32) 7871 >>> output_tensor, argmax = ops.max_pool2d(x, kernel_size=(3, 2), stride=(2, 1), return_indices=True) 7872 >>> print(output_tensor.shape) 7873 (20, 16, 24, 31) 7874 >>> print(argmax.shape) 7875 (20, 16, 24, 31) 7876 """ 7877 strides = stride if (stride is not None) else kernel_size 7878 max_pool_with_argmax_v2_ = _get_cache_prim(NN_OPS.MaxPoolWithArgmaxV2)( 7879 kernel_size, strides, padding, dilation, ceil_mode) 7880 out, indices = max_pool_with_argmax_v2_(x) 7881 if return_indices: 7882 return out, indices 7883 return out 7884 7885 7886def prompt_flash_attention(query, key, value, attn_mask, actual_seq_lengths, actual_seq_lengths_kv, pse_shift, 7887 deq_scale1, quant_scale1, deq_scale2, quant_scale2, quant_offset2, num_heads, 7888 scale_value=1.0, pre_tokens=2147483547, next_tokens=0, input_layout='BSH', 7889 num_key_value_heads=0, sparse_mode=0, inner_precise=1): 7890 r""" 7891 The interface for fully inference. 7892 B -- Batch size 7893 S -- Sequence length 7894 H -- Hidden size 7895 7896 Note: 7897 experiment ops 7898 7899 .. warning:: 7900 This is an experimental API that is subject to change or deletion. 7901 7902 Inputs: 7903 query (Tensor) - The query tensor with data type of float16 or float32. 7904 Input tensor of shape :math:`(B, S, H)` / `(B, N, S, D)`. 7905 key (Tensor) - The key tensor with data type of float16 or float32. 7906 Input tensor of shape :math:`(B, S, H)` / `(B, N, S, D)`. 7907 value (Tensor) - The value tensor with data type of float16 or float32. 7908 Input tensor of shape :math:`(B, S, H)` / `(B, N, S, D)`. 7909 attn_mask (Tensor) - The attention mask tensor with data type of float16 or float32. 7910 For each element, 0 indicates retention and 1 indicates discard. Input tensor of shape :math:`(B, 1, S, S)`. 7911 actual_seq_lengths (Tensor): Describe actual sequence length of each input with data type of int64. 7912 actual_seq_lengths_kv (Tensor): Describe actual sequence length of each input with data type of int64. 7913 pse_shift (Tensor) - The position encoding tensor with data type of float16 or float32. 7914 dep_scale1 (Tensor) 7915 quant_scale1 (Tensor) 7916 deq_scale2 (Tensor) 7917 quant_scale2 (Tensor) 7918 quant_offset2 (Tensor) 7919 num_heads (int): The number of heads. 7920 scale_value (float): The scale value indicating the scale coefficient, which is used as the scalar of 7921 Muls in the calculation. Default: 1.0. 7922 pre_tokens (int): Previous tokens. Default: 2147483547. 7923 next_tokens (int): next tokens. Default: 0. 7924 indicate the upper triangle, Indicate the number of data blocks involved in the calculation. The value 0 7925 indicates that the data blocks in the upper triangle are not involved in the calculation 7926 input_layout (str): the data layout of the input qkv, support `(BSH)` and `(BNSD)`, Default `BSH`. 7927 num_key_value_heads (int): head numbers of key/value which are used in GQA algorithm. 7928 The value o indicates if the key and value have the same head nums, use numHeads. Default: 0. 7929 sparse_mode (int): Default: 0 7930 inner_precise (int): 0, float16 high precision. 1, high performance. default 1 7931 7932 7933 Outputs: 7934 attention_out (Tensor) - Input tensor of shape :math:`(B, S, H)` / `(B, N, S, D)`. 7935 7936 Supported Platforms: 7937 ``Ascend`` 7938 7939 Examples: 7940 >>> from mindspore.ops.function.nn_func import prompt_flash_attention 7941 >>> from mindspore import Tensor 7942 >>> import numpy as np 7943 >>> B = 1 7944 >>> N = 16 7945 >>> S = 256 7946 >>> D = 16 7947 >>> query = Tensor(np.ones((B, N, S, D), dtype=np.float16)) 7948 >>> key = Tensor(np.ones((B, N, S, D), dtype=np.float16)) 7949 >>> value = Tensor(np.ones((B, N, S, D), dtype=np.float16)) 7950 >>> out = ops.prompt_flash_attention(query, key, value, None, None, None, None, None, None, None, None, 7951 None, N, input_layout='BNSD') 7952 >>> print(out.shape) 7953 (1, 16, 256, 16) 7954 """ 7955 7956 pfa = _get_cache_prim(NN_OPS.PromptFlashAttention)(num_heads, scale_value, pre_tokens, next_tokens, input_layout, 7957 num_key_value_heads, sparse_mode, inner_precise) 7958 return pfa(query, key, value, attn_mask, actual_seq_lengths, actual_seq_lengths_kv, pse_shift, deq_scale1, 7959 quant_scale1, deq_scale2, quant_scale2, quant_offset2) 7960 7961 7962def incre_flash_attention(query, key, value, attn_mask, actual_seq_lengths, pse_shift, dequant_scale1, quant_scale1, 7963 dequant_scale2, quant_scale2, quant_offset2, antiquant_scale, antiquant_offset, block_table, 7964 num_heads, input_layout="BSH", scale_value=1.0, num_key_value_heads=0, block_size=0, 7965 inner_precise=1): 7966 r""" 7967 The interface for fully inference. 7968 7969 B -- Batch size 7970 7971 S -- Sequence length 7972 7973 H -- Hidden size 7974 7975 .. warning:: 7976 This is an experimental API that is subject to change or deletion. 7977 If there is no input parameter and no default value, None needs to be passed. 7978 7979 Inputs: 7980 - **query** (Tensor) - The query tensor with data type of float16 or bfloat16. 7981 Input tensor of shape :math:`(B, 1, H)` / :math:`(B, N, 1, D)`. 7982 - **key** (TensorList) - The key tensor with data type of float16 or bfloat16. 7983 Input tensor of shape :math:`(B, S, H)` / :math:`(B, N, S, D)`. 7984 - **value** (TensorList) - The value tensor with data type of float16 or bfloat16. 7985 Input tensor of shape :math:`(B, S, H)` / :math:`(B, N, S, D)`. 7986 - **attn_mask** (Tensor) - The attention mask tensor with data type of float16 or bool. 7987 Input tensor of shape :math:`(B, S)` / :math:`(B, 1, S)` / :math:`(B, 1, 1, S)`. 7988 - **actual_seq_lengths** (Tensor) - Describe actual sequence length of each input with data type of int. 7989 - **pse_shift** (Tensor) - The position encoding tensor with data type of float16 or float32. 7990 - **dequant_scale1** (Tensor) - Quantitative parametor, the tensor with data type of uint64. 7991 - **quant_scale1** (Tensor) - Quantitative parametor, the tensor with data type of float. 7992 - **dequant_scale2** (Tensor) - Quantitative parametor, the tensor with data type of uint64. 7993 - **quant_scale2** (Tensor) - Quantitative parametor, the tensor with data type of float. 7994 - **quant_offset2** (Tensor) - Quantitative parametor, the tensor with data type of float. 7995 - **antiquant_scale** (Tensor) - Quantitative parametor, the tensor with data type of float. 7996 - **antiquant_offset** (Tensor) - Quantitative parametor, the tensor with data type of float. 7997 - **block_table** (Tensor) - The tensor with data type of float. 7998 - **num_heads** (int) - The number of heads. 7999 - **input_layout** (str) - the data layout of the input qkv, support `(BSH)` and `(BNSD)`. Default `BSH`. 8000 - **scale_value** (double) - The scale value indicating the scale coefficient, which is used as the scalar of 8001 Muls in the calculation. Default: 1.0. 8002 - **num_key_value_heads** (int) - head numbers of key/value which are used in GQA algorithm. 8003 The value o indicates if the key and value have the same head nums, use numHeads. Default: 0. 8004 - **block_size** (int) - Default: 0. 8005 - **inner_precise** (int) - Default: 1. 8006 8007 Outputs: 8008 - **attention_out** (Tensor) - Input tensor of shape :math:`(B, 1, H)` / :math:`(B, N, 1, D)`. 8009 8010 Supported Platforms: 8011 ``Ascend`` 8012 """ 8013 8014 _ifa = _get_cache_prim(NN_OPS.IncreFlashAttention)(num_heads, input_layout, scale_value, num_key_value_heads, 8015 block_size, inner_precise) 8016 return _ifa(query, key, value, attn_mask, actual_seq_lengths, pse_shift, dequant_scale1, quant_scale1, 8017 dequant_scale2, quant_scale2, quant_offset2, antiquant_scale, antiquant_offset, block_table) 8018 8019 8020def embedding(input, weight, padding_idx=None, max_norm=None, norm_type=2.0, scale_grad_by_freq=False): 8021 r""" 8022 Retrieve the word embeddings in `weight` using indices specified in `input`. 8023 8024 .. warning:: 8025 On Ascend, the behavior is unpredictable when the value of input is invalid. 8026 8027 Args: 8028 input (Tensor): The indices used to lookup in the `weight`. The data type must be mindspore.int32 or 8029 mindspore.int64, and the value should be in range `[0, weight.shape[0])`. 8030 weight (Parameter): The matrix where to lookup from. The shape must be 2D. 8031 padding_idx (int, optional): If the value is not None, the corresponding row of `weight` will not be updated 8032 in training. The value should be in range `[-weight.shape[0], weight.shape[0])` if it's not ``None``. 8033 Default ``None``. 8034 max_norm (float, optional): If not None, firstly get the p-norm result of the `weight` specified by `input` 8035 where p is specified by `norm_type`; if the result is larger then `max_norm`, update the `weight` 8036 with :math:`\frac{max\_norm}{result+1e^{-7}}` in-place. Default ``None``. 8037 norm_type (float, optional): Indicates the value of p in p-norm. Default ``2.0``. 8038 scale_grad_by_freq (bool, optional): If ``True`` the gradients will be scaled by the inverse of frequency of 8039 the index in `input`. Default ``False``. 8040 8041 Returns: 8042 Tensor, has the same data type as `weight`, the shape is :math:`(*input.shape, weight.shape[1])`. 8043 8044 Raises: 8045 ValueError: If `padding_idx` is out of valid range. 8046 ValueError: If the shape of `weight` is invalid. 8047 TypeError: `weight` is not a :class:`mindspore.Parameter`. 8048 8049 Supported Platforms: 8050 ``Ascend`` 8051 8052 Examples: 8053 >>> import mindspore 8054 >>> import numpy as np 8055 >>> from mindspore import Tensor, Parameter, ops 8056 >>> input = Tensor([[1, 0, 1, 1], [0, 0, 1, 0]]) 8057 >>> weight = Parameter(np.random.randn(3, 3).astype(np.float32)) 8058 >>> output = ops.embedding(input, weight, max_norm=0.4) 8059 >>> print(output) 8060 [[[ 5.49015924e-02, 3.47811311e-01, -1.89771220e-01], 8061 [ 2.09307984e-01, -2.24846993e-02, 3.40124398e-01], 8062 [ 5.49015924e-02, 3.47811311e-01, -1.89771220e-01], 8063 [ 5.49015924e-02, 3.47811311e-01, -1.89771220e-01]], 8064 [[ 2.09307984e-01, -2.24846993e-02, 3.40124398e-01], 8065 [ 2.09307984e-01, -2.24846993e-02, 3.40124398e-01], 8066 [ 5.49015924e-02, 3.47811311e-01, -1.89771220e-01], 8067 [ 2.09307984e-01, -2.24846993e-02, 3.40124398e-01]]] 8068 """ 8069 if not isinstance(weight, Parameter): 8070 raise TypeError(f"For Embedding, the weight must be a mindspore.Parameter, but got {type(weight)}.") 8071 return embedding_op(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq) 8072 8073 8074__all__ = [ 8075 'adaptive_avg_pool1d', 8076 'adaptive_avg_pool2d', 8077 'adaptive_avg_pool3d', 8078 'adaptive_max_pool1d', 8079 'adaptive_max_pool2d', 8080 'adaptive_max_pool3d', 8081 'avg_pool1d', 8082 'avg_pool2d', 8083 'avg_pool3d', 8084 'batch_norm', 8085 'bias_add', 8086 'bidense', 8087 'binary_cross_entropy', 8088 'binary_cross_entropy_with_logits', 8089 'cosine_embedding_loss', 8090 'max_pool2d', 8091 'max_pool3d', 8092 'kl_div', 8093 'celu', 8094 'dense', 8095 'deformable_conv2d', 8096 'dropout1d', 8097 'dropout2d', 8098 'dropout3d', 8099 'embedding', 8100 'fast_gelu', 8101 'fractional_max_pool2d', 8102 'fractional_max_pool3d', 8103 'pixel_shuffle', 8104 'pixel_unshuffle', 8105 'hardshrink', 8106 'is_floating_point', 8107 'flip', 8108 'fliplr', 8109 'flipud', 8110 'intopk', 8111 'interpolate', 8112 'upsample', 8113 'layer_norm', 8114 'log_softmax', 8115 'mish', 8116 'lrn', 8117 'hardswish', 8118 'hardtanh', 8119 'huber_loss', 8120 'softsign', 8121 'softshrink', 8122 'soft_shrink', 8123 'softplus', 8124 'selu', 8125 'silu', 8126 'soft_margin_loss', 8127 'softmax', 8128 'softmin', 8129 'pdist', 8130 'pad', 8131 'prelu', 8132 'mirror_pad', 8133 'cross_entropy', 8134 'grid_sample', 8135 'smooth_l1_loss', 8136 'l1_loss', 8137 'threshold', 8138 'leaky_relu', 8139 'nll_loss', 8140 'ctc_loss', 8141 'ctc_greedy_decoder', 8142 'dropout', 8143 'conv3d_transpose', 8144 'conv1d', 8145 'conv2d', 8146 'conv_transpose2d', 8147 'sigmoid', 8148 'logsigmoid', 8149 'relu', 8150 'relu6', 8151 'rrelu', 8152 'conv3d', 8153 'glu', 8154 'margin_ranking_loss', 8155 'multi_margin_loss', 8156 'multilabel_margin_loss', 8157 'multilabel_soft_margin_loss', 8158 'elu', 8159 'gelu', 8160 'hinge_embedding_loss', 8161 'gaussian_nll_loss', 8162 'lp_pool1d', 8163 'lp_pool2d', 8164 'max_unpool1d', 8165 'max_unpool2d', 8166 'max_unpool3d', 8167 'mse_loss', 8168 'msort', 8169 'triplet_margin_loss', 8170 'channel_shuffle', 8171 'hardsigmoid', 8172 'group_norm', 8173 'rms_norm', 8174] 8175__all__.sort() 8176