Skip to main content

coeus_nn/activation/parametric/
prelu.rs

1use crate::module::{Module, ModuleError};
2use coeus_autograd::Var;
3use coeus_core::Float;
4
5/// Functional PReLU activation with a learnable per-channel (or
6/// shared-scalar) weight — see [`coeus_autograd::prelu`] for the composition
7/// and gradient derivation.
8#[inline]
9pub fn prelu<T: Float + coeus_cpu::RealScalar, B: coeus_ops::BackendOps<T> + Default>(
10    input: &Var<T, B>,
11    weight: &Var<T, B>,
12) -> Result<Var<T, B>, B::Error>
13where
14    B::DeviceBuffer<T>:
15        coeus_core::CpuAddressableStorage<T> + coeus_core::CpuAddressableStorageMut<T>,
16{
17    coeus_autograd::prelu(input, weight)
18}
19
20/// PReLU activation module with a learnable weight (PyTorch/Burn semantics:
21/// `num_parameters = 1` for one shared slope, or the channel count for a
22/// per-channel slope broadcasting against dim 1 of the input).
23#[derive(Clone)]
24pub struct PReLU<T: Float, B: coeus_ops::BackendOps<T> + Default = coeus_core::MoiraiBackend> {
25    /// Learnable slope(s); shape `[1]` (shared) or `[num_parameters]`
26    /// (per-channel).
27    pub weight: Var<T, B>,
28}
29
30impl<T: Float + coeus_core::FloatElement, B: coeus_ops::BackendOps<T> + Default> PReLU<T, B> {
31    /// Create a PReLU module with `num_parameters` learnable slopes (`1` for
32    /// a shared scalar, or the channel count for per-channel slopes), each
33    /// initialized to `init` (PyTorch/Burn default: `0.25`).
34    pub fn new(
35        num_parameters: usize,
36        init: f64,
37    ) -> Result<Self, crate::init::InitializationError<B::Error>> {
38        let backend = B::default();
39        let weight = Var::new(
40            coeus_tensor::Tensor::full_on(
41                [num_parameters],
42                <T as coeus_core::FloatElement>::from_f64(init),
43                &backend,
44            )?,
45            true,
46        )?;
47        Ok(Self { weight })
48    }
49}
50
51impl<T: Float + coeus_cpu::RealScalar, B: coeus_ops::BackendOps<T> + Default> Module<T, B>
52    for PReLU<T, B>
53where
54    B::DeviceBuffer<T>:
55        coeus_core::CpuAddressableStorage<T> + coeus_core::CpuAddressableStorageMut<T>,
56{
57    #[inline]
58    fn parameters(&self) -> Vec<Var<T, B>> {
59        vec![self.weight.clone()]
60    }
61
62    #[inline]
63    fn load_parameters(&mut self, params: &[Var<T, B>]) {
64        self.weight = params[0].clone();
65    }
66
67    #[inline]
68    fn forward(&self, input: &Var<T, B>) -> Result<Var<T, B>, ModuleError<B::Error>> {
69        Ok(prelu(input, &self.weight)?)
70    }
71}