Shad Amethyst 2 years ago
parent dd278e7b90
commit 060b801ad6

@ -2,7 +2,7 @@ use num::ToPrimitive;
use crate::{
derivable::NeuraLoss, layer::NeuraTrainableLayerBackprop, layer::NeuraTrainableLayerSelf,
network::NeuraTrainableNetworkBase,
network::NeuraOldTrainableNetworkBase,
};
use super::*;
@ -20,12 +20,12 @@ impl<Loss> NeuraBackprop<Loss> {
impl<
Input,
Target,
Trainable: NeuraTrainableNetworkBase<Input>,
Trainable: NeuraOldTrainableNetworkBase<Input>,
Loss: NeuraLoss<Trainable::Output, Target = Target> + Clone,
> NeuraGradientSolver<Input, Target, Trainable> for NeuraBackprop<Loss>
where
<Loss as NeuraLoss<Trainable::Output>>::Output: ToPrimitive,
Trainable: for<'a> NeuraTrainableNetwork<Input, (&'a NeuraBackprop<Loss>, &'a Target)>,
Trainable: for<'a> NeuraOldTrainableNetwork<Input, (&'a NeuraBackprop<Loss>, &'a Target)>,
{
fn get_gradient(
&self,

@ -30,7 +30,7 @@ impl<
F,
Act: Clone + NeuraDerivable<f64>,
Input,
Trainable: NeuraTrainableNetwork<Input, NeuraForwardPair<Act>, Output = DVector<F>>,
Trainable: NeuraOldTrainableNetwork<Input, NeuraForwardPair<Act>, Output = DVector<F>>,
> NeuraGradientSolver<Input, bool, Trainable> for NeuraForwardForward<Act>
where
F: ToPrimitive,
@ -138,7 +138,7 @@ impl<
fn map_epsilon<From, To, Gradient, Cb: Fn(From) -> To>(
&self,
rec_opt_output: Self::Output<From, Gradient>,
callback: Cb
_callback: Cb
) -> Self::Output<To, Gradient> {
rec_opt_output
}

@ -6,7 +6,7 @@ pub use forward_forward::NeuraForwardForward;
use crate::{
layer::NeuraTrainableLayerBase,
network::{NeuraTrainableNetwork, NeuraTrainableNetworkBase},
network::{NeuraOldTrainableNetwork, NeuraOldTrainableNetworkBase},
};
pub trait NeuraGradientSolverBase {
@ -37,7 +37,7 @@ pub trait NeuraGradientSolverTransient<Input, Layer: NeuraTrainableLayerBase<Inp
) -> Self::Output<To, Gradient>;
}
pub trait NeuraGradientSolver<Input, Target, Trainable: NeuraTrainableNetworkBase<Input>> {
pub trait NeuraGradientSolver<Input, Target, Trainable: NeuraOldTrainableNetworkBase<Input>> {
fn get_gradient(
&self,
trainable: &Trainable,

@ -2,11 +2,11 @@ use crate::{
algebra::NeuraVectorSpace, gradient_solver::{NeuraGradientSolverBase, NeuraGradientSolverFinal}, layer::NeuraLayer,
};
pub mod residual;
// pub mod residual;
pub mod sequential;
// TODO: extract regularize from this, so that we can drop the trait constraints on NeuraSequential's impl
pub trait NeuraTrainableNetworkBase<Input>: NeuraLayer<Input> {
pub trait NeuraOldTrainableNetworkBase<Input>: NeuraLayer<Input> {
type Gradient: NeuraVectorSpace;
type LayerOutput;
@ -21,7 +21,7 @@ pub trait NeuraTrainableNetworkBase<Input>: NeuraLayer<Input> {
fn prepare(&mut self, train_iteration: bool);
}
pub trait NeuraTrainableNetwork<Input, Optimizer>: NeuraTrainableNetworkBase<Input>
pub trait NeuraOldTrainableNetwork<Input, Optimizer>: NeuraOldTrainableNetworkBase<Input>
where
Optimizer: NeuraGradientSolverBase,
{
@ -33,7 +33,7 @@ where
}
impl<Input: Clone, Optimizer: NeuraGradientSolverFinal<Input>>
NeuraTrainableNetwork<Input, Optimizer> for ()
NeuraOldTrainableNetwork<Input, Optimizer> for ()
{
fn traverse(
&self,

@ -174,16 +174,25 @@ where
let child_result = self.child_network.traverse(&rest, optimizer);
// TODO: maybe move this to a custom impl of NeuraGradientSolverTransient for NeuraResidualInput?
// Or have a different set of traits for NeuraTrainableNetwork specific to NeuraResidualNodes
let child_result = optimizer.map_epsilon(child_result, |epsilon| {
let child_result = optimizer.map_epsilon(child_result, |_epsilon| {
// Pop the first value from `epsilon`, then:
// - compute its sum
// - use it to compute the outcoming epsilon of the current layer
// - split the oucoming epsilon into its original components, and push those back onto the rest
// At this point, the value for `epsilon` in the gradient solver's state should be ready for another iteration,
// with the first value containing the unsummed incoming epsilon values from the downstream layers
todo!();
todo!()
});
optimizer.eval_layer(
&self.layer,
&layer_input,
&layer_output,
&layer_intermediary,
child_result,
|this_gradient, child_gradient| (this_gradient, Box::new(child_gradient))
);
todo!();
}
}

@ -1,6 +1,6 @@
use super::{NeuraTrainableNetwork, NeuraTrainableNetworkBase};
use super::{NeuraOldTrainableNetwork, NeuraOldTrainableNetworkBase};
use crate::{
gradient_solver::{NeuraGradientSolverFinal, NeuraGradientSolverTransient},
gradient_solver::{NeuraGradientSolverTransient},
layer::{
NeuraLayer, NeuraPartialLayer, NeuraShape, NeuraTrainableLayerBase, NeuraTrainableLayerSelf,
},
@ -82,8 +82,8 @@ impl<Layer, ChildNetwork> NeuraSequential<Layer, ChildNetwork> {
impl<
Input,
Layer: NeuraTrainableLayerBase<Input> + NeuraTrainableLayerSelf<Input>,
ChildNetwork: NeuraTrainableNetworkBase<Layer::Output>,
> NeuraTrainableNetworkBase<Input> for NeuraSequential<Layer, ChildNetwork>
ChildNetwork: NeuraOldTrainableNetworkBase<Layer::Output>,
> NeuraOldTrainableNetworkBase<Input> for NeuraSequential<Layer, ChildNetwork>
{
type Gradient = (Layer::Gradient, Box<ChildNetwork::Gradient>);
type LayerOutput = Layer::Output;
@ -114,7 +114,7 @@ impl<
}
/// A dummy implementation of `NeuraTrainableNetwork`, which simply calls `loss.eval` in `backpropagate`.
impl<Input: Clone> NeuraTrainableNetworkBase<Input> for () {
impl<Input: Clone> NeuraOldTrainableNetworkBase<Input> for () {
type Gradient = ();
type LayerOutput = Input;
@ -143,10 +143,10 @@ impl<
Input,
Layer: NeuraTrainableLayerBase<Input> + NeuraTrainableLayerSelf<Input>,
Optimizer: NeuraGradientSolverTransient<Input, Layer>,
ChildNetwork: NeuraTrainableNetworkBase<Layer::Output>,
> NeuraTrainableNetwork<Input, Optimizer> for NeuraSequential<Layer, ChildNetwork>
ChildNetwork: NeuraOldTrainableNetworkBase<Layer::Output>,
> NeuraOldTrainableNetwork<Input, Optimizer> for NeuraSequential<Layer, ChildNetwork>
where
ChildNetwork: NeuraTrainableNetwork<Layer::Output, Optimizer>,
ChildNetwork: NeuraOldTrainableNetwork<Layer::Output, Optimizer>,
{
fn traverse(
&self,

@ -1,6 +1,6 @@
use crate::{
algebra::NeuraVectorSpace, gradient_solver::NeuraGradientSolver,
network::NeuraTrainableNetworkBase,
network::NeuraOldTrainableNetworkBase,
};
#[non_exhaustive]
@ -73,7 +73,7 @@ impl NeuraBatchedTrainer {
pub fn train<
Input: Clone,
Target: Clone,
Network: NeuraTrainableNetworkBase<Input>,
Network: NeuraOldTrainableNetworkBase<Input>,
GradientSolver: NeuraGradientSolver<Input, Target, Network>,
Inputs: IntoIterator<Item = (Input, Target)>,
>(
@ -84,7 +84,7 @@ impl NeuraBatchedTrainer {
test_inputs: &[(Input, Target)],
) -> Vec<(f64, f64)>
where
<Network as NeuraTrainableNetworkBase<Input>>::Gradient: std::fmt::Debug,
<Network as NeuraOldTrainableNetworkBase<Input>>::Gradient: std::fmt::Debug,
{
let mut losses = Vec::new();
let mut iter = inputs.into_iter();

Loading…
Cancel
Save