Skip to main content

coeus_nn/
dropout.rs

1use crate::module::{Module, ModuleError};
2use coeus_autograd::Var;
3use coeus_core::Float;
4
5/// Dropout layer.
6///
7/// During training, randomly zeroes some of the elements of the input tensor
8/// with probability `p` using samples from a Bernoulli distribution.
9/// The outputs are scaled by `1 / (1 - p)` during training, and left unchanged during evaluation.
10#[derive(Clone)]
11pub struct Dropout {
12    /// Dropout probability.
13    pub p: f64,
14    /// Training mode flag.
15    pub is_training: bool,
16    /// Random seed for reproducibility.
17    pub seed: u64,
18}
19
20impl Dropout {
21    /// Create a new Dropout layer.
22    pub fn new(p: f64) -> Self {
23        assert!(
24            (0.0..1.0).contains(&p),
25            "Dropout probability must be in [0.0, 1.0)"
26        );
27        Self {
28            p,
29            is_training: true,
30            seed: 42,
31        }
32    }
33
34    /// Set training mode inherently without generic parameter ambiguity.
35    pub fn set_training(&mut self, mode: bool) {
36        self.is_training = mode;
37    }
38}
39
40impl<T: Float + coeus_core::FloatElement, B: coeus_ops::BackendOps<T> + Default> Module<T, B>
41    for Dropout
42{
43    fn parameters(&self) -> Vec<Var<T, B>> {
44        vec![]
45    }
46
47    fn train(&mut self, mode: bool) {
48        self.set_training(mode);
49    }
50
51    fn forward(&self, input: &Var<T, B>) -> Result<Var<T, B>, ModuleError<B::Error>> {
52        Ok(coeus_autograd::dropout(
53            input,
54            self.p,
55            self.is_training,
56            self.seed,
57        )?)
58    }
59}