pub struct MemoryEfficientLayer { /* private fields */ }
Expand description
Memory-efficient neural network layer that processes data in chunks
Implementations§
Source§impl MemoryEfficientLayer
impl MemoryEfficientLayer
Sourcepub fn new(
input_size: usize,
output_size: usize,
chunk_size: Option<usize>,
) -> Result<Self>
pub fn new( input_size: usize, output_size: usize, chunk_size: Option<usize>, ) -> Result<Self>
Create a new memory-efficient layer
Examples found in repository?
examples/memory_efficient_example.rs (lines 228-232)
223fn demo_memory_efficient_layer() -> Result<()> {
224 println!("\n🧠 Memory-Efficient Layer Demo");
225 println!("------------------------------");
226
227 // Create a memory-efficient layer
228 let layer = MemoryEfficientLayer::new(
229 784, // Input size (e.g., 28x28 MNIST)
230 128, // Output size
231 Some(64), // Chunk size
232 )?;
233
234 println!("Created memory-efficient layer: 784 -> 128");
235
236 // Create input data
237 let input =
238 Array2::from_shape_fn((256, 784), |(i, j)| ((i + j) as f32 * 0.01).sin()).into_dyn();
239
240 println!("Input shape: {:?}", input.shape());
241
242 // Forward pass
243 println!("Performing forward pass...");
244 let start = Instant::now();
245 let output = layer.forward(&input)?;
246 let forward_time = start.elapsed();
247
248 println!("Forward pass completed in {:?}", forward_time);
249 println!("Output shape: {:?}", output.shape());
250
251 // Verify output statistics
252 let mean = output.mean().unwrap_or(0.0);
253 let std = {
254 let variance = output.mapv(|x| (x - mean).powi(2)).mean().unwrap_or(0.0);
255 variance.sqrt()
256 };
257
258 println!("Output statistics:");
259 println!(" Mean: {:.6}", mean);
260 println!(" Std: {:.6}", std);
261 println!(
262 " Min: {:.6}",
263 output.iter().cloned().fold(f32::INFINITY, f32::min)
264 );
265 println!(
266 " Max: {:.6}",
267 output.iter().cloned().fold(f32::NEG_INFINITY, f32::max)
268 );
269
270 Ok(())
271}
Sourcepub fn forward(&self, input: &ArrayD<f32>) -> Result<ArrayD<f32>>
pub fn forward(&self, input: &ArrayD<f32>) -> Result<ArrayD<f32>>
Forward pass with memory-efficient chunk processing
Examples found in repository?
examples/memory_efficient_example.rs (line 245)
223fn demo_memory_efficient_layer() -> Result<()> {
224 println!("\n🧠 Memory-Efficient Layer Demo");
225 println!("------------------------------");
226
227 // Create a memory-efficient layer
228 let layer = MemoryEfficientLayer::new(
229 784, // Input size (e.g., 28x28 MNIST)
230 128, // Output size
231 Some(64), // Chunk size
232 )?;
233
234 println!("Created memory-efficient layer: 784 -> 128");
235
236 // Create input data
237 let input =
238 Array2::from_shape_fn((256, 784), |(i, j)| ((i + j) as f32 * 0.01).sin()).into_dyn();
239
240 println!("Input shape: {:?}", input.shape());
241
242 // Forward pass
243 println!("Performing forward pass...");
244 let start = Instant::now();
245 let output = layer.forward(&input)?;
246 let forward_time = start.elapsed();
247
248 println!("Forward pass completed in {:?}", forward_time);
249 println!("Output shape: {:?}", output.shape());
250
251 // Verify output statistics
252 let mean = output.mean().unwrap_or(0.0);
253 let std = {
254 let variance = output.mapv(|x| (x - mean).powi(2)).mean().unwrap_or(0.0);
255 variance.sqrt()
256 };
257
258 println!("Output statistics:");
259 println!(" Mean: {:.6}", mean);
260 println!(" Std: {:.6}", std);
261 println!(
262 " Min: {:.6}",
263 output.iter().cloned().fold(f32::INFINITY, f32::min)
264 );
265 println!(
266 " Max: {:.6}",
267 output.iter().cloned().fold(f32::NEG_INFINITY, f32::max)
268 );
269
270 Ok(())
271}
Auto Trait Implementations§
impl Freeze for MemoryEfficientLayer
impl RefUnwindSafe for MemoryEfficientLayer
impl Send for MemoryEfficientLayer
impl Sync for MemoryEfficientLayer
impl Unpin for MemoryEfficientLayer
impl UnwindSafe for MemoryEfficientLayer
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