!49157 update logspace api
Merge pull request !49157 from Henry Shi/logspace
This commit is contained in:
commit
3a1f1b1c4b
|
@ -1,21 +1,33 @@
|
|||
mindspore.ops.logspace
|
||||
======================
|
||||
|
||||
.. py:function:: mindspore.ops.logspace(start, end, steps, base=10.0, dtype=None)
|
||||
.. py:function:: mindspore.ops.logspace(start, end, steps, base=10, *, dtype=mstype.float32)
|
||||
|
||||
返回按照log scale平均分布的一组数值。
|
||||
返回一个大小为 `steps` 的1-D Tensor,其值从 :math:`base^{start}` 到 :math:`base^{end}` ,以 `base` 为底数。
|
||||
|
||||
在线性空间, 数值起始于 :math:`base ** start`,结束于 :math:`base ** end`。
|
||||
.. note::
|
||||
- 输入 `base` 必须是整数。
|
||||
|
||||
.. math::
|
||||
\begin{aligned}
|
||||
&step = (end - start)/(steps - 1)\\
|
||||
&output = [base^{start}, base^{start + 1 * step}, ... , base^{start + (steps-2) * step}, base^{end}]
|
||||
\end{aligned}
|
||||
|
||||
参数:
|
||||
- **start** (Union[int, list(int), tuple(int), Tensor]) - :math:`base ** start` 是点集的起始值。
|
||||
- **end** (Union[int, list(int), tuple(int), Tensor]) - :math:`base ** end` 是点集的结束值。
|
||||
- **steps** (int) - 构造Tensor的大小。
|
||||
- **base** (Union[int, float], 可选) - 对数函数的底。在 :math:`ln(samples) / ln(base)` (或 :math:`log_{base}(samples)`)之间的步长是一致的。默认值是10.0。
|
||||
- **dtype** (Union[:class:`mindspore.dtype`, str], 可选) - 执行计算的数据类型。如果 `dtype` 是None,从入参中推断数据类型。默认值是None。
|
||||
- **start** (Union[float, Tensor]) - 间隔的起始值。
|
||||
- **end** (Union[float, Tensor]) - 间隔的结束值。
|
||||
- **steps** (int) - `steps` 必须为非负整数。
|
||||
- **base** (int,可选) - `base` 必须为非负整数。默认值:10。
|
||||
- **dtype** (mindspore.dtype,可选) - 输出的数据类型。默认值:mstype.float32。
|
||||
|
||||
返回:
|
||||
按照log scale平均分布的一组Tensor数值。
|
||||
Tensor,shape为 :math:`(step, )` ,数据类型由属性 `dtype` 设置。
|
||||
|
||||
异常:
|
||||
- **TypeError** - 若参数的数据类型与上述不一致。
|
||||
- **TypeError** - 若 `start` 不是一个float或Tensor。
|
||||
- **TypeError** - 若 `end` 不是一个float或Tensor。
|
||||
- **TypeError** - 若 `steps` 不是一个整数。
|
||||
- **TypeError** - 若 `base` 不是一个整数。
|
||||
- **ValueError** - 若 `steps` 不是非负整数。
|
||||
- **ValueError** - 若 `base` 不是非负整数。
|
|
@ -168,7 +168,12 @@ class SequentialCell(Cell):
|
|||
self._is_dynamic_name = []
|
||||
if len(args) == 1:
|
||||
cells = args[0]
|
||||
if isinstance(cells, list):
|
||||
if isinstance(cells, Cell):
|
||||
cell = cells
|
||||
self.insert_child_to_cell(str(0), cell)
|
||||
cell.update_parameters_name(str(0) + ".")
|
||||
self._is_dynamic_name.append(True)
|
||||
elif isinstance(cells, list):
|
||||
for index, cell in enumerate(cells):
|
||||
self.insert_child_to_cell(str(index), cell)
|
||||
cell.update_parameters_name(str(index) + ".")
|
||||
|
@ -179,7 +184,7 @@ class SequentialCell(Cell):
|
|||
cell.update_parameters_name(name + ".")
|
||||
self._is_dynamic_name.append(False)
|
||||
else:
|
||||
raise TypeError(f"For '{self.__class__.__name__}', the 'args[0]' must be list or orderedDict, "
|
||||
raise TypeError(f"For '{self.__class__.__name__}', the 'args[0]' must be Cell, list or orderedDict, "
|
||||
f"but got {type(cells).__name__}")
|
||||
else:
|
||||
for index, cell in enumerate(args):
|
||||
|
|
|
@ -4508,66 +4508,53 @@ def heaviside(x, values):
|
|||
return heaviside_(x, values)
|
||||
|
||||
|
||||
def _type_checking_for_xspace(start, end, steps):
|
||||
"""utility parameter checking function for linspace, logspace, geomspace."""
|
||||
array_types = (int, float, bool, list, tuple, Tensor)
|
||||
def _get_type(x):
|
||||
"""get the dtype of input"""
|
||||
if isinstance(x, Tensor):
|
||||
return x.dtype
|
||||
return type(x)
|
||||
if not isinstance(start, array_types):
|
||||
raise TypeError(f"For 'logspace', 'start' should be int, float, bool, list, tuple, Tensor, but got"
|
||||
f" {_get_type(start)}.")
|
||||
if not isinstance(end, array_types):
|
||||
raise TypeError(f"For 'logspace', 'end' should be int, float, bool, list, tuple, Tensor, but got"
|
||||
f" {_get_type(end)}.")
|
||||
if not isinstance(steps, int):
|
||||
raise TypeError(f"For 'logspace', steps should be an integer, but got {_get_type(steps)}.")
|
||||
return start, end, steps
|
||||
def logspace(start, end, steps, base=10, *, dtype=mstype.float32):
|
||||
r"""
|
||||
Returns a Tensor whose value is evenly spaced on a logarithmic scale.
|
||||
|
||||
.. math::
|
||||
\begin{aligned}
|
||||
&step = (end - start)/(steps - 1)\\
|
||||
&output = [base^{start}, base^{start + 1 * step}, ... , base^{start + (steps-2) * step}, base^{end}]
|
||||
\end{aligned}
|
||||
|
||||
def logspace(start, end, steps, base=10.0, dtype=None):
|
||||
"""
|
||||
Returns 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 ** end (see endpoint below).
|
||||
Note:
|
||||
- Input `base` must be integer.
|
||||
|
||||
Args:
|
||||
start (Union[int, list(int), tuple(int), Tensor]): ``base ** start`` is the starting
|
||||
value of the sequence.
|
||||
end (Union[int, list(int), tuple(int), Tensor]): ``base ** end`` is the final value of
|
||||
the sequence.
|
||||
steps (int): Number of samples to generate.
|
||||
base (Union[int, float], optional): The base of the log space. The step size
|
||||
between the elements in :math:`ln(samples) / ln(base)` (or :math:`log_{base}(samples)`)
|
||||
is uniform. Default is 10.0.
|
||||
dtype (Union[:class:`mindspore.dtype`, str], optional): Designated tensor dtype.
|
||||
If `dtype` is None, infer the data type from other input arguments. Default is None.
|
||||
start (Union[float, Tensor]): Start value of interval.
|
||||
end (Union[float, Tensor]): End value of interval.
|
||||
steps (int): The steps must be a non-negative integer.
|
||||
base (int, optional): The base must be a non-negative integer. Default: 10.
|
||||
dtype (mindspore.dtype, optional): The dtype of output. Default: mstype.float32.
|
||||
|
||||
Returns:
|
||||
Tensor, equally spaced on a log scale.
|
||||
Tensor has the shape as (step, ). Its datatype is set by the attr 'dtype'.
|
||||
|
||||
Raises:
|
||||
TypeError: If input arguments have types not specified above.
|
||||
TypeError: If `start` is not a float or a Tensor.
|
||||
TypeError: If `end` is not a float or a Tensor.
|
||||
TypeError: If `steps` is not an int.
|
||||
TypeError: If `base` is not an int.
|
||||
ValueError: If `steps` is not a non-negative integer.
|
||||
ValueError: If `base` is not a non-negative integer.
|
||||
|
||||
Supported Platforms:
|
||||
``Ascend`` ``GPU`` ``CPU``
|
||||
|
||||
Examples:
|
||||
>>> from mindspore import ops
|
||||
>>> print(ops.logspace(0, 5, 6, base=2.0))
|
||||
[ 1. 2. 4. 8. 16. 32.]
|
||||
>>> start = Tensor(1, mindspore.float32)
|
||||
>>> end = Tensor(10, mindspore.float32)
|
||||
>>> output = ops.logspace(start, end, steps = 10, base = 10, dtype=mstype.float32)
|
||||
>>> print(output)
|
||||
[1.e+01 1.e+02 1.e+03 1.e+04 1.e+05 1.e+06 1.e+07 1.e+08 1.e+09 1.e+10]
|
||||
"""
|
||||
start, end, steps = _type_checking_for_xspace(start, end, steps)
|
||||
if not isinstance(base, (int, float, bool)):
|
||||
raise TypeError(f"For 'logspace', 'base' should be a number, but got {base}.")
|
||||
if dtype is None:
|
||||
dtype = mstype.float32
|
||||
linspace_res = linspace_(start, end, steps)
|
||||
_tensor_pow = _get_cache_prim(P.Pow)()
|
||||
return _tensor_pow(base, linspace_res).astype(dtype)
|
||||
if isinstance(start, float):
|
||||
start = Tensor(start, dtype=mstype.float32)
|
||||
if isinstance(end, float):
|
||||
end = Tensor(end, dtype=mstype.float32)
|
||||
logspace_ = _get_cache_prim(P.LogSpace)(steps, base, dtype)
|
||||
return logspace_(start, end)
|
||||
|
||||
|
||||
def logaddexp(x1, x2):
|
||||
|
|
|
@ -41,7 +41,6 @@ def test_net(mode):
|
|||
"""
|
||||
ms.set_context(mode=mode)
|
||||
logspace = Net()
|
||||
output = logspace(Tensor(5, dtype=ms.float32), Tensor(10, dtype=ms.float32), 5)
|
||||
np_out = np.array([1.00000000e+05, 1.77827938e+06, 3.16227760e+07,
|
||||
5.62341312e+08, 1.00000000e+10])
|
||||
output = logspace(Tensor(1, dtype=ms.float32), Tensor(10, dtype=ms.float32), 10)
|
||||
np_out = np.array([1.e+01, 1.e+02, 1.e+03, 1.e+04, 1.e+05, 1.e+06, 1.e+07, 1.e+08, 1.e+09, 1.e+10])
|
||||
assert np.allclose(output.asnumpy(), np_out)
|
||||
|
|
Loading…
Reference in New Issue