torch.full_like
function full_like<S extends Shape, D extends DType = DType, Dev extends DeviceType = DeviceType>(input: Tensor<S, D, Dev>, fillValue: number, options: TensorOptions = {}): Tensor<S, D, Dev>Create a constant-filled tensor with the same shape as another tensor.
Returns a new tensor with identical shape to the input but filled with a constant value.
Combines the shape-matching convenience of "_like" functions with the constant-fill
behavior of full(). Useful for masks, padding, or initialization with specific values.
By default, matches input's dtype, device, and gradient requirements. Override any of these with the options parameter.
- Scalar fill: All elements identical to fillValue
- Shape matching: Automatically infers shape from input
- Dtype conversion: fillValue converted to specified dtype
- Common uses: Masking, padding, normalization constants
Parameters
inputTensor<S, D, Dev>- Tensor to match shape from
fillValuenumber- Constant value to fill all elements with
optionsTensorOptionsoptional- Optional overrides: -
dtype: Data type (default: same as input) -requires_grad: Gradient tracking (default: same as input) -device: Compute device (default: same as input)
Returns
Tensor<S, D, Dev>– A constant-filled tensor with shape = input.shapeExamples
// Match input shape with specific value
const x = torch.randn(3, 4, 5);
const filled = torch.full_like(x, 2.5); // [3, 4, 5] of 2.5s
// Create padding
const padding = torch.full_like(image, 0.0); // Pad with zeros
// Attention mask (large negative values)
const attn_mask = torch.full_like(attention_scores, -1e9); // Softmax will zero out
// Initialize with specific dtype
const int_mask = torch.full_like(x, 1, { dtype: 'int32' });
// Create constant for numerical operations
const epsilon = torch.full_like(variance, 1e-5); // Stability constantSee Also
- PyTorch torch.full_like()
- full - Create constant tensor with explicit shape
- zeros_like - Zero-filled tensor with same shape
- ones_like - One-filled tensor with same shape
- empty_like - Uninitialized tensor with same shape