Burn Core: burn::tensor::Tensor::unsqueeze

Burn Core: burn::tensor::Tensor::unsqueeze

The Tensor::unsqueeze method increases the rank of a tensor by adding leading dimensions. The type parameter defines the resulting tensor rank. This operation is useful when a lower-rank tensor must be broadcast against a higher-rank tensor.

Unsqueezing does not alter the underlying scalar values. It changes the dimensional interpretation so that shape-compatible operations can proceed. The resulting rank must be greater than or equal to the original rank.

Parameters

Parameter Name Type Description
self Tensor<B, D> The source tensor whose rank will be expanded.
D2 const generic usize The resulting tensor rank after inserting additional dimensions.
let device = B::Device::default();
let bias = Tensor::<B, 1>::ones([8], &device);
let expanded = bias.unsqueeze::<2>();
println!("{expanded}");

Example Output

{
  "status": "success",
  "operation": "unsqueeze",
  "input_rank": 1,
  "output_rank": 2
}

Used In Technical Article

Rust Burn Tensor Manipulation and Gradient Tracking

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top