Class: NekonekoGen::PA

Inherits:
LinearClassifier show all
Defined in:
lib/nekoneko_gen/pa.rb

Overview

Passive Agressive

Constant Summary collapse

C =
1.0
NORM =

norm + BIAS

2.0
DEFAULT_ITERATION =
20

Instance Attribute Summary

Attributes inherited from LinearClassifier

#bias, #w

Attributes inherited from Classifier

#k

Instance Method Summary collapse

Methods inherited from LinearClassifier

#classify_method_code, #dot, #features, #parameter_code, #update

Methods inherited from Classifier

#classify_method_code, #features, #parameter_code, #update

Constructor Details

#initialize(k, n, options = {}) ⇒ PA

Returns a new instance of PA.



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
36
37
38
39
40
# File 'lib/nekoneko_gen/pa.rb', line 11

def initialize(k, n, options = {})
  @k = k
  @c = options[:c] || C
  @w = []
  @bias = []
  if (@k == 2)
    @w[0] = Array.new(n, 0.0)
    @bias[0] = 0.0
  else
    k.times do |i|
      @w[i] = Array.new(n, 0.0)
      @bias[i] = 0.0
    end
  end
  if options[:method]
    @tau = 
      case options[:method]
      when :pa
        lambda{|y, l| pa(y, l)}
      when :pa1
        lambda{|y, l| pa1(y, l)}          
      when :pa2
        lambda{|y, l| pa2(y, l)}          
      else
        lambda{|y, l| pa2(y, l)}          
      end
  else
    @tau = lambda{|y, l| pa2(y, l)}
  end
end

Instance Method Details

#default_iterationObject



65
66
67
# File 'lib/nekoneko_gen/pa.rb', line 65

def default_iteration
  DEFAULT_ITERATION
end

#pa(y, l) ⇒ Object



47
48
49
# File 'lib/nekoneko_gen/pa.rb', line 47

def pa(y, l)
  y * l / NORM
end

#pa1(y, l) ⇒ Object



44
45
46
# File 'lib/nekoneko_gen/pa.rb', line 44

def pa1(y, l)
  y * [@c, (l / NORM)].min
end

#pa2(y, l) ⇒ Object



41
42
43
# File 'lib/nekoneko_gen/pa.rb', line 41

def pa2(y, l)
  y * (l / (NORM + 0.5 / @c))
end

#update_at(i, vec, label) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/nekoneko_gen/pa.rb', line 50

def update_at(i, vec, label)
  y = label == i ? 1 : -1
  w = @w[i]
  
  score = @bias[i] + dot(vec, w)
  l = 1.0 - score * y
  if (l > 0.0)
    alpha = @tau.call(y, l)
    vec.each do |k, v|
      w[k] += alpha * v
    end
    @bias[i] += alpha
  end
  y * score < 0.0 ? 1.0 : 0.0
end