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 = 500 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 > 0 } <= 1 outputs = next_out end outputs.each_with_index.max_by { |v, _| v }[1] end