bunsen 0.34.2

bunsen is a batteries included common library for burn
Documentation
//! [`Tensor`] ops.

use burn::{
    prelude::{
        Backend,
        Tensor,
    },
    tensor::TensorCreationOptions,
};

/// Creates a 1D tensor `[for i in 0..n | start + i * step]`.
///
/// # Arguments
/// * `num`: the number a points in the result tensor.
/// * `start`: the start value.
/// * `step`: the step size.
///
/// # Returns
/// `[num]` sized vector.
pub fn vec_arange_start_step(
    num: usize,
    start: f64,
    step: Option<f64>,
) -> Vec<f64> {
    let step = step.unwrap_or(1.0);
    (0..num)
        .into_iter()
        .map(|n| start + n as f64 * step)
        .collect()
}

/// Creates a vector with evenly spaced floating point values.
///
/// This function generates a vector with `num` values starting from `start`,
/// ending at `end`, and evenly spaced.
///
/// # Arguments
///
/// - `start`: The starting value of the range.
/// - `end`: The end value of the range (inclusive).
/// - `num`: The number of points to generate in the range.
///
/// If `num` == 1; the result will be `[start]`.
///
/// # Returns
///
/// A vector containing the generated floating point values.
pub fn vec_linspace(
    start: f64,
    end: f64,
    num: usize,
) -> Vec<f64> {
    assert!(num > 0, "Number of points must be positive");

    if num == 1 {
        return vec![start];
    }

    let step = (end - start) / (num as f64 - 1.0);
    (0..num)
        .into_iter()
        .map(|n| start + (n as f64 * step))
        .collect()
}

/// Creates a 1D tensor `[for i in 0..n | start + i * step]`.
///
/// # Arguments
/// * `num`: the number a points in the result tensor.
/// * `start`: the start value.
/// * `step`: the step size.
/// * `options`: the tensor creation options.
///
/// # Returns
/// `[num]` sized tensor.
pub fn tensor_arange_start_step<B: Backend>(
    num: usize,
    start: f64,
    step: Option<f64>,
    options: impl Into<TensorCreationOptions<B>>,
) -> Tensor<B, 1> {
    let x = Tensor::arange(0..num as i64, options).float();

    let x = match step {
        None => x,
        Some(step) => x.mul_scalar(step),
    };
    x.add_scalar(start)
}

/// Creates a 1D tensor with evenly spaced floating point values.
///
/// This function generates a tensor with `num` values starting from `start`,
/// ending at `end`, and evenly spaced.
///
/// # Arguments
///
/// * `start`: The starting value of the range.
/// * `end`: The end value of the range (inclusive).
/// * `num`: The number of points to generate in the range.
/// * `options`: the tensor creation options.
///
/// If `num` == 1; the result will be `[start]`.
///
/// # Returns
///
/// A 1D tensor containing the generated floating point values.
pub fn tensor_linspace<B: Backend>(
    start: f64,
    end: f64,
    num: usize,
    options: impl Into<TensorCreationOptions<B>>,
) -> Tensor<B, 1> {
    assert!(num > 0, "Number of points must be positive");

    if num == 1 {
        return Tensor::full([1], start, options);
    }

    let step = (end - start) / (num as f64 - 1.0);
    let x = Tensor::arange(0..num as i64, options).float();
    let x = x.mul_scalar(step);
    x.add_scalar(start)
}

#[cfg(test)]
mod tests {
    use burn::{
        prelude::TensorData,
        tensor::{
            Tolerance,
            backend::BackendTypes,
        },
    };

    use super::*;
    use crate::{
        prelude::*,
        support::testing::{
            CpuBackend,
            assert_close_to_vec,
            default_device,
        },
    };
    type B = CpuBackend;
    type F = <B as BackendTypes>::FloatElem;

