examples/neural_network/xor_example.rb in ai4r-1.3 vs examples/neural_network/xor_example.rb in ai4r-1.4

- old
+ new

@@ -1,25 +1,35 @@ -require File.dirname(__FILE__) + '/training_patterns' -require File.dirname(__FILE__) + '/patterns_with_noise' -require File.dirname(__FILE__) + '/patterns_with_base_noise' +# Author:: Sergio Fierens +# License:: MPL 1.1 +# Project:: ai4r +# Url:: http://ai4r.rubyforge.org/ +# +# You can redistribute it and/or modify it under the terms of +# the Mozilla Public License version 1.1 as published by the +# Mozilla Foundation at http://www.mozilla.org/MPL/MPL-1.1.txt + require File.dirname(__FILE__) + '/../../lib/ai4r/neural_network/backpropagation' +require 'benchmark' -examples = [ - [[0, 0], [0, 1]], - [[0, 1], [1, 0]], - [[1, 0], [1, 0]], - [[1, 1], [0, 0]] -] +times = Benchmark.measure do -net = Ai4r::NeuralNetwork::Backpropagation.new([2, 1, 2, 1]) - -i=0 -200.times { - examples.each do |ex| - 2000.times {net.train(ex[0], [ex[1].first])} + srand 1 + + net = Ai4r::NeuralNetwork::Backpropagation.new([2, 2, 1]) + + puts "Training the network, please wait." + 2001.times do |i| + net.train([0,0], [0]) + net.train([0,1], [1]) + net.train([1,0], [1]) + error = net.train([1,1], [0]) + puts "Error after iteration #{i}:\t#{error}" if i%200 == 0 end - puts(i=i+1) -} + + puts "Test data" + puts "[0,0] = > #{net.eval([0,0]).inspect}" + puts "[0,1] = > #{net.eval([0,1]).inspect}" + puts "[1,0] = > #{net.eval([1,0]).inspect}" + puts "[1,1] = > #{net.eval([1,1]).inspect}" +end -examples.each do |ex| - print ex[0], ' => ', net.eval(ex[0]).inspect, ', should be ', ex[1].first, "\n" -end + puts "Elapsed time: #{times}" \ No newline at end of file