Class: DNN::Layers::InputLayer

Inherits:
Layer
  • Object
show all
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 inherited from Layer

#built?, #clean, 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.



129
130
131
132
# File 'lib/dnn/core/layers/basic_layers.rb', line 129

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



123
124
125
126
# File 'lib/dnn/core/layers/basic_layers.rb', line 123

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



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

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



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

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(dy) ⇒ Object



157
158
159
# File 'lib/dnn/core/layers/basic_layers.rb', line 157

def backward(dy)
  dy
end

#build(input_shape) ⇒ Object



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

def build(input_shape)
  @built = true
end

#call(input) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/dnn/core/layers/basic_layers.rb', line 134

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(x), Link.new(prev_link, self))
end

#forward(x) ⇒ Object



150
151
152
153
154
155
# File 'lib/dnn/core/layers/basic_layers.rb', line 150

def forward(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



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

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

#to_hashObject



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

def to_hash
  super(input_shape: @input_shape)
end

#to_procObject



161
162
163
# File 'lib/dnn/core/layers/basic_layers.rb', line 161

def to_proc
  method(:call).to_proc
end