28 lines
727 B
Ruby
28 lines
727 B
Ruby
def hamming_layer(pattern_vectors, input_vec)
|
|
n = input_vec.length
|
|
pattern_vectors.map do |pv|
|
|
dot = pv.zip(input_vec).sum { |a, b| a * b }
|
|
(dot.to_f + n) / 2.0
|
|
end
|
|
end
|
|
|
|
def maxnet_step(outputs, eps)
|
|
outputs.map.with_index do |val, i|
|
|
inhibition = eps * outputs.each_with_index.sum { |v, j| j == i ? 0.0 : v }
|
|
[val - inhibition, 0.0].max
|
|
end
|
|
end
|
|
|
|
MAX_MAXNET_ITER = 100
|
|
|
|
def recall_hamming(pattern_vectors, input_vec)
|
|
eps = 0.9 / pattern_vectors.length
|
|
outputs = hamming_layer(pattern_vectors, input_vec)
|
|
iter = 0
|
|
while outputs.count { |v| v > 1e-9 } > 1 && iter < MAX_MAXNET_ITER
|
|
outputs = maxnet_step(outputs, eps)
|
|
iter += 1
|
|
end
|
|
outputs.each_with_index.max_by { |v, _| v }[1]
|
|
end
|