1use crate::{Matrix, Result, matrix};
6
7use super::super::{Module, ModuleRegistry};
8
9pub struct Softmax {
13 dim: i32,
14 registry: ModuleRegistry,
15}
16
17impl Softmax {
18 pub const fn new(dim: i32) -> Self {
20 Self {
21 dim,
22 registry: ModuleRegistry::new(),
23 }
24 }
25
26 pub const fn dim(&self) -> i32 {
28 self.dim
29 }
30
31 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
37 matrix::softmax(input, self.dim)
38 }
39}
40
41impl Default for Softmax {
42 fn default() -> Self {
43 Self::new(-1)
44 }
45}
46
47impl Module for Softmax {
48 fn forward(&self, input: &Matrix) -> Result<Matrix> {
49 Softmax::forward(self, input)
50 }
51
52 fn registry(&self) -> &ModuleRegistry {
53 &self.registry
54 }
55}
56
57pub struct LogSoftmax {
61 dim: i32,
62 registry: ModuleRegistry,
63}
64
65impl LogSoftmax {
66 pub const fn new(dim: i32) -> Self {
68 Self {
69 dim,
70 registry: ModuleRegistry::new(),
71 }
72 }
73
74 pub const fn dim(&self) -> i32 {
76 self.dim
77 }
78
79 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
85 matrix::log_softmax(input, self.dim)
86 }
87}
88
89impl Default for LogSoftmax {
90 fn default() -> Self {
91 Self::new(-1)
92 }
93}
94
95impl Module for LogSoftmax {
96 fn forward(&self, input: &Matrix) -> Result<Matrix> {
97 LogSoftmax::forward(self, input)
98 }
99
100 fn registry(&self) -> &ModuleRegistry {
101 &self.registry
102 }
103}