torch.Tensor.Tensor.view_as
Returns a view of this tensor with the same shape as another tensor.
Creates a new view with the shape of the provided tensor, without copying data. The total number of elements must match. Useful for:
- Matching shapes between tensors for broadcasting
- Creating views with shapes from reference tensors
- Reshaping to match batch dimensions
- Shape inference and matching
- Total number of elements must be the same as other
- This creates a view, not a copy (shares data with original)
Parameters
otherTensor<O>- Reference tensor whose shape to use
Returns
Tensor<O, D, Dev>– Tensor view with same shape as other, same data as thisExamples
const x = torch.randn(12);
const shape_ref = torch.randn(3, 4);
x.view_as(shape_ref); // Reshape [12] to [3, 4]
// Match shapes between tensors
const loss = torch.randn(1);
const target = torch.randn(5, 10);
loss.view_as(target); // Reshape loss to broadcast with targetSee Also
- PyTorch torch.view_as()
- view - Reshape to specific shape
- reshape - Similar but may copy data