Module: Qoa::MatrixHelpers

Included in:
Training
Defined in:
lib/qoa/matrix_helpers.rb

Instance Method Summary collapse

Instance Method Details

#apply_dropout(matrix, dropout_rate) ⇒ Object



100
101
102
# File 'lib/qoa/matrix_helpers.rb', line 100

def apply_dropout(matrix, dropout_rate)
  matrix.map { |row| row.map { |x| rand < dropout_rate ? 0 : x } }
end

#apply_function(matrix, func) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/qoa/matrix_helpers.rb', line 26

def apply_function(matrix, func)
  matrix.map do |row|
    row.map do |x|
      x.nil? ? nil : func.call(x)  # Add a check for nil values
    end
  end
end

#matrix_add(a, b) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/qoa/matrix_helpers.rb', line 54

def matrix_add(a, b)
  a.zip(b).map do |r1, r2|
    if r1.nil? || r2.nil?
      nil
    else
      r1.zip(r2).map do |x, y|
        (x.nil? || y.nil?) ? nil : x + y
      end
    end
  end
end

#matrix_multiply(a, b) ⇒ Object

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/qoa/matrix_helpers.rb', line 3

def matrix_multiply(a, b)
  a_rows = a.size
  a_cols = a[0].size
  b_rows = b.size
  b_cols = b[0].size
  raise ArgumentError, 'incompatible dimensions' if a_cols != b_rows

  result = Array.new(a_rows) { Array.new(b_cols, 0) }
  a_rows.times do |i|
    b_cols.times do |j|
      a_cols.times do |k|
        # Check for nil values before performing multiplication
        if a[i].nil? || b[k].nil? || a[i][k].nil? || b[k][j].nil?
          result[i][j] += 0
        else
          result[i][j] += a[i][k] * b[k][j]
        end
      end
    end
  end
  result
end

#matrix_multiply_element_wise(a, b) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/qoa/matrix_helpers.rb', line 42

def matrix_multiply_element_wise(a, b)
  a.zip(b).map do |r1, r2|
    if r1.nil? || r2.nil?
      nil
    else
      r1.zip(r2).map do |x, y|
        x.nil? || y.nil? ? nil : x * y
      end
    end
  end
end

#matrix_pow(matrix, power) ⇒ Object



104
105
106
# File 'lib/qoa/matrix_helpers.rb', line 104

def matrix_pow(matrix, power)
  matrix.map { |row| row.map { |x| x.nil? ? nil : x ** power } }
end

#matrix_subtract(a, b) ⇒ Object



38
39
40
# File 'lib/qoa/matrix_helpers.rb', line 38

def matrix_subtract(a, b)
  a.zip(b).map { |r1, r2| r1.zip(r2).map { |x, y| x - y } }
end

#mean(matrix) ⇒ Object



72
73
74
# File 'lib/qoa/matrix_helpers.rb', line 72

def mean(matrix)
  matrix.map { |row| row.inject(0.0) { |sum, x| sum + x } / row.size }
end

#normalize(matrix, mean, variance) ⇒ Object



80
81
82
# File 'lib/qoa/matrix_helpers.rb', line 80

def normalize(matrix, mean, variance)
  matrix.map.with_index { |row, i| row.map { |x| (x - mean[i]) / Math.sqrt(variance[i] + 1e-8) } }
end

#scalar_add(matrix, scalar) ⇒ Object



108
109
110
# File 'lib/qoa/matrix_helpers.rb', line 108

def scalar_add(matrix, scalar)
  matrix.map { |row| row.map { |x| x.nil? ? nil : x + scalar } }
end

#scalar_multiply(scalar, matrix) ⇒ Object



66
67
68
69
70
# File 'lib/qoa/matrix_helpers.rb', line 66

def scalar_multiply(scalar, matrix)
  matrix.map do |row|
    row.nil? ? nil : row.map { |x| x.nil? ? nil : x * scalar }
  end
end

#scale_and_shift(matrix, gamma, beta) ⇒ Object



84
85
86
# File 'lib/qoa/matrix_helpers.rb', line 84

def scale_and_shift(matrix, gamma, beta)
  matrix.map.with_index { |row, i| row.map { |x| gamma[i] * x + beta[i] } }
end

#transpose(matrix) ⇒ Object



34
35
36
# File 'lib/qoa/matrix_helpers.rb', line 34

def transpose(matrix)
  matrix[0].zip(*matrix[1..-1])
end

#update_beta(beta, gradients) ⇒ Object



94
95
96
97
98
# File 'lib/qoa/matrix_helpers.rb', line 94

def update_beta(beta, gradients)
  beta.each_with_index.map do |b, i|
    b + gradients[i].inject(0.0) { |sum, d| sum + (d.nil? ? 0 : d) }
  end
end

#update_gamma(gamma, normalized, gradients) ⇒ Object



88
89
90
91
92
# File 'lib/qoa/matrix_helpers.rb', line 88

def update_gamma(gamma, normalized, gradients)
  gamma.each_with_index.map do |g, i|
    g + normalized[i].zip(gradients[i]).inject(0.0) { |sum, (n, d)| sum + n * d }
  end
end

#variance(matrix, mean) ⇒ Object



76
77
78
# File 'lib/qoa/matrix_helpers.rb', line 76

def variance(matrix, mean)
  matrix.map.with_index { |row, i| row.inject(0.0) { |sum, x| sum + (x - mean[i]) ** 2 } / row.size }
end