use core::marker::PhantomData;
use crate::activation::Activation;
use crate::neuralnet::{Connection, argmax};
use crate::optimizer::{Optimizer, Sgd};
use crate::qformat::Scalar;
use crate::training::decay_weights;
pub struct Dense<V, A, const IN: usize, const OUT: usize> {
weights: [[Connection<V>; OUT]; IN],
bias: [Connection<V>; OUT],
outputs: [V; OUT],
deltas: [V; OUT],
activation: PhantomData<fn() -> A>,
}
impl<V: Scalar, A, const IN: usize, const OUT: usize> Default for Dense<V, A, IN, OUT> {
fn default() -> Self {
Self {
weights: [[Connection::default(); OUT]; IN],
bias: [Connection::default(); OUT],
outputs: [V::ZERO; OUT],
deltas: [V::ZERO; OUT],
activation: PhantomData,
}
}
}
impl<V: Copy, A, const IN: usize, const OUT: usize> Dense<V, A, IN, OUT> {
pub fn weights(&self) -> [[V; OUT]; IN] {
self.weights.map(|row| row.map(|c| c.weight))
}
pub fn bias_weights(&self) -> [V; OUT] {
self.bias.map(|c| c.weight)
}
pub fn set_weights(&mut self, weights: &[[V; OUT]; IN]) {
for (row, new) in self.weights.iter_mut().zip(weights) {
for (c, &w) in row.iter_mut().zip(new) {
c.weight = w;
}
}
}
pub fn set_bias_weights(&mut self, weights: &[V; OUT]) {
for (c, &w) in self.bias.iter_mut().zip(weights) {
c.weight = w;
}
}
pub fn outputs(&self) -> [V; OUT] {
self.outputs
}
}
impl<V: Scalar, A: Activation<V>, const IN: usize, const OUT: usize> Dense<V, A, IN, OUT> {
fn initialize(&mut self, random_weight: &mut dyn FnMut() -> V) {
for row in self
.weights
.iter_mut()
.chain(core::iter::once(&mut self.bias))
{
for c in row.iter_mut() {
c.weight = random_weight();
}
for c in row.iter_mut() {
c.set_delta_weight(V::ZERO);
}
}
}
fn forward(&mut self, input: &[V; IN]) {
for o in 0..OUT {
let sum = self.pre_activation(input, o);
if let Some(out) = self.outputs.get_mut(o) {
*out = A::activate(sum);
}
}
}
fn pre_activation(&self, input: &[V; IN], o: usize) -> V {
let weight = |c: Option<&Connection<V>>| c.map_or(V::ZERO, |c| c.weight);
let mut sum = V::ONE * weight(self.bias.get(o));
let mut layer_sum = V::ZERO;
for (&x, row) in input.iter().zip(&self.weights) {
layer_sum += x * weight(row.get(o));
}
sum += layer_sum;
sum
}
fn output_deltas(&mut self, targets: &[V; OUT]) {
for ((delta, &out), &target) in self.deltas.iter_mut().zip(&self.outputs).zip(targets) {
*delta = (target - out) * A::derivative(out);
}
}
fn hidden_deltas(&mut self, signal: &[V; OUT]) {
for ((delta, &out), &s) in self.deltas.iter_mut().zip(&self.outputs).zip(signal) {
*delta = s * A::derivative(out);
}
}
fn back_signal(&self) -> [V; IN] {
self.weights.map(|row| {
let mut sum = V::ZERO;
for (c, &delta) in row.iter().zip(&self.deltas) {
sum += c.weight * delta;
}
sum
})
}
fn gradients(&mut self, input: &[V; IN]) {
for (row, &x) in self.weights.iter_mut().zip(input) {
for (c, &delta) in row.iter_mut().zip(&self.deltas) {
c.gradient = x * delta;
}
}
for (c, &delta) in self.bias.iter_mut().zip(&self.deltas) {
c.gradient = V::ONE * delta;
}
}
fn update<Opt: Optimizer<V>>(
&mut self,
rates: (V, V, V),
clip: Option<(V, V)>,
decay: Option<V>,
optimizer: &mut Opt,
) {
if let Some(factor) = decay {
decay_weights(self.weights.as_flattened_mut(), factor);
}
crate::optimizer::update_all(optimizer, self.weights.as_flattened_mut(), rates, clip);
crate::optimizer::update_all(optimizer, &mut self.bias, rates, clip);
}
}
pub struct InferenceDense<V, A, const IN: usize, const OUT: usize> {
pub weights: [[V; OUT]; IN],
pub bias_weights: [V; OUT],
activation: PhantomData<fn() -> A>,
}
impl<V: Copy, A, const IN: usize, const OUT: usize> Clone for InferenceDense<V, A, IN, OUT> {
fn clone(&self) -> Self {
*self
}
}
impl<V: Copy, A, const IN: usize, const OUT: usize> Copy for InferenceDense<V, A, IN, OUT> {}
impl<V, A, const IN: usize, const OUT: usize> InferenceDense<V, A, IN, OUT> {
pub const fn new(weights: [[V; OUT]; IN], bias_weights: [V; OUT]) -> Self {
Self {
weights,
bias_weights,
activation: PhantomData,
}
}
}
impl<V: Scalar, A: Activation<V>, const IN: usize, const OUT: usize> InferenceDense<V, A, IN, OUT> {
pub fn forward(&self, input: &[V; IN]) -> [V; OUT] {
let mut out = [V::ZERO; OUT];
for (o, y) in out.iter_mut().enumerate() {
let mut sum = V::ONE * self.bias_weights[o];
let mut layer_sum = V::ZERO;
for (&x, row) in input.iter().zip(&self.weights) {
layer_sum += x * row[o];
}
sum += layer_sum;
*y = A::activate(sum);
}
out
}
}
impl<V: Copy, A, const IN: usize, const OUT: usize> Dense<V, A, IN, OUT> {
pub fn to_inference(&self) -> InferenceDense<V, A, IN, OUT> {
InferenceDense::new(self.weights(), self.bias_weights())
}
pub fn set_from(&mut self, from: &InferenceDense<V, A, IN, OUT>) {
self.set_weights(&from.weights);
self.set_bias_weights(&from.bias_weights);
}
}
mod sealed {
pub trait LayersImpl {
type Value: Copy;
type Inputs: Copy;
type Outputs: Copy + AsRef<[Self::Value]>;
fn zero_inputs() -> Self::Inputs;
fn initialize(&mut self, random_weight: &mut dyn FnMut() -> Self::Value);
fn feed_forward(&mut self, inputs: &Self::Inputs);
fn outputs(&self) -> Self::Outputs;
fn output_pre_activations(&self, inputs: &Self::Inputs) -> Self::Outputs;
fn calculate_error(&self, targets: &Self::Outputs) -> Self::Value;
fn train<Opt: crate::optimizer::Optimizer<Self::Value>>(
&mut self,
inputs: &Self::Inputs,
targets: &Self::Outputs,
rates: (Self::Value, Self::Value, Self::Value),
clip: Option<(Self::Value, Self::Value)>,
decay: Option<Self::Value>,
optimizer: &mut Opt,
);
const OUTPUTS: usize;
type Inference: InferenceLayersImpl<Inputs = Self::Inputs, Outputs = Self::Outputs>;
fn to_inference(&self) -> Self::Inference;
fn set_from(&mut self, from: &Self::Inference);
}
pub trait InferenceLayersImpl {
type Inputs: Copy;
type Outputs: Copy;
fn feed_forward(&self, inputs: &Self::Inputs) -> Self::Outputs;
}
}
pub trait Layers: sealed::LayersImpl {}
impl<T: sealed::LayersImpl> Layers for T {}
pub trait InferenceLayers: sealed::InferenceLayersImpl {}
impl<T: sealed::InferenceLayersImpl> InferenceLayers for T {}
fn mean_squared_error<V: Scalar, const OUT: usize>(outputs: &[V; OUT], targets: &[V; OUT]) -> V {
let mut error = V::ZERO;
for (&target, &out) in targets.iter().zip(outputs) {
let delta = target - out;
error += delta * delta;
}
if OUT > 1 {
error = error.div_int(OUT as i32);
}
error
}
macro_rules! impl_layers {
(
[$($idx:tt: $act:ident, $din:ident, $dout:ident);+]
first $first:tt, $d0:ident; last $last:tt, $dn:ident;
fwd [$($prev:tt > $next:tt),*]; back [$($hi:tt > $lo:tt),*];
consts [$($d:ident),+]
) => {
impl<V: Scalar, $($act: Activation<V>,)+ $(const $d: usize,)+> sealed::LayersImpl
for ($(Dense<V, $act, $din, $dout>,)+)
{
type Value = V;
type Inputs = [V; $d0];
type Outputs = [V; $dn];
const OUTPUTS: usize = $dn;
fn zero_inputs() -> [V; $d0] {
[V::ZERO; $d0]
}
fn initialize(&mut self, random_weight: &mut dyn FnMut() -> V) {
$(self.$idx.initialize(random_weight);)+
}
fn feed_forward(&mut self, inputs: &[V; $d0]) {
self.$first.forward(inputs);
$(self.$next.forward(&self.$prev.outputs);)*
}
fn outputs(&self) -> [V; $dn] {
self.$last.outputs
}
fn output_pre_activations(&self, inputs: &[V; $d0]) -> [V; $dn] {
let _input = inputs;
$(let _input = &self.$prev.outputs;)*
core::array::from_fn(|o| self.$last.pre_activation(_input, o))
}
fn calculate_error(&self, targets: &[V; $dn]) -> V {
mean_squared_error(&self.$last.outputs, targets)
}
fn train<Opt: Optimizer<V>>(
&mut self,
inputs: &[V; $d0],
targets: &[V; $dn],
rates: (V, V, V),
clip: Option<(V, V)>,
decay: Option<V>,
optimizer: &mut Opt,
) {
self.$last.output_deltas(targets);
$(
let signal = self.$hi.back_signal();
self.$lo.hidden_deltas(&signal);
)*
self.$first.gradients(inputs);
$(self.$next.gradients(&self.$prev.outputs);)*
optimizer.begin_step();
self.$last.update(rates, clip, decay, optimizer);
$(self.$lo.update(rates, clip, decay, optimizer);)*
}
type Inference = ($(InferenceDense<V, $act, $din, $dout>,)+);
fn to_inference(&self) -> Self::Inference {
($(self.$idx.to_inference(),)+)
}
fn set_from(&mut self, from: &Self::Inference) {
$(self.$idx.set_from(&from.$idx);)+
}
}
impl<V: Scalar, $($act: Activation<V>,)+ $(const $d: usize,)+> sealed::InferenceLayersImpl
for ($(InferenceDense<V, $act, $din, $dout>,)+)
{
type Inputs = [V; $d0];
type Outputs = [V; $dn];
fn feed_forward(&self, inputs: &[V; $d0]) -> [V; $dn] {
let x = self.$first.forward(inputs);
$(let x = self.$next.forward(&x);)*
x
}
}
};
}
impl_layers!([0: A0, D0, D1] first 0, D0; last 0, D1; fwd []; back []; consts [D0, D1]);
impl_layers!([0: A0, D0, D1; 1: A1, D1, D2] first 0, D0; last 1, D2;
fwd [0 > 1]; back [1 > 0]; consts [D0, D1, D2]);
impl_layers!([0: A0, D0, D1; 1: A1, D1, D2; 2: A2, D2, D3] first 0, D0; last 2, D3;
fwd [0 > 1, 1 > 2]; back [2 > 1, 1 > 0]; consts [D0, D1, D2, D3]);
impl_layers!([0: A0, D0, D1; 1: A1, D1, D2; 2: A2, D2, D3; 3: A3, D3, D4] first 0, D0; last 3, D4;
fwd [0 > 1, 1 > 2, 2 > 3]; back [3 > 2, 2 > 1, 1 > 0]; consts [D0, D1, D2, D3, D4]);
impl_layers!([0: A0, D0, D1; 1: A1, D1, D2; 2: A2, D2, D3; 3: A3, D3, D4; 4: A4, D4, D5]
first 0, D0; last 4, D5;
fwd [0 > 1, 1 > 2, 2 > 3, 3 > 4]; back [4 > 3, 3 > 2, 2 > 1, 1 > 0];
consts [D0, D1, D2, D3, D4, D5]);
impl_layers!([0: A0, D0, D1; 1: A1, D1, D2; 2: A2, D2, D3; 3: A3, D3, D4; 4: A4, D4, D5; 5: A5, D5, D6]
first 0, D0; last 5, D6;
fwd [0 > 1, 1 > 2, 2 > 3, 3 > 4, 4 > 5]; back [5 > 4, 4 > 3, 3 > 2, 2 > 1, 1 > 0];
consts [D0, D1, D2, D3, D4, D5, D6]);
impl_layers!([0: A0, D0, D1; 1: A1, D1, D2; 2: A2, D2, D3; 3: A3, D3, D4; 4: A4, D4, D5; 5: A5, D5, D6;
6: A6, D6, D7]
first 0, D0; last 6, D7;
fwd [0 > 1, 1 > 2, 2 > 3, 3 > 4, 4 > 5, 5 > 6]; back [6 > 5, 5 > 4, 4 > 3, 3 > 2, 2 > 1, 1 > 0];
consts [D0, D1, D2, D3, D4, D5, D6, D7]);
impl_layers!([0: A0, D0, D1; 1: A1, D1, D2; 2: A2, D2, D3; 3: A3, D3, D4; 4: A4, D4, D5; 5: A5, D5, D6;
6: A6, D6, D7; 7: A7, D7, D8]
first 0, D0; last 7, D8;
fwd [0 > 1, 1 > 2, 2 > 3, 3 > 4, 4 > 5, 5 > 6, 6 > 7];
back [7 > 6, 6 > 5, 5 > 4, 4 > 3, 3 > 2, 2 > 1, 1 > 0];
consts [D0, D1, D2, D3, D4, D5, D6, D7, D8]);
pub struct NeuralNetwork<L: Layers, Opt = Sgd> {
layers: L,
inputs: L::Inputs,
learning_rate: L::Value,
momentum_rate: L::Value,
acceleration_rate: L::Value,
gradient_clip: Option<(L::Value, L::Value)>,
optimizer: Opt,
}
impl<V: Scalar, L: Layers<Value = V> + Default> NeuralNetwork<L, Sgd> {
pub fn new(random_weight: impl FnMut() -> V) -> Self {
Self::with_optimizer(random_weight, Sgd)
}
}
impl<V: Scalar, L: Layers<Value = V> + Default, Opt: Optimizer<V>> NeuralNetwork<L, Opt> {
pub fn with_optimizer(mut random_weight: impl FnMut() -> V, optimizer: Opt) -> Self {
let mut layers = L::default();
layers.initialize(&mut random_weight);
for _ in 0..L::OUTPUTS {
random_weight();
}
Self {
layers,
inputs: L::zero_inputs(),
learning_rate: V::pow2_neg(2),
momentum_rate: V::pow2_neg(1),
acceleration_rate: V::pow2_neg(4),
gradient_clip: None,
optimizer,
}
}
}
impl<V: Scalar, L: Layers<Value = V>, Opt: Optimizer<V>> NeuralNetwork<L, Opt> {
pub fn optimizer(&self) -> &Opt {
&self.optimizer
}
pub fn set_learning_rate(&mut self, rate: V) {
self.learning_rate = rate;
}
pub fn set_momentum_rate(&mut self, rate: V) {
self.momentum_rate = rate;
}
pub fn set_acceleration_rate(&mut self, rate: V) {
self.acceleration_rate = rate;
}
pub fn set_gradient_clip(&mut self, range: Option<(V, V)>) {
self.gradient_clip = range;
}
pub fn learning_rate(&self) -> V {
self.learning_rate
}
pub fn feed_forward(&mut self, inputs: &L::Inputs) {
self.inputs = *inputs;
self.layers.feed_forward(inputs);
}
pub fn learned_values(&self) -> L::Outputs {
self.layers.outputs()
}
pub fn output_pre_activations(&self) -> L::Outputs {
self.layers.output_pre_activations(&self.inputs)
}
pub fn predicted_class(&self) -> usize {
argmax(self.layers.output_pre_activations(&self.inputs).as_ref())
}
pub fn calculate_error(&self, targets: &L::Outputs) -> V {
self.layers.calculate_error(targets)
}
pub fn train(&mut self, targets: &L::Outputs) {
let rates = (
self.learning_rate,
self.momentum_rate,
self.acceleration_rate,
);
self.layers.train(
&self.inputs,
targets,
rates,
self.gradient_clip,
self.optimizer
.weight_decay()
.map(|lambda| self.learning_rate * lambda),
&mut self.optimizer,
);
self.learning_rate = self.optimizer.next_learning_rate(self.learning_rate);
}
pub fn layers(&self) -> &L {
&self.layers
}
pub fn layers_mut(&mut self) -> &mut L {
&mut self.layers
}
pub fn to_inference(&self) -> InferenceNetwork<L::Inference> {
InferenceNetwork::new(self.layers.to_inference())
}
pub fn set_weights(&mut self, from: &InferenceNetwork<L::Inference>) {
self.layers.set_from(&from.layers);
}
}
pub struct InferenceNetwork<L> {
pub layers: L,
}
impl<L> InferenceNetwork<L> {
pub const fn new(layers: L) -> Self {
Self { layers }
}
}
impl<L: InferenceLayers> InferenceNetwork<L> {
pub fn feed_forward(&self, inputs: &L::Inputs) -> L::Outputs {
self.layers.feed_forward(inputs)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::activation::{Linear, Relu, Sigmoid, Tanh};
use crate::rand::GlibcRand;
use crate::{MultilayerPerceptron, Q8_8, Q16_16};
fn same_as_perceptron<V, H, O, const IN: usize, const HID: usize, const OUT: usize>(
raw: impl Fn(i32) -> V,
seed: u32,
) where
V: Scalar + PartialEq + core::fmt::Debug,
H: Activation<V>,
O: Activation<V>,
{
let mut a = GlibcRand::new(seed);
let mut b = GlibcRand::new(seed);
let mut mlp = MultilayerPerceptron::<V, H, O, IN, HID, OUT>::new(|| raw(a.next_i32()));
let mut net = NeuralNetwork::<(Dense<V, H, IN, HID>, Dense<V, O, HID, OUT>)>::new(|| {
raw(b.next_i32())
});
assert_eq!(a.next_i32(), b.next_i32(), "same number of draws");
let mut data = GlibcRand::new(seed + 1000);
for step in 0..500 {
let input: [V; IN] = core::array::from_fn(|_| raw(data.next_i32()));
let target: [V; OUT] = core::array::from_fn(|_| raw(data.next_i32()));
mlp.feed_forward(&input);
net.feed_forward(&input);
assert_eq!(net.learned_values(), mlp.learned_values(), "step {step}");
assert_eq!(net.calculate_error(&target), mlp.calculate_error(&target));
assert_eq!(net.output_pre_activations(), mlp.output_pre_activations());
assert_eq!(net.predicted_class(), mlp.predicted_class());
mlp.train(&target);
net.train(&target);
let (l0, l1) = net.layers();
assert_eq!(l0.weights(), mlp.input_weights(), "step {step}");
assert_eq!(l0.bias_weights(), mlp.input_bias_weights());
assert_eq!(l1.weights(), mlp.hidden_weights());
assert_eq!(l1.bias_weights(), mlp.hidden_bias_weights());
}
}
fn unit(r: i32, one: i32) -> i32 {
r % (2 * one + 1) - one
}
#[test]
fn two_layers_match_multilayer_perceptron() {
let q16 = |r: i32| Q16_16::from_raw(unit(r, 1 << 16));
let q8 = |r: i32| Q8_8::from_raw(unit(r, 1 << 8) as i16);
same_as_perceptron::<Q16_16, Tanh, Sigmoid, 2, 4, 1>(q16, 1);
same_as_perceptron::<Q16_16, Relu, Sigmoid, 4, 8, 3>(q16, 2);
same_as_perceptron::<Q8_8, Tanh, Sigmoid, 2, 4, 1>(q8, 3);
same_as_perceptron::<Q8_8, Relu, Sigmoid, 4, 8, 3>(q8, 4);
}
#[test]
fn two_layers_match_perceptron_with_clipping() {
let mut a = GlibcRand::new(11);
let mut b = GlibcRand::new(11);
let w = |r: i32| Q16_16::from_raw(unit(r, 1 << 15));
let mut mlp =
MultilayerPerceptron::<Q16_16, Relu, Linear, 3, 5, 2>::new(|| w(a.next_i32()));
let mut net =
NeuralNetwork::<(Dense<Q16_16, Relu, 3, 5>, Dense<Q16_16, Linear, 5, 2>)>::new(|| {
w(b.next_i32())
});
let range = (Q16_16::from_raw(-(1 << 15)), Q16_16::ONE);
let lr = Q16_16::from_raw(1 << 10);
mlp.set_learning_rate(lr);
net.set_learning_rate(lr);
mlp.set_acceleration_rate(Q16_16::from_raw(1 << 12));
net.set_acceleration_rate(Q16_16::from_raw(1 << 12));
mlp.set_gradient_clip(Some(range));
net.set_gradient_clip(Some(range));
let mut data = GlibcRand::new(12);
for step in 0..1000 {
let input: [Q16_16; 3] =
core::array::from_fn(|_| Q16_16::from_raw(unit(data.next_i32(), 1 << 16)));
let target: [Q16_16; 2] =
core::array::from_fn(|_| Q16_16::from_raw(unit(data.next_i32(), 8 << 16)));
mlp.feed_forward(&input);
net.feed_forward(&input);
assert_eq!(net.learned_values(), mlp.learned_values(), "step {step}");
mlp.train(&target);
net.train(&target);
assert_eq!(net.layers().0.weights(), mlp.input_weights(), "step {step}");
assert_eq!(
net.layers().1.bias_weights(),
mlp.hidden_bias_weights(),
"step {step}"
);
}
}
fn pre_activations_reproduce_outputs<V, O, L, const N: usize>(
mut net: NeuralNetwork<L>,
mut input: impl FnMut() -> L::Inputs,
) where
V: Scalar + PartialEq + core::fmt::Debug,
O: Activation<V>,
L: Layers<Value = V, Outputs = [V; N]>,
{
for sample in 0..200 {
net.feed_forward(&input());
let pre = net.output_pre_activations();
assert_eq!(
pre.map(O::activate),
net.learned_values(),
"sample {sample}"
);
assert_eq!(net.predicted_class(), argmax(&pre), "sample {sample}");
}
}
#[test]
fn pre_activations_reproduce_outputs_for_every_depth() {
let wide = |r: i32| Q16_16::from_raw(r.wrapping_mul(2_654_435_761u32 as i32));
let q8 = |r: i32| Q8_8::from_raw(unit(r, 4 << 8) as i16);
let mut w = GlibcRand::new(21);
let mut x = GlibcRand::new(22);
let one = NeuralNetwork::<(Dense<Q16_16, Sigmoid, 3, 4>,)>::new(|| wide(w.next_i32()));
pre_activations_reproduce_outputs::<_, Sigmoid, _, 4>(one, || {
core::array::from_fn(|_| wide(x.next_i32()))
});
let three = NeuralNetwork::<(
Dense<Q8_8, Relu, 2, 5>,
Dense<Q8_8, Tanh, 5, 4>,
Dense<Q8_8, Sigmoid, 4, 3>,
)>::new(|| q8(w.next_i32()));
pre_activations_reproduce_outputs::<_, Sigmoid, _, 3>(three, || {
core::array::from_fn(|_| q8(x.next_i32()))
});
let linear =
NeuralNetwork::<(Dense<Q16_16, Relu, 2, 6>, Dense<Q16_16, Linear, 6, 2>)>::new(|| {
wide(w.next_i32())
});
pre_activations_reproduce_outputs::<_, Linear, _, 2>(linear, || {
core::array::from_fn(|_| wide(x.next_i32()))
});
}
#[test]
fn linear_and_clip() {
use crate::activation::Linear;
let x = Q8_8::from_raw(-300);
assert_eq!(<Linear as Activation<Q8_8>>::activate(x), x);
assert_eq!(<Linear as Activation<Q8_8>>::derivative(x), Q8_8::ONE);
let range = (Q16_16::from_int(-1), Q16_16::ONE);
let clip = |g: i32| crate::neuralnet::clip_gradient(Q16_16::from_int(g), range);
assert_eq!(clip(5), Q16_16::ONE);
assert_eq!(clip(-5), Q16_16::from_int(-1));
assert_eq!(clip(0), Q16_16::ZERO);
let inverted = (Q16_16::ONE, Q16_16::ZERO);
assert_eq!(
crate::neuralnet::clip_gradient(Q16_16::from_int(3), inverted),
Q16_16::ZERO
);
}
#[test]
fn weights_round_trip() {
let mut net =
NeuralNetwork::<(Dense<Q16_16, Relu, 2, 3>, Dense<Q16_16, Sigmoid, 3, 1>)>::new(|| {
Q16_16::ZERO
});
let w = [[Q16_16::ONE; 3], [Q16_16::from_int(-2); 3]];
net.layers_mut().0.set_weights(&w);
net.layers_mut().1.set_bias_weights(&[Q16_16::from_int(3)]);
assert_eq!(net.layers().0.weights(), w);
assert_eq!(net.layers().1.bias_weights(), [Q16_16::from_int(3)]);
}
}