Skip to content

Commit

Permalink
better code
Browse files Browse the repository at this point in the history
  • Loading branch information
Devarth123 committed Aug 7, 2022
1 parent 4fd5385 commit d35eea9
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 46 deletions.
13 changes: 3 additions & 10 deletions src/ml/f32/matrix.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@

#[derive(Clone, Debug)]

pub struct MatrixStruct {
pub rows: usize,
pub columns: usize,
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 2 additions & 9 deletions src/ml/f32/nn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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_),
Expand Down
10 changes: 6 additions & 4 deletions src/ml/f32/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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")
Expand All @@ -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{
Expand Down
13 changes: 2 additions & 11 deletions src/ml/f64/matrix.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct MatrixStruct {
pub rows: usize,
pub columns: usize,
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 1 addition & 8 deletions src/ml/f64/nn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions src/ml/f64/save.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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")
Expand All @@ -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{
Expand Down

0 comments on commit d35eea9

Please sign in to comment.