Brainz and Neural Networks

By Loren Segal on August 08, 2009 at 823:653:416 PM

Last night I toyed around with an implementation of a very simple Artificial Neural Network that could calculate the result of simple bitwise operations (AND, OR, XOR) written in C#. Today I ported that code to Ruby and released a library called Brainz. It’s extremely simplistic, undertested and probably really inefficient, but it was really fun. If you don’t know much about neural networks, this is from the readme:

Brainz is a Artificial Neural Network (ANN) library written by Loren Segal. Neural networks are generally used in pattern recognition, signal processing and other data intensive processing problems. ANN’s benefit by not having to explicitly define the procedural steps involved in the problem, but rather by training the neural network to return the correct output for the respective inputs. This means that the same neural network can be applied to many different problem sets without much (sometimes any) modification, and therefore make a good general solution to a large set of problem domains. The drawback, however, is that these neural networks require large sets of data to be trained and this training process can be processor intensive.

AND, OR, BUT…

Want to see some code? This is also from the readme:

# Define a 2-2-1 neural network
net = Brainz::Network.new(2, 2, 1) 

# We must train the system first
1000.times do 
  net.train([0, 0], [0])
  net.train([0, 1], [0])
  net.train([1, 0], [0])
  net.train([1, 1], [1])
end

# Now some tests:
p net.run([0, 1]).map(&:round) # => [0]
p net.run([1, 1]).map(&:round) # => [1]

With a small modification to the training data, you can reprogram that network to calculate any other bit operation with no actual modifications to the algorithm. ScAIry.

Questions? Comments? Follow me on Twitter (@lsegal) or email me.