class paddle.nn. UpsamplingNearest2D ( size=None, scale_factor=None, data_format='NCHW', name=None ) [source]

This op upsamples a batch of images, using nearest neighbours’ pixel values. The input must be a 4-D Tensor of the shape (num_batches, channels, in_h, in_w), where in_w is width of the input tensor, in_h is the height of the input tensor. And the upsampling only applies on the two dimensions(height and width). Nearest neighbor interpolation is to perform nearest neighbor interpolation in both the 3rd dimension(in height direction) and the 4th dimension(in width direction) on input tensor.

For details of nearest neighbor interpolation, please refer to Wikipedia:

  • x (Tensor) – 4-D Tensor, its data type is float32, float64, or uint8, its data format is specified by data_format.

  • size (int|list|tuple|Tensor|None) – Output shape of image resize layer, the shape is (out_h, out_w) when input is a 4-D Tensor. Default: None. If an int value, the out_h and out_w will be set as the number. If a list/tuple, each element can be an integer or a Tensor of shape: [1]. If a Tensor, its dimensions size should be a 1.

  • scale_factor (float|int|list|tuple|Tensor|None) – The multiplier for the input height or width. At least one of size or scale_factor must be set. And size has a higher priority than scale_factor. Has to match input size if it is either a list or a tuple or a Tensor. Default: None.

  • data_format (str, optional) – Specify the data format of the input, and the data format of the output will be consistent with that of the input. An optional string from:NCW, NWC, “NCHW”, “NHWC”, “NCDHW”, “NDHWC”. The default is “NCHW”. When it is “NCHW”, the data is stored in the order of: [batch_size, input_channels, input_height, input_width]. When it is “NCHW”, the data is stored in the order of: [batch_size, input_channels, input_depth, input_height, input_width].

  • name (str, optional) – The default value is None. Normally there is no need for user to set this property. For more information, please refer to Name


A 4-D Tensor of the shape (num_batches, channels, out_h, out_w) or (num_batches, out_h, out_w, channels),


>>> import paddle
>>> import paddle.nn as nn

>>> input_data = paddle.rand(shape=(2, 3, 6, 10)).astype("float32")
>>> upsample_out  = paddle.nn.UpsamplingNearest2D(size=[12, 12])
>>> input = paddle.to_tensor(input_data)
>>> output = upsample_out(x=input)
>>> print(output.shape)
[2, 3, 12, 12]
forward ( x )


Defines the computation performed at every call. Should be overridden by all subclasses.

  • *inputs (tuple) – unpacked tuple arguments

  • **kwargs (dict) – unpacked dict arguments

extra_repr ( )


Extra representation of this layer, you can have custom implementation of your own layer.