Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/nn/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl Module for Linear {

/// Uniform over `±1/√fan_in`, the bias init shared with the conv layers.
pub(super) fn uniform_bias(out_features: usize, fan_in: usize) -> Tensor {
assert!(fan_in > 0, "uniform_bias: fan_in must be positive, got 0");
let bound = 1.0 / (fan_in as f32).sqrt();
Tensor::uniform(&[out_features], -bound, bound)
}
3 changes: 3 additions & 0 deletions src/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ pub fn with_rng<T>(f: impl FnOnce(&mut StdRng) -> T) -> T {

/// `n` samples from the uniform distribution on `[lo, hi)`.
pub fn uniform(n: usize, lo: f32, hi: f32) -> Vec<f32> {
if lo == hi {
return vec![lo; n];
}
with_rng(|rng| (0..n).map(|_| rng.random_range(lo..hi)).collect())
}

Expand Down
8 changes: 8 additions & 0 deletions src/tensor/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,20 @@ impl Tensor {

/// Kaiming (He) uniform, bounds `±√(6 / fan_in)`. For ReLU-family layers.
pub fn kaiming_uniform(shape: &[usize], fan_in: usize) -> Tensor {
assert!(
fan_in > 0,
"kaiming_uniform: fan_in must be positive, got 0 for shape {shape:?}"
);
let bound = (6.0 / fan_in as f32).sqrt();
Tensor::uniform(shape, -bound, bound)
}

/// Xavier (Glorot) uniform, for `tanh` and `sigmoid` layers.
pub fn xavier_uniform(shape: &[usize], fan_in: usize, fan_out: usize) -> Tensor {
assert!(
fan_in + fan_out > 0,
"xavier_uniform: fan_in + fan_out must be positive, got 0 for shape {shape:?}"
);
let bound = (6.0 / (fan_in + fan_out) as f32).sqrt();
Tensor::uniform(shape, -bound, bound)
}
Expand Down
24 changes: 24 additions & 0 deletions tests/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,27 @@ fn no_bias_preserves_forward_shape_and_trainable_weight() {
output.sum().backward();
assert_eq!(layer.weight.grad().unwrap().shape(), &[3, 2]);
}

#[test]
#[should_panic(expected = "fan_in must be positive")]
fn linear_zero_in_features_panics_with_clear_message() {
let _ = Linear::new(0, 10);
}

#[test]
#[should_panic(expected = "fan_in must be positive")]
fn linear_no_bias_zero_in_features_panics() {
let _ = Linear::no_bias(0, 10);
}

#[test]
#[should_panic(expected = "fan_in + fan_out must be positive")]
fn xavier_zero_fan_panics() {
let _ = Tensor::xavier_uniform(&[3, 3], 0, 0);
}

#[test]
fn rng_uniform_degenerate_range_returns_constant() {
let vals = fastnn::rng::uniform(5, 3.0, 3.0);
assert_eq!(vals, vec![3.0; 5]);
}
Loading