class CooCoo::NeuronLayer

Attributes

activation_function[RW]

Public Class Methods

from_hash(h, network = nil) click to toggle source
# File lib/coo-coo/neuron_layer.rb, line 125
def from_hash(h, network = nil)
  self.new(h[:neurons].size, h[:outputs]).update_from_hash!(h)
end
new(num_inputs, size, activation_function = CooCoo.default_activation) click to toggle source
# File lib/coo-coo/neuron_layer.rb, line 15
def initialize(num_inputs, size, activation_function = CooCoo.default_activation)
  @activation_function = activation_function
  @neurons = Array.new
  size.times do |i|
    @neurons[i] = Neuron.new(num_inputs, activation_function)
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/coo-coo/neuron_layer.rb, line 118
def ==(other)
  other.kind_of?(self.class) &&
    size == other.size &&
    neurons.zip(other.neurons).all? { |a, b| a == b }
end
adjust_weights!(deltas) click to toggle source
# File lib/coo-coo/neuron_layer.rb, line 67
def adjust_weights!(deltas)
  @neurons.each_with_index do |n, i|
    n.adjust_weights!(deltas.bias_deltas[i], deltas.weight_deltas[i])
  end

  self
end
backprop(input, output, errors, hidden_state) click to toggle source
# File lib/coo-coo/neuron_layer.rb, line 44
def backprop(input, output, errors, hidden_state)
  o = @neurons.each_with_index.inject(CooCoo::Vector.zeros(size)) do |acc, (n, i)|
    acc[i] = n.backprop(input, output[i], errors[i])
    acc
  end

  return o, hidden_state
end
forward(input, hidden_state) click to toggle source
# File lib/coo-coo/neuron_layer.rb, line 35
def forward(input, hidden_state)
  o = @neurons.each_with_index.inject(CooCoo::Vector.zeros(size)) do |acc, (neuron, i)|
    acc[i] = neuron.forward(input)
    acc
  end

  return o, hidden_state
end
neurons() click to toggle source
# File lib/coo-coo/neuron_layer.rb, line 23
def neurons
  @neurons
end
num_inputs() click to toggle source
# File lib/coo-coo/neuron_layer.rb, line 27
def num_inputs
  @neurons[0].num_inputs
end
resize!(new_size) click to toggle source
# File lib/coo-coo/neuron_layer.rb, line 89
def resize!(new_size)
  n = @neurons + Array.new(new_size - @neurons.size)
  (@neurons.size...new_size).each do |i|
    n[i] = Neuron.new(num_inputs)
  end

  @neurons = n

  self
end
size() click to toggle source
# File lib/coo-coo/neuron_layer.rb, line 31
def size
  @neurons.size
end
to_hash(network = nil) click to toggle source
# File lib/coo-coo/neuron_layer.rb, line 82
def to_hash(network = nil)
  { type: self.class.to_s,
    outputs: @neurons.size,
    neurons: @neurons.collect(&:to_hash)
  }
end
transfer_error(deltas) click to toggle source
# File lib/coo-coo/neuron_layer.rb, line 53
def transfer_error(deltas)
  @neurons.each_with_index.inject(CooCoo::Vector.zeros(num_inputs)) do |acc, (n, i)|
    acc + n.transfer_error(deltas[i])
  end
end
transfer_input_error(expecting) click to toggle source
# File lib/coo-coo/neuron_layer.rb, line 59
def transfer_input_error(expecting)
  (output - expecting).to_a
end
update_from_hash!(h) click to toggle source
# File lib/coo-coo/neuron_layer.rb, line 108
def update_from_hash!(h)
  resize!(h[:outputs])
  
  h[:outputs].times do |i|
    update_neuron_from_hash!(i, h[:neurons][i])
  end

  self
end
update_neuron_from_hash!(neuron_index, h) click to toggle source
# File lib/coo-coo/neuron_layer.rb, line 100
def update_neuron_from_hash!(neuron_index, h)
  if neuron_index > @neurons.size
    resize!(neuron_index)
  end
  
  @neurons[neuron_index].update_from_hash!(h)
end
update_weights!(inputs, deltas) click to toggle source
# File lib/coo-coo/neuron_layer.rb, line 63
def update_weights!(inputs, deltas)
  adjust_weights!(weight_deltas(inputs, deltas))
end
weight_deltas(inputs, deltas) click to toggle source
# File lib/coo-coo/neuron_layer.rb, line 75
def weight_deltas(inputs, deltas)
  WeightDeltas.new(*@neurons.each_with_index.inject([ CooCoo::Vector.zeros(size), CooCoo::Sequence.new(size) ]) do |acc, (n, i)|
                     acc[0][i], acc[1][i] = n.weight_deltas(inputs, deltas[i])
                     acc
                   end)
end