def full(shape, fill_value, dtype=None): """ Return a new array of given shape and type, filled with fill_value. Args: shape (Union[int, tuple(int), list(int)]): Shape of the new array, e.g., (2, 3) or 2. fill_value (Union[int, float, bool, list, tuple]): scalar or array_like fill value. dtype (Union[mindspore.dtype, str], optional): Designated array dtype, can be in format of np.float32, or `float32`, if dtype is None, the data type of the new tensor will be inferred from fill_value. Default is None. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Returns: Tensor, with the designated shape and dtype, filled with `fill_value`. Examples: >>> import mindspore.numpy as np >>> print(np.full((2,2), True)) [[True True] [True True]] """ if dtype is None: dtype = array(fill_value).dtype shape = _check_shape(shape) _ = _check_input_for_asarray(fill_value) dtype = _check_dtype(dtype) if isinstance(fill_value, (int, float, bool)) and not _check_shape_contain_zero(shape): return P.Fill()(dtype, shape, fill_value) # if fill_value is array_like or shape contains zero. fall back to original # numpy creation return Tensor(onp.full(shape, fill_value, mindspore.dtype_to_nptype(dtype)))
def test_dtype_to_nptype(): """test_dtype2nptype""" assert ms.dtype_to_nptype(ms.bool_) == np.bool_ assert ms.dtype_to_nptype(ms.int8) == np.int8 assert ms.dtype_to_nptype(ms.int16) == np.int16 assert ms.dtype_to_nptype(ms.int32) == np.int32 assert ms.dtype_to_nptype(ms.int64) == np.int64 assert ms.dtype_to_nptype(ms.uint8) == np.uint8 assert ms.dtype_to_nptype(ms.uint16) == np.uint16 assert ms.dtype_to_nptype(ms.uint32) == np.uint32 assert ms.dtype_to_nptype(ms.uint64) == np.uint64 assert ms.dtype_to_nptype(ms.float16) == np.float16 assert ms.dtype_to_nptype(ms.float32) == np.float32 assert ms.dtype_to_nptype(ms.float64) == np.float64
def logspace(start, stop, num=50, endpoint=True, base=10.0, dtype=None, axis=0): """ Return numbers spaced evenly on a log scale. In linear space, the sequence starts at base ** start (base to the power of start) and ends with base ** stop (see endpoint below). The current implementation is a direct wrapper on top of numpy.logspace, except the default dtype is float32, compare to float64 for numpy, Args: start (Union[int, list(int), tuple(int), tensor]):The starting value of the sequence. stop (Union[int, list(int), tuple(int), tensor]):The end value of the sequence, unless `endpoint` is set to False. In that case, the sequence consists of all but the last of ``num + 1` evenly spaced samples, so that `stop` is excluded. Note that the step size changes when `endpoint` is False. num (int, optional): Number of samples to generate. Default is 50. endpoint (bool, optional): If True, `stop` is the last sample. Otherwise, it is not included. Default is True. base (Union[int, float], optional): The base of the log space. The step size between the elements in ln(samples) / ln(base) (or log_base(samples)) is uniform. Default is 10.0. dtype (Union[mindspore.dtype, str], optional): Designated array dtype, can be in format of np.float32, or `float32`.If `dtype` is None, infer the data type from other input arguments. Default is None. axis (int, optional): The axis in the result to store the samples. Relevant only if start or stop are array-like. By default (0), the samples will be along a new axis inserted at the beginning. Use -1 to get an axis at the end. Default is 0. Returns: samples (Tensor): num samples, equally spaced on a log scale. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> import mindspore.numpy as np >>> print(np.logspace(0, 5, 6, base=2.0)) [ 1. 2. 4. 8. 16. 32.] """ if isinstance(start, Tensor): start = start.asnumpy() if isinstance(stop, Tensor): stop = stop.asnumpy() final_dtype = None if dtype is not None: final_dtype = _check_dtype(dtype) final_dtype = mindspore.dtype_to_nptype(final_dtype) else: final_dtype = onp.float32 dtype = final_dtype out = onp.logspace(start, stop, num, endpoint, base, dtype, axis) tensor_out = Tensor.from_numpy(out) return tensor_out
def linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0): """ Return evenly spaced values within a given interval. The current implementation is a direct wrapper on top of numpy.linspace, except the default dtype is float32, compare to float64 for numpy, Args: start (Union[int, list(int), tuple(int),tensor]):The starting value of the sequence. stop (Union[int, list(int), tuple(int),tensor]):The end value of the sequence, unless `endpoint` is set to False. In that case, the sequence consists of all but the last of ``num + 1` evenly spaced samples, so that `stop` is excluded. Note that the step size changes when `endpoint` is False. num (int, optional): Number of samples to generate. Default is 50. endpoint (bool, optional): If True, `stop` is the last sample. Otherwise, it is not included. Default is True. retstep (bool, optional): If True, return (`samples`, `step`), where `step` is the spacing between samples. dtype (Union[mindspore.dtype, str], optional): Designated array dtype, can be in format of np.float32, or `float32`.If `dtype` is None, infer the data type from other input arguments. Default is None. axis (int, optional): The axis in the result to store the samples. Relevant only if start or stop are array-like. By default (0), the samples will be along a new axis inserted at the beginning. Use -1 to get an axis at the end. Default is 0. Returns: samples (Tensor): There are `num` equally spaced samples in the closed interval ``[start, stop]`` or the half-open interval ``[start, stop)`` (depending on whether `endpoint` is True or False). step (float, optional): Only returned if `retstep` is True. Size of spacing between samples. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> import mindspore.numpy as np >>> print(np.linspace(0, 5, 6)) [0. 1. 2. 3. 4. 5.] """ if isinstance(start, Tensor): start = start.asnumpy() if isinstance(stop, Tensor): stop = stop.asnumpy() final_dtype = None if dtype is not None: final_dtype = _check_dtype(dtype) final_dtype = mindspore.dtype_to_nptype(final_dtype) else: final_dtype = onp.float32 dtype = final_dtype out = onp.linspace(start, stop, num, endpoint, retstep, dtype, axis) if retstep: array_out, step_out = out[0], out[1] tensor_out = Tensor.from_numpy(array_out) return tensor_out, step_out tensor_out = Tensor.from_numpy(out) return tensor_out
def arange(*args, **kwargs): """ Return evenly spaced values within a given interval. Returns `num` evenly spaced samples, calculated over the interval [`start`, `stop`]. The endpoint of the interval can optionally be excluded. The current implementation is a direct wrapper on top of numpy.arange, except the default dtype is float32 and int32, compare to float64 and int64 for numpy implementation. Args: start(Union[int, float], optional): Start of interval. The interval includes this value. Default is 0. stop(Union[int, float], optional): End of interval. The interval does not include this value, except in some cases where step is not an integer and floating point round-off affects the length of out. step(Union[int, float], optional): Spacing between values. For any output out, this is the distance between two adjacent values, out[i+1] - out[i]. The default step size is 1. If step is specified as a position argument, start must also be given. dtype (Union[mindspore.dtype, str], optional): Designated array dtype, can be in format of np.float32, or `float32`. If dtype is None, the data type of the new tensor will be inferred from start, stop and step. Default is None. Returns: arangend Tensor, array of evenly spaced values. Supported Platforms: ``Ascend`` ``GPU`` ``CPU`` Examples: >>> import mindspore.numpy as np >>> print(np.arange(0, 5, 1)) [0 1 2 3 4] """ # infer the dtype, if either of start, end, step is float, default dtype is # float32, else int32. int_flag = True final_dtype = None if args: for item in args: if isinstance(item, float): int_flag = False if kwargs: if ('start' in kwargs and isinstance(kwargs['start'], float)) or \ ('stop' in kwargs and isinstance(kwargs['stop'], float)) or \ ('step' in kwargs and isinstance(kwargs['step'], float)): int_flag = False if int_flag: final_dtype = onp.int32 else: final_dtype = onp.float32 if 'dtype' in kwargs and kwargs['dtype'] is not None: final_dtype = _check_dtype(kwargs['dtype']) final_dtype = mindspore.dtype_to_nptype(final_dtype) kwargs['dtype'] = final_dtype out = onp.arange(*args, **kwargs) out = Tensor.from_numpy(out) return Tensor(out)