Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace the current neuron caching algorithm #72

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 28 additions & 134 deletions src/runnable.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,32 @@
use crate::topology::*;

#[cfg(not(feature = "rayon"))]
use std::{cell::RefCell, rc::Rc};
use std::collections::HashMap;

#[cfg(feature = "rayon")]
use rayon::prelude::*;
#[cfg(feature = "rayon")]
use std::sync::{Arc, RwLock};

/// A runnable, stated Neural Network generated from a [NeuralNetworkTopology]. Use [`NeuralNetwork::from`] to go from stateles to runnable.
/// Because this has state, you need to run [`NeuralNetwork::flush_state`] between [`NeuralNetwork::predict`] calls.
#[derive(Debug)]
#[cfg(not(feature = "rayon"))]
pub struct NeuralNetwork<const I: usize, const O: usize> {
input_layer: [Rc<RefCell<Neuron>>; I],
hidden_layers: Vec<Rc<RefCell<Neuron>>>,
output_layer: [Rc<RefCell<Neuron>>; O],
}

/// Parallelized version of the [`NeuralNetwork`] struct.
#[derive(Debug)]
#[cfg(feature = "rayon")]
pub struct NeuralNetwork<const I: usize, const O: usize> {
input_layer: [Arc<RwLock<Neuron>>; I],
hidden_layers: Vec<Arc<RwLock<Neuron>>>,
output_layer: [Arc<RwLock<Neuron>>; O],
pub struct NeuralNetwork<'a, const I: usize, const O: usize> {
topology: &'a NeuralNetworkTopology<I, O>,
}

