pub struct SparseConv2d { /* private fields */ }
Expand description
Sparse 2D Convolution layer
Implements 2D convolution operations optimized for sparse inputs and/or sparse kernels. This can significantly reduce computation when either the input or kernel has many zeros.
§Mathematical Formulation
For standard convolution: y[i,j] = Σ_m Σ_n w[m,n] * x[i+m, j+n] For sparse convolution: Only compute terms where w[m,n] ≠ 0
§Use Cases
- Computer vision with sparse activations (post-ReLU)
- Medical imaging with sparse features
- Pruned neural networks
- Specialized domains with naturally sparse data
Implementations§
Source§impl SparseConv2d
impl SparseConv2d
Sourcepub fn new(
in_channels: usize,
out_channels: usize,
kernel_size: (usize, usize),
stride: Option<(usize, usize)>,
padding: Option<(usize, usize)>,
dilation: Option<(usize, usize)>,
sparsity: f32,
use_bias: bool,
) -> TorshResult<Self>
pub fn new( in_channels: usize, out_channels: usize, kernel_size: (usize, usize), stride: Option<(usize, usize)>, padding: Option<(usize, usize)>, dilation: Option<(usize, usize)>, sparsity: f32, use_bias: bool, ) -> TorshResult<Self>
Create a new sparse 2D convolution layer
§Arguments
in_channels
- Number of input channelsout_channels
- Number of output channelskernel_size
- Convolution kernel size (height, width)stride
- Stride for convolution (default: (1, 1))padding
- Padding for convolution (default: (0, 0))dilation
- Dilation for convolution (default: (1, 1))sparsity
- Kernel sparsity level (0.0 = dense, 1.0 = fully sparse)use_bias
- Whether to include learnable bias
§Returns
TorshResult<Self>
- New sparse 2D convolution layer or error
§Example
use torsh_sparse::nn::convolution::SparseConv2d;
// Create sparse conv: 3->64 channels, 3x3 kernel, 90% sparse
let conv = SparseConv2d::new(
3, 64, (3, 3),
Some((1, 1)), Some((1, 1)), None,
0.9, true
).unwrap();
Sourcepub fn forward(&self, input: &Tensor) -> TorshResult<Tensor>
pub fn forward(&self, input: &Tensor) -> TorshResult<Tensor>
Sourcepub fn num_parameters(&self) -> usize
pub fn num_parameters(&self) -> usize
Get the number of parameters
Sourcepub fn kernel_sparsity(&self) -> f32
pub fn kernel_sparsity(&self) -> f32
Get kernel sparsity
Sourcepub fn in_channels(&self) -> usize
pub fn in_channels(&self) -> usize
Get input channels
Sourcepub fn out_channels(&self) -> usize
pub fn out_channels(&self) -> usize
Get output channels
Sourcepub fn kernel_size(&self) -> (usize, usize)
pub fn kernel_size(&self) -> (usize, usize)
Get kernel size
Trait Implementations§
Source§impl Clone for SparseConv2d
impl Clone for SparseConv2d
Source§fn clone(&self) -> SparseConv2d
fn clone(&self) -> SparseConv2d
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreAuto Trait Implementations§
impl Freeze for SparseConv2d
impl RefUnwindSafe for SparseConv2d
impl Send for SparseConv2d
impl Sync for SparseConv2d
impl Unpin for SparseConv2d
impl UnwindSafe for SparseConv2d
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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