diff --git a/Cargo.lock b/Cargo.lock index 9b45c716..47481246 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -250,7 +250,7 @@ dependencies = [ [[package]] name = "concision" -version = "0.3.1" +version = "0.3.2" dependencies = [ "anyhow", "approx", @@ -266,7 +266,7 @@ dependencies = [ [[package]] name = "concision-core" -version = "0.3.1" +version = "0.3.2" dependencies = [ "anyhow", "approx", @@ -296,7 +296,7 @@ dependencies = [ [[package]] name = "concision-data" -version = "0.3.1" +version = "0.3.2" dependencies = [ "anyhow", "approx", @@ -318,7 +318,7 @@ dependencies = [ [[package]] name = "concision-derive" -version = "0.3.1" +version = "0.3.2" dependencies = [ "proc-macro2", "quote", @@ -327,7 +327,7 @@ dependencies = [ [[package]] name = "concision-ext" -version = "0.3.1" +version = "0.3.2" dependencies = [ "anyhow", "approx", @@ -350,7 +350,7 @@ dependencies = [ [[package]] name = "concision-init" -version = "0.3.1" +version = "0.3.2" dependencies = [ "anyhow", "approx", @@ -373,7 +373,7 @@ dependencies = [ [[package]] name = "concision-macros" -version = "0.3.1" +version = "0.3.2" dependencies = [ "proc-macro2", "quote", @@ -382,7 +382,7 @@ dependencies = [ [[package]] name = "concision-params" -version = "0.3.1" +version = "0.3.2" dependencies = [ "anyhow", "approx", @@ -403,7 +403,7 @@ dependencies = [ [[package]] name = "concision-traits" -version = "0.3.1" +version = "0.3.2" dependencies = [ "approx", "hashbrown 0.16.1", @@ -412,7 +412,7 @@ dependencies = [ "num-integer", "num-traits", "paste", - "rand_core 0.9.5", + "rand 0.9.2", "rspace-traits", "serde", "serde_derive", diff --git a/Cargo.toml b/Cargo.toml index 844e9f6c..baac7082 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,21 +24,21 @@ license = "Apache-2.0" readme = "README.md" repository = "https://github.com/FL03/concision.git" rust-version = "1.85.0" -version = "0.3.1" +version = "0.3.2" [workspace.dependencies] -concision = { default-features = false, path = "concision", version = "0.3.1" } +concision = { default-features = false, path = "concision", version = "0.3.2" } # sdk -concision-core = { default-features = false, path = "core", version = "0.3.1" } -concision-data = { default-features = false, path = "data", version = "0.3.1" } -concision-derive = { default-features = false, path = "derive", version = "0.3.1" } -concision-init = { default-features = false, path = "init", version = "0.3.1" } -concision-macros = { default-features = false, path = "macros", version = "0.3.1" } -concision-neural = { default-features = false, path = "neural", version = "0.3.1" } -concision-params = { default-features = false, path = "params", version = "0.3.1" } -concision-traits = { default-features = false, path = "traits", version = "0.3.1" } +concision-core = { default-features = false, path = "core", version = "0.3.2" } +concision-data = { default-features = false, path = "data", version = "0.3.2" } +concision-derive = { default-features = false, path = "derive", version = "0.3.2" } +concision-init = { default-features = false, path = "init", version = "0.3.2" } +concision-macros = { default-features = false, path = "macros", version = "0.3.2" } +concision-neural = { default-features = false, path = "neural", version = "0.3.2" } +concision-params = { default-features = false, path = "params", version = "0.3.2" } +concision-traits = { default-features = false, path = "traits", version = "0.3.2" } # custom models & extras -concision-ext = { default-features = false, path = "ext", version = "0.3.1" } +concision-ext = { default-features = false, path = "ext", version = "0.3.2" } # custom contained = { default-features = false, features = ["derive"], version = "0.2.2" } rspace = { default-features = false, features = ["macros"], version = "0.0.8" } diff --git a/default.nix b/default.nix index b7e02189..22ba5a1d 100644 --- a/default.nix +++ b/default.nix @@ -26,7 +26,7 @@ let }; common = { - version = "0.3.1"; + version = "0.3.2"; src = self; # ./.; cargoLock = { diff --git a/flake.nix b/flake.nix index 94c1ca8e..e412326f 100644 --- a/flake.nix +++ b/flake.nix @@ -15,7 +15,7 @@ { packages.default = rustPlatform.buildRustPackage { pname = "concision"; - version = "0.3.1"; + version = "0.3.2"; src = self; # If Cargo.lock doesn't exist yet, remove or comment out this block: cargoLock = { diff --git a/traits/Cargo.toml b/traits/Cargo.toml index 160d7fca..c16ddfdf 100644 --- a/traits/Cargo.toml +++ b/traits/Cargo.toml @@ -44,7 +44,7 @@ num-complex = { optional = true, workspace = true } num-integer = { workspace = true } num-traits = { workspace = true } # random -rand_core = { optional = true, workspace = true } +rand = { optional = true, workspace = true } # serialization serde = { optional = true, workspace = true } serde_derive = { optional = true, workspace = true } @@ -69,6 +69,7 @@ autodiff = [ nightly = [ "hashbrown?/nightly", + "rand?/nightly", ] # ********* [FF] Environments ********* @@ -79,7 +80,7 @@ std = [ "num-complex?/std", "num-integer/std", "num-traits/std", - "rand_core?/std", + "rand?/std", "rspace-traits/std", "serde?/std", ] @@ -91,6 +92,7 @@ wasm = [] # ********* [FF] Dependencies ********* alloc = [ "hashbrown?/alloc", + "rand?/alloc", "rspace-traits/alloc", "serde?/alloc", ] @@ -119,7 +121,7 @@ rayon = [ ] rand = [ - "dep:rand_core", + "dep:rand", "num-complex?/rand", "rspace-traits/rand", ] @@ -129,6 +131,9 @@ serde = [ "dep:serde_derive", "serde?/derive", "hashbrown?/serde", + "ndarray/serde", + "num-complex?/serde", + "rand?/serde", "rspace-traits/serde", ] diff --git a/traits/src/init.rs b/traits/src/init.rs index 6489b9e4..b7d1cda4 100644 --- a/traits/src/init.rs +++ b/traits/src/init.rs @@ -18,7 +18,7 @@ pub trait InitWith { /// The [`InitRand`] trait provides a generic interface for initializing objects using /// random number generators. This trait is particularly useful for types that require /// random initialization, such as neural network weights, biases, or other parameters. -pub trait InitRand { +pub trait InitRand { type Output; /// use the provided random number generator `rng` to initialize the object fn init_random(rng: &mut R) -> Self::Output; diff --git a/traits/src/lib.rs b/traits/src/lib.rs index cfe252ec..134813a8 100644 --- a/traits/src/lib.rs +++ b/traits/src/lib.rs @@ -43,6 +43,7 @@ mod norm; mod predict; mod propagate; mod rounding; +mod shuffle; mod store; mod training; @@ -85,7 +86,7 @@ pub mod tensor { #[doc(inline)] pub use self::{ clip::*, codex::*, complex::*, entropy::*, gradient::*, init::*, loss::*, math::*, norm::*, - ops::*, predict::*, propagate::*, rounding::*, store::*, tensor::*, training::*, + ops::*, predict::*, propagate::*, rounding::*, shuffle::*, store::*, tensor::*, training::*, }; // prelude #[doc(hidden)] @@ -103,6 +104,7 @@ pub mod prelude { pub use crate::predict::*; pub use crate::propagate::*; pub use crate::rounding::*; + pub use crate::shuffle::*; pub use crate::store::*; pub use crate::tensor::*; pub use crate::training::*; diff --git a/traits/src/shuffle.rs b/traits/src/shuffle.rs new file mode 100644 index 00000000..227052ba --- /dev/null +++ b/traits/src/shuffle.rs @@ -0,0 +1,69 @@ +/* + Appellation: shuffle + Contrib: @FL03 +*/ +#![cfg(feature = "rand")] + +use rand::RngCore; + +/// The [`Shuffle`] trait establishes an interface for randomly shuffling collections of +/// elements using a provided random number generator. +pub trait Shuffle { + fn shuffle(&mut self, rng: &mut R); +} + +/* + ************* Implementations ************* +*/ +use rand::Rng; + +impl Shuffle for [T] +where + R: RngCore, +{ + fn shuffle(&mut self, rng: &mut R) { + for i in (1..self.len()).rev() { + self.swap(i, rng.random_range(0..=i)); + } + } +} + +impl Shuffle for [T; N] +where + R: RngCore, +{ + fn shuffle(&mut self, rng: &mut R) { + for i in (1..self.len()).rev() { + self.swap(i, rng.random_range(0..=i)); + } + } +} + +#[cfg(feature = "alloc")] +impl Shuffle for alloc::vec::Vec +where + R: RngCore, +{ + fn shuffle(&mut self, rng: &mut R) { + for i in (1..self.len()).rev() { + self.swap(i, rng.random_range(0..=i)); + } + } +} + +// #[cfg(feature = "ndarray")] +// impl Shuffle for ndarray::ArrayBase +// where +// S: ndarray::DataMut, +// D: ndarray::Dimension, +// R: RngCore, +// { +// fn shuffle(&mut self, rng: &mut R) { +// use rand::Rng; +// let len = self.len(); +// for i in (1..len).rev() { +// let j = rng.random_range(0..=i); +// self.swap([i], [j]); +// } +// } +// }