neuro 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +116 -0
- data/VERSION +1 -0
- data/examples/ocr.rb +125 -0
- data/ext/extconf.rb +6 -0
- data/ext/neuro.c +694 -0
- data/install.rb +22 -0
- data/lib/neuro/display.rb +433 -0
- data/tests/runner.rb +18 -0
- data/tests/test_even_odd.rb +69 -0
- data/tests/test_parity.rb +58 -0
- metadata +59 -0
data/tests/runner.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'test/unit/ui/console/testrunner'
|
4
|
+
require 'test/unit/testsuite'
|
5
|
+
$:.unshift File.expand_path(File.dirname($0))
|
6
|
+
require 'test_parity'
|
7
|
+
require 'test_even_odd'
|
8
|
+
|
9
|
+
class TS_AllTests
|
10
|
+
def self.suite
|
11
|
+
suite = Test::Unit::TestSuite.new
|
12
|
+
suite << TC_Parity.suite
|
13
|
+
suite << TC_EvenOdd.suite
|
14
|
+
suite
|
15
|
+
end
|
16
|
+
end
|
17
|
+
Test::Unit::UI::Console::TestRunner.run(TS_AllTests)
|
18
|
+
# vim: set et sw=2 ts=2:
|
@@ -0,0 +1,69 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
#
|
3
|
+
# Stupid little test, that finds out if 5-bit numbers are even or odd.
|
4
|
+
#
|
5
|
+
require 'test/unit'
|
6
|
+
require 'neuro'
|
7
|
+
|
8
|
+
class TC_EvenOdd < Test::Unit::TestCase
|
9
|
+
include Neuro
|
10
|
+
|
11
|
+
@@even = [
|
12
|
+
[ 0, 0, 1, 1, 0 ],
|
13
|
+
[ 1, 1, 0, 1, 0 ],
|
14
|
+
[ 1, 0, 1, 1, 0 ],
|
15
|
+
[ 0, 1, 0, 1, 0 ],
|
16
|
+
[ 1, 0, 1, 0, 0 ],
|
17
|
+
[ 0, 1, 0, 1, 0 ],
|
18
|
+
]
|
19
|
+
|
20
|
+
@@odd = [
|
21
|
+
[ 1, 0, 0, 1, 1 ],
|
22
|
+
[ 1, 1, 1, 0, 1 ],
|
23
|
+
[ 0, 0, 0, 1, 1 ],
|
24
|
+
[ 0, 1, 0, 0, 1 ],
|
25
|
+
[ 0, 1, 0, 1, 1 ],
|
26
|
+
[ 1, 1, 1, 1, 1 ],
|
27
|
+
]
|
28
|
+
|
29
|
+
def setup
|
30
|
+
@eta = 0.2
|
31
|
+
filename = 'test_even_odd.rb.dump'
|
32
|
+
if File.exist?(filename)
|
33
|
+
File.open(filename) do |f|
|
34
|
+
@network = Network.load(f)
|
35
|
+
end
|
36
|
+
else
|
37
|
+
@network = Network.new(5, 2, 1)
|
38
|
+
@network.debug = STDERR
|
39
|
+
@network.debug_step = 1000
|
40
|
+
max_error = 1.0E-5
|
41
|
+
max_count = (@@even.size + @@odd.size) * 10
|
42
|
+
count = max_count
|
43
|
+
until count < max_count
|
44
|
+
count = 0
|
45
|
+
desired = [ 0.9 ]
|
46
|
+
@@even.each do |sample|
|
47
|
+
count += @network.learn(sample, desired, max_error, @eta)
|
48
|
+
end
|
49
|
+
desired = [ 0.1 ]
|
50
|
+
@@odd.each do |sample|
|
51
|
+
count += @network.learn(sample, desired, max_error, @eta)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
File.open(filename, 'wb') do |f|
|
55
|
+
@network.dump(f)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_hypothesis
|
61
|
+
for x in 0..31
|
62
|
+
vector = (0..4).map { |i| x[i] }.reverse
|
63
|
+
result, = @network.decide vector
|
64
|
+
bit = (result - 0.9).abs < @eta ? 0 : 1
|
65
|
+
assert_equal x[0], bit
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
# vim: set et sw=2 ts=2:
|
@@ -0,0 +1,58 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'neuro'
|
5
|
+
|
6
|
+
class TC_Parity < Test::Unit::TestCase
|
7
|
+
include Neuro
|
8
|
+
|
9
|
+
MAX_BITS = 4
|
10
|
+
def setup
|
11
|
+
filename = 'test_parity.rb.dump'
|
12
|
+
if File.exist?(filename)
|
13
|
+
File.open(filename) do |f|
|
14
|
+
@network = Network.load(f)
|
15
|
+
end
|
16
|
+
else
|
17
|
+
@network = Network.new(MAX_BITS, MAX_BITS * 2, 1)
|
18
|
+
@network.debug = STDERR
|
19
|
+
@network.debug_step = 1000
|
20
|
+
eta = 0.2
|
21
|
+
max_error = 1.0E-6
|
22
|
+
vectors = all_vectors
|
23
|
+
max_count = vectors.size * 10
|
24
|
+
count = max_count
|
25
|
+
until count < max_count
|
26
|
+
count = 0
|
27
|
+
vectors.sort_by { rand }.each do |sample|
|
28
|
+
desired = [ parity(sample) == 1 ? 0.9 : 0.1 ]
|
29
|
+
count += @network.learn(sample, desired, max_error, eta)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
File.open(filename, 'wb') do |f|
|
33
|
+
@network.dump(f)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def parity(vector)
|
39
|
+
(vector.inject(1) { |s,x| s * (x < 0.5 ? -1 : 1) }) < 0 ? 1 : 0
|
40
|
+
end
|
41
|
+
|
42
|
+
def all_vectors
|
43
|
+
vectors = []
|
44
|
+
for x in 0...(2 ** MAX_BITS)
|
45
|
+
vectors << (0...MAX_BITS).map { |i| x[i].zero? ? 0.0 : 1.0 }.reverse
|
46
|
+
end
|
47
|
+
vectors
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_parities
|
51
|
+
all_vectors.each do |vector|
|
52
|
+
result, = @network.decide vector
|
53
|
+
result_parity = result > ((0.9 - 0.1) / 2) ? 1 : 0
|
54
|
+
assert_equal parity(vector), result_parity
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
# vim: set et sw=2 ts=2:
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11
|
3
|
+
specification_version: 1
|
4
|
+
name: neuro
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 0.4.0
|
7
|
+
date: 2005-10-03 00:00:00 +02:00
|
8
|
+
summary: Neural Network Extension for Ruby
|
9
|
+
require_paths:
|
10
|
+
- ext
|
11
|
+
email: flori@ping.de
|
12
|
+
homepage: http://neuro.rubyforge.org
|
13
|
+
rubyforge_project: neuro
|
14
|
+
description: "A Ruby extension that provides a 2-Layer Back Propagation Neural Network, which
|
15
|
+
can be used to categorize datasets of arbitrary size."
|
16
|
+
autorequire: neuro
|
17
|
+
default_executable:
|
18
|
+
bindir: bin
|
19
|
+
has_rdoc: true
|
20
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
21
|
+
requirements:
|
22
|
+
-
|
23
|
+
- ">"
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: 0.0.0
|
26
|
+
version:
|
27
|
+
platform: ruby
|
28
|
+
signing_key:
|
29
|
+
cert_chain:
|
30
|
+
authors:
|
31
|
+
- Florian Frank
|
32
|
+
files:
|
33
|
+
- examples
|
34
|
+
- Rakefile
|
35
|
+
- VERSION
|
36
|
+
- install.rb
|
37
|
+
- ext
|
38
|
+
- lib
|
39
|
+
- tests
|
40
|
+
- examples/ocr.rb
|
41
|
+
- ext/extconf.rb
|
42
|
+
- ext/neuro.c
|
43
|
+
- lib/neuro
|
44
|
+
- lib/neuro/display.rb
|
45
|
+
- tests/runner.rb
|
46
|
+
- tests/test_even_odd.rb
|
47
|
+
- tests/test_parity.rb
|
48
|
+
test_files:
|
49
|
+
- tests/runner.rb
|
50
|
+
rdoc_options:
|
51
|
+
- "--main"
|
52
|
+
- Neuro
|
53
|
+
extra_rdoc_files:
|
54
|
+
- ext/neuro.c
|
55
|
+
executables: []
|
56
|
+
extensions:
|
57
|
+
- ext/extconf.rb
|
58
|
+
requirements: []
|
59
|
+
dependencies: []
|