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
impl NeuralNetwork
Sourcepub fn new(layers: &Vec<usize>) -> Self
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]);
Sourcepub fn feed_forward(&self, inputs: &Vec<f64>) -> Vec<f64>
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]);
Sourcepub fn set_layer_weights(&mut self, layer: usize, weights: DMatrix<f64>)
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.
Sourcepub fn set_layer_biases(&mut self, layer: usize, biases: DVector<f64>)
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.
Sourcepub fn set_weight(
&mut self,
layer: usize,
neuron: usize,
input: usize,
weight: f64,
)
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.
Sourcepub fn get_weight(&self, layer: usize, neuron: usize, input: usize) -> f64
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.
Sourcepub fn num_layers(&self) -> usize
pub fn num_layers(&self) -> usize
Returns the number of layers of the neural network.
Sourcepub fn layer_size(&self, layer: usize) -> usize
pub fn layer_size(&self, layer: usize) -> usize
Returns the number of neurons of the given layer.
pub fn print(&self)
Trait Implementations§
Source§impl<'de> Deserialize<'de> for NeuralNetwork
impl<'de> Deserialize<'de> for NeuralNetwork
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>,
Source§impl Display for NeuralNetwork
impl Display for NeuralNetwork
Auto Trait Implementations§
impl Freeze for NeuralNetwork
impl RefUnwindSafe for NeuralNetwork
impl Send for NeuralNetwork
impl Sync for NeuralNetwork
impl Unpin for NeuralNetwork
impl UnwindSafe for NeuralNetwork
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<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self
is actually part of its subset T
(and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset
but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.