neural_network_rb 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0b0d6401b734f3aaf9ceb8fd0f500e5e8b587c90
4
+ data.tar.gz: c050c4adb730e487cbe4602d405d5e609e648808
5
+ SHA512:
6
+ metadata.gz: 2966e79612f5a59b6b78961669ace3ea6181e3709da18f94fdbbdd0501f21d8ad98acb2dfacbee6e7db2d40032ec7824f1bd812339943ea385bb6ca5e25d06dc
7
+ data.tar.gz: 6f8b34eac283b5710018e16fbce57938ca135ea86b13b2d9f776f38f0945b9706c8c19c7a1308109da9f9ad665849a5ea643216d9790781fd5df058d67a1b357
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ *.gz
13
+ *.png
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.1
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in neural_network_rb.gemspec
6
+ gemspec
7
+
8
+ gem 'guard-rspec', require: false
9
+ gem 'terminal-notifier-guard'
10
+ gem 'pry'
data/Gemfile.lock ADDED
@@ -0,0 +1,77 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ neural_network_rb (0.1.0)
5
+ chunky_png (~> 1.3.10)
6
+ numo-narray (~> 0.9.1.1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ chunky_png (1.3.10)
12
+ coderay (1.1.2)
13
+ diff-lcs (1.3)
14
+ ffi (1.9.23)
15
+ formatador (0.2.5)
16
+ guard (2.14.2)
17
+ formatador (>= 0.2.4)
18
+ listen (>= 2.7, < 4.0)
19
+ lumberjack (>= 1.0.12, < 2.0)
20
+ nenv (~> 0.1)
21
+ notiffany (~> 0.0)
22
+ pry (>= 0.9.12)
23
+ shellany (~> 0.0)
24
+ thor (>= 0.18.1)
25
+ guard-compat (1.2.1)
26
+ guard-rspec (4.7.3)
27
+ guard (~> 2.1)
28
+ guard-compat (~> 1.1)
29
+ rspec (>= 2.99.0, < 4.0)
30
+ listen (3.1.1)
31
+ rb-fsevent (>= 0.9.3)
32
+ rb-inotify (>= 0.9.7)
33
+ lumberjack (1.0.12)
34
+ method_source (0.9.0)
35
+ nenv (0.3.0)
36
+ notiffany (0.1.1)
37
+ nenv (~> 0.1)
38
+ shellany (~> 0.0)
39
+ numo-narray (0.9.1.1)
40
+ pry (0.11.3)
41
+ coderay (~> 1.1.0)
42
+ method_source (~> 0.9.0)
43
+ rake (10.5.0)
44
+ rb-fsevent (0.10.3)
45
+ rb-inotify (0.9.10)
46
+ ffi (>= 0.5.0, < 2)
47
+ rspec (3.7.0)
48
+ rspec-core (~> 3.7.0)
49
+ rspec-expectations (~> 3.7.0)
50
+ rspec-mocks (~> 3.7.0)
51
+ rspec-core (3.7.1)
52
+ rspec-support (~> 3.7.0)
53
+ rspec-expectations (3.7.0)
54
+ diff-lcs (>= 1.2.0, < 2.0)
55
+ rspec-support (~> 3.7.0)
56
+ rspec-mocks (3.7.0)
57
+ diff-lcs (>= 1.2.0, < 2.0)
58
+ rspec-support (~> 3.7.0)
59
+ rspec-support (3.7.1)
60
+ shellany (0.0.1)
61
+ terminal-notifier-guard (1.7.0)
62
+ thor (0.20.0)
63
+
64
+ PLATFORMS
65
+ ruby
66
+
67
+ DEPENDENCIES
68
+ bundler (~> 1.16)
69
+ guard-rspec
70
+ neural_network_rb!
71
+ pry
72
+ rake (~> 10.0)
73
+ rspec (~> 3.0)
74
+ terminal-notifier-guard
75
+
76
+ BUNDLED WITH
77
+ 1.16.1
data/Guardfile ADDED
@@ -0,0 +1,53 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec features) \
6
+ # .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}
7
+
8
+ ## Note: if you are using the `directories` clause above and you are not
9
+ ## watching the project directory ('.'), then you will want to move
10
+ ## the Guardfile to a watched dir and symlink it back, e.g.
11
+ #
12
+ # $ mkdir config
13
+ # $ mv Guardfile config/
14
+ # $ ln -s config/Guardfile .
15
+ #
16
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
17
+
18
+ # Note: The cmd option is now required due to the increasing number of ways
19
+ # rspec may be run, below are examples of the most common uses.
20
+ # * bundler: 'bundle exec rspec'
21
+ # * bundler binstubs: 'bin/rspec'
22
+ # * spring: 'bin/rspec' (This will use spring if running and you have
23
+ # installed the spring binstubs per the docs)
24
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
25
+ # * 'just' rspec: 'rspec'
26
+
27
+ guard :rspec, cmd: "bundle exec rspec" do
28
+ require "guard/rspec/dsl"
29
+ dsl = Guard::RSpec::Dsl.new(self)
30
+
31
+ # Feel free to open issues for suggestions and improvements
32
+
33
+ # RSpec files
34
+ rspec = dsl.rspec
35
+ watch(rspec.spec_helper) { rspec.spec_dir }
36
+ watch(rspec.spec_support) { rspec.spec_dir }
37
+ watch(rspec.spec_files)
38
+
39
+ # Ruby files
40
+ ruby = dsl.ruby
41
+ dsl.watch_spec_files_for(ruby.lib_files)
42
+
43
+ # Rails files
44
+ rails = dsl.rails(view_extensions: %w(erb haml slim))
45
+ dsl.watch_spec_files_for(rails.app_files)
46
+ dsl.watch_spec_files_for(rails.views)
47
+
48
+ # Turnip features and steps
49
+ watch(%r{^spec/acceptance/(.+)\.feature$})
50
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
51
+ Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
52
+ end
53
+ end
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # NeuralNetworkRb
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/neural_network_rb`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'neural_network_rb'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install neural_network_rb
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ 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](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/neural_network_rb.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,9 @@
1
+ require 'numo/narray'
2
+ require 'neural_network_rb/narray'
3
+ require 'neural_network_rb/mnist_image'
4
+ require 'neural_network_rb/mnist'
5
+ require "neural_network_rb/version"
6
+ require 'neural_network_rb/softmax'
7
+ require 'neural_network_rb/sigmoid'
8
+ require 'neural_network_rb/error'
9
+ require 'neural_network_rb/neural_network'
@@ -0,0 +1,15 @@
1
+ module NeuralNetworkRb
2
+ class << self
3
+ def l2error(v1, v2)
4
+ ((v1 - v2) ** 2).sum
5
+ end
6
+
7
+ def l1error(v1, v2)
8
+ (v1 - v2).abs.sum
9
+ end
10
+
11
+ def cross_entropy(values, labels)
12
+ - (labels * values.map {|x| Math.log(x)}).sum/values.ndim
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,70 @@
1
+ require "open-uri"
2
+ require 'zlib'
3
+
4
+ module NeuralNetworkRb
5
+
6
+ class MNIST
7
+
8
+ N_FEATURES = 28 * 28
9
+ N_CLASSES = 10
10
+ FILE_NAMES = %w(train-images-idx3-ubyte.gz train-labels-idx1-ubyte.gz t10k-images-idx3-ubyte.gz t10k-labels-idx1-ubyte.gz)
11
+ ROOT_URL = 'http://yann.lecun.com/exdb/mnist/'
12
+
13
+ class << self
14
+ def download!
15
+ FILE_NAMES.each do |name|
16
+ url = "#{ROOT_URL}#{name}"
17
+ fetch_file(name, url)
18
+ end
19
+ self.new
20
+ end
21
+
22
+ private
23
+ def fetch_file(filePath, url)
24
+ File.open(filePath, "w") do |output|
25
+ IO.copy_stream(open(url), output)
26
+ end
27
+ end
28
+ end
29
+
30
+ def training_labels
31
+ labels(FILE_NAMES[1])
32
+ end
33
+
34
+ def test_labels
35
+ labels(FILE_NAMES[3])
36
+ end
37
+
38
+ def training_images
39
+ images(FILE_NAMES[0])
40
+ end
41
+
42
+ def test_images
43
+ images(FILE_NAMES[2])
44
+ end
45
+
46
+ private
47
+ def images(file_name)
48
+ images = []
49
+ Zlib::GzipReader.open(file_name) do |f|
50
+ _, n_images = f.read(8).unpack('N2')
51
+ n_rows, n_cols = f.read(8).unpack('N2')
52
+
53
+ n_images.times do
54
+ images << f.read(n_rows * n_cols).unpack('C*')
55
+ end
56
+ end
57
+ Numo::Int16.cast(images)
58
+ end
59
+
60
+ def labels(file_name)
61
+ labels = nil
62
+ Zlib::GzipReader.open(file_name) do |f|
63
+ _, @n_labels = f.read(8).unpack('N2')
64
+ labels = f.read(@n_labels).unpack('C*')
65
+ end
66
+ Numo::Int16.cast(labels)
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,20 @@
1
+ require 'chunky_png'
2
+
3
+ module NeuralNetworkRb
4
+ class MNISTImage
5
+ def initialize(pixels)
6
+ @pixels = pixels
7
+ end
8
+
9
+ def save_to_file(file_path)
10
+ size = Math.sqrt(@pixels.length).to_i
11
+ square = ChunkyPNG::Canvas.new(size, size, ChunkyPNG::Color::TRANSPARENT)
12
+ size.times do |i|
13
+ size.times do |j|
14
+ square[j, i] = @pixels[i*size + j]
15
+ end
16
+ end
17
+ square.to_image.save(file_path)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,25 @@
1
+ module NeuralNetworkRb
2
+
3
+ class << self
4
+
5
+ def shuffle(data, target)
6
+ sample_size = data.shape[0]
7
+ new_order = Numo::DFloat[*(0..sample_size-1).to_a.shuffle]
8
+ [data[*subset(data, new_order)], target[*subset(target, new_order)]]
9
+ end
10
+
11
+ def split(data, ratio)
12
+ sample_size = (data.shape[0] * ratio).to_i
13
+ [data[*subset(data, 0..sample_size-1)], data[*subset(data, sample_size..-1)]]
14
+ end
15
+
16
+ private
17
+
18
+ def subset(data, obj)
19
+ Array.new(data.ndim, true).tap do |x|
20
+ x[0] = obj
21
+ end
22
+ end
23
+ end
24
+
25
+ end
@@ -0,0 +1,45 @@
1
+ module NeuralNetworkRb
2
+
3
+ class NeuralNetwork
4
+
5
+ attr_reader :target, :output, :epoch
6
+
7
+ def input=(input)
8
+ @input = input
9
+ input_width = input.shape[1]
10
+ @w_hidden = Numo::DFloat.new(input_width, @neurons_count).rand
11
+ end
12
+
13
+ def target=(target)
14
+ @target = target
15
+ output_width = target.shape[1]
16
+ @w_output = Numo::DFloat.new(@neurons_count, output_width).rand
17
+ end
18
+
19
+ def initialize(neurons_count, learning_rate)
20
+ @neurons_count = neurons_count
21
+ @learning_rate = learning_rate
22
+ @epoch = 0
23
+ end
24
+
25
+ def train()
26
+ # forward
27
+ @hidden = NeuralNetworkRb.sigmoid(@input.dot(@w_hidden))
28
+ @output = @hidden.dot(@w_output)
29
+
30
+ # calculate error
31
+ error = @target - @output
32
+
33
+ # backward
34
+ dZ = error * @learning_rate
35
+
36
+ @w_output += @hidden.transpose.dot(dZ)
37
+ dH = dZ.dot(@w_output.transpose) * NeuralNetworkRb.sigmoid_prime(@hidden)
38
+ @w_hidden += @input.transpose.dot(dH)
39
+
40
+ @epoch += 1
41
+ yield self if block_given?
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,16 @@
1
+ module NeuralNetworkRb
2
+
3
+ E = Math.exp(1)
4
+
5
+ class << self
6
+ def sigmoid(x)
7
+ 1/(1 + E**(-x))
8
+ end
9
+
10
+ def sigmoid_prime(x)
11
+ s = sigmoid(x)
12
+ s * (1-s)
13
+ end
14
+ end
15
+
16
+ end
@@ -0,0 +1,8 @@
1
+ module NeuralNetworkRb
2
+
3
+ def self.softmax(array)
4
+ exp_array = array.map {|x| Math.exp(x)}
5
+ exp_array = exp_array/exp_array.sum
6
+ end
7
+
8
+ end
@@ -0,0 +1,3 @@
1
+ module NeuralNetworkRb
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,37 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "neural_network_rb/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "neural_network_rb"
8
+ spec.version = NeuralNetworkRb::VERSION
9
+ spec.authors = ["Jack Xu"]
10
+ spec.email = ["jackxxu@gmail.com"]
11
+
12
+ spec.summary = %q{neural network ruby implementaiton}
13
+ spec.homepage = "https://github.com/jackxxu/neural_network_rb"
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
19
+ else
20
+ raise "RubyGems 2.0 or newer is required to protect against " \
21
+ "public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(%r{^(test|spec|features)/})
26
+ end
27
+
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "numo-narray", "~> 0.9.1.1"
32
+ spec.add_dependency "chunky_png", "~> 1.3.10"
33
+
34
+ spec.add_development_dependency "bundler", "~> 1.16"
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ spec.add_development_dependency "rspec", "~> 3.0"
37
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: neural_network_rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jack Xu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: numo-narray
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.1.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.9.1.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: chunky_png
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.3.10
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.3.10
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ description:
84
+ email:
85
+ - jackxxu@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - Guardfile
96
+ - README.md
97
+ - Rakefile
98
+ - lib/neural_network_rb.rb
99
+ - lib/neural_network_rb/error.rb
100
+ - lib/neural_network_rb/mnist.rb
101
+ - lib/neural_network_rb/mnist_image.rb
102
+ - lib/neural_network_rb/narray.rb
103
+ - lib/neural_network_rb/neural_network.rb
104
+ - lib/neural_network_rb/sigmoid.rb
105
+ - lib/neural_network_rb/softmax.rb
106
+ - lib/neural_network_rb/version.rb
107
+ - neural_network_rb.gemspec
108
+ homepage: https://github.com/jackxxu/neural_network_rb
109
+ licenses: []
110
+ metadata:
111
+ allowed_push_host: https://rubygems.org
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.4.6
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: neural network ruby implementaiton
132
+ test_files: []