+
Skip to content

Fix Issue #110 Fix (Dropout (test) for non-positive values) #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions src/tensor_ops/impl_dropout.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::utils::move_tape_and_add_backward_op;
use crate::prelude::*;
use rand::Rng;
use rand_distr::Standard;
use rand_distr::{Distribution, Standard};

/// Randomly drops out elements from `t` with probability `p`, and multiplies all elements by `1 / (1 - p)`.
///
Expand All @@ -14,17 +15,17 @@ pub fn dropout<T: Tensor<Dtype = f32>, R: Rng>(t: T, p: f32, rng: &mut R) -> T {
} else {
// `t` owns the tape in this branch, so apply dropout randomly.
let rinvp = (1.0 - p).recip();
map_df_uses_fx(
t,
move |x| {
if rng.sample::<f32, Standard>(Standard) < p {
0.0
} else {
x * rinvp
}
},
move |fx| if fx > &0.0 { rinvp } else { 0.0 },
)
let deriv = T::Device::filled(&mut |d| {
let val: f32 = Standard.sample(rng);
*d = if val < p { 0.0 } else { rinvp };
});
let mut result = T::NoTape::zeros();
T::Device::addmul(result.mut_data(), t.data(), deriv.as_ref());

move_tape_and_add_backward_op(t, result, move |t, result, grads| {
let (t_grad, result_grad) = grads.mut_and_ref(&t, &result);
T::Device::addmul(t_grad, deriv.as_ref(), result_grad);
})
}
}

Expand Down Expand Up @@ -79,11 +80,11 @@ mod tests {
}

#[test]
fn test_dropout_1d() {
fn test_dropout_1d_with_non_positive_values() {
let mut rng = StdRng::seed_from_u64(3);
let t = Tensor1D::new([1.0, 2.0, 3.0, 4.0, 5.0]);
let t = Tensor1D::new([0.0, 2.0, -3.0, -4.0, 0.0]);
let r = t.trace().dropout(0.5, &mut rng);
assert_eq!(r.data(), &[2.0, 0.0, 6.0, 0.0, 0.0]);
assert_eq!(r.data(), &[0.0, 0.0, -6.0, 0.0, 0.0]);
let gradients = r.mean().backward();
assert_eq!(gradients.ref_gradient(&t), &[0.4, 0.0, 0.4, 0.0, 0.0]);
}
Expand Down
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载