sabina 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.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +16 -0
  5. data/CODE_OF_CONDUCT.md +49 -0
  6. data/Gemfile +13 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +201 -0
  9. data/Rakefile +4 -0
  10. data/examples/example_ae_01/ae_learning_process.gif +0 -0
  11. data/examples/example_ae_01/ae_original_data.png +0 -0
  12. data/examples/example_ae_01/main.rb +123 -0
  13. data/examples/example_ae_01/training_data.csv +821 -0
  14. data/examples/example_mp_01/main.rb +158 -0
  15. data/examples/example_mp_01/mp_learning_process.gif +0 -0
  16. data/examples/example_mp_01/mp_training_data.png +0 -0
  17. data/examples/example_mp_01/training_data.csv +601 -0
  18. data/examples/example_mp_02/main.rb +170 -0
  19. data/examples/example_mp_02/mp_learning_process.gif +0 -0
  20. data/examples/example_mp_02/mp_training_data.png +0 -0
  21. data/examples/example_mp_02/training_data.csv +821 -0
  22. data/lib/sabina/auto_encoder.rb +46 -0
  23. data/lib/sabina/configuration.rb +41 -0
  24. data/lib/sabina/layer/ae_hidden_layer.rb +10 -0
  25. data/lib/sabina/layer/ae_input_layer.rb +4 -0
  26. data/lib/sabina/layer/ae_output_layer.rb +10 -0
  27. data/lib/sabina/layer/base_layer.rb +33 -0
  28. data/lib/sabina/layer/mp_hidden_layer.rb +4 -0
  29. data/lib/sabina/layer/mp_input_layer.rb +4 -0
  30. data/lib/sabina/layer/mp_output_layer.rb +12 -0
  31. data/lib/sabina/layer.rb +11 -0
  32. data/lib/sabina/multilayer_perceptron.rb +135 -0
  33. data/lib/sabina/sparse_auto_encoder.rb +49 -0
  34. data/lib/sabina/util.rb +11 -0
  35. data/lib/sabina/version.rb +3 -0
  36. data/lib/sabina.rb +17 -0
  37. data/sabina.gemspec +37 -0
  38. metadata +124 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0a8910b216a26a625f79de9b5d434d9eb99bee26
4
+ data.tar.gz: e4cc1963ab0f619713c933071f8c4cd2d517cb06
5
+ SHA512:
6
+ metadata.gz: 32f047a576d13cf21b1c7b672d76199d184bb7522e59eccd7d9578f99435b1385cc176ebc7b57d882ed30f85ad6a8e15b3e6b637cfabc342cdcc7d4b5639aaa7
7
+ data.tar.gz: 53f9f1cb437b6d8fbed01100c2d60264beb891b28c5eacaa7eff41b51f05bb078acad8c7c8792f041c25802ef71c89080b8d711676f189ae3a7505e4a28775ad
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /ex/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.3.0
6
+
7
+ before_install: gem install bundler -v 1.11.2
8
+
9
+ bundler_args: --without development --jobs=2
10
+
11
+ script:
12
+ - bundle exec rspec
13
+
14
+ branches:
15
+ only:
16
+ - master
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at seinosuke.3606@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rake'
4
+
5
+ group :development do
6
+ gem 'pry'
7
+ end
8
+
9
+ group :test do
10
+ gem 'rspec', '~> 3.0'
11
+ end
12
+
13
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 seinosuke
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,201 @@
1
+ # Sabina
2
+
3
+ Sabina is a machine learning library.
4
+ This gem provides tools for Multi-Layer Perceptrons and Auto-Encoders.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'sabina'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install sabina
21
+
22
+ ## Usage
23
+
24
+ ### MultilayerPerceptron
25
+
26
+ Example of normal usage is shown below.
27
+
28
+ ```ruby
29
+ require 'sabina'
30
+
31
+ DIM = 2
32
+ K = 3
33
+ LOOP_NUM = 100
34
+
35
+ training_data = Sabina::MultilayerPerceptron.load_csv('training_data.csv')
36
+
37
+ options = {
38
+ :layers => [
39
+ Sabina::Layer::MPInputLayer.new(DIM),
40
+ Sabina::Layer::MPHiddenLayer.new(8),
41
+ Sabina::Layer::MPOutputLayer.new(K)
42
+ ],
43
+ :mini_batch_size => 10,
44
+ :learning_rate => 0.01,
45
+ :training_data => training_data,
46
+ }
47
+
48
+ mp = Sabina::MultilayerPerceptron.new(options)
49
+
50
+ LOOP_NUM.times do |t|
51
+ mp.learn
52
+ error = mp.error(training_data)
53
+ puts " error : #{error}"
54
+ end
55
+ ```
56
+
57
+ ### AutoEncoder
58
+
59
+ Example of normal usage is shown below. Use `SparseAutoEncoder` class when the number of input units is less than that of hidden units.
60
+
61
+ ```ruby
62
+ require 'sabina'
63
+
64
+ DIM = 2
65
+ LOOP_NUM = 10
66
+
67
+ original_data = Sabina::AutoEncoder.load_csv('training_data.csv')
68
+
69
+ options = {
70
+ :layers => [
71
+ Sabina::Layer::AEInputLayer.new(DIM),
72
+ Sabina::Layer::AEHiddenLayer.new(8),
73
+ Sabina::Layer::AEOutputLayer.new(DIM)
74
+ ],
75
+ :mini_batch_size => 10,
76
+ :learning_rate => 0.01,
77
+ :training_data => original_data,
78
+ }
79
+
80
+ sae = Sabina::SparseAutoEncoder.new(options)
81
+
82
+ LOOP_NUM.times do |t|
83
+ sae.learn
84
+ error = sae.error(original_data)
85
+ puts " error : #{error}"
86
+ end
87
+ ```
88
+
89
+ ### About a training data CSV file format
90
+
91
+ Examples of a CSV file are shown below.
92
+
93
+ ```
94
+ x0,x1,label
95
+ 0.8616722150185228,0.7958526101017311,0
96
+ 0.548524744634457,0.8355704092991548,1
97
+ 0.2430915120750876,0.6252296416575435,1
98
+ 0.968877668321639,0.7502385938940324,0
99
+ ...
100
+ ```
101
+
102
+ This is a example for two-dimensional vector data. For example, if you want to input D-dimensional vector data, write `x0,x1,...,x(D-1),label` at the first line.
103
+ The column of `label` is used for a cluster id. For example, if there are three clusters in training data, a number at the `label` column will be 0, 1 or 2.
104
+
105
+ When you prepare a CSV file, load the file as shown below.
106
+
107
+ ```ruby
108
+ training_data = Sabina::MultilayerPerceptron.load_csv('training_data.csv')
109
+ ```
110
+
111
+ When you use a auto-encoder, load a CSV file as shown below.
112
+
113
+ ```ruby
114
+ original_data = Sabina::AutoEncoder.load_csv('training_data.csv')
115
+ ```
116
+
117
+ ### Configuration
118
+
119
+ You can set default values by using `Sabina.configure` method. These values could be overwritten by providing an argument.
120
+
121
+ ```ruby
122
+ Sabina.configure do |config|
123
+ config.layers = [
124
+ Sabina::Layer::MPInputLayer.new(2),
125
+ Sabina::Layer::MPHiddenLayer.new(8),
126
+ Sabina::Layer::MPOutputLayer.new(3)
127
+ ]
128
+ config.mini_batch_size = 10
129
+ config.learning_rate = 0.01
130
+ config.training_data = Sabina::MultilayerPerceptron.load_csv('training_data.csv')
131
+ end
132
+
133
+ options = {
134
+ :mini_batch_size => 20
135
+ }
136
+
137
+ mp_01 = Sabina::MultilayerPerceptron.new
138
+ mp_02 = Sabina::MultilayerPerceptron.new(options)
139
+
140
+ mp_01.mini_batch_size # => 10
141
+ mp_02.mini_batch_size # => 20
142
+ ```
143
+
144
+ ### Your own layer class
145
+ You can create your own layer class. In the following example below, a rectified linear function is set as an activation function. `@f_` is differentiation of `@f`.
146
+
147
+ ```ruby
148
+ class MyHiddenLayer < Sabina::Layer::BaseLayer
149
+ def initialize(size)
150
+ super
151
+ # f(x) = max(0, x)
152
+ @f = ->(x){ x > 0.0 ? x : 0.0 }
153
+ @f_ = ->(x){ x > 0.0 ? 1.0 : 0.0 }
154
+ end
155
+ end
156
+
157
+ options = {
158
+ :layers => [
159
+ Sabina::Layer::MPInputLayer.new(DIM),
160
+ MyHiddenLayer.new(16),
161
+ MyHiddenLayer.new(8),
162
+ Sabina::Layer::MPOutputLayer.new(K)
163
+ ],
164
+ :mini_batch_size => 10,
165
+ :learning_rate => 0.01,
166
+ :training_data => training_data,
167
+ }
168
+ ```
169
+
170
+ ## Examples
171
+
172
+ These examples require gnuplot version 5.0 or later.
173
+
174
+ ### examples/example_mp_01/
175
+
176
+ Run [examples/example_mp_01/main.rb](https://github.com/seinosuke/sabina/blob/master/examples/example_mp_01/main.rb).
177
+
178
+ ![mp_learning_process.gif](https://github.com/seinosuke/sabina/blob/master/examples/example_mp_01/mp_learning_process.gif)
179
+
180
+ ### examples/example_mp_02/
181
+
182
+ Run [examples/example_mp_02/main.rb](https://github.com/seinosuke/sabina/blob/master/examples/example_mp_02/main.rb).
183
+
184
+ ![mp_learning_process.gif](https://github.com/seinosuke/sabina/blob/master/examples/example_mp_02/mp_learning_process.gif)
185
+
186
+ ### examples/example_ae_01/
187
+
188
+ Run [examples/example_ae_01/main.rb](https://github.com/seinosuke/sabina/blob/master/examples/example_ae_01/main.rb).
189
+
190
+ ![ae_learning_process.gif](https://github.com/seinosuke/sabina/blob/master/examples/example_ae_01/mp_learning_process.gif)
191
+
192
+
193
+ ## Contributing
194
+
195
+ Bug reports and pull requests are welcome on GitHub at https://github.com/seinosuke/sabina. 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.
196
+
197
+
198
+ ## License
199
+
200
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
201
+
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require "rspec/core/rake_task"
4
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,123 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../../lib', __FILE__)
2
+ require 'open3'
3
+ require 'sabina'
4
+
5
+ DIM = 2
6
+ LOOP_NUM = 100
7
+ xrange = [-2.2, 2.2]
8
+ yrange = [-2.2, 2.2]
9
+
10
+ original_data = Sabina::AutoEncoder.load_csv('training_data.csv')
11
+
12
+ options = {
13
+ :layers => [
14
+ Sabina::Layer::AEInputLayer.new(DIM),
15
+ Sabina::Layer::AEHiddenLayer.new(20),
16
+ Sabina::Layer::AEOutputLayer.new(DIM)
17
+ ],
18
+ :mini_batch_size => 10,
19
+ :learning_rate => 0.0002,
20
+ :training_data => original_data,
21
+ }
22
+
23
+ # Use a sparse autoencoder
24
+ # because the number of input units is less than that of hidden units.
25
+ sae = Sabina::SparseAutoEncoder.new(options)
26
+
27
+
28
+
29
+ ##################################################
30
+ # Make a chart of original data.
31
+ ##################################################
32
+ Open3.popen3('gnuplot') do |gp_in, gp_out, gp_err|
33
+ output_file = "./ae_original_data.png"
34
+ gp_in.puts "set colorsequence classic"
35
+ gp_in.puts "set terminal png size 480, 450"
36
+ gp_in.puts "set output '#{output_file}'"
37
+ gp_in.puts "set label 2 center at screen 0.5,0.9 'Original Data' font 'Helvetica,22'"
38
+ gp_in.puts "set tmargin 3.2"
39
+ gp_in.puts "set xlabel 'x0'"
40
+ gp_in.puts "set ylabel 'x1'"
41
+ gp_in.puts "set size square"
42
+ gp_in.puts xrange.tap { |f, t| break "set xrange [#{f}:#{t}]" }
43
+ gp_in.puts yrange.tap { |f, t| break "set yrange [#{f}:#{t}]" }
44
+ plot = "plot "
45
+
46
+ plot << "'-' notitle pt 1 ps 1 lc 1,\\\n"
47
+ plot.gsub!(/,\\\n\z/, "\n")
48
+
49
+ original_data.each do |data|
50
+ data[:x].tap { |x, y| plot << "#{x}, #{y}\n" }
51
+ end
52
+ plot << "e\n"
53
+
54
+ gp_in.puts plot
55
+ puts output_file
56
+ gp_in.puts "set output"
57
+ gp_in.puts "exit"
58
+ gp_in.close
59
+ end
60
+
61
+
62
+ ##################################################
63
+ # Make a GIF of the learning process.
64
+ ##################################################
65
+ Open3.popen3('gnuplot') do |gp_in, gp_out, gp_err|
66
+ output_file = "./ae_learning_process.gif"
67
+ gp_in.puts "set colorsequence classic"
68
+ gp_in.puts "set terminal gif animate delay 10 optimize size 880, 450"
69
+ gp_in.puts "set output '#{output_file}'"
70
+ gp_in.puts "set tmargin 3.2"
71
+ gp_in.puts "set size square"
72
+ gp_in.puts xrange.tap { |f, t| break "set xrange [#{f}:#{t}]" }
73
+ gp_in.puts yrange.tap { |f, t| break "set yrange [#{f}:#{t}]" }
74
+ gp_in.puts "set xlabel 'x0'"
75
+ gp_in.puts "set ylabel 'x1'"
76
+ log = []
77
+ x_mat = Matrix.columns( original_data.map { |data| data[:x] } )
78
+
79
+ LOOP_NUM.times do |t|
80
+ sae.learn
81
+ log << sae.error(original_data)
82
+ gp_in.puts "set multiplot layout 1, 2"
83
+
84
+ ##################################################
85
+ # Original Data
86
+ ##################################################
87
+ gp_in.puts "set label 2 center at screen 0.28,0.9 'Original Data' font 'Helvetica,22'"
88
+ plot = "plot "
89
+ plot << "'-' notitle pt 1 ps 1 lc 1,\\\n"
90
+ plot.gsub!(/,\\\n\z/, "\n")
91
+
92
+ original_data.each do |data|
93
+ data[:x].tap { |x, y| plot << "#{x}, #{y}\n" }
94
+ end
95
+ plot << "e\n"
96
+ gp_in.puts plot
97
+
98
+ ##################################################
99
+ # Decoded Data
100
+ ##################################################
101
+ gp_in.puts "set label 2 center at screen 0.79,0.9 'Decoded Data' font 'Helvetica,22'"
102
+ plot = "plot "
103
+ plot << "'-' notitle pt 1 ps 1 lc 1,\\\n"
104
+ plot.gsub!(/,\\\n\z/, "\n")
105
+
106
+ decoded_data = sae.propagate_forward(x_mat).t.to_a
107
+ decoded_data.each do |data|
108
+ data.tap { |x, y| plot << "#{x}, #{y}\n" }
109
+ end
110
+ plot << "e\n"
111
+ gp_in.puts plot
112
+
113
+ # progress bar
114
+ puts " error : #{log.last}"
115
+ puts " [#{("*"*(t / (LOOP_NUM / 10))).ljust(10, " ")}]"
116
+ print "\e[2A"; STDOUT.flush;
117
+ end
118
+
119
+ gp_in.puts "set output"
120
+ puts output_file
121
+ gp_in.puts "exit"
122
+ gp_in.close
123
+ end