Class: DNN::Layers::InputLayer

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

Instance Attribute Summary

Attributes inherited from Layer

#input_shape, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Layer

#built?, from_hash, #output_shape

Constructor Details

#initialize(input_dim_or_shape) ⇒ InputLayer



106
107
108
109
# File 'lib/dnn/core/layers.rb', line 106

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



100
101
102
103
# File 'lib/dnn/core/layers.rb', line 100

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



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

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



142
143
144
145
146
147
# File 'lib/dnn/core/layers.rb', line 142

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



134
135
136
# File 'lib/dnn/core/layers.rb', line 134

def backward(dy)
  dy
end

#buildObject



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

def build
  @built = true
end

#call(input) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/dnn/core/layers.rb', line 111

def call(input)
  build 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



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

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



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

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

#to_hashObject



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

def to_hash
  super(input_shape: @input_shape)
end

#to_procObject



138
139
140
# File 'lib/dnn/core/layers.rb', line 138

def to_proc
  method(:call).to_proc
end