Sha256: 38fa10eb0ecd5a56e1087f5ac76ae079e33001bfdab574f92b6bea7ff5fb65ba

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module MLP
  class Neuron
    attr_reader :last_output, :weights
    attr_accessor :delta

    def initialize(number_of_inputs)
      create_weights(number_of_inputs)
    end

    def fire(input)
      @last_output = activation_function(input)
    end

    def update_weight(inputs, training_rate)
      inputs << -1 # Add the bias
      @weights.each_index do |i|
        @weights[i] += training_rate * delta * inputs[i]
      end
    end

    def inspect
      @weights
    end

    private

    def activation_function(input)
      sum = 0
      input.each_with_index do |n, index|
        sum += @weights[index] * n
      end
      sum += @weights.last * -1 # bias node
      sigmoid_function(sum)
    end

    # g(h) = 1 / (1+exp(-B*h(j)))
    def sigmoid_function(x)
      1 / (1 + Math.exp(-1 * x))
    end

    def create_weights(number_of_inputs)
      # Create random weights between 0 & 1
      #  Plus another one for the bias node
      @weights = []
      (number_of_inputs + 1).times do
        @weights << (rand > 0.5 ? -rand : rand)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mlpnn-0.0.1 lib/mlp/neuron.rb