Class: Ignis::AI::NN::Sequential
- Defined in:
- lib/nnw/ai/nn/sequential.rb
Overview
Sequential container: chains modules in order. forward(x) passes x through each module sequentially.
Instance Attribute Summary
Attributes inherited from Module
Instance Method Summary collapse
-
#[](idx) ⇒ Module
Access layer by index.
-
#forward(x) ⇒ Tensor
Forward pass: reduce input through all modules.
-
#initialize(*modules) ⇒ Sequential
constructor
A new instance of Sequential.
-
#length ⇒ Integer
Number of layers.
- #to_s ⇒ String
Methods inherited from Module
#call, #eval!, #load_state_dict, #named_parameters, #num_parameters, #parameters, #state_dict, #to, #train!, #zero_grad!
Constructor Details
#initialize(*modules) ⇒ Sequential
Returns a new instance of Sequential.
12 13 14 15 16 17 18 |
# File 'lib/nnw/ai/nn/sequential.rb', line 12 def initialize(*modules) super() modules.each_with_index do |mod, i| register_module(i.to_s, mod) end @layers = modules end |
Instance Method Details
#[](idx) ⇒ Module
Access layer by index.
36 37 38 |
# File 'lib/nnw/ai/nn/sequential.rb', line 36 def [](idx) @layers[idx] end |
#forward(x) ⇒ Tensor
Forward pass: reduce input through all modules.
23 24 25 |
# File 'lib/nnw/ai/nn/sequential.rb', line 23 def forward(x) @layers.reduce(x) { |h, m| m.call(h) } end |
#length ⇒ Integer
Number of layers.
29 30 31 |
# File 'lib/nnw/ai/nn/sequential.rb', line 29 def length @layers.length end |
#to_s ⇒ String
41 42 43 44 45 46 47 48 |
# File 'lib/nnw/ai/nn/sequential.rb', line 41 def to_s lines = ["Sequential("] @layers.each_with_index do |layer, i| lines << " (#{i}): #{layer}" end lines << ")" lines.join("\n") end |