pub struct NeuralNetwork { /* private fields */ }
Expand description

Neural Network

This is the main struct of the library. It contains a vector of layers and an activation function. You can use this struct and its methods to create, manipulate and even implement your ways to train a neural network.

Example

use no_brain::NeuralNetwork;
use nalgebra::dmatrix;
use nalgebra::dvector;

fn main() {
    let mut nn = NeuralNetwork::new(&vec![2, 2, 1]);

    nn.set_layer_weights(1, dmatrix![0.1, 0.2;
                                     0.3, 0.4]);
    nn.set_layer_biases(1, dvector![0.1, 0.2]);

    nn.set_layer_weights(2, dmatrix![0.9, 0.8]);
    nn.set_layer_biases(2, dvector![0.1]);

    let input = vec![0.5, 0.2];
    let output = nn.feed_forward(&input);

    println!("{:?}", output);
}

Implementations§

source§

impl NeuralNetwork

source

pub fn new(layers: &Vec<usize>) -> Self

Creates a new Neural Network with the given layers. The layers vector must contain the number of neurons for each layer.

Example
let nn = NeuralNetwork::new(&vec![2, 2, 1]);
source

pub fn feed_forward(&self, inputs: &Vec<f64>) -> Vec<f64>

Feeds the given inputs to the neural network and returns the output. The inputs vector must have the same size as the first layer of the network.

Example
let mut nn = NeuralNetwork::new(&vec![1, 1]);

nn.set_layer_weights(1, dmatrix![0.5]);
nn.set_layer_biases(1, dvector![0.5]);

let input = vec![0.5];
let output = nn.feed_forward(&input);
assert_eq!(output, vec![0.679178699175393]);
source

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

Sets the layer weights for the given layer. The weights matrix must have the size of the layer neurons x layer inputs. The layer index must be greater than 0 since it corresponds to the layer number that receives these weights.

source

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

Sets the layer biases for the given layer. The biases vector must have the size of the layer neurons. The layer index must be greater than 0 since the input layer does not have biases.

source

pub fn set_weight( &mut self, layer: usize, neuron: usize, input: usize, weight: f64 )

Sets the weight of a specific neuron connection. The layer index must be greater than 0 since the input layer does not have weights.

source

pub fn print(&self)

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> 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> Same for T

§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
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.
§

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

§

fn vzip(self) -> V