    #[test]
    fn test_arange_start_step() {
        let device = default_device();

        let num = 5;

        // Pos step
        for step in [None, Some(1.0)] {
            let start: f64 = -3.0;

            let expected = vec![-3.0, -2.0, -1.0, 0.0, 1.0];

            let vec_actual = vec_arange_start_step(num, start, step);
            let tensor_actual = tensor_arange_start_step::<B>(num, start, step, &device);

            assert_close_to_vec(&vec_actual, &expected, 0.0001);
            tensor_actual
                .to_data()
                .assert_eq(&TensorData::from(expected.as_slice()), false);
        }

        // Neg step
        {
            let step = Some(-1.0);

            let start: f64 = 3.0;

            let expected = vec![3.0, 2.0, 1.0, 0.0, -1.0];

            let vec_actual = vec_arange_start_step(num, start, step);
            let tensor_actual = tensor_arange_start_step::<B>(num, start, step, &device);

            assert_close_to_vec(&vec_actual, &expected, 0.0001);

            tensor_actual
                .to_data()
                .assert_eq(&TensorData::from(expected.as_slice()), false);
        }
    }

    #[test]
    fn test_arange_linspace() {
        let device = default_device();

        let num = 5;

        for expected in [
            vec![3.0, 2.0, 1.0, 0.0, -1.0],
            vec![-3.0, -2.0, -1.0, 0.0, 1.0],
        ] {
            let num = expected.len();
            let start = expected[0];
            let end = expected[num - 1];

            let vec_actual = vec_linspace(start, end, num);
            let tensor_actual = tensor_linspace::<B>(start, end, num, &device);

            assert_close_to_vec(&vec_actual, &expected, 0.0001);

            tensor_actual
                .to_data()
                .assert_eq(&TensorData::from(expected.as_slice()), false);
        }

        // Pos.
        {
            let start: f64 = -3.0;
            let end = 1.0;

            let expected = vec![-3.0, -2.0, -1.0, 0.0, 1.0];

            let vec_actual = vec_linspace(start, end, num);
            let tensor_actual = tensor_linspace::<B>(start, end, num, &device);

            assert_close_to_vec(&vec_actual, &expected, 0.0001);
            tensor_actual
                .to_data()
                .assert_eq(&TensorData::from(expected.as_slice()), false);
        }

        // Neg step
        {
            let start: f64 = 3.0;
            let end = -1.0;

            let expected = vec![3.0, 2.0, 1.0, 0.0, -1.0];

            let vec_actual = vec_linspace(start, end, num);
            let tensor_actual = tensor_linspace::<B>(start, end, num, &device);

            assert_close_to_vec(&vec_actual, &expected, 0.0001);
            tensor_actual
                .to_data()
                .assert_eq(&TensorData::from(expected.as_slice()), false);
        }
    }

    #[test]
    fn test_linspace_int_step() {
        let device = default_device();

        let start: f64 = 0.0;
        let end: f64 = 1.0;
        let num: usize = 5;

        let actual = tensor_linspace::<B>(start, end, num, &device);

        actual.to_data_as::<F>().assert_approx_eq::<F>(
            &TensorData::from([0.0, 0.25, 0.5, 0.75, 1.0]),
            Tolerance::default(),
        );
    }

    #[test]
    fn test_float_vec_linspace_neg_float_step() {
        let device = default_device();

        let start: f64 = 1.0;
        let end: f64 = -0.2;
        let num: usize = 5;

        let actual = tensor_linspace::<B>(start, end, num, &device);

        actual.to_data_as::<F>().assert_approx_eq::<F>(
            &TensorData::from([1.0, 0.7, 0.4, 0.1, -0.2]),
            Tolerance::default(),
        );
    }

    #[test]
    fn test_float_vec_linspace_n1() {
        let device = default_device();

        let start: f64 = 0.0;
        let end: f64 = 1.0;
        let num: usize = 1;

        let actual = tensor_linspace::<B>(start, end, num, &device);

        actual
            .to_data_as::<F>()
            .assert_approx_eq::<F>(&TensorData::from([0.0]), Tolerance::default());
    }
}