Softmax

Struct Softmax 

Source
pub struct Softmax { /* private fields */ }
Expand description

Softmax activation function

Implementations§

Source§

impl Softmax

Source

pub fn new(axis: isize) -> Self

Examples found in repository?
examples/test_softmax.rs (line 10)
5fn main() {
6    println!("Testing softmax implementation...\n");
7    // Test case 1: Simple 1D array
8    let input = arr1(&[1.0, 2.0, 3.0]);
9    println!("Input: {input:?}");
10    let softmax = Softmax::new(0);
11    let output = softmax.forward(&input.clone().into_dyn()).unwrap();
12    println!("Softmax output: {output:?}");
13    // Verify that output sums to 1
14    let sum: f64 = output.sum();
15    println!("Sum of softmax: {sum}");
16    assert!((sum - 1.0).abs() < 1e-6, "Softmax should sum to 1");
17    // Test case 2: 2D array (batch processing)
18    println!("\nTest case 2: 2D batch");
19    let input_2d = arr2(&[[1.0, 2.0, 3.0], [3.0, 2.0, 1.0], [2.0, 2.0, 2.0]]);
20    println!("Input 2D:\n{input_2d:?}");
21    // Apply softmax along axis 1 (row-wise)
22    let softmax_2d = Softmax::new(1);
23    let output_2d = softmax_2d.forward(&input_2d.clone().into_dyn()).unwrap();
24    println!("Softmax output 2D:\n{output_2d:?}");
25    // Verify each row sums to 1
26    for i in 0..output_2d.shape()[0] {
27        let row_sum: f64 = output_2d.slice(scirs2_core::ndarray::s![i, ..]).sum();
28        println!("Row {i} sum: {row_sum}");
29        assert!((row_sum - 1.0).abs() < 1e-6, "Each row should sum to 1");
30    }
31    // Test case 3: Gradient computation
32    println!("\nTest case 3: Gradient computation");
33    let grad_output = arr1(&[0.1, 0.2, 0.3]).into_dyn();
34    let forward_output = softmax.forward(&input.clone().into_dyn()).unwrap();
35    let grad_input = softmax.backward(&grad_output, &forward_output).unwrap();
36    println!("Gradient input: {grad_input:?}");
37    println!("\nAll tests passed!");
38}

Trait Implementations§

Source§

impl<F: Float + Debug> Activation<F> for Softmax

Source§

fn forward(&self, input: &Array<F, IxDyn>) -> Result<Array<F, IxDyn>>

Forward pass of the activation function
Source§

fn backward( &self, grad_output: &Array<F, IxDyn>, input: &Array<F, IxDyn>, ) -> Result<Array<F, IxDyn>>

Backward pass of the activation function
Source§

impl Clone for Softmax

Source§

fn clone(&self) -> Softmax

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Softmax

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Softmax

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Copy for Softmax

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V