torch.nn.functional.upsample
function upsample(input: Tensor, options?: UpsampleOptions): Tensorfunction upsample(input: Tensor, size: number | number[] | undefined, scale_factor: number | number[] | undefined, mode: 'nearest' | 'linear' | 'bilinear' | 'trilinear' | undefined, align_corners: boolean, options?: UpsampleOptions): TensorUpsample: increases spatial dimensions via interpolation (alias for interpolate).
Increases spatial resolution of input tensors using various interpolation methods. Flexible upsampling supporting multiple interpolation modes (nearest, linear, bilinear, trilinear) and both absolute sizing and relative scale factors. Essential for:
- Spatial upsampling in neural networks (decoders, feature pyramids)
- Image/feature map resizing (change resolution with interpolation)
- Multi-scale processing (different resolutions for different features)
- Super-resolution and generative models (smooth upsampling)
- Video processing and temporal upsampling
- Matching spatial dimensions between feature maps
Interpolation Modes:
- nearest: Fastest, uses nearest neighbor (pixelated)
- linear: 1D interpolation (for 3D tensors)
- bilinear: 2D interpolation (smooth, standard for images)
- trilinear: 3D interpolation (for videos/volumetric data)
When to use Upsample:
- Decoder networks (upsampling feature maps)
- U-Net and encoder-decoder architectures
- Smooth spatial resizing (bilinear is standard choice)
- Variable input sizes (use scale_factor for flexibility)
- Fine-tuning spatial alignment between layers
Comparison with alternatives:
- Pixel Shuffle: Channels to spatial (information preserving)
- Transposed Conv: Learnable upsampling (with learned weights)
- Nearest neighbor: Fastest but pixelated results
- Bilinear: Smooth but slower than nearest
- Alias for interpolate: upsample() calls interpolate() (same functionality)
- Flexible sizing: Use either size (absolute) or scale_factor (relative)
- Bilinear default choice: Smooth results for most use cases
- Nearest fastest: Minimal computation, good for rough upsampling
- Mode-dependent: Different interpolation methods give different output quality
- Align corners: Affects how edge pixels are handled
- Differentiable: Gradients flow through for backpropagation
- Size or scale_factor required: One must be specified (not both None)
- Memory intensive: Upsampling large tensors significantly increases memory
- Quality vs speed: Bilinear is smoother but slower than nearest
- Edge artifacts: Bilinear interpolation can create artifacts at boundaries
Parameters
inputTensor- Input tensor to upsample (shape depends on mode)
optionsUpsampleOptionsoptional
Returns
Tensor– Upsampled tensor with specified size/scaleExamples
// Basic upsampling: double spatial resolution
const x = torch.randn(batch, channels, 16, 16);
const upsampled = torch.nn.functional.upsample(x, [32, 32]); // 2x upsampling to 32x32
// Using scale factor instead of absolute size
const x = torch.randn(batch, 64, 16, 16);
const scaled = torch.nn.functional.upsample(x, undefined, 2); // 2x scale → 32x32
// Different interpolation modes
const x = torch.randn(1, 3, 8, 8);
const nearest = torch.nn.functional.upsample(x, [16, 16], undefined, 'nearest'); // Pixelated
const bilinear = torch.nn.functional.upsample(x, [16, 16], undefined, 'bilinear'); // Smooth
// bilinear looks better but slightly slower
// Decoder in U-Net: progressively upsample to match original size
let decoder = encoded; // From encoder, downsampled
decoder = torch.nn.functional.upsample(decoder, undefined, 2); // 2x up
decoder = conv_block(decoder);
decoder = torch.nn.functional.upsample(decoder, undefined, 2); // 4x up
// Progressive upsampling to reconstruct original resolution
// Multi-scale feature pyramid: upsampling to get finer scales
const c5 = coarse_features; // Coarse resolution
const c4 = torch.nn.functional.upsample(c5, undefined, 2); // Finer
const c3 = torch.nn.functional.upsample(c4, undefined, 2); // Even finer
// Feature pyramid networks use this for multi-scale detection
// Options object interface
const upsampled = torch.nn.functional.upsample(x, {
scale_factor: 2,
mode: 'bilinear',
align_corners: false
});See Also
- PyTorch torch.nn.functional.upsample
- torch.nn.functional.interpolate - Core function (upsample is alias)
- torch.nn.functional.pixel_shuffle - Channel-based upsampling (different approach)
- torch.nn.Upsample - Module wrapper
- torch.nn.ConvTranspose2d - Learnable upsampling alternative