class

UpsamplingNearest2d

extendsUpsample
UpsamplingNearest2d(size: int | tuple[int, int] | None = None, scale_factor: float | tuple[float, float] | None = None)
source

Nearest-neighbour upsampling for 4-D tensors (N, C, H, W).

A convenience wrapper around Upsample with mode='nearest' fixed. Accepts either a target size or a scale_factor; the semantics are identical to Upsample.

Nearest-neighbour interpolation assigns each output pixel the value of the spatially nearest input pixel:

y[n,c,i,j]=x ⁣[n,c,iscaleH,jscaleW]y[n, c, i, j] = x\!\left[n,\, c,\, \left\lfloor \frac{i}{\text{scale}_H} \right\rfloor,\, \left\lfloor \frac{j}{\text{scale}_W} \right\rfloor \right]

Parameters

sizeint or tuple[int, int] or None= None
Target output spatial size (H_out, W_out). Mutually exclusive with scale_factor.
scale_factorfloat or tuple[float, float] or None= None
Spatial scale multiplier (s_H, s_W). Mutually exclusive with size.

Notes

  • Input: (N,C,H,W)(N, C, H, W).
  • Output: (N,C,Hout,Wout)(N, C, H_{\text{out}}, W_{\text{out}}).
  • align_corners is not applicable for 'nearest' mode and is always None.
  • This class is marked as deprecated in some reference implementations but remains widely used in legacy codebases and super-resolution architectures.
  • Equivalent to Upsample(size=size, scale_factor=scale_factor, mode='nearest').

Examples

**Double the spatial resolution of a feature map:**
>>> import lucid
>>> import lucid.nn as nn
>>>
>>> up = nn.UpsamplingNearest2d(scale_factor=2)
>>> x = lucid.zeros(1, 32, 7, 7)
>>> up(x).shape
(1, 32, 14, 14)
**Upsample to a fixed size:**
>>> up = nn.UpsamplingNearest2d(size=(224, 224))
>>> x = lucid.zeros(4, 3, 56, 56)
>>> up(x).shape
(4, 3, 224, 224)

Methods (1)

dunder

__init__

None
__init__(size: int | tuple[int, int] | None = None, scale_factor: float | tuple[float, float] | None = None)
source

Initialise the UpsamplingNearest2d module. See the class docstring for parameter semantics.