Skip to content

Commit

Permalink
Give more flesh to the docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
elinorbgr committed Jul 13, 2015
1 parent 403701d commit 85b703b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
12 changes: 7 additions & 5 deletions src/activations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@

use num::{Float, one, zero};

/// Wraps two functions or closures as an activation function that can be
/// used by a network.
pub struct ActivationFunction<F, V, D>
where F: Float,
V: Fn(F) -> F,
D: Fn(F) -> F
{
_marker: ::std::marker::PhantomData<F>,
/// Mathematical definition of the activation function, to be evaluated
/// at any point.
pub value: V,
/// Mathematical derivative of the activation function, to be evaluated
/// at any point.
pub derivative: D
}

Expand All @@ -17,11 +23,7 @@ impl<F, V, D> ActivationFunction<F, V, D>
V: Fn(F) -> F,
D: Fn(F) -> F
{
/// Wraps the two provided functions or closures as an activation function
/// that can be used by a network.
///
/// The second function is supposed to be the mathematical derivative of the
/// first one, and most of the algorithms rely on that.
/// Create an `ActivationFunction` out of two functions or closures.
pub fn new(value: V, derivative: D) -> ActivationFunction<F, V, D> {
ActivationFunction {
_marker: ::std::marker::PhantomData,
Expand Down
12 changes: 11 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ pub mod util;
///
/// This computation is not supposed to alter the internal state of the object.
pub trait Compute<F: Float>{
/// Process input into output
/// Process input into output.
fn compute(&self, input: &[F]) -> Vec<F>;
/// The number of inputs this network expects.
fn input_size(&self) -> usize;
/// The number of outputs generated by this network.
fn output_size(&self) -> usize;
}

Expand All @@ -36,18 +38,26 @@ pub trait Method {}
/// A trait for networks that can be trained using a certain method of
/// unsupervised training.
pub trait UnsupervisedTrain<F: Float, M: Method> {
/// Performs one step of unsupervised training on given input using
/// the learning parameters defined by `rule`.
fn unsupervised_train(&mut self, rule: &M, input: &[F]);
}

/// A trait for networks that can be trained using a certain method of
/// supervised training.
pub trait SupervisedTrain<F: Float, M: Method> {
/// Performs one step of supervised training on given input and target
/// value using the learning parameters defined by `rule`.
fn supervised_train(&mut self, rule: &M, input: &[F], target: &[F]);
}

/// A trait for networks that can be trained using a certain method in a
/// back-propagation way: the training returns a values vector that is
/// to be used as a target value for the previous layer.
pub trait BackpropTrain<F: Float, M: Method> {
/// Performs one step of supervised training on given input and target
/// value using the learning parameters defined by `rule`.
///
/// Returns the value to feed to the previous layer.
fn backprop_train(&mut self, rule: &M, input: &[F], target: &[F]) -> Vec<F>;
}
8 changes: 8 additions & 0 deletions src/training.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ use Method;
/// The gradient descend approach, consisting on finding a minimum of the
/// error by going down its gradient.
pub struct GradientDescent<F: Float> {
/// The learning rate associated with this gradient descent rule.
///
/// A very small value will make the training slow, but a too big one
/// will make it unstable.
pub rate: F
}

Expand All @@ -18,6 +22,10 @@ impl<F: Float> Method for GradientDescent<F> {}
/// The perceptron rule, a classic learning rule for one-layered
/// feedforward networks.
pub struct PerceptronRule<F: Float> {
/// The learning rate associated with this perceptron rule.
///
/// A very small value will make the training slow, but a too big one
/// will make it unstable.
pub rate: F
}

Expand Down
2 changes: 2 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ pub struct FixedOutput<F: Float> {
}

impl<F: Float> FixedOutput<F> {
/// Creates a new fixed output network that will always return the
/// values provided as `output`.
pub fn new(output: &[F]) -> FixedOutput<F> {
FixedOutput { output: output.to_owned() }
}
Expand Down

0 comments on commit 85b703b

Please sign in to comment.