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 prepares the random inital values of the network. Can be re-run as much as needed, if needed. This is done automatically during training if it was not compiled before hand.
Compilation should be done after the hidden layers are set, but before setting any custom layer values.
§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: Array1<f64>)
pub fn set_layer_biases(&mut self, layer: usize, biases: Array1<f64>)
Sets the biases of the given layer
Sourcepub fn layer_biases(&self, layer: usize) -> Array1<f64>
pub fn layer_biases(&self, layer: usize) -> Array1<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 set_ui_update_interval(&mut self, interval: usize)
pub fn set_ui_update_interval(&mut self, interval: usize)
Sets the UI progress update interval 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.
It will compile the network if it was not compiled at least once
§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);
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Network
impl<'de> Deserialize<'de> for Network
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Auto Trait Implementations§
impl Freeze for Network
impl RefUnwindSafe for Network
impl Send for Network
impl Sync for Network
impl Unpin for Network
impl UnwindSafe for Network
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more