From cf1d37f605bbe9f7000ab0327da0602b186df27e Mon Sep 17 00:00:00 2001 From: Mitchell Shibilski-Unkel <133730558+MitchellShibilski-Unkel@users.noreply.github.com> Date: Tue, 26 Nov 2024 04:57:59 +0000 Subject: [PATCH] v1.0 --- .gitignore | 1 + Cargo.toml | 9 +++++++++ README.md | 24 ++++++++++++++++++++++++ src/example.rs | 9 +++++++++ src/rustml.rs | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 82 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 src/example.rs create mode 100644 src/rustml.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..0f9fdb4 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +cargo-features = ["edition2024"] + +[package] +name = "RustML" +version = "1.0.0" +edition = "2024" +authors = ["Mitchell Shibilski-Unkel"] + +[dependencies] \ No newline at end of file diff --git a/README.md b/README.md index 865e4f6..ad0fe69 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,26 @@ # RustML Open-source AI Library for Rust + +## About +`RustML` is an AI/machine learning library built for the `Rust` programming language. It's desgined to work similarly to `Python` libraries, such as PyTorch & Tensorflow. + +## Current Functions & Features +- Sigmod function +```rust + Sigmod(x: i32) -> i32 +``` + +- ReLU function +```rust + ReLU(x: i32) -> i32 +``` + +- Softmax function +```rust + Softmax(x: &[i32]) -> f64 +``` + +- RNN algoritm +```rust + RNN(x: &[i32], y: &[i32], weights: &[i32], bias: i32) -> Vec +``` \ No newline at end of file diff --git a/src/example.rs b/src/example.rs new file mode 100644 index 0000000..d3c44be --- /dev/null +++ b/src/example.rs @@ -0,0 +1,9 @@ +mod rustml; + +fn main() { + let RML = rustml::RustML; + let test: [i32; 4] = [1, 2, 3, 4]; + let testy: [i32; 4] = [5, 6, 7, 8]; + let testw: [i32; 4] = [55, 66, 77, 88]; + println!("{:?}", RML.RNN(&test, &testy, &testw, 1)); +} diff --git a/src/rustml.rs b/src/rustml.rs new file mode 100644 index 0000000..ca5f5f5 --- /dev/null +++ b/src/rustml.rs @@ -0,0 +1,39 @@ +pub struct RustML; +impl RustML { + fn sum(x: &[i32]) -> i32 { + x.iter().sum() + } + + fn exp(x: i32) -> i32 { + f64::exp(x as f64) as i32 + } + + pub fn Sigmod(x: i32) -> i32 { + 1 / (1 + Self::exp(-x)) + } + + pub fn Softmax(x: &[i32]) -> f64 { + // Apply exp to each element and then sum the results + let exp_x: Vec = x.iter().map(|&v| Self::exp(v)).collect(); + let sum_exp_x = Self::sum(&exp_x); + + // Return the Softmax result as a float (f64) to avoid integer division + exp_x.iter().map(|&v| v as f64 / sum_exp_x as f64).sum() + } + + pub fn ReLU(x: i32) -> i32 { + std::cmp::max(0, x) + } + + pub fn RNN(&self, x: &[i32], y: &[i32], weights: &[i32], bias: i32) -> Vec { + let mut yt: Vec = Vec::new(); + + // ht (hidden state) calculation using Sigmoid + for i in 0..x.len() { + let temp_value: i32 = (x[i] * weights[i]) + y[i] + bias; + yt.push(Self::Sigmod(temp_value)); + } + + yt + } +} \ No newline at end of file