Class: Torch::NN::ModuleList

Inherits:
Module
  • Object
show all
Includes:
Enumerable
Defined in:
lib/torch/nn/module_list.rb

Instance Attribute Summary

Attributes inherited from Module

#training

Instance Method Summary collapse

Methods inherited from Module

#_apply, #add_module, #apply, #buffers, #call, #children, #cpu, #cuda, #deep_dup, #double, #eval, #float, #forward, #half, #inspect, #load_state_dict, #method_missing, #modules, #named_buffers, #named_children, #named_modules, #named_parameters, #parameters, #register_buffer, #register_parameter, #requires_grad!, #respond_to?, #share_memory, #state_dict, #to, #train, #type, #zero_grad

Methods included from Utils

#_activation_fn, #_clones, #_ntuple, #_pair, #_quadrupal, #_single, #_triple

Constructor Details

#initialize(mods = nil) ⇒ ModuleList

Returns a new instance of ModuleList.



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

def initialize(mods = nil)
  super()

  self.concat(mods) if mods
end

Dynamic Method Handling

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

Instance Method Details

#[](idx) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/torch/nn/module_list.rb', line 40

def [](idx)
  if idx.is_a?(Range)
    self.class.new(@modules.values[idx])
  else
    @modules[idx.to_s]
  end
end

#append(mod) ⇒ Object

Raises:

  • (ArgumentError)


34
35
36
37
38
# File 'lib/torch/nn/module_list.rb', line 34

def append(mod)
  raise ArgumentError, "Provided element is not a module" unless mod.is_a?(Module)
  add_module(length.to_s, mod)
  self
end

#concat(mods) ⇒ Object

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
# File 'lib/torch/nn/module_list.rb', line 18

def concat(mods)
  raise ArgumentError, "Modules should respond to #each" unless mods.respond_to?(:each)

  mods.each { |m| append m }

  self
end

#each(&block) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/torch/nn/module_list.rb', line 26

def each(&block)
  if block_given?
    @modules.values.each(&block)
  else
    to_enum(:each)
  end
end

#lengthObject Also known as: count, size



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

def length
  @modules.length
end