Sha256: 3ce2d0d104f76ffb765f09aac030b45d6e0b9bc8c7e025ea4e479260125b30a1
Contents?: true
Size: 1.31 KB
Versions: 3
Compression:
Stored size: 1.31 KB
Contents
require 'test/unit' require 'neuro' class TestParity < Test::Unit::TestCase include Neuro MAX_BITS = 4 def setup filename = 'test_parity.rb.dump' if File.exist?(filename) File.open(filename) do |f| @network = Network.load(f) end else @network = Network.new(MAX_BITS, MAX_BITS * 2, 1) @network.debug = STDERR @network.debug_step = 1000 eta = 0.2 max_error = 1.0E-6 vectors = all_vectors max_count = vectors.size * 10 count = max_count until count < max_count count = 0 vectors.sort_by { rand }.each do |sample| desired = [ parity(sample) == 1 ? 0.9 : 0.1 ] count += @network.learn(sample, desired, max_error, eta) end end File.open(filename, 'wb') do |f| @network.dump(f) end end end def parity(vector) (vector.inject(1) { |s,x| s * (x < 0.5 ? -1 : 1) }) < 0 ? 1 : 0 end def all_vectors vectors = [] for x in 0...(2 ** MAX_BITS) vectors << (0...MAX_BITS).map { |i| x[i].zero? ? 0.0 : 1.0 }.reverse end vectors end def test_parities all_vectors.each do |vector| result, = @network.decide vector result_parity = result > ((0.9 - 0.1) / 2) ? 1 : 0 assert_equal parity(vector), result_parity end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
neuro-0.4.3 | tests/test_parity.rb |
neuro-0.4.2 | tests/test_parity.rb |
neuro-0.4.1 | tests/test_parity.rb |