Training Neural Network for XOR in Ruby

I am trying to prepare a direct-connect network to work for XOR operations with the Ruby AI4R library. However, when I evaluate XOR after his training. I do not get the correct result. Has anyone used this library before and got it to learn about the XOR operation.

I use two input neurons, three neurons in a hidden layer and one level for output, since I saw a pre-computed XOR feed forward to the neural network, as it was before.

require "rubygems" require "ai4r" # Create the network with: # 2 inputs # 1 hidden layer with 3 neurons # 1 outputs net = Ai4r::NeuralNetwork::Backpropagation.new([2, 3, 1]) example = [[0,0],[0,1],[1,0],[1,1]] result = [[0],[1],[1],[0]] # Train the network 400.times do |i| j = i % result.length puts net.train(example[j], result[j]) end # Use it: Evaluate data with the trained network puts "evaluate 0,0: #{net.eval([0,0])}" # => evaluate 0,0: 0.507531383375123 puts "evaluate 0,1: #{net.eval([0,1])}" # => evaluate 0,1: 0.491957823618629 puts "evaluate 1,0: #{net.eval([1,0])}" # => evaluate 1,0: 0.516413912471401 puts "evaluate 1,1: #{net.eval([1,1])}" # => evaluate 1,1: 0.500197884691668 

Ted

+6
ruby artificial-intelligence machine-learning neural-network
source share
2 answers

You have not prepared it for enough iterations. If you change 400.times to 8000.times , you will be much closer (and even closer to 20000.times ).

At 20000.times , I get

 puts "evaluate 0,0: #{net.eval([0,0])}" # => evaluate 0,0: 0.030879848321403 puts "evaluate 0,1: #{net.eval([0,1])}" # => evaluate 0,1: 0.97105714994505 puts "evaluate 1,0: #{net.eval([1,0])}" # => evaluate 1,0: 0.965055940880282 puts "evaluate 1,1: #{net.eval([1,1])}" # => evaluate 1,1: 0.0268317078331645 

You can also increase net.learning_rate (but not too much).

+5
source share

If you want to consider Neuroevolution, you can check out the neuroevo . Run the specs to see it install XOR in 15 iterations ( [2,2,1] direct-link network, XNES optimizer):

https://github.com/giuse/neuroevo/blob/master/spec/solver_spec.rb

Full disclosure: I'm a developer (hello there!).
I recently started publishing my code and looking for feedback.

+2
source share

All Articles