Class: Ocarina::Network
Overview
a network of neurons
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#current_error ⇒ Object
Returns the value of attribute current_error.
Class Method Summary collapse
-
.load_network_from_file(filepath) ⇒ Object
load a previously-trained network.
Instance Method Summary collapse
-
#initialize(config) ⇒ Network
constructor
A new instance of Network.
-
#recognize(image) ⇒ Object
Attempt to recognize the character displayed on the given image.
-
#save_network_to_file(filepath) ⇒ Object
persist the network.
-
#train(image, target_char) ⇒ Object
Train the network on the image, using target_char as the expected result.
Methods included from Util
#char_to_binary_string, #filename_for_noise_image, #filename_for_quantized_image, #filename_for_training_image, #int_to_binary_string, #is_lower?, #is_upper?, #noise_image_for_char, #pixel_number_to_col, #pixel_number_to_row, #pixel_to_bit, #quantize_image, #reference_image_for_char, #sigma
Constructor Details
#initialize(config) ⇒ Network
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/ocarina/network.rb', line 13 def initialize(config) @config = config @num_inputs = config.num_inputs # total of bits in the image #@hidden_count = (1.5 * num_inputs).to_i # somewhat arbitrary @hidden_count = 25 @input_values = [] # image bits @input_weights = [] # weights from inputs -> hidden nodes @hidden_outputs = [] # after feed-forward, what the hidden nodes output @output_weights = [] # weights from hidden nodes -> output nodes @output_values = [] # after feed-forward, what the output nodes output @output_errors = [] @hidden_errors = [] assign_random_weights #puts "@input_weights: #{@input_weights}" total_input_weights = @input_weights.map { |array| array.reduce(:+) }.reduce(:+) puts "total_input_weights: #{total_input_weights}" total_output_weights = @output_weights.map { |array| array.reduce(:+) }.reduce(:+) puts "total_output_weights: #{total_output_weights}" end |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
11 12 13 |
# File 'lib/ocarina/network.rb', line 11 def config @config end |
#current_error ⇒ Object
Returns the value of attribute current_error.
11 12 13 |
# File 'lib/ocarina/network.rb', line 11 def current_error @current_error end |
Class Method Details
.load_network_from_file(filepath) ⇒ Object
load a previously-trained network
101 102 103 104 105 |
# File 'lib/ocarina/network.rb', line 101 def self.load_network_from_file(filepath) File.open(filepath) do |file| Marshal.load(file) end end |
Instance Method Details
#recognize(image) ⇒ Object
Attempt to recognize the character displayed on the given image. image should be an instance of Magick::Image.
Returns the integer ASCII code for the recognized character.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ocarina/network.rb', line 48 def recognize(image) # quantize to two-color image = quantize_image(image) # the binary string we expect to see from the output nodes assign_inputs image calculate_hidden_outputs calculate_final_outputs #@output_values.each.with_index { |v, i| puts "index: #{i} => #{v}" } # process results # binary_string = quantized_result.inject("") { |accum, bit| "#{accum}#{bit.to_s}" } binary_string.to_i(2) end |
#save_network_to_file(filepath) ⇒ Object
persist the network
93 94 95 96 97 |
# File 'lib/ocarina/network.rb', line 93 def save_network_to_file(filepath) File.open(filepath,'w') do|file| Marshal.dump(self, file) end end |
#train(image, target_char) ⇒ Object
Train the network on the image, using target_char as the expected result.
image should be an instance of Magick::Image.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ocarina/network.rb', line 70 def train(image, target_char) # quantize to two-color image = quantize_image(image) #image.write(filename_for_quantized_image(target_char, 'gif')) # the binary string we expect to see from the output nodes @target_binary_string = char_to_binary_string(target_char) assign_inputs image calculate_hidden_outputs calculate_final_outputs calculate_output_errors calculate_hidden_errors # process results # adjust_output_weights adjust_input_weights end |