Struct NeuralNetwork

Source
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 only_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 get_weight(&self, layer: usize, neuron: usize, input: usize) -> f64

Gets 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 num_layers(&self) -> usize

Returns the number of layers of the neural network.

Source

pub fn layer_size(&self, layer: usize) -> usize

Returns the number of neurons of the given layer.

Source

pub fn print(&self)

Trait Implementations§

Source§

impl<'de> Deserialize<'de> for NeuralNetwork

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 NeuralNetwork

Source§

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

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

impl Serialize for NeuralNetwork

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

Source§

type Output = T

Should always be Self
Source§

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

Source§

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

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

fn is_in_subset(&self) -> bool

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

fn to_subset_unchecked(&self) -> SS

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

fn from_subset(element: &SS) -> SP

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