def _typecheck_input(x1_type, x2_type): """ Check input tensor types to be valid and confirm they are the same type. """ const_utils.check_type_valid(x1_type, [mstype.float32, mstype.float16], 'x1') const_utils.check_type_valid(x2_type, [mstype.float32, mstype.float16], 'x2') if x1_type != x2_type: raise TypeError(f'Both Inputs must be the same Type. x1 is \'{x1_type}\' and x2 is \'{x2_type}\' ')
def sequence_mask(lengths, maxlen=None): """ Returns a mask tensor representing the first N positions of each cell. If lengths has shape [d_1, d_2, ..., d_n], then the resulting tensor mask has type dtype and shape [d_1, d_2, ..., d_n, maxlen], with mask[i_1, i_2, ..., i_n, j] = (j < lengths[i_1, i_2, ..., i_n]) Inputs: - **lengths** (Tensor) - Tensor to calculate the mask for. All values in this tensor should be less than or equal to `maxlen`. Values greater than `maxlen` will be treated as `maxlen`. Must be type int32 or int64. - **maxlen** (int) - size of the last dimension of returned tensor. Must be positive and same type as elements in `lengths`. Outputs: One mask tensor of shape lengths.shape + (maxlen,). Supported Platforms: ``GPU`` Examples: >>> x = Tensor(np.array([[1, 3], [2, 0]])) >>> output = C.sequence_mask(x, 3) >>> print(output) [[[True, False, False], [True, True, True]], [[True, True, False], [False, False, False]]] """ argmax_op = P.ArgMaxWithValue() reshape_op = P.Reshape() range_op = P.Range() expand_op = P.ExpandDims() cast_op = P.Cast() shape_op = P.Shape() to_tensor_op = P.ScalarToArray() const_utils.check_type_valid(F.dtype(lengths), [mstype.int64, mstype.int32], 'lengths') _check_sequence_mask_input_len(shape_op(lengths)) if maxlen is None: flatten_data = reshape_op(lengths, (-1, )) flatten_data = cast_op(flatten_data, mstype.float32) _, value = argmax_op(flatten_data) maxlen = cast_op(value, mstype.int32) else: maxlen = _check_positive_int(maxlen, "maxlen", "sequence_mask") maxlen = to_tensor_op(maxlen) range_vector = range_op(to_tensor_op(0), maxlen, to_tensor_op(1)) mask = expand_op(lengths, -1) result = range_vector < mask return result
def repeat_elements(x, rep, axis=0): """ Repeat elements of a tensor along an axis, like np.repeat. Args: x (Tensor): The tensor to repeat values for. Must be of type: float16, float32, int8, uint8, int16, int32, or int64. rep (int): The number of times to repeat, must be positive, required. axis (int): The axis along which to repeat, default 0. Outputs: One tensor with values repeated along the specified axis. If x has shape (s1, s2, ..., sn) and axis is i, the output will have shape (s1, s2, ..., si * rep, ..., sn). The output type will be the same as the type of `x`. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> x = Tensor(np.array([[0, 1, 2], [3, 4, 5]]), mindspore.int32) >>> output = C.repeat_elements(x, rep = 2, axis = 0) >>> print(output) [[0 1 2] [0 1 2] [3 4 5] [3 4 5]] """ const_utils.check_type_valid(F.dtype(x), mstype.number_type, 'input x') rep = _check_positive_int(rep, "rep", "repeat_elements") axis = _check_is_int(axis, "axis", "repeat_elements") shape_op = P.Shape() rank_op = P.Rank() tile_op = P.Tile() expand_dims_op = P.ExpandDims() reshape_op = P.Reshape() x_rank = rank_op(x) axis = _check_axis_range(axis, x_rank, "axis", "repeat_elements") expand_axis = axis + 1 x_expand = expand_dims_op(x, expand_axis) rep_dims = _cal_repeat_dims(x_rank, rep, expand_axis) x_expand = tile_op(x_expand, rep_dims) x_shape = shape_op(x) x_reshape = _cal_reshape(x_shape, rep, axis) x_rep = reshape_op(x_expand, x_reshape) return x_rep
def count_nonzero(x, axis=(), keep_dims=False, dtype=mstype.int32): r""" Count number of nonzero elements across axis of input tensor Args: x (Tensor): Input data is used to count non-zero numbers. axis (Union[int, tuple(int), list(int)]): The dimensions to reduce. Only constant value is allowed. Default: (), reduce all dimensions. keep_dims (bool): If true, keep these reduced dimensions and the length is 1. If false, don't keep these dimensions. Default: False. dtype (Union[Number, mstype.bool\_]): The data type of the output tensor. Only constant value is allowed. Default: mstype.int32 Returns: Tensor, number of nonzero element. The data type is dtype. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> input_x = Tensor(np.array([[0, 1, 0], [1, 1, 0]]).astype(np.float32)) >>> nonzero_num = count_nonzero(x=input_x, axis=[0, 1], keep_dims=True, dtype=mstype.int32) >>> print(nonzero_num) [[3]] """ const_utils.check_type_valid(F.dtype(x), mstype.number_type, 'input x') axis = _check_validate_axis(axis, "count_nonzero") keep_dims = _check_validate_keepdims(keep_dims, "count_nonzero") const_utils.check_type_valid(dtype, mstype.number_type + (mstype.bool_, ), 'dtype') not_equal = P.NotEqual() cast = P.Cast() reduce_sum = P.ReduceSum(keep_dims) nonzero_bool = not_equal(x, 0) # ReduceSum only support float16 or float32 tensor. nonzero_val = cast(nonzero_bool, mstype.float32) nonzero_num = cast(reduce_sum(nonzero_val, axis), dtype) return nonzero_num
def construct(self, weights): const_utils.check_type_valid(F.dtype(weights), mstype.number_type, 'weights') l1_regularization = self.scale * self.reduce_sum(self.abs(weights)) return l1_regularization