impl<const I: usize, const O: usize> NeuralNetwork<I, O> {
impl<const I: usize, const O: usize> NeuralNetwork<'_, I, O> {
/// Predicts an output for the given inputs.
#[cfg(not(feature = "rayon"))]
pub fn predict(&self, inputs: [f32; I]) -> [f32; O] {
let mut state_cache = HashMap::new();

for (i, v) in inputs.iter().enumerate() {
let mut nw = self.input_layer[i].borrow_mut();
nw.state.value = *v;
nw.state.processed = true;
state_cache.insert(NeuronLocation::Input(i), *v);
}

(0..O)
.map(NeuronLocation::Output)
.map(|loc| self.process_neuron(loc))
.map(|loc| self.process_neuron(loc, &mut state_cache))
.collect::<Vec<_>>()
.try_into()
.unwrap()
Expand All @@ -65,26 +52,22 @@ impl<const I: usize, const O: usize> NeuralNetwork<I, O> {
}

#[cfg(not(feature = "rayon"))]
fn process_neuron(&self, loc: NeuronLocation) -> f32 {
let n = self.get_neuron(loc);

{
let nr = n.borrow();

if nr.state.processed {
return nr.state.value;
}
fn process_neuron(&self, loc: NeuronLocation, cache: &mut HashMap<NeuronLocation, f32>) -> f32 {
if let Some(v) = cache.get(&loc) {
return *v;
}

let mut n = n.borrow_mut();

for (l, w) in n.inputs.clone() {
n.state.value += self.process_neuron(l) * w;
let n = self.get_neuron(loc).unwrap();
let mut v = 0.;
for (l, w) in &n.inputs {
v += self.process_neuron(*l, cache) * w;
}

n.activate();
v = n.activate(v);

cache.insert(loc, v);

n.state.value
v
}

#[cfg(feature = "rayon")]
Expand Down Expand Up @@ -118,11 +101,11 @@ impl<const I: usize, const O: usize> NeuralNetwork<I, O> {
}

#[cfg(not(feature = "rayon"))]
fn get_neuron(&self, loc: NeuronLocation) -> Rc<RefCell<Neuron>> {
fn get_neuron<'a>(&self, loc: NeuronLocation) -> Option<&'a NeuronTopology> {
match loc {
NeuronLocation::Input(i) => self.input_layer[i].clone(),
NeuronLocation::Hidden(i) => self.hidden_layers[i].clone(),
NeuronLocation::Output(i) => self.output_layer[i].clone(),
NeuronLocation::Input(i) => self.topology.input_layer.get(i),
NeuronLocation::Hidden(i) => self.topology.hidden_layers.get(i),
NeuronLocation::Output(i) => self.topology.output_layer.get(i),
}
}

Expand All @@ -135,22 +118,6 @@ impl<const I: usize, const O: usize> NeuralNetwork<I, O> {
}
}

/// Flushes the network's state after a [prediction][NeuralNetwork::predict].
#[cfg(not(feature = "rayon"))]
pub fn flush_state(&self) {
for n in &self.input_layer {
n.borrow_mut().flush_state();
}

for n in &self.hidden_layers {
n.borrow_mut().flush_state();
}

for n in &self.output_layer {
n.borrow_mut().flush_state();
}
}

/// Flushes the neural network's state.
#[cfg(feature = "rayon")]
pub fn flush_state(&self) {
Expand All @@ -168,36 +135,12 @@ impl<const I: usize, const O: usize> NeuralNetwork<I, O> {
}
}

impl<const I: usize, const O: usize> From<&NeuralNetworkTopology<I, O>> for NeuralNetwork<I, O> {
impl<'a, const I: usize, const O: usize> From<&'a NeuralNetworkTopology<I, O>>
for NeuralNetwork<'a, I, O>
{
#[cfg(not(feature = "rayon"))]
fn from(value: &NeuralNetworkTopology<I, O>) -> Self {
let input_layer = value
.input_layer
.iter()
.map(|n| Rc::new(RefCell::new(Neuron::from(&n.read().unwrap().clone()))))
.collect::<Vec<_>>()
.try_into()
.unwrap();

let hidden_layers = value
.hidden_layers
.iter()
.map(|n| Rc::new(RefCell::new(Neuron::from(&n.read().unwrap().clone()))))
.collect();

let output_layer = value
.output_layer
.iter()
.map(|n| Rc::new(RefCell::new(Neuron::from(&n.read().unwrap().clone()))))
.collect::<Vec<_>>()
.try_into()
.unwrap();

Self {
input_layer,
hidden_layers,
output_layer,
}
fn from(topology: &'a NeuralNetworkTopology<I, O>) -> Self {
Self { topology }
}

#[cfg(feature = "rayon")]
Expand Down Expand Up @@ -232,55 +175,6 @@ impl<const I: usize, const O: usize> From<&NeuralNetworkTopology<I, O>> for Neur
}
}

/// A state-filled neuron.
#[derive(Clone, Debug)]
pub struct Neuron {
inputs: Vec<(NeuronLocation, f32)>,
bias: f32,

/// The current state of the neuron.
pub state: NeuronState,

/// The neuron's activation function
pub activation: ActivationFn,
}

impl Neuron {
/// Flushes a neuron's state. Called by [`NeuralNetwork::flush_state`]
pub fn flush_state(&mut self) {
self.state.value = self.bias;
}

/// Applies the activation function to the neuron
pub fn activate(&mut self) {
self.state.value = self.activation.func.activate(self.state.value);
}
}

impl From<&NeuronTopology> for Neuron {
fn from(value: &NeuronTopology) -> Self {
Self {
inputs: value.inputs.clone(),
bias: value.bias,
state: NeuronState {
value: value.bias,
..Default::default()
},
activation: value.activation.clone(),
}
}
}

/// A state used in [`Neuron`]s for cache.
#[derive(Clone, Debug, Default)]
pub struct NeuronState {
/// The current value of the neuron. Initialized to a neuron's bias when flushed.
pub value: f32,

/// Whether or not [`value`][NeuronState::value] has finished processing.
pub processed: bool,
}

/// A blanket trait for iterators meant to help with interpreting the output of a [`NeuralNetwork`]
#[cfg(feature = "max-index")]
pub trait MaxIndex<T: PartialOrd> {
Expand Down
4 changes: 4 additions & 0 deletions src/topology/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,10 @@ impl NeuronTopology {
activation,
}
}

pub fn activate(&self, value: f32) -> f32 {
self.activation.func.activate(value)
}
}

/// A pseudo-pointer of sorts used to make structural conversions very fast and easy to write.
Expand Down
Loading