From d35eea92dc0dc9c21c6effa44359c898715dd4e5 Mon Sep 17 00:00:00 2001 From: Devarth123 Date: Sun, 7 Aug 2022 16:21:03 +0530 Subject: [PATCH] better code --- src/ml/f32/matrix.rs | 13 +++---------- src/ml/f32/nn.rs | 11 ++--------- src/ml/f32/save.rs | 10 ++++++---- src/ml/f64/matrix.rs | 13 ++----------- src/ml/f64/nn.rs | 9 +-------- src/ml/f64/save.rs | 10 ++++++---- 6 files changed, 20 insertions(+), 46 deletions(-) diff --git a/src/ml/f32/matrix.rs b/src/ml/f32/matrix.rs index d74ee2c..d3ec9f0 100644 --- a/src/ml/f32/matrix.rs +++ b/src/ml/f32/matrix.rs @@ -1,4 +1,6 @@ +#[derive(Clone, Debug)] + pub struct MatrixStruct { pub rows: usize, pub columns: usize, @@ -100,18 +102,9 @@ impl MatrixStruct { matrix } - pub fn clone(m: &MatrixStruct) -> MatrixStruct { - let mut cp_matrix = MatrixStruct::from(&m.rows, &m.columns); - for i in 0..m.rows { - for j in 0..m.columns { - cp_matrix.matrix[i][j] = m.matrix[i][j]; - } - } - cp_matrix - } //sometimes the rust is not in the mood pub fn scale(num: &f32, m1: &MatrixStruct) -> MatrixStruct { - let mut m = MatrixStruct::clone(m1); + let mut m = m1.clone(); for i in 0..m.rows { for j in 0..m.columns { m.matrix[i][j] *= num; diff --git a/src/ml/f32/nn.rs b/src/ml/f32/nn.rs index 394209a..479ba1a 100644 --- a/src/ml/f32/nn.rs +++ b/src/ml/f32/nn.rs @@ -5,6 +5,8 @@ use crate::ml::f32::img::Img; use crate::ml::f32::matrix::MatrixStruct; // use crate::ml::f32::img::Img; +#[derive(Debug, Clone)] + pub struct NeuralNetwork { pub parameters: Box<[usize]>, //the first index will be the input layer (parameters[0]) , the last index will be the output layer (parameters[parameters.len()-1]) and lastly the rest of the vaues will be the hidden_layer (parameters[1..parameters.len()-2]) pub learning_rate: f32, @@ -14,15 +16,6 @@ pub struct NeuralNetwork { impl NeuralNetwork { - pub fn clone(nn: &NeuralNetwork) -> NeuralNetwork{ - NeuralNetwork{ - parameters: Box::clone(&nn.parameters), - learning_rate: nn.learning_rate.clone(), - hidden_weights: MatrixStruct::clone(&nn.hidden_weights), - output_weights: MatrixStruct::clone(&nn.output_weights) - } - } - pub fn from(parameters_: &Box<[usize]>, learning_rate_: &f32) -> NeuralNetwork { NeuralNetwork { parameters: Box::clone(parameters_), diff --git a/src/ml/f32/save.rs b/src/ml/f32/save.rs index fb67053..04a16cc 100644 --- a/src/ml/f32/save.rs +++ b/src/ml/f32/save.rs @@ -5,6 +5,7 @@ use crate::ml::f32::matrix::MatrixStruct; use std::process::Command; #[allow(dead_code)] +#[derive(Clone, Debug)] struct Save { path: String, nn: NeuralNetwork, @@ -21,14 +22,15 @@ impl Save { panic!("where is the path big brother?, didnt ask for a null string :< "); } }, - nn: NeuralNetwork::clone(&nn_), + nn: nn_.clone(), } } #[allow(dead_code)] pub fn mkdirs(&self) { - let cmd = Command::new("sh") + let cmd = Command::new("/bin/sh") + .arg("-c") .arg(format!( - "-c mkdir -v -p {} {} {}", + "mkdir -v -p {} {} {}", (self.path.clone() + &"/NeuralNetwork/output"), (self.path.clone() + &"/NeuralNetwork/hidden"), (self.path.clone() + &"/NeuralNetwork/input") @@ -40,7 +42,7 @@ impl Save { #[allow(dead_code)] pub fn touch_files(&self){ - let mut cmd = Command::new("touch"); + let mut cmd = Command::new("/usr/bin/touch"); let len = self.nn.parameters.len(); for i in 0..len{ match i{ diff --git a/src/ml/f64/matrix.rs b/src/ml/f64/matrix.rs index d94a26d..1697938 100644 --- a/src/ml/f64/matrix.rs +++ b/src/ml/f64/matrix.rs @@ -1,5 +1,5 @@ -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct MatrixStruct { pub rows: usize, pub columns: usize, @@ -106,18 +106,9 @@ impl MatrixStruct { } matrix } - pub fn clone(m: &MatrixStruct) -> MatrixStruct { - let mut cp_matrix = MatrixStruct::from(&m.rows, &m.columns); - for i in 0..m.rows { - for j in 0..m.columns { - cp_matrix.matrix[i][j] = m.matrix[i][j]; - } - } - cp_matrix - } //sometimes the rust is not in the mood pub fn scale(num: &f64, m1: &MatrixStruct) -> MatrixStruct { - let mut m = MatrixStruct::clone(m1); + let mut m = m1.clone(); for i in 0..m.rows { for j in 0..m.columns { m.matrix[i][j] *= num; diff --git a/src/ml/f64/nn.rs b/src/ml/f64/nn.rs index cd1c0f4..2a72ef3 100644 --- a/src/ml/f64/nn.rs +++ b/src/ml/f64/nn.rs @@ -3,6 +3,7 @@ use crate::ml::f64::activation; use crate::ml::f64::img::Img; use crate::ml::f64::matrix::MatrixStruct; +#[derive(Clone, Debug)] pub struct NeuralNetwork { pub parameters: Box<[usize]>, //the first index will be the input layer (parameters[0]) , the last index will be the output layer (parameters[parameters.len()-1]) and lastly the rest of the vaues will be the hidden_layer (parameters[1..parameters.len()-2]) pub learning_rate: f64, @@ -12,14 +13,6 @@ pub struct NeuralNetwork { impl NeuralNetwork { - pub fn clone(nn: &NeuralNetwork) -> NeuralNetwork{ - NeuralNetwork{ - parameters: Box::clone(&nn.parameters), - learning_rate: nn.learning_rate.clone(), - hidden_weights: MatrixStruct::clone(&nn.hidden_weights), - output_weights: MatrixStruct::clone(&nn.output_weights) - } - } pub fn from(parameters_: &Box<[usize]>, learning_rate_: &f64) -> NeuralNetwork { NeuralNetwork { diff --git a/src/ml/f64/save.rs b/src/ml/f64/save.rs index b50f208..e9d3221 100644 --- a/src/ml/f64/save.rs +++ b/src/ml/f64/save.rs @@ -4,6 +4,7 @@ use std::io::prelude::*; use crate::ml::f64::matrix::MatrixStruct; use std::process::Command; +#[derive(Clone, Debug)] #[allow(dead_code)] struct Save { path: String, @@ -21,14 +22,15 @@ impl Save { panic!("where is the path big brother?, didnt ask for a null string :< "); } }, - nn: NeuralNetwork::clone(&nn_), + nn: nn_.clone(), } } #[allow(dead_code)] pub fn mkdirs(&self) { - let cmd = Command::new("sh") + let cmd = Command::new("/bin/sh") + .arg("-c") .arg(format!( - "-c mkdir -v -p {} {} {}", + "mkdir -v -p {} {} {}", (self.path.clone() + &"/NeuralNetwork/output"), (self.path.clone() + &"/NeuralNetwork/hidden"), (self.path.clone() + &"/NeuralNetwork/input") @@ -40,7 +42,7 @@ impl Save { #[allow(dead_code)] pub fn touch_files(&self){ - let mut cmd = Command::new("touch"); + let mut cmd = Command::new("/usr/bin/touch"); let len = self.nn.parameters.len(); for i in 0..len{ match i{