use crate::{Matrix, Result, matrix};
use super::super::{Module, ModuleRegistry};
pub struct Softmax {
dim: i32,
registry: ModuleRegistry,
}
impl Softmax {
pub const fn new(dim: i32) -> Self {
Self {
dim,
registry: ModuleRegistry::new(),
}
}
pub const fn dim(&self) -> i32 {
self.dim
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
matrix::softmax(input, self.dim)
}
}
impl Default for Softmax {
fn default() -> Self {
Self::new(-1)
}
}
impl Module for Softmax {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
Softmax::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct LogSoftmax {
dim: i32,
registry: ModuleRegistry,
}
impl LogSoftmax {
pub const fn new(dim: i32) -> Self {
Self {
dim,
registry: ModuleRegistry::new(),
}
}
pub const fn dim(&self) -> i32 {
self.dim
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
matrix::log_softmax(input, self.dim)
}
}
impl Default for LogSoftmax {
fn default() -> Self {
Self::new(-1)
}
}
impl Module for LogSoftmax {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
LogSoftmax::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}