Struct fann::Fann

source ·
pub struct Fann { /* private fields */ }

Implementations§

source§

impl Fann

source

pub fn new(layers: &[c_uint]) -> FannResult<Fann>

Create a fully connected neural network.

There will be a bias neuron in each layer except the output layer, and this bias neuron will be connected to all neurons in the next layer. When running the network, the bias nodes always emit 1.

§Arguments
  • layers - Specifies the number of neurons in each layer, starting with the input and ending with the output layer.
§Example
// Creating a network with 2 input neurons, 1 output neuron,
// and two hidden layers with 8 and 9 neurons.
let layers = [2, 8, 9, 1];
fann::Fann::new(&layers).unwrap();
source

pub fn new_sparse( connection_rate: c_float, layers: &[c_uint] ) -> FannResult<Fann>

Create a neural network that is not necessarily fully connected.

There will be a bias neuron in each layer except the output layer, and this bias neuron will be connected to all neurons in the next layer. When running the network, the bias nodes always emit 1.

§Arguments
  • connection_rate - The share of pairs of neurons in consecutive layers that will be connected.
  • layers - Specifies the number of neurons in each layer, starting with the input and ending with the output layer.
source

pub fn new_shortcut(layers: &[c_uint]) -> FannResult<Fann>

Create a neural network which has shortcut connections, i. e. it doesn’t connect only each layer to its successor, but every layer with every later layer: Each neuron has connections to all neurons in all subsequent layers.

source

pub fn from_file<P: AsRef<Path>>(path: P) -> FannResult<Fann>

Read a neural network from a file.

source

pub fn try_clone(&self) -> FannResult<Fann>

Create a deep copy of a neural network.

The Clone trait is intentionally not implemented, because this operation might fail.

source

pub fn save<P: AsRef<Path>>(&self, path: P) -> FannResult<()>

Save the network to a configuration file.

The file will contain all information about the neural network, except parameters generated during training, like mean square error and the bit fail limit.

source

pub fn randomize_weights(&mut self, min_weight: FannType, max_weight: FannType)

Give each connection a random weight between min_weight and max_weight.

By default, weights in a new network are random between -0.1 and 0.1.

source

pub fn init_weights(&mut self, train_data: &TrainData)

Initialize the weights using Widrow & Nguyen’s algorithm.

The algorithm developed by Derrick Nguyen and Bernard Widrow sets the weight in a way that can speed up training with the given training data. This technique is not always successful and in some cases can even be less efficient that a purely random initialization.

source

pub fn print_connections(&self)

Print the connections of the network in a compact matrix, for easy viewing of its internals.

The output on a small (2 2 1) network trained on the xor problem:

Layer / Neuron 012345
L   1 / N    3 BBa...
L   1 / N    4 BBA...
L   1 / N    5 ......
L   2 / N    6 ...BBA
L   2 / N    7 ......

This network has five real neurons and two bias neurons. This gives a total of seven neurons named from 0 to 6. The connections between these neurons can be seen in the matrix. “.” is a place where there is no connection, while a character tells how strong the connection is on a scale from a-z. The two real neurons in the hidden layer (neuron 3 and 4 in layer 1) have connections from the three neurons in the previous layer as is visible in the first two lines. The output neuron 6 has connections from the three neurons in the hidden layer 3 - 5 as is visible in the fourth line.

To simplify the matrix output neurons are not visible as neurons that connections can come from, and input and bias neurons are not visible as neurons that connections can go to.

source

pub fn print_parameters(&self)

Print all parameters and options of the network.

source

pub fn train( &mut self, input: &[FannType], desired_output: &[FannType] ) -> FannResult<()>

Train with a single pair of input and output. This is always incremental training (see TrainAlg), since only one pattern is presented.

source

pub fn on_data<'a>(&'a mut self, data: &'a TrainData) -> FannTrainer<'a>

Create a training configuration for the given data set.

source

pub fn on_file<P: AsRef<Path>>(&mut self, path: P) -> FannTrainer<'_>

Create a training configuration, reading the training data from the given file.

source

pub fn train_epoch(&mut self, data: &TrainData) -> FannResult<c_float>

Train one epoch with a set of training data, i. e. each sample from the training data is considered exactly once.

Returns the mean square error as it is calculated either before or during the actual training. This is not the actual MSE after the training epoch, but since calculating this will require to go through the entire training set once more, it is more than adequate to use this value during training.

source

pub fn test( &mut self, input: &[FannType], desired_output: &[FannType] ) -> FannResult<Vec<FannType>>

Test with a single pair of input and output. This operation updates the mean square error but does not change the network.

Returns the actual output of the network.

source

pub fn test_data(&mut self, data: &TrainData) -> FannResult<c_float>

Test with a training data set and calculate the mean square error.

source

pub fn get_mse(&self) -> c_float

Get the mean square error.

source

pub fn get_bit_fail(&self) -> c_uint

Get the number of fail bits, i. e. the number of neurons which differed from the desired output by more than the bit fail limit since the previous reset.

source

pub fn reset_mse_and_bit_fail(&mut self)

Reset the mean square error and bit fail count.

source

pub fn run(&self, input: &[FannType]) -> FannResult<Vec<FannType>>

Run the input through the neural network and returns the output. The length of the input must equal the number of input neurons and the length of the output will equal the number of output neurons.

source

pub fn get_num_input(&self) -> c_uint

Get the number of input neurons.

source

pub fn get_num_output(&self) -> c_uint

Get the number of output neurons.

source

pub fn get_total_neurons(&self) -> c_uint

Get the total number of neurons, including the bias neurons.

