Created
March 24, 2013 20:47
-
-
Save jquave/5233445 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'ruby_fann/neural_network' | |
# Create Training data with 2 each of inputs(array of 3) & desired outputs(array of 1). | |
# | |
inputs = [] | |
(1..100000).each do |i| | |
inputs << [(Random.rand(100)/100.0).round(1), (Random.rand(100)/100.0).round(1), (Random.rand(100)/100.0).round(1)] | |
end | |
def process(set) | |
#puts 'test: '+set[0].to_s | |
(set[0] + set[1] + set[2])/3.0 | |
end | |
outputs = Array.new | |
inputs.each do |i| | |
outputs << [process(i)] | |
end | |
training_data = RubyFann::TrainData.new( | |
:inputs=>inputs, | |
:desired_outputs=>outputs) | |
# Create FANN Neural Network to match appropriate training data: | |
fann = RubyFann::Standard.new( | |
:num_inputs=>3, | |
:hidden_neurons=>[3,2], | |
:num_outputs=>1) | |
# Training using data created above: | |
fann.train_on_data(training_data, 500, 100, 1e-5) | |
test_values = inputs[1] | |
# Run with different input data: | |
puts fann.run(test_values) | |
puts 'Result should be: ' + process(test_values).to_s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment