oa/ml/nn/
sequential.gen.rs1use std::rc::Rc;
6
7use crate::{Matrix, Result};
8
9use super::super::{Module, ModuleRegistry};
10
11pub struct Sequential {
15 registry: ModuleRegistry,
16}
17
18impl Sequential {
19 pub const fn new() -> Self {
21 Self {
22 registry: ModuleRegistry::new(),
23 }
24 }
25
26 pub fn add(&mut self, module: Rc<dyn Module>) -> Result<&mut Self> {
32 let name = format!("layer_{}", self.len());
33 self.add_named(name, module)
34 }
35
36 pub fn add_named(
42 &mut self,
43 name: impl Into<String>,
44 module: Rc<dyn Module>,
45 ) -> Result<&mut Self> {
46 self.registry.register_module(name, module)?;
47 Ok(self)
48 }
49
50 pub fn len(&self) -> usize {
52 self.registry.child_modules().len()
53 }
54
55 pub fn is_empty(&self) -> bool {
57 self.len() == 0
58 }
59
60 pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
66 let mut output = input.clone();
67 for child in self.registry.child_modules() {
68 output = child.forward(&output)?;
69 }
70 Ok(output)
71 }
72}
73
74impl Default for Sequential {
75 fn default() -> Self {
76 Self::new()
77 }
78}
79
80impl Module for Sequential {
81 fn forward(&self, input: &Matrix) -> Result<Matrix> {
82 Sequential::forward(self, input)
83 }
84
85 fn registry(&self) -> &ModuleRegistry {
86 &self.registry
87 }
88}