E. g. a 2-4-2 network has 3 + 5 + 2 = 10 neurons (because two layers have bias neurons).

source

pub fn get_total_connections(&self) -> c_uint

Get the total number of connections.

source

pub fn get_network_type(&self) -> NetType

Get the type of the neural network.

source

pub fn get_connection_rate(&self) -> c_float

Get the connection rate used when the network was created.

source

pub fn get_num_layers(&self) -> c_uint

Get the number of layers in the network.

source

pub fn get_layer_sizes(&self) -> Vec<c_uint>

Get the number of neurons in each layer of the network.

source

pub fn get_bias_counts(&self) -> Vec<c_uint>

Get the number of bias neurons in each layer of the network.

source

pub fn get_connections(&self) -> Vec<Connection>

Get a list of all connections in the network.

source

pub fn set_connections<'a, I: IntoIterator<Item = &'a Connection>>( &mut self, connections: I )

Set the weights of all given connections.

Connections that don’t already exist are ignored.

source

pub fn set_weight( &mut self, from_neuron: c_uint, to_neuron: c_uint, weight: FannType )

Set the weight of the given connection.

source

pub fn get_activation_func( &self, layer: c_int, neuron: c_int ) -> FannResult<ActivationFunc>

Get the activation function for neuron number neuron in layer number layer, counting the input layer as number 0. Input layer neurons do not have an activation function, so layer must be at least 1.

source

pub fn set_activation_func( &mut self, af: ActivationFunc, layer: c_int, neuron: c_int )

Set the activation function for neuron number neuron in layer number layer, counting the input layer as number 0. Input layer neurons do not have an activation function, so layer must be at least 1.

source

pub fn set_activation_func_hidden(&mut self, activation_func: ActivationFunc)

Set the activation function for all hidden layers.

source

pub fn set_activation_func_output(&mut self, activation_func: ActivationFunc)

Set the activation function for the output layer.

source

pub fn get_activation_steepness( &self, layer: c_int, neuron: c_int ) -> Option<FannType>

Get the activation steepness for neuron number neuron in layer number layer.

source

pub fn set_activation_steepness( &self, steepness: FannType, layer: c_int, neuron: c_int )

Set the activation steepness for neuron number neuron in layer number layer, counting the input layer as number 0. Input layer neurons do not have an activation steepness, so layer must be at least 1.

The steepness determines how fast the function goes from minimum to maximum. A higher value will result in more aggressive training.

A steep activation function is adequate if outputs are binary, e. e. they are supposed to be either almost 0 or almost 1.

The default value is 0.5.

source

pub fn set_activation_steepness_layer(&self, steepness: FannType, layer: c_int)

Set the activation steepness for layer number layer.

source

pub fn set_activation_steepness_hidden(&self, steepness: FannType)

Set the activation steepness for all hidden layers.

source

pub fn set_activation_steepness_output(&self, steepness: FannType)

Set the activation steepness for the output layer.

source

pub fn get_error_func(&self) -> ErrorFunc

Get the error function used during training.

source

pub fn set_error_func(&mut self, ef: ErrorFunc)

Set the error function used during training.

The default is Tanh.

source

pub fn get_stop_func(&self) -> StopFunc

Get the stop criterion for training.

source

pub fn set_stop_func(&mut self, sf: StopFunc)

Set the stop criterion for training.

The default is Mse.

source

pub fn get_bit_fail_limit(&self) -> FannType

Get the bit fail limit.

source

pub fn set_bit_fail_limit(&mut self, bit_fail_limit: FannType)

Set the bit fail limit.

Each output neuron value that differs from the desired output by more than the bit fail limit is counted as a failed bit.

source

pub fn get_cascade_params(&self) -> CascadeParams

Get cascade training parameters.

source

pub fn set_cascade_params(&mut self, params: &CascadeParams)

Set cascade training parameters.

source

pub fn get_train_algorithm(&self) -> TrainAlgorithm

Get the currently configured training algorithm.

source

pub fn set_train_algorithm(&mut self, ta: TrainAlgorithm)

Set the algorithm to be used for training.

source

pub fn set_input_scaling_params( &mut self, data: &TrainData, new_input_min: c_float, new_input_max: c_float ) -> FannResult<()>

Calculate input scaling parameters for future use based on the given training data.

source

pub fn set_output_scaling_params( &mut self, data: &TrainData, new_output_min: c_float, new_output_max: c_float ) -> FannResult<()>

Calculate output scaling parameters for future use based on the given training data.

source

pub fn set_scaling_params( &mut self, data: &TrainData, new_input_min: c_float, new_input_max: c_float, new_output_min: c_float, new_output_max: c_float ) -> FannResult<()>

Calculate scaling parameters for future use based on the given training data.

source

pub fn clear_scaling_params(&mut self) -> FannResult<()>

Clear scaling parameters.

source

pub fn scale_input(&self, input: &mut [FannType]) -> FannResult<()>

Scale data in input vector before feeding it to the network, based on previously calculated parameters.

source

pub fn scale_output(&self, output: &mut [FannType]) -> FannResult<()>

Scale data in output vector before feeding it to the network, based on previously calculated parameters.

source

pub fn descale_input(&self, input: &mut [FannType]) -> FannResult<()>

Descale data in input vector after feeding it to the network, based on previously calculated parameters.

source

pub fn descale_output(&self, output: &mut [FannType]) -> FannResult<()>

Descale data in output vector after getting it from the network, based on previously calculated parameters.

Trait Implementations§

source§

impl Drop for Fann

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl RefUnwindSafe for Fann

§

impl !Send for Fann

§

impl !Sync for Fann

§

impl Unpin for Fann

§

impl UnwindSafe for Fann

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.