Module: Informers::Utils

Defined in:
lib/informers/utils/hub.rb,
lib/informers/utils/core.rb,
lib/informers/utils/math.rb,
lib/informers/utils/tensor.rb

Defined Under Namespace

Modules: Hub

Class Method Summary collapse

Class Method Details

.dispatch_callback(progress_callback, data) ⇒ Object



3
4
5
# File 'lib/informers/utils/core.rb', line 3

def self.dispatch_callback(progress_callback, data)
  progress_callback.(data) if progress_callback
end

.get_top_items(items, top_k = 0) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/informers/utils/math.rb', line 23

def self.get_top_items(items, top_k = 0)
  # if top == 0, return all

  items = items
    .map.with_index { |x, i| [i, x] } # Get indices ([index, score])
    .sort_by { |v| -v[1] }            # Sort by log probabilities

  if !top_k.nil? && top_k > 0
    items = items.slice(0, top_k)     # Get top k items
  end

  items
end

.max(arr) ⇒ Object



37
38
39
40
41
42
# File 'lib/informers/utils/math.rb', line 37

def self.max(arr)
  if arr.length == 0
    raise Error, "Array must not be empty"
  end
  arr.map.with_index.max_by { |v, _| v }
end

.mean_pooling(last_hidden_state, attention_mask) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/informers/utils/tensor.rb', line 3

def self.mean_pooling(last_hidden_state, attention_mask)
  last_hidden_state.zip(attention_mask).map do |state, mask|
    state[0].size.times.map do |k|
      sum = 0.0
      count = 0

      state.zip(mask) do |s, m|
        count += m
        sum += s[k] * m
      end

      sum / count
    end
  end
end

.normalize(result) ⇒ Object



19
20
21
22
23
24
# File 'lib/informers/utils/tensor.rb', line 19

def self.normalize(result)
  result.map do |row|
    norm = Math.sqrt(row.sum { |v| v * v })
    row.map { |v| v / norm }
  end
end

.sigmoid(arr) ⇒ Object



19
20
21
# File 'lib/informers/utils/math.rb', line 19

def self.sigmoid(arr)
  arr.map { |v| 1 / (1 + Math.exp(-v)) }
end

.softmax(arr) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/informers/utils/math.rb', line 3

def self.softmax(arr)
  # Compute the maximum value in the array
  max_val = arr.max

  #  Compute the exponentials of the array values
  exps = arr.map { |x| Math.exp(x - max_val) }

  # Compute the sum of the exponentials
  sum_exps = exps.sum

  # Compute the softmax values
  softmax_arr = exps.map { |x| x / sum_exps }

  softmax_arr
end