Giter VIP home page Giter VIP logo

cerebrum's Introduction

GEM: cerebrum Gem Version

cerebrum is an implementation of ANNs in Ruby. There's no reason to train a neural network in Ruby, I'm using it to experiment and play around with the bare fundamentals of ANNs, original idea for this project here is currently unmaintained. Extensions on top of that are personal experimentation.

Installation

Add this line to your application's Gemfile:

gem 'cerebrum'

And then execute:

$ bundle

Or install it yourself as:

$ gem install cerebrum

Usage

require 'cerebrum'

network = Cerebrum.new

network.train([
  {input: [0, 0], output: [0]},
  {input: [0, 1], output: [1]},
  {input: [1, 0], output: [1]},
  {input: [1, 1], output: [0]}
])

result = network.run([1, 0])
# => [0.9333206724219677]

Training

Use Cerebrum#train to train the network with an array of training data.

Data format

Each training pattern should have an input: and an output:, both of which can either be an array of numbers from 0 to 1 or a hash of numbers from 0 to 1. An example of the latter is demonstrated below:

network = Cerebrum.new

network.train([
    {input: { r: 0.03, g: 0.7, b: 0.5 }, output: { black: 1 }},
    {input: { r: 0.16, g: 0.09, b: 0.2 }, output: { white: 1 }},
    {input: { r: 0.5, g: 0.5, b: 1.0 }, output: { white: 1 }}
]);

result = network.run({ r: 1, g: 0.4, b: 0 })
# => { black: 0.011967728530458011, white: 0.9871010273923573 }

Cerebrum Options

Cerebrum#new takes a hash of options that would set defaults if not specified in the Cerebrum#train procedure call:

network = Cerebrum.new({
  learning_rate:  0.3,
  momentum:       0.1,
  binary_thresh:  0.5,
  hidden_layers:  [3, 4]
})

Training Options

Cerebrum#train optionally takes in a configuration hash as the second argument:

network.train(data, {
  error_threshold: 0.005,
  iterations:      20000,
  log:             true,
  log_period:      100,
  learning_rate:   0.3
})

The network will train until the training error has gone below the threshold or the max number of iterations has been reached, whichever comes first.

By default training won't let you know how its doing until the end, but set log to true to get periodic updates on the current training error of the network. The training error should decrease every time. The updates will be printed to console. If you set log to a function, this function will be called with the updates instead of printing to the console.

The learning_rate is a parameter that influences how quickly the network trains, a number from 0 to 1. If the learning rate is close to 0 it will take longer to train. If the learning rate is closer to 1 it will train faster but it's in danger of training to a local minimum and performing badly on new data.

Output

The output of Cerebrum#train is a hash of information about how the training went:

network.train(data, options)
# => { error: 0.005324233132423, iterations: 9001 }

JSON

Serialize or load in the state of a trained network with JSON:

saved_state = network.save_state
nn = Cerebrum.new
nn.load_state(saved_state)

nn.run({ r: 1, g: 0.4, b: 0 })
# => { black: 0.011967728530458011, white: 0.9871010273923573 }

Additionally the new network can be separately trained whilst retaining all previous training from the other network.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at irfansharif.

License

The gem is available as open source under the terms of the MIT License.

cerebrum's People

Contributors

arhamahmed avatar irfansharif avatar jjgh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cerebrum's Issues

Error when trying to set number of hidden layers

Hi, I think you have a little bug in "/lib/cerebrum/cerebrum.rb".

Specifically, in the train method you have:

    hidden_layer_sizes = [ [3, (input_size/2).floor].max ] unless @hidden_layers
    layer_sizes = [input_size, hidden_layer_sizes, output_size].flatten

which causes "hidden_layer_sizes" to be nil when @hidden_layers exists and subsequent methods to fail. Maybe you just forgot to assign it, and you probably wanted something like this:

hidden_layer_sizes = [ [3, (input_size/2).floor].max ] unless hidden_layer_sizes = @hidden_layers

Is it ok if I make a PR ? Cheers.

output to C function

Is possible to create from trained network C function to eval data?
For example I learn my network, and need using it in C program. With all bias,variables etc inside C function not a file or other way.

?

Porting to Crystal

Hi,
Your project is really cool, I used it to quickly test hypothesis of ideas and quick checks, but I think it can be more then that.
If you will like the idea of porting to Crystal https://crystal-lang.org/ , I think the network can be much much quicker and can be use for production needs.

Crystal is really similar to Ruby in syntax, so the need for changes should be minimal , if you like that idea and want a helping hand ping me :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.