class
UpsamplingNearest2d
extends
UpsampleUpsamplingNearest2d(size: int | tuple[int, int] | None = None, scale_factor: float | tuple[float, float] | None = None)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:
Parameters
sizeint or tuple[int, int] or None= NoneTarget output spatial size
(H_out, W_out). Mutually exclusive
with scale_factor.scale_factorfloat or tuple[float, float] or None= NoneSpatial scale multiplier
(s_H, s_W). Mutually exclusive with
size.Notes
- Input: .
- Output: .
align_cornersis not applicable for'nearest'mode and is alwaysNone.- 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)Initialise the UpsamplingNearest2d module. See the class docstring for parameter semantics.