Class: DNN::Layers::Layer
- Inherits:
-
Object
- Object
- DNN::Layers::Layer
- Defined in:
- lib/dnn/core/layers/basic_layers.rb
Overview
Super class of all layer classes.
Direct Known Subclasses
Dropout, ELU, Exp, Flatten, GlobalAvgPool2D, InputLayer, Lasso, LeakyReLU, Log, Mean, MergeLayer, Mish, Neg, Pool2D, Pow, ReLU, Reshape, Ridge, Sigmoid, Softmax, Softplus, Softsign, Split, Sqrt, Sum, Swish, Tanh, TrainableLayer, UnPool2D
Instance Attribute Summary collapse
-
#input_shape ⇒ Object
readonly
Returns the value of attribute input_shape.
-
#output_shape ⇒ Object
readonly
Returns the value of attribute output_shape.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(tensor) ⇒ Object
-
#build(input_shape) ⇒ Object
Build the layer.
-
#built? ⇒ Boolean
If layer have already been built then return true.
-
#call(input) ⇒ Tensor
Forward propagation and create a link.
-
#clean ⇒ Object
Clean the layer state.
-
#compute_output_shape ⇒ Array
Please reimplement this method as needed.
-
#forward(input) ⇒ Tensor
Forward propagation.
-
#initialize ⇒ Layer
constructor
A new instance of Layer.
- #load_hash(hash) ⇒ Object
-
#to_hash(merge_hash = nil) ⇒ Object
Layer to a hash.
Constructor Details
#initialize ⇒ Layer
Returns a new instance of Layer.
42 43 44 |
# File 'lib/dnn/core/layers/basic_layers.rb', line 42 def initialize @built = false end |
Instance Attribute Details
#input_shape ⇒ Object (readonly)
Returns the value of attribute input_shape.
26 27 28 |
# File 'lib/dnn/core/layers/basic_layers.rb', line 26 def input_shape @input_shape end |
#output_shape ⇒ Object (readonly)
Returns the value of attribute output_shape.
27 28 29 |
# File 'lib/dnn/core/layers/basic_layers.rb', line 27 def output_shape @output_shape end |
Class Method Details
.call(x, *args, **kwargs) ⇒ Object
29 30 31 |
# File 'lib/dnn/core/layers/basic_layers.rb', line 29 def self.call(x, *args, **kwargs) new(*args, **kwargs).(x) end |
.from_hash(hash) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/dnn/core/layers/basic_layers.rb', line 33 def self.from_hash(hash) return nil unless hash layer_class = DNN.const_get(hash[:class]) layer = layer_class.allocate raise DNNError, "#{layer.class} is not an instance of #{self} class." unless layer.is_a?(self) layer.load_hash(hash) layer end |
Instance Method Details
#<<(tensor) ⇒ Object
82 83 84 |
# File 'lib/dnn/core/layers/basic_layers.rb', line 82 def <<(tensor) self.(tensor) end |
#build(input_shape) ⇒ Object
Build the layer.
57 58 59 60 61 |
# File 'lib/dnn/core/layers/basic_layers.rb', line 57 def build(input_shape) @input_shape = input_shape @output_shape = compute_output_shape @built = true end |
#built? ⇒ Boolean
Returns If layer have already been built then return true.
64 65 66 |
# File 'lib/dnn/core/layers/basic_layers.rb', line 64 def built? @built end |
#call(input) ⇒ Tensor
Forward propagation and create a link.
49 50 51 52 53 |
# File 'lib/dnn/core/layers/basic_layers.rb', line 49 def call(input) input = Tensor.convert(input) if !input.is_a?(Tensor) && !input.is_a?(Param) build(input.data.shape[1..-1]) unless built? forward(input) end |
#clean ⇒ Object
Clean the layer state.
98 99 100 101 102 103 104 105 106 |
# File 'lib/dnn/core/layers/basic_layers.rb', line 98 def clean input_shape = @input_shape hash = to_hash instance_variables.each do |ivar| instance_variable_set(ivar, nil) end load_hash(hash) build(input_shape) end |
#compute_output_shape ⇒ Array
Please reimplement this method as needed. The default implementation return input_shape.
78 79 80 |
# File 'lib/dnn/core/layers/basic_layers.rb', line 78 def compute_output_shape @input_shape end |
#forward(input) ⇒ Tensor
Forward propagation.
71 72 73 |
# File 'lib/dnn/core/layers/basic_layers.rb', line 71 def forward(input) raise NotImplementedError, "Class '#{self.class.name}' has implement method 'forward'" end |
#load_hash(hash) ⇒ Object
93 94 95 |
# File 'lib/dnn/core/layers/basic_layers.rb', line 93 def load_hash(hash) initialize end |
#to_hash(merge_hash = nil) ⇒ Object
Layer to a hash.
87 88 89 90 91 |
# File 'lib/dnn/core/layers/basic_layers.rb', line 87 def to_hash(merge_hash = nil) hash = { class: self.class.name } hash.merge!(merge_hash) if merge_hash hash end |