Skip to content

FANN (Fast Artifical Neural Network) binding in Crystal

License

Notifications You must be signed in to change notification settings

NeuraLegion/crystal-fann

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

crystal-fann

Join the chat at https://gitter.im/crystal-fann/Lobby

Build Status

Crystal bindings for the FANN C lib

Installation

Add this to your application's shard.yml:

dependencies:
  crystal-fann:
    github: NeuraLegion/crystal-fann

Usage

Look at the spec for most functions

ann = Fann::Network::Standard.new(2, [2], 1)
ann.randomize_weights(0.0, 1.0)
3000.times do
  ann.train_single([1.0, 0.0], [0.5])
end
result = ann.run([1.0, 0.0])
# Remember to close the network when done to free allocated C memory
ann.close
(result < [0.55] && result > [0.45]).should be_true
# Work on array of test data (batch)
ann = Fann::Network::Standard.new(2, [3], 1)
input = [[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]]
output = [[0.0], [1.0], [1.0], [0.0]]
train_data = Fann::TrainData.new(input, output)
data = train_data.train_data
ann.randomize_weights(0.0, 1.0)
if data
  ann.train_batch(data, {:max_runs => 8000, :desired_mse => 0.001, :log_each => 1000})
end
result = ann.run([1.0, 1.0])
ann.close
(result < [0.15]).should be_true
# Work on array of test data using the Cascade2 algorithm (no hidden layers, net will build it alone)
ann = Fann::Network::Cascade.new(2, 1)
input = [[0.0, 0.0], [0.0, 1.0], [1.0, 0.0], [1.0, 1.0]]
output = [[0.0], [1.0], [1.0], [0.0]]
train_data = Fann::TrainData.new(input, output)
data = train_data.train_data
ann.train_algorithm(LibFANN::TrainEnum::TrainRprop)
ann.randomize_weights(0.0, 1.0)
if data
  ann.train_batch(data, {:max_neurons => 500, :desired_mse => 0.001, :log_each => 10})
end
result = ann.run([1.0, 1.0])
ann.close
(result < [0.1]).should be_true

Development

All C lib docs can be found here -> http://libfann.github.io/fann/docs/files/fann-h.html

  • Add TrainData class
  • Add network call method to train on train data
  • Add binding to the 'Parallel' binding to work on multi CPU at same time
  • Clean unneeded bindings in the LibFANN binding
  • Add specific Exceptions
  • Add binding and checks for lib errors

I guess more stuff will be added once more people will use it.

Contributing

  1. Fork it ( https://github.com/NeuraLegion/crystal-fann/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors