Implement trace functionality for Hopfield and Hamming networks; update Gemfile.lock and app.rb for new dependencies and features
This commit is contained in:
@@ -4,11 +4,12 @@ require_relative 'loader'
|
||||
require_relative 'functions'
|
||||
require_relative 'hopfield'
|
||||
require_relative 'hamming'
|
||||
require_relative 'trace'
|
||||
|
||||
EPS = 0.01
|
||||
|
||||
set :bind, '0.0.0.0'
|
||||
set :port, 4567
|
||||
set :port, 4568
|
||||
set :server, 'webrick'
|
||||
disable :protection
|
||||
set :host_authorization, { allow_if: ->(_env) { true } }
|
||||
@@ -64,6 +65,44 @@ post '/recall_steps' do
|
||||
}.to_json
|
||||
end
|
||||
|
||||
post '/recall_trace' do
|
||||
content_type :json
|
||||
noisy = JSON.parse(request.body.read)['vector'].map(&:to_f)
|
||||
|
||||
hopfield_steps = [noisy.map(&:to_i)]
|
||||
hopfield_nets = []
|
||||
current = noisy.dup
|
||||
MAX_HOPFIELD_ITER.times do
|
||||
net = trace_hopfield_step(WEIGHTS, current)
|
||||
nxt = net.map { |v| v >= 0 ? 1 : -1 }
|
||||
changed = current.each_with_index.count { |v, i| v.to_i != nxt[i] }
|
||||
hopfield_nets << { net: net.map { |v| v.round(3) }, changed: changed }
|
||||
hopfield_steps << nxt
|
||||
break if vectors_equal?(current, nxt)
|
||||
current = nxt
|
||||
end
|
||||
|
||||
eps_val = 0.9 / PATTERN_VECTORS.length
|
||||
outputs = hamming_layer(PATTERN_VECTORS, noisy)
|
||||
hamming_steps = [outputs.map { |v| v.round(2) }]
|
||||
MAX_MAXNET_ITER.times do
|
||||
nxt = maxnet_step(outputs, eps_val)
|
||||
hamming_steps << nxt.map { |v| v.round(2) }
|
||||
break if nxt.count { |v| v > 0 } <= 1
|
||||
outputs = nxt
|
||||
end
|
||||
winner_idx = outputs.each_with_index.max_by { |v, _| v }[1]
|
||||
|
||||
{
|
||||
hopfield_steps: hopfield_steps,
|
||||
hopfield_nets: hopfield_nets,
|
||||
hamming_steps: hamming_steps,
|
||||
pattern_names: PATTERN_NAMES,
|
||||
hamming_winner: PATTERN_NAMES[winner_idx],
|
||||
hamming_trace: hamming_trace(PATTERN_VECTORS, noisy)
|
||||
}.to_json
|
||||
end
|
||||
|
||||
post '/recall' do
|
||||
content_type :json
|
||||
noisy = JSON.parse(request.body.read)['vector'].map(&:to_f)
|
||||
|
||||
Reference in New Issue
Block a user