Hpt
Home
GitHub
crate.io
Benchmarks
Home
GitHub
crate.io
Benchmarks
  • Docs

    • unary

      • sin
      • sin_
      • cos
      • cos_
      • tan
      • tan_
      • sinh
      • sinh_
      • cosh
      • cosh_
      • tanh
      • tanh_
      • asinh
      • asinh_
      • acosh
      • acosh_
      • atanh
      • atanh_
      • asin
      • asin_
      • acos
      • acos_
      • atan
      • atan_
      • exp
      • exp_
      • exp2
      • exp2_
      • sqrt
      • sqrt_
      • recip
      • recip_
      • ln
      • ln_
      • log2
      • log2_
      • log10
      • log10_
      • celu
      • celu_
      • sigmoid
      • sigmoid_
      • elu
      • elu_
      • erf
      • gelu
      • gelu_
      • selu
      • selu_
      • hard_sigmoid
      • hard_sigmoid_
      • hard_swish
      • hard_swish_
      • softplus
      • softplus_
      • softsign
      • softsign_
      • mish
      • mish_
      • cbrt
      • cbrt_
      • sincos
      • sincos_
      • exp10
      • exp10_
    • binary

      • add
      • add_
      • sub
      • sub_
      • mul
      • mul_
      • div
      • div_
      • rem
      • rem_
      • pow
      • pow_
      • hypot
      • hypot_
    • reduce

      • logsumexp
      • argmin
      • argmax
      • max
      • min
      • mean
      • sum
      • sum_
      • nansum
      • nansum_
      • prod
      • nanprod
      • sum_square
      • reducel1
      • reducel2
      • reducel3
      • all
      • any
    • conv

      • cuda

        • conv2d_group
        • conv2d
        • dwconv2d
        • batchnorm_conv2d
      • cpu

        • batchnorm_conv2d
        • conv2d_group
        • conv2d_transpose
        • conv2d
        • dwconv2d
    • pooling

      • maxpool2d
      • avgpool2d
      • adaptive_maxpool2d
      • adaptive_avgpool2d
    • compare

      • tensor_neq
      • tensor_eq
      • tensor_gt
      • tensor_lt
      • tensor_ge
      • tensor_le
    • advanced

      • scatter
      • hardmax
      • tensor_where
      • topk
      • onehot
    • normalization

      • log_softmax
      • layernorm
      • softmax
    • cumulative

      • cumsum
      • cumprod
    • regularization

      • dropout
      • shrinkage
    • linalg

      • matmul
      • matmul_post
      • gemm
      • tensordot
    • random

      • randn
      • randn_like
      • rand
      • rand_like
      • beta
      • beta_like
      • chisquare
      • chisquare_like
      • exponential
      • exponential_like
      • gamma
      • gamma_like
      • gumbel
      • gumbel_like
      • lognormal
      • lognormal_like
      • normal_gaussian
      • normal_gaussian_like
      • pareto
      • pareto_like
      • poisson
      • poisson_like
      • weibull
      • weibull_like
      • zipf
      • zipf_like
      • triangular
      • triangular_like
      • bernoulli
      • randint
      • randint_like
    • shape manipulate

      • squeeze
      • unsqueeze
      • reshape
      • transpose
      • permute
      • permute_inv
      • expand
      • t
      • mt
      • flip
      • fliplr
      • flipud
      • tile
      • trim_zeros
      • repeat
      • split
      • dsplit
      • hsplit
      • vsplit
      • swap_axes
      • flatten
      • concat
      • vstack
      • hstack
      • dstack
    • creation

      • empty
      • zeros
      • ones
      • empty_like
      • zeros_like
      • ones_like
      • full
      • full_like
      • arange
      • arange_step
      • eye
      • linspace
      • logspace
      • geomspace
      • tri
      • tril
      • triu
      • identity
    • windows

      • hamming_window
      • hann_window
      • blackman_window
    • iterator

      • par_iter
      • par_iter_mut
      • par_iter_simd
      • par_iter_simd_mut
      • strided_map
      • strided_map_simd
      • collect
    • utils

      • set_display_elements
      • resize_cpu_lru_cache
      • resize_cuda_lru_cache
      • set_seed
      • num_threads
    • associated methods

      • cpu

        • forget
        • forget_copy
        • from_raw
        • to_cuda
      • cuda

        • forget
        • forget_copy
        • from_raw
        • to_cpu
      • all_close
      • astype
    • custom type
    • custom allocator
    • slice
    • save/load

expand

expand(
    x: &Tensor<T>,
    shape: 
        &[i64]
        | &[i64; _]
        | [i64; _] 
        | Vec<i64> 
        | &Vec<i64>
) -> Result<Tensor<T>, TensorError>

Expands the tensor to a larger size, replicating the data along specified dimensions.

Parameters:

x: Input tensor

shape: The desired expanded shape. Must be compatible with the input tensor's shape, where each dimension must either be equal to the input dimension or the input dimension must be 1.

Returns:

A new tensor with expanded dimensions.

Examples:

use hpt::{
    common::TensorInfo,
    error::TensorError,
    ops::{ShapeManipulate, TensorCreator},
    Tensor,
};
fn main() -> Result<(), TensorError> {
    // Create a tensor with shape [1, 3, 1]
    let a = Tensor::<f32>::zeros(&[1, 3, 1])?;

    // Expand to shape [2, 3, 4]
    let b = a.expand(&[2, 3, 4])?;
    println!("{}", b.shape());

    // This will return an error as we can't expand dimension 1 from 3 to 4
    let c = a.expand(&[2, 4, 4]);
    assert!(c.is_err());

    Ok(())
}

Backend Support

BackendSupported
CPU✅
Cuda✅
最近更新: 2025/6/24 21:23
Contributors: Jianqoq
Prev
permute_inv
Next
t