class DNN::Layers::Layer

Super class of all layer classes.

Attributes

input_shape[R]
output_shape[R]

Public Class Methods

call(x, *args) click to toggle source
# File lib/dnn/core/layers/basic_layers.rb, line 29
def self.call(x, *args)
  new(*args).(x)
end
from_hash(hash) click to toggle source
# File lib/dnn/core/layers/basic_layers.rb, line 33
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
new() click to toggle source
# File lib/dnn/core/layers/basic_layers.rb, line 42
def initialize
  @built = false
end

Public Instance Methods

<<(tensor) click to toggle source
# File lib/dnn/core/layers/basic_layers.rb, line 82
def <<(tensor)
  self.(tensor)
end
build(input_shape) click to toggle source

Build the layer. @param [Array] input_shape Setting the shape of the input data.

# File lib/dnn/core/layers/basic_layers.rb, line 57
def build(input_shape)
  @input_shape = input_shape
  @output_shape = compute_output_shape
  @built = true
end
built?() click to toggle source

@return [Boolean] If layer have already been built then return true.

# File lib/dnn/core/layers/basic_layers.rb, line 64
def built?
  @built
end
call(input) click to toggle source

Forward propagation and create a link. @param [Tensor | Param] input Input tensor or param. @return [Tensor] Output tensor.

# File lib/dnn/core/layers/basic_layers.rb, line 49
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
clean() click to toggle source

Clean the layer state.

# File lib/dnn/core/layers/basic_layers.rb, line 98
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_shape() click to toggle source

Please reimplement this method as needed. The default implementation return input_shape. @return [Array] Return the shape of the output data.

# File lib/dnn/core/layers/basic_layers.rb, line 78
def compute_output_shape
  @input_shape
end
forward(input) click to toggle source

Forward propagation. @param [Tensor] input Input tensor or param. @return [Tensor] Output tensor.

# File lib/dnn/core/layers/basic_layers.rb, line 71
def forward(input)
  raise NotImplementedError, "Class '#{self.class.name}' has implement method 'forward'"
end
load_hash(hash) click to toggle source
# File lib/dnn/core/layers/basic_layers.rb, line 93
def load_hash(hash)
  initialize
end
to_hash(merge_hash = nil) click to toggle source

Layer to a hash.

# File lib/dnn/core/layers/basic_layers.rb, line 87
def to_hash(merge_hash = nil)
  hash = { class: self.class.name }
  hash.merge!(merge_hash) if merge_hash
  hash
end