Struct fast_neural_network::neural_network::Network
source · pub struct Network { /* private fields */ }
Expand description
The main heap based neural network struct.
Implementations§
source§impl Network
impl Network
sourcepub fn new(
inputs: usize,
outputs: usize,
activation_func: ActivationType,
alpha: f64
) -> Self
pub fn new( inputs: usize, outputs: usize, activation_func: ActivationType, alpha: f64 ) -> Self
Creates a new empty network with the given number of inputs and outputs.
Example
use fast_neural_network::{activation::*, neural_network::*};
let mut network = Network::new(3, 1, ActivationType::Relu, 0.005);
assert_eq!(network.dimensions().0, 3);
assert_eq!(network.dimensions().1, 1);
assert_eq!(network.hidden_layers_size(), 0);
assert_eq!(network.learning_rate(), 0.005);
sourcepub fn new_with_layers(
inputs: usize,
outputs: usize,
hidden_layers: Vec<Layer>,
activation_func: ActivationType,
alpha: f64
) -> Self
pub fn new_with_layers( inputs: usize, outputs: usize, hidden_layers: Vec<Layer>, activation_func: ActivationType, alpha: f64 ) -> Self
Creates a new empty network with the given number of inputs and outputs and the given hidden layers.
Example
use fast_neural_network::{activation::*, neural_network::*};
let mut network = Network::new_with_layers(3, 1, vec![Layer::new(4), Layer::new(4)], ActivationType::Relu, 0.005);
assert_eq!(network.dimensions().0, 3);
assert_eq!(network.dimensions().1, 1);
assert_eq!(network.hidden_layers_size(), 2);
assert_eq!(network.learning_rate(), 0.005);
sourcepub fn load(path: &str) -> Self
pub fn load(path: &str) -> Self
Loads a network from a json file.
Example
use fast_neural_network::neural_network::*;
let mut network = Network::load("network.json");
sourcepub fn save(&self, path: &str)
pub fn save(&self, path: &str)
Saves the network to a json file.
Example
use fast_neural_network::{activation::*, neural_network::*};
let mut network = Network::new(3, 1, ActivationType::Relu, 0.005);
network.save("network.json");
Adds a hidden layer to the network.
Adds a hidden layer to the network with the given size.
Example
use fast_neural_network::{activation::*, neural_network::*};
let mut network = Network::new(3, 1, ActivationType::Relu, 0.005);
network.add_hidden_layer_with_size(4);
sourcepub fn compile(&mut self)
pub fn compile(&mut self)
Compiles the network. This is done automatically during training.
Compilations should be done after the hidden layers are set.
Example
use fast_neural_network::{activation::*, neural_network::*};
let mut network = Network::new(3, 1, ActivationType::Relu, 0.005);
network.add_hidden_layer_with_size(4);
network.add_hidden_layer_with_size(4);
network.compile();
Panics
Panics if any of the dimentions is 0 or if no hidden layers are present.
sourcepub fn dimensions(&self) -> (usize, usize)
pub fn dimensions(&self) -> (usize, usize)
Returns a Tuple with the dimentions of the Neural Network (inputs, outputs)
sourcepub fn set_activation(&mut self, activation: ActivationType)
pub fn set_activation(&mut self, activation: ActivationType)
Sets the activation function to be used by the network
sourcepub fn activation(&self) -> ActivationType
pub fn activation(&self) -> ActivationType
Returns the activation function being used
sourcepub fn set_layer_weights(&mut self, layer: usize, weights: Array2<f64>)
pub fn set_layer_weights(&mut self, layer: usize, weights: Array2<f64>)
Sets the weights and biases of the given layer
sourcepub fn layer_weights(&self, layer: usize) -> Array2<f64>
pub fn layer_weights(&self, layer: usize) -> Array2<f64>
Returns the weights of the given layer
sourcepub fn set_layer_biases(&mut self, layer: usize, biases: Array2<f64>)
pub fn set_layer_biases(&mut self, layer: usize, biases: Array2<f64>)
Sets the biases of the given layer
sourcepub fn layer_biases(&self, layer: usize) -> Array2<f64>
pub fn layer_biases(&self, layer: usize) -> Array2<f64>
Returns the biases of the given layer
Returns the number of hidden layers
sourcepub fn set_learning_rate(&mut self, alpha: f64)
pub fn set_learning_rate(&mut self, alpha: f64)
Sets the learning rate of the network
sourcepub fn learning_rate(&self) -> f64
pub fn learning_rate(&self) -> f64
Returns the learning rate of the network
sourcepub fn forward(&mut self, input: &Array1<f64>) -> Array2<f64>
pub fn forward(&mut self, input: &Array1<f64>) -> Array2<f64>
Predicts the output of the network for the given input.
Example
// ... imports here
let mut network = Network::new(3, 1, ActivationType::Relu, 0.005);
// ... training done here
let prediction = network.forward(&array![2., 1., -1.]);
sourcepub fn train(
&mut self,
training_set: &[(Array1<f64>, Array1<f64>)],
epochs: usize,
decay_time: usize
)
pub fn train( &mut self, training_set: &[(Array1<f64>, Array1<f64>)], epochs: usize, decay_time: usize )
Trains the network with the given training set for the given number of epochs.
Example
use fast_neural_network::{activation::*, neural_network::*};
use ndarray::*;
let mut network = Network::new(3, 1, ActivationType::Relu, 0.005);
network.add_hidden_layer_with_size(4);
network.add_hidden_layer_with_size(4);
network.compile();
network.train(&[(array![2., 1., -1.], array![9.])], 100, 100);