Class: DNN::Layers::Layer

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

Overview

Super class of all layer classes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLayer

Returns a new instance of Layer.



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

def initialize
  @built = false
end

Instance Attribute Details

#input_shapeObject (readonly)

Returns the value of attribute input_shape.



25
26
27
# File 'lib/dnn/core/layers/basic_layers.rb', line 25

def input_shape
  @input_shape
end

#output_shapeObject (readonly)

Returns the value of attribute output_shape.



26
27
28
# File 'lib/dnn/core/layers/basic_layers.rb', line 26

def output_shape
  @output_shape
end

Class Method Details

.call(x, *args) ⇒ Object



28
29
30
# File 'lib/dnn/core/layers/basic_layers.rb', line 28

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

.from_hash(hash) ⇒ Object

Raises:



32
33
34
35
36
37
38
39
# File 'lib/dnn/core/layers/basic_layers.rb', line 32

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



81
82
83
# File 'lib/dnn/core/layers/basic_layers.rb', line 81

def <<(tensor)
  self.(tensor)
end

#build(input_shape) ⇒ Object

Build the layer.

Parameters:

  • input_shape (Array)

    Setting the shape of the input data.



56
57
58
59
60
# File 'lib/dnn/core/layers/basic_layers.rb', line 56

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.

Returns:

  • (Boolean)

    If layer have already been built then return true.



63
64
65
# File 'lib/dnn/core/layers/basic_layers.rb', line 63

def built?
  @built
end

#call(input) ⇒ Tensor

Forward propagation and create a link.

Parameters:

Returns:



48
49
50
51
52
# File 'lib/dnn/core/layers/basic_layers.rb', line 48

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

#cleanObject

Clean the layer state.



97
98
99
100
101
102
103
104
105
# File 'lib/dnn/core/layers/basic_layers.rb', line 97

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_shapeArray

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

Returns:

  • (Array)

    Return the shape of the output data.



77
78
79
# File 'lib/dnn/core/layers/basic_layers.rb', line 77

def compute_output_shape
  @input_shape
end

#forward(input) ⇒ Tensor

Forward propagation.

Parameters:

  • input (Tensor)

    Input tensor or param.

Returns:

Raises:

  • (NotImplementedError)


70
71
72
# File 'lib/dnn/core/layers/basic_layers.rb', line 70

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

#load_hash(hash) ⇒ Object



92
93
94
# File 'lib/dnn/core/layers/basic_layers.rb', line 92

def load_hash(hash)
  initialize
end

#to_hash(merge_hash = nil) ⇒ Object

Layer to a hash.



86
87
88
89
90
# File 'lib/dnn/core/layers/basic_layers.rb', line 86

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