Struct InPlaceOperations

Source
pub struct InPlaceOperations;
Expand description

In-place operations manager for minimizing memory allocations

Implementations§

Source§

impl InPlaceOperations

Source

pub fn relu_inplace<F: Float + Debug>(array: &mut ArrayD<F>)

In-place ReLU activation

Examples found in repository?
examples/memory_efficient_example.rs (line 157)
127fn demo_in_place_operations() -> Result<()> {
128    println!("\n⚡ In-place Operations Demo");
129    println!("--------------------------");
130
131    // Create test arrays
132    let mut relu_test = Array2::from_shape_vec(
133        (3, 4),
134        vec![
135            -1.0, 2.0, -3.0, 4.0, 0.5, -0.5, 1.5, -2.5, 3.0, -1.0, 0.0, 2.0,
136        ],
137    )?
138    .into_dyn();
139
140    let mut sigmoid_test =
141        Array2::from_shape_vec((2, 3), vec![-2.0, 0.0, 2.0, -1.0, 1.0, 3.0])?.into_dyn();
142
143    let mut add_test = Array2::from_elem((2, 2), 1.0).into_dyn();
144    let add_source = Array2::from_elem((2, 2), 0.5).into_dyn();
145
146    let mut norm_test =
147        Array2::from_shape_vec((2, 3), vec![1.0, 4.0, 7.0, 2.0, 5.0, 8.0])?.into_dyn();
148
149    println!("Before operations:");
150    println!("ReLU input (should clip negatives): {:?}", relu_test);
151    println!("Sigmoid input: {:?}", sigmoid_test);
152    println!("Addition target: {:?}", add_test);
153    println!("Normalization input: {:?}", norm_test);
154
155    // Apply in-place operations
156    println!("\nApplying in-place operations...");
157    InPlaceOperations::relu_inplace(&mut relu_test);
158    InPlaceOperations::sigmoid_inplace(&mut sigmoid_test);
159    InPlaceOperations::add_inplace(&mut add_test, &add_source)?;
160    InPlaceOperations::normalize_inplace(&mut norm_test)?;
161
162    println!("\nAfter operations:");
163    println!("ReLU result: {:?}", relu_test);
164    println!("Sigmoid result: {:?}", sigmoid_test);
165    println!("Addition result: {:?}", add_test);
166    println!("Normalized result: {:?}", norm_test);
167
168    // Test scaling
169    let mut scale_test = Array2::from_elem((2, 2), 2.0).into_dyn();
170    println!("\nScaling test - before: {:?}", scale_test);
171    InPlaceOperations::scale_inplace(&mut scale_test, 3.0);
172    println!("Scaling test - after: {:?}", scale_test);
173
174    Ok(())
175}
Source

pub fn sigmoid_inplace<F: Float + Debug>(array: &mut ArrayD<F>)

In-place sigmoid activation

Examples found in repository?
examples/memory_efficient_example.rs (line 158)
127fn demo_in_place_operations() -> Result<()> {
128    println!("\n⚡ In-place Operations Demo");
129    println!("--------------------------");
130
131    // Create test arrays
132    let mut relu_test = Array2::from_shape_vec(
133        (3, 4),
134        vec![
135            -1.0, 2.0, -3.0, 4.0, 0.5, -0.5, 1.5, -2.5, 3.0, -1.0, 0.0, 2.0,
136        ],
137    )?
138    .into_dyn();
139
140    let mut sigmoid_test =
141        Array2::from_shape_vec((2, 3), vec![-2.0, 0.0, 2.0, -1.0, 1.0, 3.0])?.into_dyn();
142
143    let mut add_test = Array2::from_elem((2, 2), 1.0).into_dyn();
144    let add_source = Array2::from_elem((2, 2), 0.5).into_dyn();
145
146    let mut norm_test =
147        Array2::from_shape_vec((2, 3), vec![1.0, 4.0, 7.0, 2.0, 5.0, 8.0])?.into_dyn();
148
149    println!("Before operations:");
150    println!("ReLU input (should clip negatives): {:?}", relu_test);
151    println!("Sigmoid input: {:?}", sigmoid_test);
152    println!("Addition target: {:?}", add_test);
153    println!("Normalization input: {:?}", norm_test);
154
155    // Apply in-place operations
156    println!("\nApplying in-place operations...");
157    InPlaceOperations::relu_inplace(&mut relu_test);
158    InPlaceOperations::sigmoid_inplace(&mut sigmoid_test);
159    InPlaceOperations::add_inplace(&mut add_test, &add_source)?;
160    InPlaceOperations::normalize_inplace(&mut norm_test)?;
161
162    println!("\nAfter operations:");
163    println!("ReLU result: {:?}", relu_test);
164    println!("Sigmoid result: {:?}", sigmoid_test);
165    println!("Addition result: {:?}", add_test);
166    println!("Normalized result: {:?}", norm_test);
167
168    // Test scaling
169    let mut scale_test = Array2::from_elem((2, 2), 2.0).into_dyn();
170    println!("\nScaling test - before: {:?}", scale_test);
171    InPlaceOperations::scale_inplace(&mut scale_test, 3.0);
172    println!("Scaling test - after: {:?}", scale_test);
173
174    Ok(())
175}
Source

pub fn tanh_inplace<F: Float + Debug>(array: &mut ArrayD<F>)

In-place tanh activation

Source

pub fn add_inplace<F: Float + Debug>( target: &mut ArrayD<F>, source: &ArrayD<F>, ) -> Result<()>

In-place addition

Examples found in repository?
examples/memory_efficient_example.rs (line 159)
127fn demo_in_place_operations() -> Result<()> {
128    println!("\n⚡ In-place Operations Demo");
129    println!("--------------------------");
130
131    // Create test arrays
132    let mut relu_test = Array2::from_shape_vec(
133        (3, 4),
134        vec![
135            -1.0, 2.0, -3.0, 4.0, 0.5, -0.5, 1.5, -2.5, 3.0, -1.0, 0.0, 2.0,
136        ],
137    )?
138    .into_dyn();
139
140    let mut sigmoid_test =
141        Array2::from_shape_vec((2, 3), vec![-2.0, 0.0, 2.0, -1.0, 1.0, 3.0])?.into_dyn();
142
143    let mut add_test = Array2::from_elem((2, 2), 1.0).into_dyn();
144    let add_source = Array2::from_elem((2, 2), 0.5).into_dyn();
145
146    let mut norm_test =
147        Array2::from_shape_vec((2, 3), vec![1.0, 4.0, 7.0, 2.0, 5.0, 8.0])?.into_dyn();
148
149    println!("Before operations:");
150    println!("ReLU input (should clip negatives): {:?}", relu_test);
151    println!("Sigmoid input: {:?}", sigmoid_test);
152    println!("Addition target: {:?}", add_test);
153    println!("Normalization input: {:?}", norm_test);
154
155    // Apply in-place operations
156    println!("\nApplying in-place operations...");
157    InPlaceOperations::relu_inplace(&mut relu_test);
158    InPlaceOperations::sigmoid_inplace(&mut sigmoid_test);
159    InPlaceOperations::add_inplace(&mut add_test, &add_source)?;
160    InPlaceOperations::normalize_inplace(&mut norm_test)?;
161
162    println!("\nAfter operations:");
163    println!("ReLU result: {:?}", relu_test);
164    println!("Sigmoid result: {:?}", sigmoid_test);
165    println!("Addition result: {:?}", add_test);
166    println!("Normalized result: {:?}", norm_test);
167
168    // Test scaling
169    let mut scale_test = Array2::from_elem((2, 2), 2.0).into_dyn();
170    println!("\nScaling test - before: {:?}", scale_test);
171    InPlaceOperations::scale_inplace(&mut scale_test, 3.0);
172    println!("Scaling test - after: {:?}", scale_test);
173
174    Ok(())
175}
Source

pub fn scale_inplace<F: Float + Debug>(array: &mut ArrayD<F>, factor: F)

In-place scalar multiplication

