Files
lr_miad_nn/hamming.rb
T
2026-05-17 17:26:23 +03:00

30 lines
785 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)
MAX_MAXNET_ITER.times do
next_out = maxnet_step(outputs, eps)
return next_out.each_with_index.max_by { |v, _| v }[1] if next_out.count { |v| v > 1e-9 } <= 1
outputs = next_out
end
outputs.each_with_index.max_by { |v, _| v }[1]
end