-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
154 additions
and
45 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,45 +1,87 @@ | ||
use nalgebra::max; | ||
|
||
pub fn sigmoid(a: f32) -> f32 { | ||
1f32 / (1f32 + a.powf(-1f32)) | ||
#[derive(Copy, Clone)] | ||
pub enum Activation { | ||
Sigmoid, | ||
Tanh, | ||
ReLU, | ||
Linear | ||
} | ||
|
||
pub fn sigmoid_prime(a: f32) -> f32 { | ||
sigmoid(a) * (1f32 - sigmoid(a)) | ||
|
||
pub fn activation_from(activation: Activation) -> impl Fn(f32) -> f32 { | ||
match activation { | ||
Activation::Sigmoid => Sigmoid::call, | ||
Activation::Tanh => Tanh::call, | ||
Activation::ReLU => Relu::call, | ||
Activation::Linear => Linear::call | ||
} | ||
} | ||
|
||
pub fn tanh(a: f32) -> f32 { | ||
a.tanh() | ||
|
||
pub fn activation_derivative(activation: Activation) -> impl Fn(f32) -> f32 { | ||
match activation { | ||
Activation::Sigmoid => Sigmoid::first_prime, | ||
Activation::Tanh => Tanh::first_prime, | ||
Activation::ReLU => Relu::first_prime, | ||
Activation::Linear => Linear::first_prime | ||
} | ||
} | ||
|
||
pub fn tanh_prime(a: f32) -> f32 { | ||
let tanh_a = a.tanh(); | ||
|
||
1 - tanh_a * tanh_a | ||
pub trait ActivationFunction { | ||
fn call(a: f32) -> f32; | ||
fn first_prime(a: f32) -> f32; | ||
} | ||
|
||
pub fn relu(a: f32) -> f32 { | ||
max(0f32, a) | ||
|
||
pub struct Sigmoid; | ||
impl ActivationFunction for Sigmoid { | ||
fn call(a: f32) -> f32 { | ||
1f32 / (1f32 + a.powf(-1f32)) | ||
} | ||
fn first_prime(a: f32) -> f32 { | ||
Sigmoid::call(a) * (1f32 - Sigmoid::call(a)) | ||
} | ||
} | ||
|
||
pub fn relu_prime (a: f32) -> f32 { | ||
if a <= 0f32 { 0f32 } else { 1f32 } | ||
|
||
pub struct Tanh; | ||
impl ActivationFunction for Tanh { | ||
fn call(a: f32) -> f32 { | ||
a.tanh() | ||
} | ||
|
||
fn first_prime(a: f32) -> f32 { | ||
let tanh_a = a.tanh(); | ||
1f32 - tanh_a * tanh_a | ||
} | ||
} | ||
|
||
pub fn linear(a: f32) -> f32 { | ||
a | ||
|
||
pub struct Relu; | ||
impl ActivationFunction for Relu { | ||
fn call(a: f32) -> f32 { | ||
if a > 0f32 { a } else { 0f32 } | ||
} | ||
|
||
fn first_prime(a: f32) -> f32 { | ||
if a <= 0f32 { 0f32 } else { 1f32 } | ||
} | ||
} | ||
|
||
pub fn linear_prime(a: f32) -> f32 { | ||
1f32 | ||
|
||
pub struct Linear; | ||
impl ActivationFunction for Linear { | ||
fn call(a: f32) -> f32 { | ||
a | ||
} | ||
|
||
fn first_prime(a: f32) -> f32 { | ||
1f32 | ||
} | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
pub fn test() { | ||
sigmoid(5f32); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,13 +1,47 @@ | ||
use nalgebra::base::Vector2; | ||
use crate::synapse::Synapse; | ||
use crate::activation::{Activation}; | ||
|
||
pub struct Neuron { | ||
pub location: Vector2<f32> | ||
inputs: Vec<Synapse>, | ||
outputs: Vec<Synapse>, | ||
bias: f32, | ||
input_total: u32, | ||
output_total: u32, | ||
|
||
/// Error derivative with respect to the node's output. | ||
output_derivative: f32, | ||
|
||
/// Error derivative with respect to this node's total input. | ||
input_derivative: f32, | ||
|
||
|
||
/// Accumulated error derivative with respect to this node's total input since | ||
/// the last update. This derivative equals dE/db where b is the node's | ||
/// bias term. | ||
accumulated_input_derivative: f32, | ||
|
||
/// Number of accumulated err. derivatives with respect to the total input | ||
/// since the last update. | ||
accumulated_derivatives: u32, | ||
|
||
/// A function that accepts an input, in this case the sum of the total input, and returns | ||
/// an output. | ||
activation: Activation | ||
} | ||
|
||
impl Neuron { | ||
pub fn new() -> Neuron { | ||
pub fn new(activation: Activation) -> Neuron { | ||
Neuron { | ||
location: Vector2::new(0f32, 0f32) | ||
inputs: Vec::new(), | ||
outputs: Vec::new(), | ||
bias: 0.1f32, | ||
input_total: 0, | ||
output_total: 0, | ||
output_derivative: 0f32, | ||
input_derivative: 0f32, | ||
accumulated_input_derivative: 0f32, | ||
accumulated_derivatives: 0, | ||
activation | ||
} | ||
} | ||
} |
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