Class: Brainz::Brainz
- Inherits:
-
Object
- Object
- Brainz::Brainz
- Defined in:
- lib/brainz/brainz.rb,
lib/brainz/loader.rb
Instance Attribute Summary collapse
-
#input ⇒ Object
Returns the value of attribute input.
-
#input_order ⇒ Object
Returns the value of attribute input_order.
-
#last_iterations ⇒ Object
readonly
Returns the value of attribute last_iterations.
-
#learning_rate ⇒ Object
Returns the value of attribute learning_rate.
-
#max_iterations ⇒ Object
Returns the value of attribute max_iterations.
-
#momentum ⇒ Object
Returns the value of attribute momentum.
-
#network ⇒ Object
readonly
Returns the value of attribute network.
- #num_hidden ⇒ Object
-
#num_input ⇒ Object
readonly
Returns the value of attribute num_input.
-
#num_output ⇒ Object
readonly
Returns the value of attribute num_output.
-
#output_order ⇒ Object
Returns the value of attribute output_order.
-
#wanted_error ⇒ Object
Returns the value of attribute wanted_error.
Class Method Summary collapse
Instance Method Summary collapse
- #error ⇒ Object
- #evaluate(*args) ⇒ Object
- #explain(*args) ⇒ Object
- #format_input(*args) ⇒ Object
- #format_output(*args) ⇒ Object
- #guess(*args) ⇒ Object
-
#initialize(algorithm = :backpropagation) ⇒ Brainz
constructor
A new instance of Brainz.
- #is(*args) ⇒ Object
- #learning_cycle ⇒ Object
- #learning_options(options) ⇒ Object
- #learning_time ⇒ Object
- #output_act ⇒ Object
- #save(file_name) ⇒ Object
- #teach(options = {}) ⇒ Object
- #that(*args) ⇒ Object
Constructor Details
#initialize(algorithm = :backpropagation) ⇒ Brainz
Returns a new instance of Brainz.
16 17 18 |
# File 'lib/brainz/brainz.rb', line 16 def initialize(algorithm = :backpropagation) self.extend ::Brainz::Algorithms.const_get(algorithm.to_s.capitalize) end |
Instance Attribute Details
#input ⇒ Object
Returns the value of attribute input.
5 6 7 |
# File 'lib/brainz/brainz.rb', line 5 def input @input end |
#input_order ⇒ Object
Returns the value of attribute input_order.
5 6 7 |
# File 'lib/brainz/brainz.rb', line 5 def input_order @input_order end |
#last_iterations ⇒ Object (readonly)
Returns the value of attribute last_iterations.
10 11 12 |
# File 'lib/brainz/brainz.rb', line 10 def last_iterations @last_iterations end |
#learning_rate ⇒ Object
Returns the value of attribute learning_rate.
4 5 6 |
# File 'lib/brainz/brainz.rb', line 4 def learning_rate @learning_rate end |
#max_iterations ⇒ Object
Returns the value of attribute max_iterations.
4 5 6 |
# File 'lib/brainz/brainz.rb', line 4 def max_iterations @max_iterations end |
#momentum ⇒ Object
Returns the value of attribute momentum.
4 5 6 |
# File 'lib/brainz/brainz.rb', line 4 def momentum @momentum end |
#network ⇒ Object (readonly)
Returns the value of attribute network.
3 4 5 |
# File 'lib/brainz/brainz.rb', line 3 def network @network end |
#num_hidden ⇒ Object
24 25 26 |
# File 'lib/brainz/brainz.rb', line 24 def num_hidden @num_hidden || (num_input + num_output) * 2 / 3 end |
#num_input ⇒ Object (readonly)
Returns the value of attribute num_input.
8 9 10 |
# File 'lib/brainz/brainz.rb', line 8 def num_input @num_input end |
#num_output ⇒ Object (readonly)
Returns the value of attribute num_output.
8 9 10 |
# File 'lib/brainz/brainz.rb', line 8 def num_output @num_output end |
#output_order ⇒ Object
Returns the value of attribute output_order.
5 6 7 |
# File 'lib/brainz/brainz.rb', line 5 def output_order @output_order end |
#wanted_error ⇒ Object
Returns the value of attribute wanted_error.
4 5 6 |
# File 'lib/brainz/brainz.rb', line 4 def wanted_error @wanted_error end |
Class Method Details
.current ⇒ Object
12 13 14 |
# File 'lib/brainz/brainz.rb', line 12 def self.current @@current end |
.load(file_name) ⇒ Object
3 4 5 |
# File 'lib/brainz/loader.rb', line 3 def self.load(file_name) Marshal.load(File.read(file_name)) end |
Instance Method Details
#error ⇒ Object
120 121 122 |
# File 'lib/brainz/brainz.rb', line 120 def error @network.mse end |
#evaluate(*args) ⇒ Object
106 107 108 109 |
# File 'lib/brainz/brainz.rb', line 106 def evaluate(*args) input = format_input(*args) update(input) end |
#explain(*args) ⇒ Object
111 112 113 114 |
# File 'lib/brainz/brainz.rb', line 111 def explain(*args) evaluate(*args) output_order ? output_act.to_hash(output_order) : output_act end |
#format_input(*args) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/brainz/brainz.rb', line 60 def format_input(*args) if args.first.is_a?(Hash) hash = args.first if input_order input_order.collect { |key| hash[key] } else self.input_order = hash.keys hash.values end elsif args.any? args end end |
#format_output(*args) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/brainz/brainz.rb', line 79 def format_output(*args) if args.first.is_a?(Hash) hash = args.first if output_order output_order.collect { |key| hash[key] } else self.output_order = hash.keys hash.values end else args end end |
#guess(*args) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/brainz/brainz.rb', line 124 def guess(*args) evaluate(*args) if num_output == 1 output_act.first.round else max = output_act.max unless output_order a = 'a' self.output_order = [:a] + (num_output - 1).times.collect { a.succ!.to_sym } end output_order[output_act.index(max)] end end |
#is(*args) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/brainz/brainz.rb', line 93 def is(*args) unless input.nil? output = format_output(*args) @num_output ||= output.length @num_input ||= input.size update(input) fix_weights(output) end end |
#learning_cycle ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/brainz/brainz.rb', line 28 def learning_cycle old_error = 100.0 @last_iterations = 0 max_iterations.times do |i| @last_iterations += 1 if @network old_error = @network.mse @network.mse = 0 end yield(i, old_error) break if @network.mse <= wanted_error end end |
#learning_options(options) ⇒ Object
20 21 22 |
# File 'lib/brainz/brainz.rb', line 20 def () .each { |option, value| send("#{option}=", value) if respond_to? ("#{option}=")} end |
#learning_time ⇒ Object
44 45 46 |
# File 'lib/brainz/brainz.rb', line 44 def learning_time @learning_ended - @learning_started end |
#output_act ⇒ Object
116 117 118 |
# File 'lib/brainz/brainz.rb', line 116 def output_act @network.output.neurons.collect(&:activation) end |
#save(file_name) ⇒ Object
7 8 9 |
# File 'lib/brainz/loader.rb', line 7 def save(file_name) File.open(file_name, 'wb') { |file| file.write(Marshal.dump(self)) } end |
#teach(options = {}) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/brainz/brainz.rb', line 48 def teach( = {}) @@current = self @learning_started = Time.new ({momentum: 0.15, learning_rate: 0.5, wanted_error: 0.02, max_iterations: 1000}.merge()) learning_cycle do |iteration, error| yield(iteration, error) end @learning_ended = Time.new end |
#that(*args) ⇒ Object
74 75 76 77 |
# File 'lib/brainz/brainz.rb', line 74 def that(*args) self.input = format_input(*args) self end |