pub struct InPlaceOperations;
Expand description
In-place operations manager for minimizing memory allocations
Implementations§
Source§impl InPlaceOperations
impl InPlaceOperations
Sourcepub fn relu_inplace<F: Float + Debug>(array: &mut ArrayD<F>)
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}
Sourcepub fn sigmoid_inplace<F: Float + Debug>(array: &mut ArrayD<F>)
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}
Sourcepub fn tanh_inplace<F: Float + Debug>(array: &mut ArrayD<F>)
pub fn tanh_inplace<F: Float + Debug>(array: &mut ArrayD<F>)
In-place tanh activation
Sourcepub fn add_inplace<F: Float + Debug>(
target: &mut ArrayD<F>,
source: &ArrayD<F>,
) -> Result<()>
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}
Sourcepub fn scale_inplace<F: Float + Debug>(array: &mut ArrayD<F>, factor: F)
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}
Sourcepub fn normalize_inplace<F: Float + Debug + Clone + FromPrimitive>(
array: &mut ArrayD<F>,
) -> Result<()>
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}
Auto Trait Implementations§
impl Freeze for InPlaceOperations
impl RefUnwindSafe for InPlaceOperations
impl Send for InPlaceOperations
impl Sync for InPlaceOperations
impl Unpin for InPlaceOperations
impl UnwindSafe for InPlaceOperations
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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