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