Class: Torch::NN::Sequential

Inherits:
Module
  • Object
show all
Defined in:
lib/torch/nn/sequential.rb

Instance Method Summary collapse

Methods inherited from Module

#call, #inspect, #method_missing, #respond_to?, #zero_grad

Constructor Details

#initialize(*args) ⇒ Sequential

Returns a new instance of Sequential.



4
5
6
7
8
9
10
# File 'lib/torch/nn/sequential.rb', line 4

def initialize(*args)
  @modules = {}
  # TODO support hash arg (named modules)
  args.each_with_index do |mod, idx|
    add_module(idx.to_s, mod)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Torch::NN::Module

Instance Method Details

#add_module(name, mod) ⇒ Object



12
13
14
15
# File 'lib/torch/nn/sequential.rb', line 12

def add_module(name, mod)
  # TODO add checks
  @modules[name] = mod
end

#forward(input) ⇒ Object



17
18
19
20
21
22
# File 'lib/torch/nn/sequential.rb', line 17

def forward(input)
  @modules.values.each do |mod|
    input = mod.call(input)
  end
  input
end

#parametersObject



24
25
26
# File 'lib/torch/nn/sequential.rb', line 24

def parameters
  @modules.flat_map { |_, mod| mod.parameters }
end