pub struct NeuralNet<A: Activation> { /* private fields */ }Expand description
A fully-connected neural network.
Implementations§
Source§impl<A: Activation + Serialize + DeserializeOwned> NeuralNet<A>
impl<A: Activation + Serialize + DeserializeOwned> NeuralNet<A>
Sourcepub fn new(node_counts: &[usize]) -> Self
pub fn new(node_counts: &[usize]) -> Self
Creates a new NeuralNet with the given node configuration.
Note that you must supply a type annotation so that it knows which
Activation to use.
§Examples
use scholar::{NeuralNet, Sigmoid};
// Creates a neural network with two input nodes, a single hidden layer with two nodes,
// and one output node
let brain: NeuralNet<Sigmoid> = NeuralNet::new(&[2, 2, 1]);§Panics
This function panics if the number of layers (i.e. the length of the given node_counts
slice) is less than 2.
Sourcepub fn from_file(path: impl AsRef<Path>) -> Result<Self, LoadErr>
pub fn from_file(path: impl AsRef<Path>) -> Result<Self, LoadErr>
Creates a new NeuralNet from a valid file (those created using
NeuralNet::save()).
§Examples
use scholar::{NeuralNet, Sigmoid};
let brain: NeuralNet<Sigmoid> = NeuralNet::from_file("brain.network")?;Sourcepub fn train(
&mut self,
training_dataset: Dataset,
iterations: u64,
learning_rate: f64,
)
pub fn train( &mut self, training_dataset: Dataset, iterations: u64, learning_rate: f64, )
Trains the network on the given Dataset for the given number of iterations.
§Examples
use scholar::{Dataset, NeuralNet, Sigmoid};
let dataset = Dataset::from_csv("iris.csv", false, 4)?;
let mut brain: NeuralNet<Sigmoid> = NeuralNet::new(&[4, 10, 10, 1]);
// Trains the network by iterating over the entire dataset 10,000 times. The last parameter
// (the 'learning rate') dictates how quickly the network 'adapts to the dataset'
brain.train(dataset, 10_000, 0.01);Sourcepub fn test(&mut self, testing_dataset: Dataset) -> f64
pub fn test(&mut self, testing_dataset: Dataset) -> f64
Calculates the average cost of the network.
§Examples
use scholar::{Dataset, NeuralNet, Sigmoid};
let dataset = Dataset::from_csv("iris.csv", false, 4)?;
let (training_data, testing_data) = dataset.split(0.75);
let mut brain: NeuralNet<Sigmoid> = NeuralNet::new(&[4, 10, 10, 1]);
brain.train(training_data, 10_000, 0.01);
let avg_cost = brain.test(testing_data);
println!("Accuracy: {:.2}%", (1.0 - avg_cost) * 100.0);Sourcepub fn save(&self, path: impl AsRef<Path>) -> Result<(), SaveErr>
pub fn save(&self, path: impl AsRef<Path>) -> Result<(), SaveErr>
Saves the network in a binary format to the specified path.
§Examples
use scholar::{NeuralNet, Sigmoid};
let brain: NeuralNet<Sigmoid> = NeuralNet::new(&[2, 2, 1]);
// Note that the file doesn't have to use the '.network' extension; you can actually
// choose anything you wish!
brain.save("brain.network")?;Sourcepub fn guess(&mut self, inputs: &[f64]) -> Vec<f64>
pub fn guess(&mut self, inputs: &[f64]) -> Vec<f64>
Performs the feedforward algorithm on the given input slice, returning the value of the output layer as a vector.
§Examples
use scholar::{NeuralNet, Sigmoid};
let mut brain: NeuralNet<Sigmoid> = NeuralNet::new(&[3, 10, 2]);
let result = brain.guess(&[1.0, 0.0, -0.5]);
assert_eq!(result.len(), 2);§Panics
This method panics if the number of given input values is not equal to the number of nodes in the network’s input layer.
Trait Implementations§
Source§impl<'de, A: Activation> Deserialize<'de> for NeuralNet<A>
impl<'de, A: Activation> Deserialize<'de> for NeuralNet<A>
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>,
Auto Trait Implementations§
impl<A> Freeze for NeuralNet<A>
impl<A> RefUnwindSafe for NeuralNet<A>where
A: RefUnwindSafe,
impl<A> Send for NeuralNet<A>where
A: Send,
impl<A> Sync for NeuralNet<A>where
A: Sync,
impl<A> Unpin for NeuralNet<A>where
A: Unpin,
impl<A> UnwindSafe for NeuralNet<A>where
A: UnwindSafe,
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.