Class: DNN::Models::Chain
- Inherits:
-
Object
- Object
- DNN::Models::Chain
- Defined in:
- lib/dnn/core/models.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#call(input_tensor) ⇒ Tensor
Forward propagation and create a link.
-
#forward(input_tensor) ⇒ Tensor
Forward propagation.
-
#layers ⇒ Array
Get the all layers.
- #load_hash(layers_hash) ⇒ Object
- #to_hash ⇒ Object
Instance Method Details
#call(input_tensor) ⇒ Tensor
Forward propagation and create a link.
52 53 54 |
# File 'lib/dnn/core/models.rb', line 52 def call(input_tensor) forward(input_tensor) end |
#forward(input_tensor) ⇒ Tensor
Forward propagation.
45 46 47 |
# File 'lib/dnn/core/models.rb', line 45 def forward(input_tensor) raise NotImplementedError, "Class '#{self.class.name}' has implement method 'forward'" end |
#layers ⇒ Array
Get the all layers.
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/dnn/core/models.rb', line 58 def layers layers_array = [] instance_variables.sort.each do |ivar| obj = instance_variable_get(ivar) if obj.is_a?(Layers::Layer) layers_array << obj elsif obj.is_a?(Chain) || obj.is_a?(LayersList) layers_array.concat(obj.layers) end end layers_array end |
#load_hash(layers_hash) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/dnn/core/models.rb', line 84 def load_hash(layers_hash) instance_variables.sort.each do |ivar| hash_or_array = layers_hash[ivar] if hash_or_array.is_a?(Array) instance_variable_set(ivar, LayersList.from_hash_list(hash_or_array)) elsif hash_or_array.is_a?(Hash) obj_class = DNN.const_get(hash_or_array[:class]) obj = obj_class.allocate if obj.is_a?(Chain) obj = obj_class.new obj.load_hash(hash_or_array) instance_variable_set(ivar, obj) else instance_variable_set(ivar, Layers::Layer.from_hash(hash_or_array)) end end end end |
#to_hash ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/dnn/core/models.rb', line 71 def to_hash layers_hash = { class: self.class.name } instance_variables.sort.each do |ivar| obj = instance_variable_get(ivar) if obj.is_a?(Layers::Layer) || obj.is_a?(Chain) layers_hash[ivar] = obj.to_hash elsif obj.is_a?(LayersList) layers_hash[ivar] = obj.to_hash_list end end layers_hash end |