From 409aa4891fe61c5fd16ff48063aa0ac56d4387eb Mon Sep 17 00:00:00 2001 From: Asthestarsfalll <1186454801@qq.com> Date: Mon, 6 Dec 2021 18:49:54 +0800 Subject: [PATCH 1/2] docs(mge/functional): update functional.ones docstring --- imperative/python/megengine/functional/tensor.py | 45 ++++++++++++++---------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/imperative/python/megengine/functional/tensor.py b/imperative/python/megengine/functional/tensor.py index 040a4981..50027530 100755 --- a/imperative/python/megengine/functional/tensor.py +++ b/imperative/python/megengine/functional/tensor.py @@ -6,7 +6,7 @@ # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -from typing import Iterable, Optional, Sequence, Union +from typing import Iterable, Optional, Sequence, Union, Tuple import numpy as np @@ -148,33 +148,40 @@ def full( return broadcast_to(x, shape) -def ones(shape, dtype="float32", device=None) -> Tensor: - r"""Returns a ones tensor with given shape. +def ones( + shape: Union[int, Tuple[int, ...]], + *, + dtype="float32", + device: Optional[CompNode] = None +) -> Tensor: + r"""Returns a new tensor having a specified shape and filled with ones. Args: - shape: a list, tuple or integer defining the shape of the output tensor. - dtype: the desired data type of the output tensor. Default: ``float32``. - device: the desired device of the output tensor. Default: if ``None``, - use the default device (see :func:`~.megengine.get_default_device`). + shape (int or sequence of ints): the shape of the output tensor. + + Keyword args: + dtype (:attr:`.Tensor.dtype`): output tensor data type. Default: ``float32``. + device (:attr:`.Tensor.device`): device on which to place the created tensor. Default: ``None``. Returns: - output tensor. + a tensor containing ones. Examples: - .. testcode:: + >>> megengine.functional.ones(5) + Tensor([1. 1. 1. 1. 1.], device=xpux:0) - import megengine.functional as F - - out = F.ones((2, 1)) - print(out.numpy()) - - Outputs: - - .. testoutput:: + >>> megengine.functional.ones((5, ), dtype='int32') + Tensor([1 1 1 1 1], dtype=int32, device=xpux:0) + + >>> megengine.functional.ones((2, 2)) + Tensor([[1. 1.] + [1. 1.]], device=xpux:0) + + >>> megengine.functional.ones([2, 1]) + Tensor([[1.] + [1.]], device=xpux:0) - [[1.] - [1.]] """ return full(shape, 1.0, dtype=dtype, device=device) From 541bf3af29520b1caee552012da7568afb206632 Mon Sep 17 00:00:00 2001 From: Asthestarsfalll <1186454801@qq.com> Date: Wed, 8 Dec 2021 12:34:25 +0800 Subject: [PATCH 2/2] fix(mge/functional): fix one_hot irregular coding style --- imperative/python/megengine/functional/nn.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/imperative/python/megengine/functional/nn.py b/imperative/python/megengine/functional/nn.py index c127cc57..36f13632 100644 --- a/imperative/python/megengine/functional/nn.py +++ b/imperative/python/megengine/functional/nn.py @@ -1587,8 +1587,8 @@ def one_hot(inp: Tensor, num_classes: int) -> Tensor: [0 0 1 0] [0 0 0 1]] """ - zeros_tensor = zeros(list(inp.shape) + [num_classes], inp.dtype, inp.device) - ones_tensor = ones(list(inp.shape) + [1], inp.dtype, inp.device) + zeros_tensor = zeros(list(inp.shape) + [num_classes], dtype=inp.dtype, device=inp.device) + ones_tensor = ones(list(inp.shape) + [1], dtype=inp.dtype, device=inp.device) op = builtin.IndexingSetOneHot(axis=inp.ndim) (result,) = apply(op, zeros_tensor, inp, ones_tensor)