artificial intelligence - Training neural network for XOR in Ruby -
i trying train feedforward network work perform xor operations ruby library ai4r. however, when evaluate xor after training it. not getting correct output. has used library before , gotten learn xor operation.
i using 2 input neurons, 3 neurons in hidden layer, , 1 layer output, saw precomputed xor feed forward neural network before.
require "rubygems" require "ai4r" # create network with: # 2 inputs # 1 hidden layer 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 network 400.times |i| j = % result.length puts net.train(example[j], result[j]) end # use it: evaluate data 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
you haven't trained enough iterations. if change 400.times
8000.times
you'll come closer (and closer still @ 20000.times
).
at 20000.times
, 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 increase net.learning_rate
(but not much).
Comments
Post a Comment