Class: SkillExtractor::MLP

Inherits:
Object
  • Object
show all
Defined in:
lib/skill_extractor/mlp.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ MLP

Returns a new instance of MLP.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/skill_extractor/mlp.rb', line 8

def initialize(path)
  spec = JSON.parse(File.read(path))
  unless spec["format"] == "mlp-weights-v1"
    raise ArgumentError, "unsupported weights format: #{spec["format"]}"
  end
  @layers = spec["layers"].map do |l|
    {
      rows: l["shape"][0],
      cols: l["shape"][1],
      w: Base64.decode64(l["weights_b64"]).unpack("e*"),
      b: Base64.decode64(l["bias_b64"]).unpack("e*")
    }
  end
end

Instance Method Details

#predict_proba(xs) ⇒ Object

xs: array of 384-dim L2-normalized embeddings -> array of P(skill)



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/skill_extractor/mlp.rb', line 24

def predict_proba(xs)
  last = @layers.length - 1
  xs.map do |x|
    h = x
    @layers.each_with_index do |l, i|
      cols = l[:cols]
      w = l[:w]
      nxt = l[:b].dup
      h.each_with_index do |hv, r|
        next if hv.zero?
        off = r * cols
        cols.times { |j| nxt[j] += hv * w[off + j] }
      end
      nxt.map! { |v| v.negative? ? 0.0 : v } if i < last # relu
      h = nxt
    end
    1.0 / (1.0 + Math.exp(-h[0])) # logistic
  end
end