-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refine unsqueeze, test=develop #25470
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
de1599e
refine unsqueeze, test=develop
zhiqiu 254c899
update unsqueeze, test=develop
zhiqiu 5d46976
refine unsqueeze, test=develop
zhiqiu aa9118f
refine unsqueeze, test=develop
zhiqiu d28b24d
update
zhiqiu b12d692
remove None, test=develop
zhiqiu 3c4444e
follow comments
zhiqiu 32ec529
support bool
zhiqiu a802366
update doc
zhiqiu 6c16d80
follow comments
zhiqiu 6860851
merge develop
zhiqiu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,11 +42,32 @@ | |
import paddle | ||
|
||
__all__ = [ | ||
'cast', 'concat', 'expand', 'expand_as', 'flatten', 'gather', 'gather_nd', | ||
'reshape', 'reverse', 'scatter', 'scatter_nd_add', 'scatter_nd', | ||
'shard_index', 'slice', 'split', 'squeeze', 'stack', 'strided_slice', | ||
'transpose', 'unique', 'unique_with_counts', 'unsqueeze', 'unstack', 'flip', | ||
'unbind', 'roll' | ||
'cast', | ||
'concat', | ||
'expand', | ||
'expand_as', | ||
'flatten', | ||
'gather', | ||
'gather_nd', | ||
'reshape', | ||
'reverse', | ||
'scatter', | ||
'scatter_nd_add', | ||
'scatter_nd', | ||
'shard_index', | ||
'slice', | ||
'split', | ||
'squeeze', | ||
'stack', | ||
'strided_slice', | ||
'transpose', | ||
'unique', | ||
'unique_with_counts', | ||
'unsqueeze', | ||
'unstack', | ||
'flip', | ||
'unbind', | ||
'roll', | ||
] | ||
|
||
|
||
|
@@ -417,7 +438,7 @@ def stack(x, axis=0, name=None): | |
Args: | ||
x (Tensor|list[Tensor]): Input ``x`` can be a single tensor, or a ``list`` of tensors. | ||
If ``x`` is a ``list``, the Tensors in ``x`` | ||
must be of the same shape and dtype. Support data types: float32, float64, int32, int64. | ||
must be of the same shape and dtype. Supported data types: float32, float64, int32, int64. | ||
axis (int, optional): The axis along which all inputs are stacked. ``axis`` range is ``[-(R+1), R+1)``, | ||
where ``R`` is the number of dimensions of the first input tensor ``x[0]``. | ||
If ``axis < 0``, ``axis = axis+R+1``. The default value of axis is 0. | ||
|
@@ -559,18 +580,19 @@ def squeeze(x, axis=None, name=None): | |
out.shape = [1, 3, 5] | ||
|
||
Args: | ||
input (Tensor): The input Tensor. Support data type: float32, float64, int8, int32, int64. | ||
x (Tensor): The input Tensor. Supported data type: float32, float64, bool, int8, int32, int64. | ||
axis (int|list|tuple, optional): An integer or list of integers, indicating the dimensions to be squeezed. Default is None. | ||
The range of axis is :math:`[-ndim(input), ndim(input))`. | ||
If axis is negative, :math:`axis = axis + ndim(input)`. | ||
If axis is None, all the dimensions of input of size 1 will be removed. | ||
The range of axis is :math:`[-ndim(x), ndim(x))`. | ||
If axis is negative, :math:`axis = axis + ndim(x)`. | ||
If axis is None, all the dimensions of x of size 1 will be removed. | ||
name (str, optional): Please refer to :ref:`api_guide_Name`, Default None. | ||
|
||
Returns: | ||
Tensor: Output squeezed Tensor. Data type is same as input Tensor. | ||
Tensor: Squeezed Tensor with the same data type as input Tensor. | ||
|
||
Examples: | ||
.. code-block:: python | ||
|
||
import paddle | ||
|
||
paddle.enable_imperative() | ||
|
@@ -590,87 +612,50 @@ def squeeze(x, axis=None, name=None): | |
return layers.squeeze(x, axis, name) | ||
|
||
|
||
def unsqueeze(input, axes, out=None, name=None): | ||
def unsqueeze(x, axis, name=None): | ||
""" | ||
:alias_main: paddle.unsqueeze | ||
:alias: paddle.unsqueeze,paddle.tensor.unsqueeze,paddle.tensor.manipulation.unsqueeze | ||
|
||
Insert single-dimensional entries to the shape of a Tensor. Takes one | ||
required argument axes, a list of dimensions that will be inserted. | ||
Dimension indices in axes are as seen in the output tensor. | ||
|
||
For example: | ||
|
||
.. code-block:: text | ||
:alias: paddle.unsqueeze, paddle.tensor.unsqueeze, paddle.tensor.manipulation.unsqueeze | ||
|
||
Given a tensor such that tensor with shape [3, 4, 5], | ||
then Unsqueezed tensor with axes=[0, 4] has shape [1, 3, 4, 5, 1]. | ||
Insert single-dimensional entries to the shape of input Tensor ``x``. Takes one | ||
required argument axis, a dimension or list of dimensions that will be inserted. | ||
Dimension indices in axis are as seen in the output tensor. | ||
|
||
Args: | ||
input (Variable): The input Tensor to be unsqueezed. It is a N-D Tensor of data types float32, float64, int32. | ||
axes (int|list|tuple|Variable): Indicates the dimensions to be inserted. The data type is ``int32`` . If ``axes`` is a list or tuple, the elements of it should be integers or Tensors with shape [1]. If ``axes`` is an Variable, it should be an 1-D Tensor . | ||
name (str|None): Name for this layer. | ||
x (Tensor): The input Tensor to be unsqueezed. Supported data type: float32, float64, bool, int8, int32, int64. | ||
axis (int|list|tuple|Tensor): Indicates the dimensions to be inserted. The data type is ``int32`` . | ||
If ``axis`` is a list or tuple, the elements of it should be integers or Tensors with shape [1]. | ||
If ``axis`` is a Tensor, it should be an 1-D Tensor . | ||
If ``axis`` is negative, ``axis = axis + ndim(x) + 1``. | ||
name (str|None): Name for this layer. Please refer to :ref:`api_guide_Name`, Default None. | ||
|
||
Returns: | ||
Variable: Output unsqueezed Tensor, with data type being float32, float64, int32, int64. | ||
Tensor: Unsqueezed Tensor with the same data type as input Tensor. | ||
|
||
Examples: | ||
.. code-block:: python | ||
import numpy as np | ||
|
||
import paddle | ||
import paddle.fluid as fluid | ||
|
||
with fluid.dygraph.guard(): | ||
input_1 = np.random.random([5, 10]).astype("int32") | ||
# input is a variable which shape is [5, 10] | ||
input = fluid.dygraph.to_variable(input_1) | ||
paddle.enable_imperative() | ||
x = paddle.rand([5, 10]) | ||
print(x.shape) # [5, 10] | ||
|
||
out1 = paddle.unsqueeze(x, axis=0) | ||
print(out1.shape) # [1, 5, 10] | ||
|
||
out2 = paddle.unsqueeze(x, axis=[0, 2]) | ||
print(out2.shape) # [1, 5, 1, 10] | ||
|
||
output = paddle.unsqueeze(input, axes=[1]) | ||
# output.shape [5, 1, 10] | ||
axis = paddle.fluid.dygraph.to_variable([0, 1, 2]) | ||
out3 = paddle.unsqueeze(x, axis=axis) | ||
print(out3.shape) # [1, 1, 1, 5, 10] | ||
|
||
""" | ||
if not isinstance(axes, (int, list, tuple, Variable)): | ||
raise TypeError( | ||
"The type of 'axes' in unsqueeze must be int, list, tuple or Variable, but " | ||
"received %s." % (type(axes))) | ||
helper = LayerHelper("unsqueeze2", **locals()) | ||
inputs = {"X": input} | ||
attrs = {} | ||
|
||
def _to_Variable_list(one_list): | ||
Variable_list = [] | ||
for ele in one_list: | ||
if isinstance(ele, Variable): | ||
ele.stop_gradient = True | ||
Variable_list.append(ele) | ||
else: | ||
assert (isinstance(ele, int)) | ||
temp_out = helper.create_variable_for_type_inference('int32') | ||
fill_constant([1], 'int32', ele, force_cpu=True, out=temp_out) | ||
Variable_list.append(temp_out) | ||
return Variable_list | ||
|
||
if isinstance(axes, int): | ||
axes = [axes] | ||
if isinstance(axes, Variable): | ||
axes.stop_gradient = True | ||
inputs["AxesTensor"] = axes | ||
elif isinstance(axes, (list, tuple)): | ||
contain_var = not all(not isinstance(ele, Variable) for ele in axes) | ||
if contain_var: | ||
inputs["AxesTensorList"] = _to_Variable_list(axes) | ||
else: | ||
attrs["axes"] = axes | ||
|
||
out = helper.create_variable_for_type_inference(dtype=input.dtype) | ||
x_shape = helper.create_variable_for_type_inference(dtype=input.dtype) | ||
helper.append_op( | ||
type="unsqueeze2", | ||
inputs=inputs, | ||
attrs=attrs, | ||
outputs={"Out": out, | ||
"XShape": x_shape}) | ||
if isinstance(axis, int): | ||
axis = [axis] | ||
|
||
return out | ||
return layers.unsqueeze(x, axis, name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 报错问题是怎么解决的? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
|
||
def gather(input, index, overwrite=True): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
应该有一个axis含有tensor的例子
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好的