Class: Informers::FeatureExtraction
- Inherits:
-
Object
- Object
- Informers::FeatureExtraction
- Defined in:
- lib/informers/feature_extraction.rb
Instance Method Summary collapse
-
#initialize(model_path) ⇒ FeatureExtraction
constructor
A new instance of FeatureExtraction.
- #predict(texts) ⇒ Object
Constructor Details
#initialize(model_path) ⇒ FeatureExtraction
Returns a new instance of FeatureExtraction.
18 19 20 21 22 |
# File 'lib/informers/feature_extraction.rb', line 18 def initialize(model_path) tokenizer_path = File.("../../vendor/bert_base_cased_tok.bin", __dir__) @tokenizer = BlingFire.load_model(tokenizer_path) @model = OnnxRuntime::Model.new(model_path) end |
Instance Method Details
#predict(texts) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/informers/feature_extraction.rb', line 24 def predict(texts) singular = !texts.is_a?(Array) texts = [texts] if singular # tokenize input_ids = texts.map do |text| tokens = @tokenizer.text_to_ids(text, nil, 100) # unk token tokens.unshift(101) # cls token tokens << 102 # sep token tokens end max_tokens = input_ids.map(&:size).max attention_mask = [] input_ids.each do |ids| zeros = [0] * (max_tokens - ids.size) mask = ([1] * ids.size) + zeros attention_mask << mask ids.concat(zeros) end # infer input = { input_ids: input_ids, attention_mask: attention_mask } output = @model.predict(input) scores = output["output_0"] singular ? scores.first : scores end |