class CooCoo::Recurrence::Frontend

Public Class Methods

from_hash(h, network = nil) click to toggle source
# File lib/coo-coo/recurrence/frontend.rb, line 91
def self.from_hash(h, network = nil)
  self.new(h.fetch(:inputs), h.fetch(:recurrent_outputs)).update_from_hash!(h)
end
new(num_inputs, num_recurrent_outputs) click to toggle source
# File lib/coo-coo/recurrence/frontend.rb, line 10
def initialize(num_inputs, num_recurrent_outputs)
  @num_inputs = num_inputs
  @num_recurrent_outputs = num_recurrent_outputs
end

Public Instance Methods

==(other) click to toggle source
# File lib/coo-coo/recurrence/frontend.rb, line 71
def ==(other)
  other.kind_of?(self.class) &&
    num_inputs == other.num_inputs &&
    recurrent_size == other.recurrent_size
end
activation_function() click to toggle source
# File lib/coo-coo/recurrence/frontend.rb, line 19
def activation_function
  nil
end
adjust_weights!(deltas) click to toggle source
# File lib/coo-coo/recurrence/frontend.rb, line 67
def adjust_weights!(deltas)
  self
end
backend() click to toggle source
# File lib/coo-coo/recurrence/frontend.rb, line 31
def backend
  @backend ||= Backend.new(self, @num_inputs, recurrent_size)
end
backend=(layer) click to toggle source
# File lib/coo-coo/recurrence/frontend.rb, line 35
def backend=(layer)
  @backend = layer
end
backprop(input, outputs, errors, hidden_state) click to toggle source
# File lib/coo-coo/recurrence/frontend.rb, line 45
def backprop(input, outputs, errors, hidden_state)
  # split for real and recurrent errors
  norm_errors = errors[0, num_inputs]
  recurrent_errors = errors[num_inputs, recurrent_size]

  # buffer the recurrent output
  hidden_state ||= Hash.new
  hidden_state[self] ||= Array.new
  hidden_state[self].push(recurrent_errors)

  # return real errors
  return norm_errors, hidden_state
end
forward(inputs, hidden_state) click to toggle source
# File lib/coo-coo/recurrence/frontend.rb, line 39
def forward(inputs, hidden_state)
  layer_state = hidden_state[@backend]
  recurrent_input = layer_state && layer_state.pop
  return inputs.append(recurrent_input || empty_input), hidden_state
end
num_inputs() click to toggle source
# File lib/coo-coo/recurrence/frontend.rb, line 15
def num_inputs
  @num_inputs
end
recurrent_size() click to toggle source
# File lib/coo-coo/recurrence/frontend.rb, line 27
def recurrent_size
  @num_recurrent_outputs
end
size() click to toggle source
# File lib/coo-coo/recurrence/frontend.rb, line 23
def size
  @num_inputs + recurrent_size
end
to_hash(network = nil) click to toggle source
# File lib/coo-coo/recurrence/frontend.rb, line 77
def to_hash(network = nil)
  { type: self.class.name,
    inputs: @num_inputs,
    recurrent_outputs: @num_recurrent_outputs
  }
end
transfer_error(deltas) click to toggle source
# File lib/coo-coo/recurrence/frontend.rb, line 59
def transfer_error(deltas)
  deltas
end
update_from_hash!(h) click to toggle source
# File lib/coo-coo/recurrence/frontend.rb, line 84
def update_from_hash!(h)
  @num_inputs = h.fetch(:inputs)
  @num_recurrent_outputs = h.fetch(:recurrent_outputs)

  self
end
weight_deltas(inputs, deltas) click to toggle source
# File lib/coo-coo/recurrence/frontend.rb, line 63
def weight_deltas(inputs, deltas)
  inputs * deltas
end

Private Instance Methods

empty_input() click to toggle source
# File lib/coo-coo/recurrence/frontend.rb, line 96
def empty_input
  CooCoo::Vector.zeros(recurrent_size)
end