brainz 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Vilius Luneckas
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest ADDED
@@ -0,0 +1,23 @@
1
+ LICENSE
2
+ README.rdoc
3
+ Rakefile
4
+ brainz.gemspec
5
+ examples/and.rb
6
+ examples/sign.rb
7
+ examples/xor.rb
8
+ lib/brainz.rb
9
+ lib/brainz/backpropagation.rb
10
+ lib/brainz/brainz.rb
11
+ lib/brainz/layer.rb
12
+ lib/brainz/loader.rb
13
+ lib/brainz/network.rb
14
+ lib/brainz/neuron.rb
15
+ lib/brainz/synapse.rb
16
+ lib/brainz/version.rb
17
+ lib/ext/array.rb
18
+ spec/array_spec.rb
19
+ spec/brainz_spec.rb
20
+ spec/loader_spec.rb
21
+ spec/spec_helper.rb
22
+ spec/temp/brainz.b
23
+ Manifest
data/README.rdoc ADDED
@@ -0,0 +1,20 @@
1
+ = brainz
2
+
3
+ Artificial Neural Network Library for Ruby programming language.
4
+
5
+ == Supported algorithms
6
+ * Backpropagation
7
+
8
+ == Contributing to brainz
9
+
10
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
11
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
12
+ * Fork the project
13
+ * Start a feature/bugfix branch
14
+ * Commit and push until you are happy with your contribution
15
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
16
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
17
+
18
+ == Copyright
19
+
20
+ Copyright (c) 2011 Vilius Luneckas. See LICENSE for further details.
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rake'
4
+ require 'echoe'
5
+ require './lib/brainz/version.rb'
6
+
7
+ Echoe.new('brainz', Brainz::VERSION) do |gem|
8
+ gem.description = "Simple artificial intelligence"
9
+ gem.url = "https://github.com/ViliusLuneckas/brainz/"
10
+ gem.email = "vilius.luneckas@gmail.com"
11
+ gem.author = "Vilius Luneckas"
12
+ gem.development_dependencies = ['rspec', 'mocha']
13
+ end
data/brainz.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "brainz"
5
+ s.version = "0.1.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Vilius Luneckas"]
9
+ s.date = "2012-03-20"
10
+ s.description = "Simple artificial intelligence"
11
+ s.email = "vilius.luneckas@gmail.com"
12
+ s.extra_rdoc_files = ["LICENSE", "README.rdoc", "lib/brainz.rb", "lib/brainz/backpropagation.rb", "lib/brainz/brainz.rb", "lib/brainz/layer.rb", "lib/brainz/loader.rb", "lib/brainz/network.rb", "lib/brainz/neuron.rb", "lib/brainz/synapse.rb", "lib/brainz/version.rb", "lib/ext/array.rb"]
13
+ s.files = ["LICENSE", "README.rdoc", "Rakefile", "brainz.gemspec", "examples/and.rb", "examples/sign.rb", "examples/xor.rb", "lib/brainz.rb", "lib/brainz/backpropagation.rb", "lib/brainz/brainz.rb", "lib/brainz/layer.rb", "lib/brainz/loader.rb", "lib/brainz/network.rb", "lib/brainz/neuron.rb", "lib/brainz/synapse.rb", "lib/brainz/version.rb", "lib/ext/array.rb", "spec/array_spec.rb", "spec/brainz_spec.rb", "spec/loader_spec.rb", "spec/spec_helper.rb", "spec/temp/brainz.b", "Manifest"]
14
+ s.homepage = "https://github.com/ViliusLuneckas/brainz/"
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Brainz", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = "brainz"
18
+ s.rubygems_version = "1.8.19"
19
+ s.summary = "Simple artificial intelligence"
20
+
21
+ if s.respond_to? :specification_version then
22
+ s.specification_version = 3
23
+
24
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
25
+ s.add_development_dependency(%q<rspec>, [">= 0"])
26
+ s.add_development_dependency(%q<mocha>, [">= 0"])
27
+ else
28
+ s.add_dependency(%q<rspec>, [">= 0"])
29
+ s.add_dependency(%q<mocha>, [">= 0"])
30
+ end
31
+ else
32
+ s.add_dependency(%q<rspec>, [">= 0"])
33
+ s.add_dependency(%q<mocha>, [">= 0"])
34
+ end
35
+ end
data/examples/and.rb ADDED
@@ -0,0 +1,18 @@
1
+ require_relative '../lib/brainz'
2
+
3
+ # AND problem
4
+
5
+ brainz = Brainz::Brainz.new
6
+
7
+ brainz.teach do |iteration, error|
8
+ that(1, 1).is(1)
9
+ that(1, 0).is(0)
10
+ that(0, 1).is(0)
11
+ that(0, 0).is(0)
12
+ p "error_rate = #{'%.3f' % error || 0 } after #{iteration} iterations" if iteration % 10 == 0
13
+ end
14
+
15
+ puts "0 and 0 = #{brainz.guess(a: 0, b: 0)}, #{brainz.guess(a: 0, b: 0) == 0}"
16
+ puts "0 and 1 = #{brainz.guess(a: 0, b: 1)}, #{brainz.guess(a: 0, b: 1) == 0}"
17
+ puts "1 and 1 = #{brainz.guess(a: 1, b: 1)}, #{brainz.guess(a: 1, b: 1) == 1}"
18
+ puts "1 and 0 = #{brainz.guess(a: 1, b: 0)}, #{brainz.guess(a: 1, b: 0) == 0}"
data/examples/sign.rb ADDED
@@ -0,0 +1,27 @@
1
+ require_relative '../lib/brainz'
2
+
3
+ # SIGN function
4
+
5
+ brainz = Brainz::Brainz.new
6
+
7
+ brainz.teach do |iteration, error|
8
+ that(1).is(1)
9
+ that(10).is(1)
10
+ that(90).is(1)
11
+ that(-1).is(-1)
12
+ that(-20).is(-1)
13
+ that(-4).is(-1)
14
+ that(0).is(0)
15
+ p "error_rate = #{'%.3f' % error || 0 } after #{iteration} iterations"
16
+ end
17
+
18
+ (-100..100).each do |test_value|
19
+ puts "Error with (#{test_value}) " if brainz.guess(test_value) != (test_value > 0 ? 1 : test_value < 0 ? -1 : 0)
20
+ end
21
+
22
+ puts "sign(0) = #{brainz.guess(0)}, #{brainz.guess(0) == 0}"
23
+ puts "sign(-1) = #{brainz.guess(-1)}, #{brainz.guess(-1) == -1}"
24
+ puts "sign(1) = #{brainz.guess(1)}, #{brainz.guess(1) == 1}"
25
+ puts "sign(22) = #{brainz.guess(22)}, #{brainz.guess(22) == 1}"
26
+ puts "sign(-100) = #{brainz.guess(-100)}, #{brainz.guess(-100) == -1}"
27
+ puts "sign(6) = #{brainz.guess(6)}, #{brainz.guess(6) == 1}"
data/examples/xor.rb ADDED
@@ -0,0 +1,14 @@
1
+ require_relative '../lib/brainz'
2
+
3
+ # XOR problem
4
+
5
+ brainz = Brainz::Brainz.new
6
+
7
+ brainz.teach(learning_rate: 0.2, momentum: 0.01, wanted_error: 0.01, max_iterations: 2000) do |iteration, error|
8
+ that(a: 1, b: 1).is(0)
9
+ that(1, 0).is(1)
10
+ that(0, 1).is(1)
11
+ that(0, 0).is(0)
12
+ end
13
+
14
+ p "Learning took #{brainz.last_iterations}, error: #{brainz.error}, time: #{brainz.learning_time} s."
data/lib/brainz.rb ADDED
@@ -0,0 +1,10 @@
1
+ require_relative 'ext/array.rb'
2
+
3
+ require_relative 'brainz/version.rb'
4
+ require_relative 'brainz/backpropagation.rb'
5
+ require_relative 'brainz/synapse.rb'
6
+ require_relative 'brainz/neuron.rb'
7
+ require_relative 'brainz/layer'
8
+ require_relative 'brainz/network'
9
+ require_relative 'brainz/brainz.rb'
10
+ require_relative 'brainz/loader.rb'
@@ -0,0 +1,17 @@
1
+ module Brainz
2
+ module Algorithms
3
+ module Backpropagation
4
+ def update(input)
5
+ @network ||= ::Brainz::Network.new(
6
+ @num_input, [num_hidden, @num_output * 2], @num_output, momentum: momentum, learning_rate: learning_rate
7
+ )
8
+ @network.update(input)
9
+ self
10
+ end
11
+
12
+ def fix_weights(targets)
13
+ @network.fix_weights(targets)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,147 @@
1
+ module Brainz
2
+ class Brainz
3
+ attr_reader :network
4
+ attr_accessor :max_iterations, :wanted_error, :momentum, :learning_rate
5
+ attr_accessor :input_order, :output_order, :input
6
+
7
+ attr_writer :num_hidden
8
+ attr_reader :num_output, :num_input
9
+
10
+ attr_reader :last_iterations
11
+
12
+ def self.current
13
+ @@current
14
+ end
15
+
16
+ def initialize(algorithm = :backpropagation)
17
+ self.extend ::Brainz::Algorithms.const_get(algorithm.to_s.capitalize)
18
+ end
19
+
20
+ def learning_options(options)
21
+ options.each { |option, value| send("#{option}=", value) if respond_to? ("#{option}=")}
22
+ end
23
+
24
+ def num_hidden
25
+ @num_hidden || (num_input + num_output) * 2 / 3
26
+ end
27
+
28
+ def learning_cycle
29
+ old_error = 100.0
30
+ @last_iterations = 0
31
+ max_iterations.times do |i|
32
+ @last_iterations += 1
33
+ if @network
34
+ old_error = @network.mse
35
+ @network.mse = 0
36
+ end
37
+
38
+ yield(i, old_error)
39
+
40
+ break if @network.mse <= wanted_error
41
+ end
42
+ end
43
+
44
+ def learning_time
45
+ @learning_ended - @learning_started
46
+ end
47
+
48
+ def teach(options = {})
49
+ @@current = self
50
+ @learning_started = Time.new
51
+
52
+ learning_options({momentum: 0.15, learning_rate: 0.5, wanted_error: 0.02, max_iterations: 1000}.merge(options))
53
+
54
+ learning_cycle do |iteration, error|
55
+ yield(iteration, error)
56
+ end
57
+ @learning_ended = Time.new
58
+ end
59
+
60
+ def format_input(*args)
61
+ if args.first.is_a?(Hash)
62
+ hash = args.first
63
+ if input_order
64
+ input_order.collect { |key| hash[key] }
65
+ else
66
+ self.input_order = hash.keys
67
+ hash.values
68
+ end
69
+ elsif args.any?
70
+ args
71
+ end
72
+ end
73
+
74
+ def that(*args)
75
+ self.input = format_input(*args)
76
+ self
77
+ end
78
+
79
+ def format_output(*args)
80
+ if args.first.is_a?(Hash)
81
+ hash = args.first
82
+ if output_order
83
+ output_order.collect { |key| hash[key] }
84
+ else
85
+ self.output_order = hash.keys
86
+ hash.values
87
+ end
88
+ else
89
+ args
90
+ end
91
+ end
92
+
93
+ def is(*args)
94
+ unless input.nil?
95
+ output = format_output(*args)
96
+
97
+ @num_output ||= output.length
98
+
99
+ @num_input ||= input.size
100
+
101
+ update(input)
102
+ fix_weights(output)
103
+ end
104
+ end
105
+
106
+ def evaluate(*args)
107
+ input = format_input(*args)
108
+ update(input)
109
+ end
110
+
111
+ def explain(*args)
112
+ evaluate(*args)
113
+ output_order ? output_act.to_hash(output_order) : output_act
114
+ end
115
+
116
+ def output_act
117
+ @network.output.neurons.collect(&:activation)
118
+ end
119
+
120
+ def error
121
+ @network.mse
122
+ end
123
+
124
+ def guess(*args)
125
+ evaluate(*args)
126
+ if num_output == 1
127
+ output_act.first.round
128
+ else
129
+ max = output_act.max
130
+ unless output_order
131
+ a = 'a'
132
+ self.output_order = [:a] + (num_output - 1).times.collect { a.succ!.to_sym }
133
+ end
134
+ output_order[output_act.index(max)]
135
+ end
136
+ end
137
+ end
138
+ end
139
+
140
+
141
+ def self.method_missing(meth = nil, *args, &block)
142
+ if meth == :that
143
+ Brainz::Brainz.current.send(meth, *args)
144
+ else
145
+ raise NoMethodError
146
+ end
147
+ end
@@ -0,0 +1,78 @@
1
+ module Brainz
2
+ class Layer
3
+ attr_reader :neurons, :next_layer, :prev_layer, :network
4
+ attr_accessor :mse
5
+
6
+ def initialize(size, network)
7
+ @network = network
8
+ @neurons = Array.new(size) { Neuron.new(self) }
9
+ @mse = 0
10
+ end
11
+
12
+ def momentum
13
+ network.momentum
14
+ end
15
+
16
+ def learning_rate
17
+ network.learning_rate
18
+ end
19
+
20
+ def link_to(layer)
21
+ @next_layer = layer
22
+ neurons.each do |my_neuron|
23
+ layer.neurons.each do |next_neuron|
24
+ ::Brainz::Synapse.link(my_neuron, next_neuron)
25
+ end
26
+ end
27
+
28
+ layer.back_link(self)
29
+ end
30
+
31
+ def back_link(layer)
32
+ @prev_layer = layer
33
+ end
34
+
35
+ def update_forward
36
+ if next_layer
37
+ next_layer.reset
38
+ neurons.each(&:send_signals)
39
+ next_layer.activate
40
+ next_layer.update_forward
41
+ end
42
+ end
43
+
44
+ def activate
45
+ neurons.each(&:activate)
46
+ end
47
+
48
+ def calculate_deltas
49
+ if prev_layer
50
+ neurons.each(&:calculate_delta)
51
+ prev_layer.calculate_deltas
52
+ end
53
+ end
54
+
55
+ def reset
56
+ neurons.each(&:reset)
57
+ end
58
+
59
+ def update(*values)
60
+ values.each_with_index do |value, index|
61
+ neurons[index].output = value
62
+ end
63
+ end
64
+
65
+ def adjust_weights
66
+ neurons.each(&:adjust_weights)
67
+ prev_layer.adjust_weights if prev_layer
68
+ end
69
+
70
+ def calculate_mse(targets)
71
+ mse = 0
72
+ neurons.each_with_index do |neuron, index|
73
+ mse += 0.5 * (targets[index] - neuron.activation) ** 2
74
+ end
75
+ mse
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,11 @@
1
+ module Brainz
2
+ class Brainz
3
+ def self.load(file_name)
4
+ Marshal.load(File.read(file_name))
5
+ end
6
+
7
+ def save(file_name)
8
+ File.open(file_name, 'wb') { |file| file.write(Marshal.dump(self)) }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,49 @@
1
+ module Brainz
2
+ class Network
3
+ # layers
4
+ attr_reader :input, :hidden, :output
5
+
6
+ attr_accessor :training_algorithm, :learning_rate, :momentum, :mse
7
+
8
+ def initialize(input_size, hidden_sizes, output_size, options = {})
9
+ self.training_algorithm = :backpropagation
10
+
11
+ @input = Layer.new(input_size + 1, self)
12
+ @hidden = hidden_sizes.collect { |size| Layer.new(size, self) }
13
+ @output = Layer.new(output_size, self)
14
+
15
+ @learning_rate = options[:learning_rate] || 0.5
16
+ @momentum = options[:momentum] || 0.15
17
+ @mse = 0
18
+
19
+ join_layers
20
+ end
21
+
22
+ def join_layers
23
+ input.link_to(hidden.first)
24
+ hidden.each_with_index { |layer, i| layer.link_to(hidden[i + 1]) if hidden[i + 1] }
25
+ hidden.last.link_to(output)
26
+ end
27
+
28
+ def update(inputs)
29
+ inputs.each_with_index do |value, index|
30
+ input.neurons[index].activation = value
31
+ end
32
+ input.update_forward
33
+ end
34
+
35
+ def calculate_deltas(targets)
36
+ output.neurons.each_with_index do |neuron, index|
37
+ neuron.calculate_delta targets[index]
38
+ end
39
+ self.mse += output.calculate_mse(targets)
40
+
41
+ hidden.last.calculate_deltas
42
+ end
43
+
44
+ def fix_weights(targets)
45
+ calculate_deltas(targets)
46
+ output.adjust_weights
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,67 @@
1
+ module Brainz
2
+ class Neuron
3
+ attr_accessor :activation, :delta, :output_change
4
+ attr_reader :dendrites, :axon_synapses, :sum, :layer
5
+
6
+ def initialize(layer)
7
+ @layer = layer
8
+ @dendrites = []
9
+ @axon_synapses = []
10
+ self.output_change = 0
11
+ self.activation = 0
12
+ reset
13
+ end
14
+
15
+ def learning_rate
16
+ layer.learning_rate
17
+ end
18
+
19
+ def momentum
20
+ layer.momentum
21
+ end
22
+
23
+ def reset
24
+ @sum = 0
25
+ end
26
+
27
+ def add(value)
28
+ @sum += value
29
+ end
30
+
31
+ def activate
32
+ @activation = fi(@sum)
33
+ end
34
+
35
+ def send_signals
36
+ axon_synapses.each { |s| s.to.add(@activation * s.weight) }
37
+
38
+ end
39
+
40
+ def calculate_delta(target = nil)
41
+ if target
42
+ # output layer
43
+ @delta = d_sigmoid(activation) * (target - activation)
44
+ else
45
+ # middle layer
46
+ error = axon_synapses.collect { |synapse| synapse.to.delta * synapse.weight }.inject(:+)
47
+ @delta = d_sigmoid(activation) * error
48
+ end
49
+ end
50
+
51
+ def adjust_weights
52
+ dendrites.each do |synapse|
53
+ change = delta * synapse.from.activation
54
+ synapse.adjust(learning_rate * change + momentum * synapse.change)
55
+ synapse.change = change
56
+ end
57
+ end
58
+
59
+ def fi(x)
60
+ Math.tanh(x)
61
+ end
62
+
63
+ def d_sigmoid(y)
64
+ 1.0 - y ** 2
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,23 @@
1
+ module Brainz
2
+ class Synapse
3
+ attr_reader :from, :to, :weight
4
+ attr_accessor :change
5
+
6
+ def initialize(from, to)
7
+ @weight = Kernel.rand(0.4) - 0.2
8
+ @change = 0
9
+ @from, @to = from, to
10
+ end
11
+
12
+ def adjust(diff)
13
+ @weight += diff
14
+ end
15
+
16
+ def self.link(from, to)
17
+ synapse = Synapse.new(from, to)
18
+ to.dendrites.push(synapse)
19
+ from.axon_synapses.push(synapse)
20
+ end
21
+ Kernel.srand(Time.now.to_i)
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module Brainz
2
+ VERSION = '0.1.1'
3
+ end
data/lib/ext/array.rb ADDED
@@ -0,0 +1,7 @@
1
+ class Array
2
+ def to_hash(keys)
3
+ hash = {}
4
+ keys.zip(self).each { |k, v| hash[k] = v }
5
+ hash
6
+ end
7
+ end
@@ -0,0 +1,9 @@
1
+ require_relative '../spec/spec_helper'
2
+
3
+ describe Array do
4
+ describe "#to_hash" do
5
+ it "should return new hash" do
6
+ [1, 2, 3].to_hash([:a, :b, :c]).should == {a: 1, b: 2, c: 3}
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,131 @@
1
+ require_relative '../spec/spec_helper'
2
+
3
+ describe Brainz::Brainz do
4
+ subject { Brainz::Brainz.new }
5
+
6
+ describe "#that" do
7
+ it "should return itself" do
8
+ subject.that.should == subject
9
+ end
10
+
11
+ it "should reset input if no args" do
12
+ subject.input = {speed: 5}
13
+ subject.that.input.should be_nil
14
+ end
15
+
16
+ it "should change input order" do
17
+ expect {
18
+ subject.that(speed: 15)
19
+ }.to change { subject.input_order }.from(nil).to([:speed])
20
+ end
21
+
22
+ it "should add value to input hash" do
23
+ expect {
24
+ subject.that speed: 100, time: 0.25
25
+ }.to change { subject.input }.from(nil).to([100, 0.25])
26
+ end
27
+
28
+ it "should add unnamed value to input" do
29
+ expect {
30
+ subject.that 100, 0.25
31
+ }.to change { subject.input }.from(nil).to([100, 0.25])
32
+ end
33
+ end
34
+
35
+ describe "#teach" do
36
+ it "should return iteration and error" do
37
+ Kernel.stubs(random: 0)
38
+ last_iteration = last_error = 0
39
+ subject.teach(max_iterations: 2) do |iteration, error|
40
+ subject.that(1, 1).is(1)
41
+ last_iteration, last_error = iteration, error
42
+ end
43
+
44
+ last_iteration.should == 1
45
+ last_error.should be_within(1).of(1)
46
+ end
47
+ end
48
+
49
+ describe "#num_hidden" do
50
+ it "should return 7" do
51
+ subject.stubs(num_input: 5, num_output: 6)
52
+ subject.num_hidden.should == 7
53
+ end
54
+ end
55
+
56
+ describe "#explain" do
57
+ it "should return output_activation" do
58
+ subject.stubs(output_act: [1], update: nil)
59
+ subject.explain(1, 1).should == [1]
60
+ end
61
+ end
62
+
63
+ describe "#guess" do
64
+ it "should return return rounded value if only one output" do
65
+ subject.stubs(num_output: 1)
66
+ subject.stubs(output_act: [0.5], update: nil)
67
+ subject.guess(1).should == 1
68
+ end
69
+
70
+ it "should return best matching key if more than one output" do
71
+ subject.stubs(output_act: [0.5, 0.7], update: nil, output_order: [:green, :yellow])
72
+ subject.guess([]).should == :yellow
73
+ end
74
+
75
+ it "should return b" do
76
+ subject.stubs(num_output: 2)
77
+ subject.stubs(output_act: [0.5, 0.7], update: nil)
78
+ subject.guess([]).should == :b
79
+ end
80
+ end
81
+
82
+
83
+ describe "#update" do
84
+
85
+ before do
86
+ pending
87
+ subject.stubs(num_hidden: 2, num_output: 1)
88
+ end
89
+
90
+ it "should return subject" do
91
+ subject.update([]).should == subject
92
+ end
93
+
94
+ it "should create weight matrices" do
95
+ Kernel.stubs(rand: 0)
96
+ expect {
97
+ expect {
98
+ subject.update([1.0, 1.0])
99
+ }.to change { subject.input_weights }.from(nil).to([[-0.2, -0.2], [-0.2, -0.2], [-0.2, -0.2]])
100
+ }.to change { subject.output_weights }.from(nil).to([[-2], [-2]])
101
+ end
102
+
103
+ it "should create input weights matrix size of input and hidden" do
104
+ matrix = subject.update([0, 1, 0]).input_weights
105
+ matrix.size.should == 4
106
+ matrix[0].size.should == 2
107
+ end
108
+
109
+ it "should create output weights matrix size of hidden and output" do
110
+ matrix = subject.update([0, 1, 0]).output_weights
111
+ matrix.size.should == 2
112
+ matrix[0].size.should == 1
113
+ end
114
+ end
115
+
116
+ describe "xor" do
117
+ it "should learn xor" do
118
+ subject.teach do
119
+ subject.that(a: 1, b: 1).is(0)
120
+ subject.that(1, 0).is(1)
121
+ subject.that(0, 1).is(1)
122
+ subject.that(0, 0).is(0)
123
+ end
124
+
125
+ subject.explain(a: 0, b: 0).first.round.should == 0
126
+ subject.explain(a: 0, b: 1).first.round.should == 1
127
+ subject.explain(a: 1, b: 1).first.round.should == 0
128
+ subject.explain(a: 1, b: 0).first.round.should == 1
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,31 @@
1
+ require_relative '../spec/spec_helper'
2
+ require 'fileutils'
3
+
4
+ describe "loader" do
5
+ describe "xor" do
6
+ subject { Brainz::Brainz.new }
7
+
8
+ it "should work after load" do
9
+ subject.teach do
10
+ subject.that(a: 1, b: 1).is(0)
11
+ subject.that(1, 0).is(1)
12
+ subject.that(0, 1).is(1)
13
+ subject.that(0, 0).is(0)
14
+ end
15
+
16
+ subject.explain(a: 0, b: 0).first.round.should == 0
17
+ subject.explain(a: 0, b: 1).first.round.should == 1
18
+ subject.explain(a: 1, b: 1).first.round.should == 0
19
+ subject.explain(a: 1, b: 0).first.round.should == 1
20
+
21
+ subject.save(File.expand_path('.', 'temp/brainz.b'))
22
+
23
+
24
+ old_brainz = Brainz::Brainz.load(File.expand_path('.', 'temp/brainz.b'))
25
+ old_brainz.explain(a: 0, b: 0).first.round.should == 0
26
+ old_brainz.explain(a: 0, b: 1).first.round.should == 1
27
+ old_brainz.explain(a: 1, b: 1).first.round.should == 0
28
+ old_brainz.explain(a: 1, b: 0).first.round.should == 1
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,7 @@
1
+ require 'rspec'
2
+ require 'mocha'
3
+ require_relative '../lib/brainz'
4
+
5
+ RSpec.configure do |config|
6
+ config.mock_with :mocha
7
+ end
Binary file
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brainz
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Vilius Luneckas
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: mocha
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Simple artificial intelligence
47
+ email: vilius.luneckas@gmail.com
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files:
51
+ - LICENSE
52
+ - README.rdoc
53
+ - lib/brainz.rb
54
+ - lib/brainz/backpropagation.rb
55
+ - lib/brainz/brainz.rb
56
+ - lib/brainz/layer.rb
57
+ - lib/brainz/loader.rb
58
+ - lib/brainz/network.rb
59
+ - lib/brainz/neuron.rb
60
+ - lib/brainz/synapse.rb
61
+ - lib/brainz/version.rb
62
+ - lib/ext/array.rb
63
+ files:
64
+ - LICENSE
65
+ - README.rdoc
66
+ - Rakefile
67
+ - brainz.gemspec
68
+ - examples/and.rb
69
+ - examples/sign.rb
70
+ - examples/xor.rb
71
+ - lib/brainz.rb
72
+ - lib/brainz/backpropagation.rb
73
+ - lib/brainz/brainz.rb
74
+ - lib/brainz/layer.rb
75
+ - lib/brainz/loader.rb
76
+ - lib/brainz/network.rb
77
+ - lib/brainz/neuron.rb
78
+ - lib/brainz/synapse.rb
79
+ - lib/brainz/version.rb
80
+ - lib/ext/array.rb
81
+ - spec/array_spec.rb
82
+ - spec/brainz_spec.rb
83
+ - spec/loader_spec.rb
84
+ - spec/spec_helper.rb
85
+ - spec/temp/brainz.b
86
+ - Manifest
87
+ homepage: https://github.com/ViliusLuneckas/brainz/
88
+ licenses: []
89
+ post_install_message:
90
+ rdoc_options:
91
+ - --line-numbers
92
+ - --inline-source
93
+ - --title
94
+ - Brainz
95
+ - --main
96
+ - README.rdoc
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '1.2'
111
+ requirements: []
112
+ rubyforge_project: brainz
113
+ rubygems_version: 1.8.19
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: Simple artificial intelligence
117
+ test_files: []