Class: ML::Learner::PocketLearner

Inherits:
PerceptronLearner show all
Defined in:
lib/method/pocket.rb

Overview

Implementation of pocket learning algorithm

Instance Attribute Summary

Attributes included from LinearToolbox

#current_vector

Instance Method Summary collapse

Methods inherited from PerceptronLearner

#initialize

Methods included from LinearToolbox

#line, #predict

Methods included from Toolbox

#classify_error, #predict

Constructor Details

This class inherits a constructor from ML::Learner::PerceptronLearner

Instance Method Details

#train!(data, iteration = 1000) ⇒ Object

Train with supervised data

Parameters:

  • data (Hash)

    supervised input data (mapping from array to integer)

  • iteration (Integer) (defaults to: 1000)

    the number of the iterations



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/method/pocket.rb', line 9

def train! data, iteration = 1000
  pool = data.to_a
  best_error, pocket = 1.0/0, nil

  iteration.times do
    # update pocket
    error = classify_error pool
    if error < best_error
      error = best_error
      pocket = current_vector.dup
    end
    break if best_error == 0

    # the random order
    order = (1...(pool.size)).to_a.shuffle

    for i in order
      dat, result = pool[i]
      aug_data = Matrix.column_vector(dat)

      if wrongly_classify aug_data, result
        update_vector aug_data, result
        break
      end
    end
  end
end