This is a WIP. This project demonstrates the basics of a neural network. It features a network, layers, neurons and connections.
This is a ruby gem but unpublished from rubygems.org at the moment.
git clone https://github.com/jason-rutherford/rann.git
Then in your project Gemfile:
gem 'rann', path: 'YOUR_PATH_TO_RANN'
require 'rann'
net = Rann::Network.new(size: [2, 1])
trainer = Rann::Trainer.new(net, Rann::Trainer::DATA[:or_gate])
trainer.train({ epochs: 10000 })
puts "Training Data Set: OR Gate"
puts Rann::Trainer::DATA[:or_gate]
puts "Testing Network:"
Rann::Trainer::DATA[:or_gate].each do |sample|
output = net.activate(sample[:input]).first.round
puts "in: #{sample[:input]} out: #{output}"
end
will output
Training Data Set: OR Gate
{:input=>[0, 0], :output=>[0]}
{:input=>[0, 1], :output=>[1]}
{:input=>[1, 0], :output=>[1]}
{:input=>[1, 1], :output=>[1]}
Testing Network:
in: [0, 0] out: 0
in: [0, 1] out: 1
in: [1, 0] out: 1
in: [1, 1] out: 1
This can be useful while developing.
network = Rann::Network.new({size: [1,2,1]})
network.to_s
will print out
Network
Input Layer 70272740914000
Neuron 1 (IN: 0 => OUT: 0)
Bias Neuron 5 (IN: 0 => OUT: 1)
Hidden Layer 70272740913620
Neuron 2 (IN: 0 => OUT: 0)
Neuron 3 (IN: 0 => OUT: 0)
Bias Neuron 6 (IN: 0 => OUT: 1)
Output Layer 70272740913040
Neuron 4 (IN: 0 => OUT: 0)
You send input through your network be sending date through the activate method. When you activate the network it takes your inputs and passes it to the input layer, hidden layer and returns the output layer.
network = Network.new({size: [1,2,1]})
network.activate([1])
activate() returns an array of outputs which can be thought of as the output layer neuron output values. The last line above returns:
[
[0] 0.6626302265192633
]
The activate()
method will return the network output. Alternatively, you can get the current output layer via the output()
method.
network.output
Just like activate()
, this returns an array of output layer neuron output values.
[
[0] 0.6626302265192633
]
You can run the full test suite with:
$ rspec
or for specific files
$ rspec spec/some-file.rb spec/some-other-file.rb
Using guard you can automatically run individual test files as you make changes. Just run guard
from the command-line and it will monitor your .rb files for changes and run tests that match in filename. If you don't have guard install it with gem install guard
.
To reload any changes you make while in irb, you can simply call reload!
. This method is in app.rb
and it searches the project path for ruby files, but it will filter some out, like any in your spec directory.
- Lots of things
- More tests
- Activiation expansion. Support other activation functions? Expand activation thresholds?
- Backpropogation and Training
- I know. Ruby might not be the best language for a fast neural network.
- Thanks to my local dev-coop for meetups
- Special thanks to Levi Thomason (levithomason) for teaching and driving inspiration