Skip to content

Commit

Permalink
Merge pull request #19 from BradenEverson/graph
Browse files Browse the repository at this point in the history
Renamed network to sequential
  • Loading branch information
BradenEverson authored Feb 19, 2024
2 parents 96d9d0a + b84f205 commit 5b5ba76
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
4 changes: 2 additions & 2 deletions examples/breast_cancer/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::error::Error;
use unda::core::{data::input::Input, network::Network, layer::{layers::{LayerTypes, InputTypes}, methods::{activations::Activations, errors::ErrorTypes}}};
use unda::core::{data::input::Input, network::Sequential, layer::{layers::{LayerTypes, InputTypes}, methods::{activations::Activations, errors::ErrorTypes}}};


use rand::seq::SliceRandom;
Expand All @@ -20,7 +20,7 @@ fn main() -> Result<(), Box<dyn Error>>{
inputs.push(&model.0);
});

let mut network = Network::new(128);
let mut network = Sequential::new(128);
network.set_input(InputTypes::DENSE(inputs[0].to_param().len()));
network.add_layer(LayerTypes::DENSE(16, Activations::RELU, 0.001));
network.add_layer(LayerTypes::DENSE(1, Activations::SIGMOID, 0.001));
Expand Down
4 changes: 2 additions & 2 deletions examples/mnist.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use unda::{core::{data::{input::Input, matrix::Matrix}, network::Network, layer::{layers::{LayerTypes, InputTypes}, methods::{activations::Activations, errors::ErrorTypes}}}, util::{mnist::MnistEntry, categorical::to_categorical}};
use unda::{core::{data::{input::Input, matrix::Matrix}, network::Sequential, layer::{layers::{LayerTypes, InputTypes}, methods::{activations::Activations, errors::ErrorTypes}}}, util::{mnist::MnistEntry, categorical::to_categorical}};

