class CooCoo::Neuron
Attributes
bias[R]
num_inputs[R]
weights[R]
Public Class Methods
from_hash(h)
click to toggle source
# File lib/coo-coo/neuron.rb, line 32 def self.from_hash(h) self.new(h[:num_inputs] || h[:weights].size).update_from_hash!(h) end
new(num_inputs, activation_func = CooCoo.default_activation)
click to toggle source
# File lib/coo-coo/neuron.rb, line 9 def initialize(num_inputs, activation_func = CooCoo.default_activation) @num_inputs = num_inputs @activation_func = activation_func @weights = @activation_func.initial_weights(num_inputs, 1) @bias = @activation_func.initial_bias(1)[0] end
Public Instance Methods
==(other)
click to toggle source
# File lib/coo-coo/neuron.rb, line 77 def ==(other) if other.kind_of?(self.class) num_inputs == other.num_inputs && @weights == other.weights else false end end
activate(input)
click to toggle source
# File lib/coo-coo/neuron.rb, line 44 def activate(input) (@weights * input).sum + @bias end
adjust_weights!(bias_delta, weight_deltas)
click to toggle source
# File lib/coo-coo/neuron.rb, line 72 def adjust_weights!(bias_delta, weight_deltas) @bias -= bias_delta @weights -= weight_deltas end
backprop(input, output, error)
click to toggle source
# File lib/coo-coo/neuron.rb, line 52 def backprop(input, output, error) # Properly: error * @activation_func.derivative(activate(input), output) error * @activation_func.derivative(nil, output) end
forward(input)
click to toggle source
# File lib/coo-coo/neuron.rb, line 40 def forward(input) transfer(activate(input)) end
to_hash()
click to toggle source
# File lib/coo-coo/neuron.rb, line 16 def to_hash { num_inputs: @num_inputs, weights: @weights.to_a, bias: @bias, f: @activation_func.name } end
transfer(activation)
click to toggle source
# File lib/coo-coo/neuron.rb, line 48 def transfer(activation) @activation_func.call(activation) end
transfer_error(delta)
click to toggle source
# File lib/coo-coo/neuron.rb, line 57 def transfer_error(delta) @weights * delta end
update_from_hash!(h)
click to toggle source
# File lib/coo-coo/neuron.rb, line 24 def update_from_hash!(h) @num_inputs = h.fetch(:num_inputs, h.fetch(:weights, []).size) @weights = CooCoo::Vector[h[:weights]] @activation_func = CooCoo::ActivationFunctions.from_name(h[:f] || CooCoo.default_activation.name) @bias = h.fetch(:bias, @activation_func.initial_bias(1)[0]) self end
update_weights!(inputs, delta)
click to toggle source
# File lib/coo-coo/neuron.rb, line 68 def update_weights!(inputs, delta) adjust_weights!(*weight_deltas(inputs, delta)) end
weight_deltas(inputs, delta)
click to toggle source
# File lib/coo-coo/neuron.rb, line 61 def weight_deltas(inputs, delta) [ delta, inputs * delta ] rescue CooCoo.debug("#{$!}\n\t#{inputs.class}\t#{inputs}\n\t#{@weights.class}\t#{@weights}\n\t#{delta.class}\t#{delta}") raise end