Class: Ignis::AI::NN::Sequential

Inherits:
Module
  • Object
show all
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

#training

Instance Method Summary collapse

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.

Parameters:

  • modules (Array<Module>)

    modules to chain



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.

Parameters:

  • idx (Integer)

Returns:



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.

Parameters:

  • x (Tensor)

Returns:

  • (Tensor)


23
24
25
# File 'lib/nnw/ai/nn/sequential.rb', line 23

def forward(x)
  @layers.reduce(x) { |h, m| m.call(h) }
end

#lengthInteger

Number of layers.

Returns:

  • (Integer)


29
30
31
# File 'lib/nnw/ai/nn/sequential.rb', line 29

def length
  @layers.length
end

#to_sString

Returns:

  • (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