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

onehot

onehot(
    x: &Tensor<T>,
    depth: usize,
    axis: i64,
    true_val: T,
    false_val: T
) -> Result<Tensor<T>, TensorError>

Creates a one-hot tensor from the input tensor. The output tensor will have an additional dimension of size depth inserted at axis, where indices from the input tensor select which index in this dimension gets the true_val value while all other indices get the false_val value.

Parameters:

x: Input tensor containing indices.

depth: Size of the new dimension.

axis: Position to insert the new dimension. Supports negative indexing.

true_val: Value to place at the index specified by the input tensor.

false_val: Value to place at all other indices.

Returns:

A new tensor with an additional dimension of size depth.

Examples:

use hpt::{Tensor, error::TensorError, ops::AdvancedOps};

fn main() -> Result<(), TensorError> {
    let indices = Tensor::<i64>::new(&[1, 0, 2]);
    
    // Create one-hot encoding with depth 3
    let onehot = indices.onehot(3, -1, 1, 0)?;
    println!("One-hot encoding:\n{}", onehot);
    // Output:
    // [[0, 1, 0],
    //  [1, 0, 0],
    //  [0, 0, 1]]
    
    Ok(())
}
最近更新: 2025/6/24 21:23
Contributors: Jianqoq
Prev
topk