Class: NekonekoGen::LinearClassifier
- Inherits:
-
Classifier
- Object
- Classifier
- NekonekoGen::LinearClassifier
- Defined in:
- lib/nekoneko_gen/linear_classifier.rb
Instance Attribute Summary collapse
-
#bias ⇒ Object
readonly
Returns the value of attribute bias.
-
#w ⇒ Object
readonly
Returns the value of attribute w.
Attributes inherited from Classifier
Instance Method Summary collapse
- #classify_method_code(lang) ⇒ Object
- #dot(vec, w) ⇒ Object
- #features(i = -1)) ⇒ Object
- #parameter_code(lang = :ruby) ⇒ Object
- #update(vec, label) ⇒ Object
Methods inherited from Classifier
Instance Attribute Details
#bias ⇒ Object (readonly)
Returns the value of attribute bias.
7 8 9 |
# File 'lib/nekoneko_gen/linear_classifier.rb', line 7 def bias @bias end |
#w ⇒ Object (readonly)
Returns the value of attribute w.
7 8 9 |
# File 'lib/nekoneko_gen/linear_classifier.rb', line 7 def w @w end |
Instance Method Details
#classify_method_code(lang) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/nekoneko_gen/linear_classifier.rb', line 48 def classify_method_code(lang) lang ||= :ruby case lang when :ruby else raise NotImplementedError end <<CODE def self.classify(svec) if (K == 2) w0 = W[0] (BIAS[0] + svec.map{|k, v| v * w0[k]}.reduce(0.0, :+)) > 0.0 ? 0 : 1 else W.each_with_index.map {|w, i| [BIAS[i] + svec.map{|k, v| v * w[k]}.reduce(0.0, :+), i] }.max.pop end end CODE end |
#dot(vec, w) ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'lib/nekoneko_gen/linear_classifier.rb', line 8 def dot(vec, w) dot = 0.0 vec.each do |k, v| if (a = w[k]) dot += a * v end end dot end |
#features(i = -1)) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/nekoneko_gen/linear_classifier.rb', line 29 def features(i = -1) if (i < 0) w.reduce(0){|sum, v| sum + v.size } else w[i].size end end |
#parameter_code(lang = :ruby) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/nekoneko_gen/linear_classifier.rb', line 36 def parameter_code(lang = :ruby) lang ||= :ruby case lang when :ruby else raise NotImplementedError end <<CODE BIAS = #{self.bias.inspect} W = JSON.load(#{@w.to_json.inspect}) CODE end |
#update(vec, label) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/nekoneko_gen/linear_classifier.rb', line 17 def update(vec, label) loss = 0.0 if (@k == 2) loss = update_at(0, vec, label) else s = 1.0 / @k @k.times do |i| loss += update_at(i, vec, label) * s end end loss end |