Struct Network

Source
pub struct Network { /* private fields */ }
Expand description

The main heap based neural network struct.

Implementations§

Source§

impl Network

Source

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);
Source

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);
Source

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");
Source

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");
Source

pub fn from_json(json: &str) -> Self

Creates a network from the given JSON string.

§Panics

Panics if the JSON string is not valid.

Source

pub fn add_hidden_layer(&mut self, layer: Layer)

Adds a hidden layer to the network.

Source

pub fn add_hidden_layer_with_size(&mut self, size: usize)

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);
Source

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.

Source

pub fn dimensions(&self) -> (usize, usize)

Returns a Tuple with the dimentions of the Neural Network (inputs, outputs)

Source

pub fn set_activation(&mut self, activation: ActivationType)

Sets the activation function to be used by the network

Source

pub fn activation(&self) -> ActivationType

Returns the activation function being used

Source

pub fn set_layer_weights(&mut self, layer: usize, weights: Array2<f64>)

Sets the weights and biases of the given layer

Source

pub fn layer_weights(&self, layer: usize) -> Array2<f64>

Returns the weights of the given layer

Source

pub fn set_layer_biases(&mut self, layer: usize, biases: Array1<f64>)

Sets the biases of the given layer

Source

pub fn layer_biases(&self, layer: usize) -> Array1<f64>

Returns the biases of the given layer

Source

pub fn hidden_layers_size(&self) -> usize

Returns the number of hidden layers

Source

pub fn set_learning_rate(&mut self, alpha: f64)

Sets the learning rate of the network

Source

pub fn learning_rate(&self) -> f64

Returns the learning rate of the network

Source

pub fn set_ui_update_interval(&mut self, interval: usize)

Sets the UI progress update interval of the network

Source

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.]);
Source

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 Clone for Network

Source§

fn clone(&self) -> Network

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Network

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Network

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Network

Formats the network to be printed

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Serialize for Network

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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>,

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,