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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use crate::prelude::*;
use rand::{prelude::StdRng, Rng, SeedableRng};
use std::{cell::RefCell, ops::DerefMut};
#[derive(Clone, Debug)]
pub struct DropoutOneIn<const N: usize> {
rng: RefCell<StdRng>,
}
impl<const N: usize> Default for DropoutOneIn<N> {
fn default() -> Self {
let seed = unique_id().as_u64();
Self {
rng: RefCell::new(StdRng::seed_from_u64(seed)),
}
}
}
impl<const N: usize> CanUpdateWithGradients for DropoutOneIn<N> {
fn update<G: GradientProvider>(&mut self, _: &mut G, _: &mut UnusedTensors) {}
}
impl<const N: usize> ResetParams for DropoutOneIn<N> {
fn reset_params<R: Rng>(&mut self, _: &mut R) {}
}
impl<const N: usize> SaveToNpz for DropoutOneIn<N> {}
impl<const N: usize> LoadFromNpz for DropoutOneIn<N> {}
impl<const N: usize, T: Tensor<Dtype = f32>> Module<T> for DropoutOneIn<N> {
type Output = T;
fn forward(&self, input: T) -> Self::Output {
let mut rng = self.rng.borrow_mut();
dropout(input, 1.0 / N as f32, rng.deref_mut())
}
}
#[derive(Clone, Debug)]
pub struct Dropout {
pub p: f32,
rng: RefCell<StdRng>,
}
impl Dropout {
pub fn new(p: f32, rng_seed: u64) -> Self {
Self {
p,
rng: RefCell::new(StdRng::seed_from_u64(rng_seed)),
}
}
pub fn p(p: f32) -> Self {
let seed = unique_id().as_u64();
Self {
p,
rng: RefCell::new(StdRng::seed_from_u64(seed)),
}
}
}
impl Default for Dropout {
fn default() -> Self {
Self::new(0.5, 0)
}
}
impl CanUpdateWithGradients for Dropout {
fn update<G: GradientProvider>(&mut self, _: &mut G, _: &mut UnusedTensors) {}
}
impl ResetParams for Dropout {
fn reset_params<R: rand::Rng>(&mut self, _: &mut R) {}
}
impl SaveToNpz for Dropout {}
impl LoadFromNpz for Dropout {}
impl<T: Tensor<Dtype = f32>> Module<T> for Dropout {
type Output = T;
fn forward(&self, input: T) -> Self::Output {
let mut rng = self.rng.borrow_mut();
dropout(input, self.p, rng.deref_mut())
}
}
impl<R: Rng + SeedableRng, T: Tensor<Dtype = f32>> Module<(T, R)> for Dropout {
type Output = (T, R);
fn forward(&self, input: (T, R)) -> Self::Output {
let (t, mut rng) = input;
let t = dropout(t, self.p, &mut rng);
(t, rng)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_dropout_internal_rng_reproduce() {
let d1 = Dropout::new(0.5, 0);
let d2 = Dropout::new(0.5, 0);
let t: Tensor1D<100> = Tensor1D::ones();
let r1 = d1.forward(t.trace());
let r2 = d2.forward(t.trace());
let r1_2 = d1.forward(t.trace());
assert_eq!(r1.data(), r2.data());
assert!(r1.data() != r1_2.data());
}
#[test]
fn test_dropout_external_rng() {
let rng = StdRng::seed_from_u64(0);
let d = Dropout::p(0.5);
let t: Tensor1D<100> = Tensor1D::ones();
let (r, _rng) = d.forward((t.trace(), rng));
assert!(t.data() != r.data());
}
#[test]
fn test_dropout_no_tape() {
let dropout = Dropout::p(0.5);
let t: Tensor1D<100> = Tensor1D::ones();
let r = dropout.forward(t.clone());
assert_eq!(t.data(), r.data());
}
#[test]
fn test_dropout_tape() {
let dropout = Dropout::p(0.5);
let t: Tensor1D<100> = Tensor1D::ones();
let r = dropout.forward(t.trace());
assert!(t.data() != r.data());
}
}