unsqueeze
- paddle. unsqueeze ( x: Tensor, axis: int | Sequence[Tensor | int] | Tensor, name: str | None = None ) Tensor [source]
-
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.Note that the output Tensor will share data with origin Tensor and doesn’t have a Tensor copy in
dygraph
mode. If you want to use the Tensor copy version, please use Tensor.clone likeunsqueeze_clone_x = x.unsqueeze(-1).clone()
.Note
Alias Support: The parameter name
input
can be used as an alias forx
, anddim
can be used as an alias foraxis
. For example,unsqueeze(input=tensor_x, dim=1)
is equivalent tounsqueeze(x=tensor_x, axis=1)
.- Parameters
-
x (Tensor) – The input Tensor to be unsqueezed. Supported data type: bfloat16, float16, float32, float64, bool, int8, int32, int64. alias:
input
.axis (int|list|tuple|Tensor) –
-
Indicates the dimensions to be inserted. The data type is
int32
. -
If
axis
is a list or tuple, each element of it should be integer or 0-D Tensor with shape []. Ifaxis
is a Tensor, it should be an 1-D Tensor . Ifaxis
is negative,axis = axis + ndim(x) + 1
.
alias:
dim
.-
Indicates the dimensions to be inserted. The data type is
name (str|None, optional) – Name for this layer. Please refer to api_guide_Name, Default None.
- Returns
-
Tensor, Unsqueezed Tensor with the same data type as input Tensor.
Examples
>>> import paddle >>> 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] >>> axis = paddle.to_tensor([0, 1, 2]) >>> out3 = paddle.unsqueeze(x, axis=axis) >>> print(out3.shape) [1, 1, 1, 5, 10] >>> # out1, out2, out3 share data with x in dygraph mode >>> x[0, 0] = 10. >>> print(out1[0, 0, 0]) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 10.) >>> print(out2[0, 0, 0, 0]) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 10.) >>> print(out3[0, 0, 0, 0, 0]) Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True, 10.)