use burn_core as burn;
use burn::config::Config;
use burn::module::Module;
use burn::module::{Content, DisplaySettings, ModuleDisplay};
use burn::tensor::Tensor;
use burn::tensor::activation::celu;
#[derive(Module, Debug)]
#[module(custom_display)]
pub struct Celu {
pub alpha: f64,
}
#[derive(Config, Debug)]
pub struct CeluConfig {
#[config(default = "1.0")]
pub alpha: f64,
}
impl CeluConfig {
pub fn init(&self) -> Celu {
if !self.alpha.is_finite() || self.alpha == 0.0 {
panic!(
"CELU alpha must be finite and non-zero, but got {}",
self.alpha
);
}
Celu { alpha: self.alpha }
}
}
impl ModuleDisplay for Celu {
fn custom_settings(&self) -> Option<DisplaySettings> {
DisplaySettings::new()
.with_new_line_after_attribute(false)
.optional()
}
fn custom_content(&self, content: Content) -> Option<Content> {
content.add("alpha", &self.alpha).optional()
}
}
impl Celu {
pub fn forward<const D: usize>(&self, input: Tensor<D>) -> Tensor<D> {
celu(input, self.alpha)
}
}
#[cfg(test)]
mod tests {
use super::*;
use burn::tensor::TensorData;
use burn::tensor::Tolerance;
type FT = f32;
#[test]
fn test_celu_forward() {
let device = Default::default();
let model = CeluConfig::new().init();
let input = Tensor::<2>::from_data(TensorData::from([[0.5, -0.5, -1.0]]), &device);
let out = model.forward(input);
let expected = TensorData::from([[0.5, -0.393469, -0.632121]]);
out.to_data()
.assert_approx_eq::<FT>(&expected, Tolerance::default());
}
#[test]
fn test_celu_with_alpha() {
let device = Default::default();
let model = CeluConfig::new().with_alpha(2.0).init();
let input = Tensor::<2>::from_data(TensorData::from([[0.0, -2.0]]), &device);
let out = model.forward(input);
let expected = TensorData::from([[0.0, -1.264241]]);
out.to_data()
.assert_approx_eq::<FT>(&expected, Tolerance::default());
}
#[test]
fn display() {
let config = CeluConfig::new().init();
assert_eq!(alloc::format!("{config}"), "Celu {alpha: 1}");
}
#[test]
#[should_panic(expected = "CELU alpha must be finite and non-zero")]
fn zero_alpha_should_panic() {
CeluConfig::new().with_alpha(0.0).init();
}
#[test]
fn negative_alpha_is_valid() {
let device = Default::default();
let model = CeluConfig::new().with_alpha(-1.0).init();
let input = Tensor::<2>::from_data(TensorData::from([[-1.0, 0.0, 1.0]]), &device);
let expected = TensorData::from([[-1.7182819, 0.0, 1.0]]);
model
.forward(input)
.to_data()
.assert_approx_eq::<FT>(&expected, Tolerance::default());
}
#[test]
#[should_panic(expected = "CELU alpha must be finite and non-zero")]
fn nan_alpha_should_panic() {
CeluConfig::new().with_alpha(f64::NAN).init();
}
#[test]
#[should_panic(expected = "CELU alpha must be finite and non-zero")]
fn infinite_alpha_should_panic() {
CeluConfig::new().with_alpha(f64::INFINITY).init();
}
}