torch.nn.ConstantPad1d
class ConstantPad1d extends Modulenew ConstantPad1d(padding: Padding1D, value: number)
- readonly
padding([number, number]) - readonly
value(number)
1D constant padding: pads with a custom constant value.
Extends sequences by filling with a user-specified constant value (not just zeros). Useful when you need to pad with values other than zero (e.g., edge values, means, etc.).
When to use ConstantPad1d:
- Padding with values other than zero (e.g., mean value)
- Special boundary value important to problem
- Custom padding value in preprocessing pipeline
- When zero padding shows bias in specific domains
Trade-offs:
- vs ZeroPad: ConstantPad flexible; zero is simplest (always 0)
- vs ReflectionPad: ConstantPad for custom values; reflection natural
- Computation: Simple (just use constant value)
- Flexibility: Can pad with any value
- Flexible padding: Any constant value acceptable
- Custom preprocessing: Different domains may need different values
- Zero alternative: When zero padding biased in your domain
Examples
// Pad with mean value
const mean_value = 0.5;
const pad = new torch.nn.ConstantPad1d(2, mean_value);
const x = torch.randn([32, 64, 100]);
const padded = pad.forward(x); // Padded with 0.5 (not zero)// Pad with edge value
const pad = new torch.nn.ConstantPad1d(3, -1); // Use -1 for padding
const signal = torch.randn([1, 1, 1000]);
const padded = pad.forward(signal);