Struct only_brain::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 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
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.
pub fn print(&self)
Auto Trait Implementations§
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
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read more§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).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.