Class: DNN::Layers::InputLayer

Inherits:
Layer
  • Object
show all
Includes:
LayerNode
Defined in:
lib/dnn/core/layers/basic_layers.rb

Instance Attribute Summary

Attributes inherited from Layer

#input_shape

Class Method Summary collapse

Instance Method Summary collapse

Methods included from LayerNode

#backward, #forward

Methods inherited from Layer

#built?, #clean, #forward, from_hash, #output_shape

Constructor Details

#initialize(input_dim_or_shape) ⇒ InputLayer

Returns a new instance of InputLayer.

Parameters:

  • input_dim_or_shape (Array)

    Setting the shape or dimension of the input data.



146
147
148
149
# File 'lib/dnn/core/layers/basic_layers.rb', line 146

def initialize(input_dim_or_shape)
  super()
  @input_shape = input_dim_or_shape.is_a?(Array) ? input_dim_or_shape : [input_dim_or_shape]
end

Class Method Details

.call(input) ⇒ Object



140
141
142
143
# File 'lib/dnn/core/layers/basic_layers.rb', line 140

def self.call(input)
  shape = input.is_a?(Tensor) ? input.data.shape : input.shape
  new(shape[1..-1]).(input)
end

Instance Method Details

#<<(layer) ⇒ Object



189
190
191
192
193
194
# File 'lib/dnn/core/layers/basic_layers.rb', line 189

def <<(layer)
  if RUBY_VERSION < "2.6.0"
    raise DNN_Error, "Function composition is not supported before ruby version 2.6.0."
  end
  to_proc << layer
end

#>>(layer) ⇒ Object



182
183
184
185
186
187
# File 'lib/dnn/core/layers/basic_layers.rb', line 182

def >>(layer)
  if RUBY_VERSION < "2.6.0"
    raise DNN_Error, "Function composition is not supported before ruby version 2.6.0."
  end
  to_proc >> layer
end

#backward_node(dy) ⇒ Object



174
175
176
# File 'lib/dnn/core/layers/basic_layers.rb', line 174

def backward_node(dy)
  dy
end

#build(input_shape) ⇒ Object



163
164
165
# File 'lib/dnn/core/layers/basic_layers.rb', line 163

def build(input_shape)
  @built = true
end

#call(input) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/dnn/core/layers/basic_layers.rb', line 151

def call(input)
  build(@input_shape) unless built?
  if input.is_a?(Tensor)
    x = input.data
    prev_link = input&.link
  else
    x = input
    prev_link = nil
  end
  Tensor.new(forward_node(x), Link.new(prev_link, self))
end

#forward_node(x) ⇒ Object



167
168
169
170
171
172
# File 'lib/dnn/core/layers/basic_layers.rb', line 167

def forward_node(x)
  unless x.shape[1..-1] == @input_shape
    raise DNN_ShapeError, "The shape of x does not match the input shape. input shape is #{@input_shape}, but x shape is #{x.shape[1..-1]}."
  end
  x
end

#load_hash(hash) ⇒ Object



200
201
202
# File 'lib/dnn/core/layers/basic_layers.rb', line 200

def load_hash(hash)
  initialize(hash[:input_shape])
end

#to_hashObject



196
197
198
# File 'lib/dnn/core/layers/basic_layers.rb', line 196

def to_hash
  super(input_shape: @input_shape)
end

#to_procObject



178
179
180
# File 'lib/dnn/core/layers/basic_layers.rb', line 178

def to_proc
  method(:call).to_proc
end