unsqueeze¶
- paddle. unsqueeze ( x, axis, name=None ) [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
dygraphmode. If you want to use the Tensor copy version, please use Tensor.clone likeunsqueeze_clone_x = x.unsqueeze(-1).clone().- Parameters
-
x (Tensor) – The input Tensor to be unsqueezed. Supported data type: bfloat16, float16, float32, float64, bool, int8, int32, int64.
axis (int|list|tuple|Tensor) – Indicates the dimensions to be inserted. The data type is
int32. Ifaxisis a list or tuple, each element of it should be integer or 0-D Tensor with shape []. Ifaxisis a Tensor, it should be an 1-D Tensor . Ifaxisis negative,axis = axis + ndim(x) + 1.name (str|None) – Name for this layer. Please refer to 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.)