Examples found in repository?
examples/memory_efficient_example.rs (line 171)
127fn demo_in_place_operations() -> Result<()> {
128    println!("\n⚡ In-place Operations Demo");
129    println!("--------------------------");
130
131    // Create test arrays
132    let mut relu_test = Array2::from_shape_vec(
133        (3, 4),
134        vec![
135            -1.0, 2.0, -3.0, 4.0, 0.5, -0.5, 1.5, -2.5, 3.0, -1.0, 0.0, 2.0,
136        ],
137    )?
138    .into_dyn();
139
140    let mut sigmoid_test =
141        Array2::from_shape_vec((2, 3), vec![-2.0, 0.0, 2.0, -1.0, 1.0, 3.0])?.into_dyn();
142
143    let mut add_test = Array2::from_elem((2, 2), 1.0).into_dyn();
144    let add_source = Array2::from_elem((2, 2), 0.5).into_dyn();
145
146    let mut norm_test =
147        Array2::from_shape_vec((2, 3), vec![1.0, 4.0, 7.0, 2.0, 5.0, 8.0])?.into_dyn();
148
149    println!("Before operations:");
150    println!("ReLU input (should clip negatives): {:?}", relu_test);
151    println!("Sigmoid input: {:?}", sigmoid_test);
152    println!("Addition target: {:?}", add_test);
153    println!("Normalization input: {:?}", norm_test);
154
155    // Apply in-place operations
156    println!("\nApplying in-place operations...");
157    InPlaceOperations::relu_inplace(&mut relu_test);
158    InPlaceOperations::sigmoid_inplace(&mut sigmoid_test);
159    InPlaceOperations::add_inplace(&mut add_test, &add_source)?;
160    InPlaceOperations::normalize_inplace(&mut norm_test)?;
161
162    println!("\nAfter operations:");
163    println!("ReLU result: {:?}", relu_test);
164    println!("Sigmoid result: {:?}", sigmoid_test);
165    println!("Addition result: {:?}", add_test);
166    println!("Normalized result: {:?}", norm_test);
167
168    // Test scaling
169    let mut scale_test = Array2::from_elem((2, 2), 2.0).into_dyn();
170    println!("\nScaling test - before: {:?}", scale_test);
171    InPlaceOperations::scale_inplace(&mut scale_test, 3.0);
172    println!("Scaling test - after: {:?}", scale_test);
173
174    Ok(())
175}
Source

pub fn normalize_inplace<F: Float + Debug + Clone + FromPrimitive>( array: &mut ArrayD<F>, ) -> Result<()>

In-place normalization (subtract mean, divide by std)

Examples found in repository?
examples/memory_efficient_example.rs (line 160)
127fn demo_in_place_operations() -> Result<()> {
128    println!("\n⚡ In-place Operations Demo");
129    println!("--------------------------");
130
131    // Create test arrays
132    let mut relu_test = Array2::from_shape_vec(
133        (3, 4),
134        vec![
135            -1.0, 2.0, -3.0, 4.0, 0.5, -0.5, 1.5, -2.5, 3.0, -1.0, 0.0, 2.0,
136        ],
137    )?
138    .into_dyn();
139
140    let mut sigmoid_test =
141        Array2::from_shape_vec((2, 3), vec![-2.0, 0.0, 2.0, -1.0, 1.0, 3.0])?.into_dyn();
142
143    let mut add_test = Array2::from_elem((2, 2), 1.0).into_dyn();
144    let add_source = Array2::from_elem((2, 2), 0.5).into_dyn();
145
146    let mut norm_test =
147        Array2::from_shape_vec((2, 3), vec![1.0, 4.0, 7.0, 2.0, 5.0, 8.0])?.into_dyn();
148
149    println!("Before operations:");
150    println!("ReLU input (should clip negatives): {:?}", relu_test);
151    println!("Sigmoid input: {:?}", sigmoid_test);
152    println!("Addition target: {:?}", add_test);
153    println!("Normalization input: {:?}", norm_test);
154
155    // Apply in-place operations
156    println!("\nApplying in-place operations...");
157    InPlaceOperations::relu_inplace(&mut relu_test);
158    InPlaceOperations::sigmoid_inplace(&mut sigmoid_test);
159    InPlaceOperations::add_inplace(&mut add_test, &add_source)?;
160    InPlaceOperations::normalize_inplace(&mut norm_test)?;
161
162    println!("\nAfter operations:");
163    println!("ReLU result: {:?}", relu_test);
164    println!("Sigmoid result: {:?}", sigmoid_test);
165    println!("Addition result: {:?}", add_test);
166    println!("Normalized result: {:?}", norm_test);
167
168    // Test scaling
169    let mut scale_test = Array2::from_elem((2, 2), 2.0).into_dyn();
170    println!("\nScaling test - before: {:?}", scale_test);
171    InPlaceOperations::scale_inplace(&mut scale_test, 3.0);
172    println!("Scaling test - after: {:?}", scale_test);
173
174    Ok(())
175}
Source

pub fn dropout_inplace<F: Float + Debug>( array: &mut ArrayD<F>, dropout_rate: f64, training: bool, ) -> Result<()>

In-place dropout (sets elements to zero based on probability)

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V