Class: DNN::Layers::Layer

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

Overview

Super class of all layer classes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Layer

Returns a new instance of Layer.



13
14
15
# File 'lib/dnn/core/layers.rb', line 13

def initialize
  @built = false
end

Instance Attribute Details

#input_shape ⇒ Object (readonly)

Returns the value of attribute input_shape.



7
8
9
# File 'lib/dnn/core/layers.rb', line 7

def input_shape
  @input_shape
end

#name ⇒ Object

Returns the value of attribute name.



6
7
8
# File 'lib/dnn/core/layers.rb', line 6

def name
  @name
end

Class Method Details

.call(x, *args) ⇒ Object



9
10
11
# File 'lib/dnn/core/layers.rb', line 9

def self.call(x, *args)
  self.new(*args).(x)
end

Instance Method Details

#backward(dy) ⇒ Object

Backward propagation.

Parameters:

  • dy (Numo::SFloat) —

    Differential value of output data.

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/dnn/core/layers.rb', line 47

def backward(dy)
  raise NotImplementedError.new("Class '#{self.class.name}' has implement method 'backward'")
end

#build(input_shape) ⇒ Object

Build the layer.

Parameters:

  • input_shape (Array) —

    Setting the shape of the input data.



29
30
31
32
# File 'lib/dnn/core/layers.rb', line 29

def build(input_shape)
  @input_shape = input_shape
  @built = true
end

#built? ⇒ Boolean

Returns If layer have already been built then return true.

Returns:

  • (Boolean) —

    If layer have already been built then return true.



35
36
37
# File 'lib/dnn/core/layers.rb', line 35

def built?
  @built
end

#call(input) ⇒ Object

Forward propagation and create a link.

Parameters:

  • input (Array) —

    Array of the form [x_input_data, prev_link].



19
20
21
22
23
24
25
# File 'lib/dnn/core/layers.rb', line 19

def call(input)
  x, prev_link = *input
  build(x.shape[1..-1]) unless built?
  y = forward(x)
  link = Link.new(prev_link, self)
  [y, link]
end

#forward(x) ⇒ Object

Forward propagation.

Parameters:

  • x (Numo::SFloat) —

    Input data.

Raises:

  • (NotImplementedError)


41
42
43
# File 'lib/dnn/core/layers.rb', line 41

def forward(x)
  raise NotImplementedError.new("Class '#{self.class.name}' has implement method 'forward'")
end

#output_shape ⇒ Array

Please reimplement this method as needed. The default implementation return input_shape.

Returns:

  • (Array) —

    Return the shape of the output data.



54
55
56
# File 'lib/dnn/core/layers.rb', line 54

def output_shape
  @input_shape
end

#to_hash(merge_hash = nil) ⇒ Object

Layer to a hash.



59
60
61
62
63
# File 'lib/dnn/core/layers.rb', line 59

def to_hash(merge_hash = nil)
  hash = { class: self.class.name }
  hash.merge!(merge_hash) if merge_hash
  hash
end