Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AI and Deep Learning by (50.2k points)

I am trying to train a feedforward network to work to perform an XOR operation with the Ruby Library AI4R. However, when I evaluate the XOR after training it. I am not getting the correct output. Has anyone used this library before and gotten it to learn the XOR operation.

I am using two input neurons, three neurons in a hidden layer, and one layer for the output, as I saw a precomputed XOR feed-forward neural network like this 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

1 Answer

0 votes
by (107k points)

You have to train it for more iterations. If you change 400.times to 8000.times you'll come much closer (and closer still at 20000.times).

At 20000.times, you will get

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

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,1: #{net.eval([1,1])}"  # => evaluate 1,1: 0.0268317078331645

You can also slightly increase net.learning_rate for better output.

If you wish to know about Neural Network then visit this Neural Network Tutorial.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...