class Perceptron::Unit

Attributes

weight[R]

Public Class Methods

new(features_number, weight = Weight.new(features_number)) click to toggle source
# File lib/perceptron/unit.rb, line 6
def initialize(features_number, weight = Weight.new(features_number))
  @weight = weight
end

Public Instance Methods

predict(vector) click to toggle source
# File lib/perceptron/unit.rb, line 10
def predict(vector)
  scalar_product(@weight.vector, vector) > 0 ? 1 : 0
end
train(hash) click to toggle source
# File lib/perceptron/unit.rb, line 14
def train(hash)
  learn(hash) unless calculate_error(hash).zero?
end

Private Instance Methods

calculate_error(hash) click to toggle source
# File lib/perceptron/unit.rb, line 24
def calculate_error(hash)
  hash[:expected] - predict(Vector[*hash[:vector]])
end
learn(hash) click to toggle source
# File lib/perceptron/unit.rb, line 28
def learn(hash)
  @weight.update(updated_weight_arr(hash))
end
scalar_product(vector_1, vector_2) click to toggle source
# File lib/perceptron/unit.rb, line 20
def scalar_product(vector_1, vector_2)
  vector_1.inner_product vector_2
end
updated_weight_arr(hash) click to toggle source
# File lib/perceptron/unit.rb, line 32
def updated_weight_arr(hash)
  (@weight.vector + Vector[*hash[:vector]] * calculate_error(hash)).to_a
end