torch.nn.ReplicationPad2d
class ReplicationPad2d extends Modulenew ReplicationPad2d(padding: Padding2D)
- readonly
padding([number, number, number, number])
2D replication padding: pads images by repeating edge pixels.
Extends images by replicating edge pixel values. Simpler than reflection, good when you assume image naturally continues at boundary. Creates visible edge repetition pattern.
When to use ReplicationPad2d:
- Simpler alternative to reflection for images
- Tasks assuming boundary pixels continue naturally
- Performance-critical applications
- General image processing (when artifacts acceptable)
Trade-offs:
- vs ReflectionPad2d: Replication simpler; reflection smoother
- vs ZeroPad2d: Replication preserves edge info; zero adds black border
- Computation: Simple pixel repetition
- Visible edge: Creates obvious repeated edge pattern
- Simple edge repeat: Edge pixels duplicated for padding
- Visible repetition: Creates noticeable pattern at boundary
- No reflection limit: Can pad any amount
- Fast: Simpler computation than reflection
Examples
// Image replication padding
const pad = new torch.nn.ReplicationPad2d(2);
const img = torch.randn([32, 3, 224, 224]);
const padded = pad.forward(img); // [32, 3, 228, 228]
// Edges replicate: left column repeated, top row repeated