Sigmoid

Struct Sigmoid 

Source
pub struct Sigmoid;
Expand description

Sigmoid activation function

Implementations§

Source§

impl Sigmoid

Source

pub fn new() -> Self

Examples found in repository?
examples/activations_example.rs (line 14)
5fn main() -> Result<(), Box<dyn std::error::Error>> {
6    println!("Activation Functions Demonstration");
7    // Create a set of input values
8    let x_values: Vec<f64> = (-50..=50).map(|i| i as f64 / 10.0).collect();
9    let x = Array1::from(x_values.clone());
10    let x_dyn = x.clone().into_dyn();
11    // Initialize all activation functions
12    let relu = ReLU::new();
13    let leaky_relu = ReLU::leaky(0.1);
14    let sigmoid = Sigmoid::new();
15    let tanh = Tanh::new();
16    let gelu = GELU::new();
17    let gelu_fast = GELU::fast();
18    // Compute outputs for each activation function
19    let relu_output = relu.forward(&x_dyn)?;
20    let leaky_relu_output = leaky_relu.forward(&x_dyn)?;
21    let sigmoid_output = sigmoid.forward(&x_dyn)?;
22    let tanh_output = tanh.forward(&x_dyn)?;
23    let gelu_output = gelu.forward(&x_dyn)?;
24    let gelu_fast_output = gelu_fast.forward(&x_dyn)?;
25    // Note: Swish and Mish are not available in the minimal activation set
26    // Print sample values for each activation
27    println!("Sample activation values for input x = -2.0, -1.0, 0.0, 1.0, 2.0:");
28    let indices = [5, 40, 50, 60, 95]; // Corresponding to x = -2, -1, 0, 1, 2
29    println!(
30        "| {:<10} | {:<10} | {:<10} | {:<10} | {:<10} | {:<10} |",
31        "x", "-2.0", "-1.0", "0.0", "1.0", "2.0"
32    );
33    println!(
34        "|{:-<12}|{:-<12}|{:-<12}|{:-<12}|{:-<12}|{:-<12}|",
35        "", "", "", "", "", ""
36    );
37    println!(
38        "| {:<10} | {:<10.6} | {:<10.6} | {:<10.6} | {:<10.6} | {:<10.6} |",
39        "ReLU",
40        relu_output[[indices[0]]],
41        relu_output[[indices[1]]],
42        relu_output[[indices[2]]],
43        relu_output[[indices[3]]],
44        relu_output[[indices[4]]]
45    );
46    println!(
47        "| {:<10} | {:<10.6} | {:<10.6} | {:<10.6} | {:<10.6} | {:<10.6} |",
48        "LeakyReLU",
49        leaky_relu_output[[indices[0]]],
50        leaky_relu_output[[indices[1]]],
51        leaky_relu_output[[indices[2]]],
52        leaky_relu_output[[indices[3]]],
53        leaky_relu_output[[indices[4]]]
54    );
55    println!(
56        "| {:<10} | {:<10.6} | {:<10.6} | {:<10.6} | {:<10.6} | {:<10.6} |",
57        "Sigmoid",
58        sigmoid_output[[indices[0]]],
59        sigmoid_output[[indices[1]]],
60        sigmoid_output[[indices[2]]],
61        sigmoid_output[[indices[3]]],
62        sigmoid_output[[indices[4]]]
63    );
64    println!(
65        "| {:<10} | {:<10.6} | {:<10.6} | {:<10.6} | {:<10.6} | {:<10.6} |",
66        "Tanh",
67        tanh_output[[indices[0]]],
68        tanh_output[[indices[1]]],
69        tanh_output[[indices[2]]],
70        tanh_output[[indices[3]]],
71        tanh_output[[indices[4]]]
72    );
73    println!(
74        "| {:<10} | {:<10.6} | {:<10.6} | {:<10.6} | {:<10.6} | {:<10.6} |",
75        "GELU",
76        gelu_output[[indices[0]]],
77        gelu_output[[indices[1]]],
78        gelu_output[[indices[2]]],
79        gelu_output[[indices[3]]],
80        gelu_output[[indices[4]]]
81    );
82    println!(
83        "| {:<10} | {:<10.6} | {:<10.6} | {:<10.6} | {:<10.6} | {:<10.6} |",
84        "GELU Fast",
85        gelu_fast_output[[indices[0]]],
86        gelu_fast_output[[indices[1]]],
87        gelu_fast_output[[indices[2]]],
88        gelu_fast_output[[indices[3]]],
89        gelu_fast_output[[indices[4]]]
90    );
91    // Swish and Mish not available in minimal activation set
92    // Now test the backward pass with some dummy gradient output
93    println!("\nTesting backward pass...");
94    // Create a dummy gradient output
95    let dummy_grad = Array1::<f64>::ones(x.len()).into_dyn();
96    // Compute gradients for each activation function
97    let _relu_grad = relu.backward(&dummy_grad, &relu_output)?;
98    let _leaky_relu_grad = leaky_relu.backward(&dummy_grad, &leaky_relu_output)?;
99    let _sigmoid_grad = sigmoid.backward(&dummy_grad, &sigmoid_output)?;
100    let _tanh_grad = tanh.backward(&dummy_grad, &tanh_output)?;
101    let _gelu_grad = gelu.backward(&dummy_grad, &gelu_output)?;
102    let _gelu_fast_grad = gelu_fast.backward(&dummy_grad, &gelu_fast_output)?;
103    // Note: Swish and Mish gradients not available in minimal set
104    println!("Backward pass completed successfully.");
105    // Test with matrix input instead of vector
106    println!("\nTesting with matrix input...");
107    // Create a 3x4 matrix
108    let mut matrix = Array2::<f64>::zeros((3, 4));
109    for i in 0..3 {
110        for j in 0..4 {
111            matrix[[i, j]] = -2.0 + (i as f64 * 4.0 + j as f64) * 0.5;
112        }
113    }
114    // Print input matrix
115    println!("Input matrix:");
116    for i in 0..3 {
117        print!("[ ");
118        for j in 0..4 {
119            print!("{:6.2} ", matrix[[i, j]]);
120        }
121        println!("]");
122    }
123    // Apply GELU activation to the matrix
124    let gelu_matrix_output = gelu.forward(&matrix.into_dyn())?;
125    // Print output matrix
126    println!("\nAfter GELU activation:");
127    for i in 0..3 {
128        print!("[ ");
129        for j in 0..4 {
130            print!("{:6.2} ", gelu_matrix_output[[i, j]]);
131        }
132        println!("]");
133    }
134    println!("\nActivation functions demonstration completed successfully!");
135    // Note about visualization
136    println!("\nFor visualization of activation functions:");
137    println!("1. You can use external plotting libraries like plotly or matplotlib");
138    println!("2. To visualize these functions, you would plot the x_values against");
139    println!("   the output values for each activation function");
140    println!("3. The data from this example can be exported for plotting as needed");
141    // Example of how to access the data for plotting
142    println!("\nExample data points for plotting ReLU:");
143    for i in 0..5 {
144        let idx = i * 20; // Sample every 20th point
145        if idx < x_values.len() {
146            println!(
147                "x: {:.2}, y: {:.6}",
148                x_values[idx],
149                convert_to_vec(&relu_output)[idx]
150            );
151        }
152    }
153    Ok(())
154}

Trait Implementations§

Source§

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

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 Sigmoid

Source§

fn clone(&self) -> Sigmoid

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 Sigmoid

Source§

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

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

impl Default for Sigmoid

Source§

fn default() -> Self

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

impl Copy for Sigmoid

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