Sha256: 144f0eef4caa89a72c05c9994e0ec39810a26c408aadb113697e106ab1b287ca

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

module Brainz
  class Neuron
    attr_accessor :activation, :delta, :output_change
    attr_reader :dendrites, :axon_synapses, :sum, :layer

    def initialize(layer)
      @layer = layer
      @dendrites = []
      @axon_synapses = []
      self.output_change = 0
      self.activation = 0
      reset
    end

    def learning_rate
      layer.learning_rate
    end

    def momentum
      layer.momentum
    end

    def reset
      @sum = 0
    end

    def add(value)
      @sum += value
    end

    def activate
      @activation = fi(@sum)
    end

    def send_signals
      axon_synapses.each { |s| s.to.add(@activation * s.weight) }

    end

    def calculate_delta(target = nil)
      if target
        # output layer
        @delta = d_sigmoid(activation) * (target - activation)
      else
        # middle layer
        error = axon_synapses.collect { |synapse| synapse.to.delta * synapse.weight }.inject(:+)
        @delta = d_sigmoid(activation) * error
      end
    end

    def adjust_weights
      dendrites.each do |synapse|
        change = delta * synapse.from.activation
        synapse.adjust(learning_rate * change + momentum * synapse.change)
        synapse.change = change
      end
    end

    def fi(x)
      Math.tanh(x)
    end

    def d_sigmoid(y)
      1.0 - y ** 2
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
brainz-0.2.0 lib/brainz/neuron.rb
brainz-0.1.3 lib/brainz/neuron.rb
brainz-0.1.2 lib/brainz/neuron.rb
brainz-0.1.1 lib/brainz/neuron.rb