-
Notifications
You must be signed in to change notification settings - Fork 489
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix the problem of sigmoid gradient generating NaN (#1140)
* use sigmoid derivative formulas * add test * fix test error * move sigmoid to tensor/ops/activation.rs * use full precision in the default implementation * rename the param of `sigmoid_backward`
- Loading branch information
Showing
6 changed files
with
103 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#[burn_tensor_testgen::testgen(ad_sigmoid)] | ||
mod tests { | ||
use super::*; | ||
use burn_tensor::{activation, Data}; | ||
|
||
#[test] | ||
fn should_diff_sigmoid() { | ||
let data = Data::<f32, 1>::from([0.8762]); | ||
|
||
let device = Default::default(); | ||
let tensor_1 = TestAutodiffTensor::from_data(data, &device).require_grad(); | ||
let tensor_2 = activation::sigmoid(tensor_1.clone()); | ||
let grads = tensor_2.backward(); | ||
|
||
let grad = tensor_1.grad(&grads).unwrap(); | ||
|
||
grad.to_data().assert_approx_eq(&Data::from([0.207549]), 4); | ||
} | ||
|
||
#[test] | ||
fn small_neg_val_should_not_cause_grad_overflow() { | ||
let data = Data::<f32, 1>::from([-90.0]); | ||
|
||
let device = Default::default(); | ||
let tensor_1 = TestAutodiffTensor::from_data(data, &device).require_grad(); | ||
let tensor_2 = activation::sigmoid(tensor_1.clone()); | ||
let grads = tensor_2.backward(); | ||
|
||
let grad = tensor_1.grad(&grads).unwrap(); | ||
|
||
grad.to_data().assert_approx_eq(&Data::from([0.0]), 4); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters