red-chainer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +23 -0
  8. data/README.md +60 -0
  9. data/Rakefile +8 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +8 -0
  12. data/examples/mnist.rb +42 -0
  13. data/lib/chainer.rb +59 -0
  14. data/lib/chainer/configuration.rb +10 -0
  15. data/lib/chainer/dataset/convert.rb +62 -0
  16. data/lib/chainer/dataset/download.rb +56 -0
  17. data/lib/chainer/dataset/iterator.rb +15 -0
  18. data/lib/chainer/datasets/mnist.rb +89 -0
  19. data/lib/chainer/datasets/tuple_dataset.rb +33 -0
  20. data/lib/chainer/function.rb +80 -0
  21. data/lib/chainer/functions/activation/log_softmax.rb +37 -0
  22. data/lib/chainer/functions/activation/relu.rb +23 -0
  23. data/lib/chainer/functions/connection/linear.rb +48 -0
  24. data/lib/chainer/functions/evaluation/accuracy.rb +42 -0
  25. data/lib/chainer/functions/loss/softmax_cross_entropy.rb +134 -0
  26. data/lib/chainer/functions/math/basic_math.rb +119 -0
  27. data/lib/chainer/gradient_method.rb +63 -0
  28. data/lib/chainer/hyperparameter.rb +23 -0
  29. data/lib/chainer/initializer.rb +12 -0
  30. data/lib/chainer/initializers/constant.rb +18 -0
  31. data/lib/chainer/initializers/init.rb +24 -0
  32. data/lib/chainer/initializers/normal.rb +28 -0
  33. data/lib/chainer/iterators/serial_iterator.rb +74 -0
  34. data/lib/chainer/link.rb +118 -0
  35. data/lib/chainer/links/connection/linear.rb +43 -0
  36. data/lib/chainer/links/model/classifier.rb +39 -0
  37. data/lib/chainer/optimizer.rb +69 -0
  38. data/lib/chainer/optimizers/adam.rb +62 -0
  39. data/lib/chainer/parameter.rb +53 -0
  40. data/lib/chainer/reporter.rb +130 -0
  41. data/lib/chainer/training/extension.rb +25 -0
  42. data/lib/chainer/training/extensions/evaluator.rb +26 -0
  43. data/lib/chainer/training/extensions/log_report.rb +72 -0
  44. data/lib/chainer/training/extensions/print_report.rb +62 -0
  45. data/lib/chainer/training/extensions/progress_bar.rb +89 -0
  46. data/lib/chainer/training/standard_updater.rb +63 -0
  47. data/lib/chainer/training/trainer.rb +136 -0
  48. data/lib/chainer/training/triggers/interval.rb +27 -0
  49. data/lib/chainer/training/updater.rb +33 -0
  50. data/lib/chainer/training/util.rb +13 -0
  51. data/lib/chainer/utils/array.rb +10 -0
  52. data/lib/chainer/utils/initializer.rb +14 -0
  53. data/lib/chainer/utils/variable.rb +20 -0
  54. data/lib/chainer/variable.rb +204 -0
  55. data/lib/chainer/variable_node.rb +71 -0
  56. data/lib/chainer/version.rb +4 -0
  57. data/red-chainer.gemspec +27 -0
  58. metadata +156 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d1ae034ef57d8a580df9b0116bedf7a3194c7710
4
+ data.tar.gz: 915a0479a543ef59e8194a5b46860261651c332d
5
+ SHA512:
6
+ metadata.gz: 5d2aa77bda65c3736c153c7e813f864a7d48ab890d81e7ca34398b100fe03727f3b1c9cbfab58fc2b6c1711947ec5c3b531094ddba52601a38dfbbbaa83c3828
7
+ data.tar.gz: 5eeb8cb7f7e7f334a6a0abd46b55e215f5807c3a8fedd8b99f7661132c3ccc62a64695f6b80cd7a40507287eca1f72f8d711ad438c084d2ff2dde1fdc0586ea1
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.1
5
+ before_install: gem install bundler -v 1.15.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at hata.yusaku.1225@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in red-chainer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,23 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Preferred Infrastructure, Inc.
4
+ Copyright (c) 2015 Preferred Networks, Inc.
5
+ Copyright (c) 2017 yusaku.hatanaka
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in
15
+ all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,60 @@
1
+ # README
2
+
3
+ ## Name
4
+
5
+ Red Cahiner
6
+
7
+ ## Description
8
+
9
+ 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/chainer`. To experiment with that code, run `bin/console` for an interactive prompt.
10
+
11
+ TODO: Delete this and the text above, and describe your gem
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'red-chainer', github: 'red-data-tools/red-chainer'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ ```ruby
24
+ $ bundle
25
+ ```
26
+
27
+ Or install it yourself as:
28
+
29
+ ```ruby
30
+ gem install specific_install
31
+ gem specific_install -l 'https://github.com/red-data-tools/red-chainer'
32
+ ```
33
+
34
+ ## Usage
35
+ mnist sample program is [here](./examples/mnist.rb)
36
+
37
+ ```ruby
38
+ # install Gemfile
39
+ $ bundle exec ruby examples/mnist.rb
40
+ # install yourself
41
+ $ ruby examples/mnist.rb
42
+ ```
43
+
44
+ ## Development
45
+
46
+ 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.
47
+
48
+ 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).
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/red-chainer. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
57
+
58
+ ## Code of Conduct
59
+
60
+ Everyone interacting in the Chainer project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/red-chainer/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ desc "Run tests"
4
+ task :test do
5
+ ruby("test/run_test.rb")
6
+ end
7
+
8
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "red-chainer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/examples/mnist.rb ADDED
@@ -0,0 +1,42 @@
1
+ require 'chainer'
2
+ require 'fileutils'
3
+ require 'tmpdir'
4
+
5
+ class MLP < Chainer::Chain
6
+ L = Chainer::Links::Connection::Linear
7
+ R = Chainer::Functions::Activation::Relu
8
+
9
+ def initialize(n_units, n_out)
10
+ super()
11
+ init_scope do
12
+ @l1 = L.new(nil, out_size: n_units)
13
+ @l2 = L.new(nil, out_size: n_units)
14
+ @l3 = L.new(nil, out_size: n_out)
15
+ end
16
+ end
17
+
18
+ def call(x)
19
+ h1 = R.relu(@l1.(x))
20
+ h2 = R.relu(@l2.(h1))
21
+ @l3.(h2)
22
+ end
23
+ end
24
+
25
+ model = Chainer::Links::Model::Classifier.new(MLP.new(1000, 10))
26
+
27
+ optimizer = Chainer::Optimizers::Adam.new
28
+ optimizer.setup(model)
29
+ train, test = Chainer::Datasets::Mnist.get_mnist
30
+
31
+ train_iter = Chainer::Iterators::SerialIterator.new(train, 100)
32
+ test_iter = Chainer::Iterators::SerialIterator.new(test, 100, repeat: false, shuffle: false)
33
+
34
+ updater = Chainer::Training::StandardUpdater.new(train_iter, optimizer, device: -1)
35
+ trainer = Chainer::Training::Trainer.new(updater, stop_trigger: [20, 'epoch'], out: 'result')
36
+
37
+ trainer.extend(Chainer::Training::Extensions::Evaluator.new(test_iter, model, device: -1))
38
+ trainer.extend(Chainer::Training::Extensions::LogReport.new)
39
+ trainer.extend(Chainer::Training::Extensions::PrintReport.new(['epoch', 'main/loss', 'validation/main/loss', 'main/accuracy', 'validation/main/accuracy', 'elapsed_time']))
40
+ trainer.extend(Chainer::Training::Extensions::ProgressBar.new)
41
+
42
+ trainer.run
data/lib/chainer.rb ADDED
@@ -0,0 +1,59 @@
1
+ require "weakref"
2
+
3
+ require "chainer/version"
4
+
5
+ require 'chainer/configuration'
6
+ require 'chainer/function'
7
+ require 'chainer/optimizer'
8
+ require 'chainer/gradient_method'
9
+ require 'chainer/hyperparameter'
10
+ require 'chainer/dataset/iterator'
11
+ require 'chainer/dataset/convert'
12
+ require 'chainer/initializer'
13
+ require 'chainer/initializers/init'
14
+ require 'chainer/initializers/constant'
15
+ require 'chainer/initializers/normal'
16
+ require 'chainer/iterators/serial_iterator'
17
+ require 'chainer/link'
18
+ require 'chainer/links/connection/linear'
19
+ require 'chainer/links/model/classifier'
20
+ require 'chainer/variable'
21
+ require 'chainer/variable_node'
22
+ require 'chainer/utils/initializer'
23
+ require 'chainer/utils/variable'
24
+ require 'chainer/utils/array'
25
+ require 'chainer/functions/activation/relu'
26
+ require 'chainer/functions/activation/log_softmax'
27
+ require 'chainer/functions/evaluation/accuracy'
28
+ require 'chainer/functions/math/basic_math'
29
+ require 'chainer/functions/loss/softmax_cross_entropy'
30
+ require 'chainer/functions/connection/linear'
31
+ require 'chainer/training/extension'
32
+ require 'chainer/training/extensions/evaluator'
33
+ require 'chainer/training/extensions/log_report'
34
+ require 'chainer/training/extensions/print_report'
35
+ require 'chainer/training/extensions/progress_bar'
36
+ require 'chainer/training/trainer'
37
+ require 'chainer/training/updater'
38
+ require 'chainer/training/util'
39
+ require 'chainer/training/standard_updater'
40
+ require 'chainer/training/triggers/interval'
41
+ require 'chainer/parameter'
42
+ require 'chainer/optimizers/adam'
43
+ require 'chainer/dataset/download'
44
+ require 'chainer/datasets/mnist'
45
+ require 'chainer/datasets/tuple_dataset'
46
+ require 'chainer/reporter'
47
+
48
+ require 'numo/narray'
49
+
50
+ module Chainer
51
+ def self.configure
52
+ yield(configuration)
53
+ end
54
+
55
+ def self.configuration
56
+ @configuration ||= Configuration.new
57
+ end
58
+ end
59
+
@@ -0,0 +1,10 @@
1
+ module Chainer
2
+ class Configuration
3
+ attr_accessor :enable_backprop
4
+
5
+ def initialize
6
+ @enable_backprop = true
7
+ end
8
+ end
9
+ end
10
+
@@ -0,0 +1,62 @@
1
+ module Chainer
2
+ module Dataset
3
+ module Convert
4
+ def self.to_device(device, x)
5
+ # TODO: support cuda
6
+ x
7
+ end
8
+
9
+ def self.concat_examples(batch, device: nil, padding: nil)
10
+ raise "batch is empty" if batch.size == 0
11
+ first_elem = batch[0]
12
+
13
+ if first_elem.kind_of?(Array)
14
+ result = []
15
+ unless padding.kind_of?(Array)
16
+ padding = [padding] * first_elem.size
17
+ end
18
+
19
+ first_elem.size.times do |i|
20
+ x = concat_arrays(batch.map { |b| b[i] }, padding[i])
21
+ result.push(to_device(device, x))
22
+ end
23
+
24
+ return result
25
+ else
26
+ return to_device(device, concat_arrays(batch, padding))
27
+ end
28
+ end
29
+
30
+ def self.concat_arrays(arrays, padding)
31
+ unless arrays[0].kind_of?(Numo::NArray)
32
+ arrays = Numo::NArray.cast(arrays)
33
+ end
34
+
35
+ if padding
36
+ return concat_arrays_with_padding(arrays, padding)
37
+ end
38
+
39
+ Numo::NArray.[](*arrays.to_a.map { |arr| arr.kind_of?(Numeric) ? arr : Numo::NArray.[](*arr) })
40
+ end
41
+
42
+ def self.concat_arrays_with_padding(arrays, padding)
43
+ shape = Numo::Int32.[](arrays[0].shape)
44
+ arrays[1...arrays.len].each do |array|
45
+ if Numo::Bit.[](shape != array.shape).any?
46
+ # TODO: numpy maximum
47
+ end
48
+ end
49
+
50
+ shape = [shape.insert(0, arrays.size)]
51
+ result = arrays[0].dtype.[](*shape).full(padding)
52
+ arrays.size.times do |i|
53
+ src = arrays[i]
54
+ slices = src.shape.map { |s| [s] }
55
+ result[[i] + slices] = src
56
+ end
57
+
58
+ result
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,56 @@
1
+ require "open-uri"
2
+ require "pstore"
3
+
4
+ module Chainer
5
+ module Dataset
6
+ module Download
7
+ DATASET_ROOT = ENV.fetch("RED_CHAINER_DATASET_ROOT", File.expand_path(".chainer/dataset", "~"))
8
+
9
+ def self.cached_download(url)
10
+ cache_root = File.expand_path('_dl_cache', DATASET_ROOT)
11
+ FileUtils.mkdir_p(cache_root)
12
+ lock_path = File.expand_path('_dl_lock', cache_root)
13
+ urlhash = Digest::MD5.hexdigest(url)
14
+ cache_path = File.expand_path(urlhash, cache_root)
15
+
16
+ return cache_path if File.exist?(cache_path)
17
+
18
+ temp_root = Dir.mktmpdir(nil, cache_root)
19
+ temp_path = File.expand_path('dl', temp_root)
20
+ open(url) do |f|
21
+ puts "Downloading from #{url}"
22
+ open(temp_path, "w+b") do |out|
23
+ out.write(f.read)
24
+ end
25
+ FileUtils.mv(temp_path, cache_path)
26
+ FileUtils.rm_r(temp_root)
27
+ end
28
+ cache_path
29
+ end
30
+
31
+ def self.get_dataset_directory(dataset_name, create_directory: true)
32
+ path = File.expand_path(dataset_name, DATASET_ROOT)
33
+ FileUtils.mkdir_p(path) if create_directory
34
+ path
35
+ end
36
+
37
+ def self.cache_or_load_file(path, &creator)
38
+ raise 'Please set dataset creator on block' if creator.nil?
39
+
40
+ return PStore.new(path).transaction { |t| t['data'] } if File.exist?(path)
41
+
42
+ data = creator.call
43
+ PStore.new(path).transaction do |t|
44
+ t['data'] = data
45
+ end
46
+ data
47
+ rescue TypeError => e
48
+ puts e.message
49
+ FileUtils.rm_f(path)
50
+ cache_or_load_file(path) do
51
+ creator.call
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end