Class: NekonekoGen::Arow

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

Overview

Adaptive Regularization of Weight Vector

Constant Summary collapse

R =
10.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 = {}) ⇒ Arow

Returns a new instance of Arow.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/nekoneko_gen/arow.rb', line 10

def initialize(k, n, options = {})
  @r = options[:c] || R
  @k = k
  @cov = []
  @covb = []
  @w = []
  @bias = []
  if (@k == 2)
    @cov[0] = Array.new(n, 1.0)
    @w[0] = Array.new(n, 0.0)
    @covb[0] = 1.0
    @bias[0] = 0.0
  else
    k.times do |i|
      @cov[i] = Array.new(n, 1.0)
      @w[i] = Array.new(n, 0.0)
      @covb[i] = 1.0
      @bias[i] = 0.0
    end
  end
end

Instance Method Details

#default_iterationObject



53
54
55
# File 'lib/nekoneko_gen/arow.rb', line 53

def default_iteration
  DEFAULT_ITERATION
end

#update_at(i, vec, label) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/nekoneko_gen/arow.rb', line 31

def update_at(i, vec, label)
  w = @w[i]
  cov = @cov[i]
  covb = @covb[i]
  bias = @bias[i]

  y = label == i ? 1 : -1
  score = bias + dot(vec, w)
  alpha = 1.0 - y * score
  if (alpha > 0.0)
    r_inv= 1.0 / @r
    var = vec.map{|k, v| cov[k] * v * v }.reduce(:+) + covb
    alpha *= (1.0 / (var + @r)) * y
    vec.each do |k, v|
      w[k] += alpha * cov[k] * v
      cov[k] = 1.0 / ((1.0 / cov[k]) + (v * v * r_inv))
    end
    @bias[i] += alpha * covb
    @covb[i] = 1.0 / ((1.0 / covb) + r_inv)
  end
  score * y < 0.0 ? 1.0 : 0.0
end