Struct NeuralNet

Source
pub struct NeuralNet<A: Activation> { /* private fields */ }
Expand description

A fully-connected neural network.

Implementations§

Source§

impl<A: Activation + Serialize + DeserializeOwned> NeuralNet<A>

Source

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.

Source

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")?;
Source

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);
Source

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);
Source

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")?;
Source

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>

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<A: Activation> Serialize for NeuralNet<A>

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§

§

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