This commit is contained in:
lluni 2023-01-21 15:19:55 +01:00
parent d80bd3c5e5
commit 2f3745a31c
Signed by: lluni
GPG key ID: ACEEB468BC325D35
7 changed files with 105 additions and 61 deletions

View file

@ -6,16 +6,21 @@ pub enum Type {
Logistic,
Tanh,
Relu,
LeakyRelu
LeakyRelu,
}
pub fn parse_type(t: Type) -> (fn(&Array1<f64>) -> Array1<f64>, fn(&Array1<f64>) -> Array1<f64>) {
pub fn parse_type(
t: Type,
) -> (
fn(&Array1<f64>) -> Array1<f64>,
fn(&Array1<f64>) -> Array1<f64>,
) {
match t {
Type::Identity => (identity, identity_prime),
Type::Logistic => (logistic, logistic_prime),
Type::Tanh => (tanh, tanh_prime),
Type::Relu => (relu, relu_prime),
Type::LeakyRelu => (leaky_relu, leaky_relu_prime)
Type::LeakyRelu => (leaky_relu, leaky_relu_prime),
}
}
@ -78,7 +83,7 @@ pub fn relu(matrix: &Array1<f64>) -> Array1<f64> {
pub fn relu_prime(matrix: &Array1<f64>) -> Array1<f64> {
let mut result = matrix.clone();
for x in result.iter_mut() {
*x = if (*x) <= 0.0 {0.0} else {1.0};
*x = if (*x) <= 0.0 { 0.0 } else { 1.0 };
}
result
}
@ -94,7 +99,7 @@ pub fn leaky_relu(matrix: &Array1<f64>) -> Array1<f64> {
pub fn leaky_relu_prime(matrix: &Array1<f64>) -> Array1<f64> {
let mut result = matrix.clone();
for x in result.iter_mut() {
*x = if (*x) <= 0.0 {0.001} else {1.0};
*x = if (*x) <= 0.0 { 0.001 } else { 1.0 };
}
result
}

View file

@ -2,13 +2,18 @@ use ndarray::{Array1, ArrayView1};
pub enum Type {
MSE,
MAE
MAE,
}
pub fn parse_type(t: Type) -> (fn(ArrayView1<f64>, ArrayView1<f64>) -> f64, fn(ArrayView1<f64>, ArrayView1<f64>) -> Array1<f64>) {
pub fn parse_type(
t: Type,
) -> (
fn(ArrayView1<f64>, ArrayView1<f64>) -> f64,
fn(ArrayView1<f64>, ArrayView1<f64>) -> Array1<f64>,
) {
match t {
Type::MSE => (mse, mse_prime),
Type::MAE => (mae, mae_prime)
Type::MAE => (mae, mae_prime),
}
}

View file

@ -1,13 +1,13 @@
use ndarray::{Array1, arr1, ArrayView1};
use ndarray::{arr1, Array1, ArrayView1};
use crate::functions::activation_functions::*;
use super::Layer;
use crate::functions::activation_functions::*;
pub struct ActivationLayer {
input: Array1<f64>,
output: Array1<f64>,
activation: fn(&Array1<f64>) -> Array1<f64>,
activation_prime: fn(&Array1<f64>) -> Array1<f64>
activation_prime: fn(&Array1<f64>) -> Array1<f64>,
}
impl ActivationLayer {
@ -17,7 +17,7 @@ impl ActivationLayer {
input: arr1(&[]),
output: arr1(&[]),
activation,
activation_prime
activation_prime,
}
}
}
@ -36,5 +36,4 @@ impl Layer for ActivationLayer {
temp.zip_mut_with(&output_error, |x, y| *x *= y);
temp
}
}

View file

@ -1,8 +1,8 @@
extern crate ndarray;
use ndarray::{Array1, Array2, arr1, arr2, Array, ArrayView1, ShapeBuilder};
use ndarray_rand::RandomExt;
use ndarray::{arr1, arr2, Array, Array1, Array2, ArrayView1, ShapeBuilder};
use ndarray_rand::rand_distr::{Normal, Uniform};
use ndarray_rand::RandomExt;
use super::Layer;
@ -11,21 +11,25 @@ pub enum Initializer {
Ones,
Gaussian(f64, f64),
GaussianWFactor(f64, f64, f64),
Uniform(f64, f64)
Uniform(f64, f64),
}
impl Initializer {
pub fn init<Sh, D>(&self, shape: Sh) -> Array<f64, D>
where
Sh: ShapeBuilder<Dim = D>, D: ndarray::Dimension
Sh: ShapeBuilder<Dim = D>,
D: ndarray::Dimension,
{
match self {
Self::Zeros => Array::zeros(shape),
Self::Ones => Array::ones(shape),
Self::Gaussian(mean, stddev) => Array::random(shape, Normal::new(*mean, *stddev).unwrap()),
Self::GaussianWFactor(mean, stddev, factor)
=> Array::random(shape, Normal::new(*mean, *stddev).unwrap()) * *factor,
Self::Uniform(low, high) => Array::random(shape, Uniform::new(low, high))
Self::Gaussian(mean, stddev) => {
Array::random(shape, Normal::new(*mean, *stddev).unwrap())
}
Self::GaussianWFactor(mean, stddev, factor) => {
Array::random(shape, Normal::new(*mean, *stddev).unwrap()) * *factor
}
Self::Uniform(low, high) => Array::random(shape, Uniform::new(low, high)),
}
}
}
@ -42,7 +46,11 @@ pub struct FCLayer {
}
impl FCLayer {
pub fn new(num_neurons: usize, weight_initializer: Initializer, bias_initializer: Initializer) -> Self {
pub fn new(
num_neurons: usize,
weight_initializer: Initializer,
bias_initializer: Initializer,
) -> Self {
FCLayer {
num_neurons,
is_initialized: false,
@ -51,7 +59,7 @@ impl FCLayer {
input: arr1(&[]),
output: arr1(&[]),
weights: arr2(&[[]]),
biases: arr1(&[])
biases: arr1(&[]),
}
}
@ -75,11 +83,18 @@ impl Layer for FCLayer {
fn backward_pass(&mut self, output_error: ArrayView1<f64>, learning_rate: f64) -> Array1<f64> {
let input_error = output_error.dot(&self.weights.t());
let delta_weights =
self.input.to_owned().into_shape((self.input.len(), 1usize)).unwrap()
.dot(&output_error.into_shape((1usize, output_error.len())).unwrap());
let delta_weights = self
.input
.to_owned()
.into_shape((self.input.len(), 1usize))
.unwrap()
.dot(
&output_error
.into_shape((1usize, output_error.len()))
.unwrap(),
);
self.weights = &self.weights + learning_rate * &delta_weights;
self.biases = &self.biases + learning_rate * &output_error;
input_error
}
}
}

View file

@ -8,7 +8,7 @@ use ndarray::{Array1, ArrayView1};
pub struct Network {
layers: Vec<Box<dyn Layer>>,
loss: fn(ArrayView1<f64>, ArrayView1<f64>) -> f64,
loss_prime: fn(ArrayView1<f64>, ArrayView1<f64>) -> Array1<f64>
loss_prime: fn(ArrayView1<f64>, ArrayView1<f64>) -> Array1<f64>,
}
impl Network {
@ -17,7 +17,7 @@ impl Network {
Network {
layers: vec![],
loss,
loss_prime
loss_prime,
}
}
@ -41,7 +41,14 @@ impl Network {
result
}
pub fn fit(&mut self, x_train: Vec<Array1<f64>>, y_train: Vec<Array1<f64>>, epochs: usize, learning_rate: f64, trivial_optimize: bool) {
pub fn fit(
&mut self,
x_train: Vec<Array1<f64>>,
y_train: Vec<Array1<f64>>,
epochs: usize,
learning_rate: f64,
trivial_optimize: bool,
) {
assert!(x_train.len() > 0);
assert!(x_train.len() == y_train.len());
let num_samples = x_train.len();
@ -63,7 +70,7 @@ impl Network {
let mut error = (self.loss_prime)(y_train[j].view(), output.view());
for layer in self.layers.iter_mut().rev() {
if trivial_optimize {
error = layer.backward_pass(error.view(), learning_rate / (i+1) as f64);
error = layer.backward_pass(error.view(), learning_rate / (i + 1) as f64);
} else {
error = layer.backward_pass(error.view(), learning_rate);
}
@ -71,7 +78,7 @@ impl Network {
}
// calculate average error on all samples
err /= num_samples as f64;
println!("epoch {}/{} error={}", i+1, epochs, err);
println!("epoch {}/{} error={}", i + 1, epochs, err);
}
}
}