fn main() {
let mut inputs: Vec<&dyn Input> = vec![];
Expand All @@ -17,7 +17,7 @@ fn main() {
inputs.push(&inputs_undyn[i]);
true_outputs.push(outputs[i].clone());
}
let mut network = Network::new(128);
let mut network = Sequential::new(128);

network.set_input(InputTypes::DENSE(784));
network.add_layer(LayerTypes::DENSE(256, Activations::RELU, 0.001));
Expand Down
4 changes: 2 additions & 2 deletions examples/xor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use unda::core::{data::{input::Input, matrix::Matrix, matrix3d::Matrix3D}, network::Network, layer::{layers::{LayerTypes, InputTypes}, methods::{activations::Activations, errors::ErrorTypes}}};
use unda::core::{data::{input::Input, matrix::Matrix, matrix3d::Matrix3D}, network::Sequential, layer::{layers::{LayerTypes, InputTypes}, methods::{activations::Activations, errors::ErrorTypes}}};

fn main() {
let mut inputs: Vec<&dyn Input> = vec![];
Expand All @@ -15,7 +15,7 @@ fn main() {

loop{

let mut new_net = Network::new(4);
let mut new_net = Sequential::new(4);
new_net.set_log(false);

new_net.set_input(InputTypes::DENSE(2));
Expand Down
4 changes: 2 additions & 2 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ pub mod serialize;

#[cfg(test)]
mod test {
use crate::core::{network::Network, layer::layers::InputTypes};
use crate::core::{network::Sequential, layer::layers::InputTypes};

#[test]
fn check_set_input() {
let mut net: Network = Network::new(10);
let mut net = Sequential::new(10);
net.set_input(InputTypes::DENSE(10));
net.set_input(InputTypes::DENSE(20));
assert_eq!(net.layer_sizes[0], 20);
Expand Down
36 changes: 18 additions & 18 deletions src/core/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::{
};

#[derive(Serialize, Deserialize)]
pub struct Network {
pub struct Sequential {
batch_size: usize,
pub layer_sizes: Vec<usize>,
pub loss: f32,
Expand All @@ -29,14 +29,14 @@ pub struct Network {
uncompiled_layers: Vec<LayerTypes>,
seed: Option<String>,
#[serde(skip)]
#[serde(default = "Network::thread_rng")]
#[serde(default = "Sequential::thread_rng")]
rng: Box<dyn RngCore>,
log: bool
}

const ITERATIONS_PER_EPOCH: usize = 1000;

impl Network{
impl Sequential {
fn thread_rng() -> Box<dyn RngCore> {
Box::new(thread_rng())
}
Expand All @@ -47,11 +47,11 @@ impl Network{
///
///Example:
///```
///use unda::core::network::Network;
///let mut new_net = Network::new(10);
///use unda::core::network::Sequential;
///let mut new_net = Sequential::new(10);
///```
pub fn new(batch_size: usize) -> Network{
Network{
pub fn new(batch_size: usize) -> Self {
Self {
batch_size,
layer_sizes: vec![],
loss: 1.0,
Expand Down Expand Up @@ -82,11 +82,11 @@ impl Network{
///# Example
///
///```
///use unda::core::network::Network;
///use unda::core::network::Sequential;
///use unda::core::layer::layers::LayerTypes;
///use unda::core::layer::methods::activations::Activations;
///
///let mut new_net = Network::new(2);
///let mut new_net = Sequential::new(2);
///new_net.add_layer(LayerTypes::DENSE(4, Activations::SIGMOID, 0.01));
///```
///Adds a new Dense layer of 4 nodes with the sigmoid activation and a learning rate of 0.01
Expand Down Expand Up @@ -180,11 +180,11 @@ impl Network{
///# Examples
///
///```
///use unda::core::network::Network;
///use unda::core::network::Sequential;
///use unda::core::layer::layers::LayerTypes;
///use unda::core::layer::methods::activations::Activations;
///let mut new_net = Network::new(4);
///let mut new_net = Sequential::new(4);
///new_net.add_layer(LayerTypes::DENSE(2, Activations::SIGMOID, 0.01));
///new_net.add_layer(LayerTypes::DENSE(3, Activations::SIGMOID, 0.01));
///new_net.add_layer(LayerTypes::DENSE(4, Activations::SIGMOID, 0.01));
Expand Down Expand Up @@ -406,26 +406,26 @@ impl Network{
let res_file = File::create(path).expect("Unable to save file");
serde_cbor::to_writer(res_file, self).expect("Unable to write or compile cbor");
}
pub fn load(path: &str) -> Network{
pub fn load(path: &str) -> Self{
let mut buffer = String::new();
let mut file = File::open(path).expect("Unable to read file :(");

file.read_to_string(&mut buffer).expect("Unable to read file but even sadder :(");

let mut net: Network = from_str(&buffer).expect("Json was not formatted well >:(");
let mut net: Self = from_str(&buffer).expect("Json was not formatted well >:(");
net.rng = net.get_rng();
net
}
pub fn load_cbor(path: &str) -> Result<Network, serde_cbor::Error> {
pub fn load_cbor(path: &str) -> Result<Self, serde_cbor::Error> {
let file = File::open(path).expect("error loading file");
let mut network: Network = serde_cbor::from_reader(file)?;
let mut network: Self = serde_cbor::from_reader(file)?;
network.rng = network.get_rng();
Ok(network)
}
pub fn to_vec(&self) -> Result<Vec<u8>, serde_cbor::Error> {
serde_cbor::to_vec(self)
}
pub fn from_vec(data: Vec<u8>) -> Result<Network, serde_cbor::Error> {
pub fn from_vec(data: Vec<u8>) -> Result<Self, serde_cbor::Error> {
serde_cbor::from_slice(&data[..])
}

Expand All @@ -437,8 +437,8 @@ impl Network{
}
fs::write(path, str_fmt.join("#")).expect("Error writing to file");
}
pub fn deserialize_unda_fmt_string(format_string: String) -> Network {
let mut net: Network = Network::new(0);
pub fn deserialize_unda_fmt_string(format_string: String) -> Self {
let mut net: Self = Self::new(0);
let parse_triton = format_string.split("#");
for layer in parse_triton {
let new_layer: Box<dyn Layer> = SerializedLayer::from_string(layer.to_string()).from();
Expand Down

0 comments on commit 5b5ba76

Please sign in to comment.