Optimized activation functions
This commit is contained in:
parent
f2d3d00ce6
commit
a02110f2db
1 changed files with 8 additions and 24 deletions
|
@ -42,64 +42,48 @@ fn sigmoid(x: f64) -> f64 {
|
||||||
|
|
||||||
pub fn logistic(matrix: &Array1<f64>) -> Array1<f64> {
|
pub fn logistic(matrix: &Array1<f64>) -> Array1<f64> {
|
||||||
let mut result = matrix.clone();
|
let mut result = matrix.clone();
|
||||||
for x in result.iter_mut() {
|
result.mapv_inplace(sigmoid);
|
||||||
*x = sigmoid(*x);
|
|
||||||
}
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn logistic_prime(matrix: &Array1<f64>) -> Array1<f64> {
|
pub fn logistic_prime(matrix: &Array1<f64>) -> Array1<f64> {
|
||||||
let mut result = matrix.clone();
|
let mut result = matrix.clone();
|
||||||
for x in result.iter_mut() {
|
result.mapv_inplace(|x| sigmoid(x * (1.0 - sigmoid(x))));
|
||||||
*x = sigmoid(*x * (1.0 - sigmoid(*x)));
|
|
||||||
}
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn tanh(matrix: &Array1<f64>) -> Array1<f64> {
|
pub fn tanh(matrix: &Array1<f64>) -> Array1<f64> {
|
||||||
let mut result = matrix.clone();
|
let mut result = matrix.clone();
|
||||||
for x in result.iter_mut() {
|
result.mapv_inplace(|x| x.tanh());
|
||||||
*x = (*x).tanh();
|
|
||||||
}
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn tanh_prime(matrix: &Array1<f64>) -> Array1<f64> {
|
pub fn tanh_prime(matrix: &Array1<f64>) -> Array1<f64> {
|
||||||
let mut result = matrix.clone();
|
let mut result = matrix.clone();
|
||||||
for x in result.iter_mut() {
|
result.mapv_inplace(|x| 1.0 - x.tanh().pow(2));
|
||||||
*x = 1.0 - (*x).tanh().pow(2);
|
|
||||||
}
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn relu(matrix: &Array1<f64>) -> Array1<f64> {
|
pub fn relu(matrix: &Array1<f64>) -> Array1<f64> {
|
||||||
let mut result = matrix.clone();
|
let mut result = matrix.clone();
|
||||||
for x in result.iter_mut() {
|
result.mapv_inplace(|x| x.max(0.0));
|
||||||
*x = (*x).max(0.0);
|
|
||||||
}
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn relu_prime(matrix: &Array1<f64>) -> Array1<f64> {
|
pub fn relu_prime(matrix: &Array1<f64>) -> Array1<f64> {
|
||||||
let mut result = matrix.clone();
|
let mut result = matrix.clone();
|
||||||
for x in result.iter_mut() {
|
result.mapv_inplace(|x| if x <= 0.0 { 0.0 } else { 1.0 });
|
||||||
*x = if (*x) <= 0.0 { 0.0 } else { 1.0 };
|
|
||||||
}
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn leaky_relu(matrix: &Array1<f64>) -> Array1<f64> {
|
pub fn leaky_relu(matrix: &Array1<f64>) -> Array1<f64> {
|
||||||
let mut result = matrix.clone();
|
let mut result = matrix.clone();
|
||||||
for x in result.iter_mut() {
|
result.mapv_inplace(|x| x.max(0.001 * x));
|
||||||
*x = (*x).max(0.001 * (*x));
|
|
||||||
}
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn leaky_relu_prime(matrix: &Array1<f64>) -> Array1<f64> {
|
pub fn leaky_relu_prime(matrix: &Array1<f64>) -> Array1<f64> {
|
||||||
let mut result = matrix.clone();
|
let mut result = matrix.clone();
|
||||||
for x in result.iter_mut() {
|
result.mapv_inplace(|x| if x <= 0.0 { 0.001 } else { 1.0 });
|
||||||
*x = if (*x) <= 0.0 { 0.001 } else { 1.0 };
|
|
||||||
}
|
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue