brains 0.1.1-java → 0.2.0-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +12 -0
- data/brains.gemspec +1 -1
- data/example/colors.rb +67 -15
- data/example/iris.data +150 -0
- data/example/sine_function.rb +53 -0
- data/lib/brains/brains.jar +0 -0
- data/lib/brains/net.rb +66 -3
- data/lib/brains/version.rb +1 -1
- metadata +20 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b6deee0363d18d2c2d4a7ea8b4047681776566a
|
4
|
+
data.tar.gz: 88dd27bf0f07c4895f0871e80dba2f7909ab02bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0cef015a95f1237f286606cbb439e8370920e3fc6bd9eebf2f68d03471c20383cb930495740a34dc51d5543e6560c3f739ccb61ceabeb1eed1e3d649fd60a64d
|
7
|
+
data.tar.gz: 690b8de89fd2dfe1f98f9bbe9594c5af7bf28b1abb2940589433a0e304d7dd3e95dbc2420b3c68768f8a4f1fd5e72d35d1bb266e9a838edf0d4aa309a138036a
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -154,12 +154,24 @@ https://github.com/jedld/brains
|
|
154
154
|
|
155
155
|
You can compile the java source code as brains.jar and use it directly with this gem.
|
156
156
|
|
157
|
+
## RNNs (Recurrent Neural Networks)
|
158
|
+
|
159
|
+
For recurrent neural networks (Look at the sine function in the examples). Only
|
160
|
+
the backpropagation through time (BPTT) training algorithm is
|
161
|
+
supported for now.
|
162
|
+
|
157
163
|
## Development
|
158
164
|
|
159
165
|
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.
|
160
166
|
|
161
167
|
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).
|
162
168
|
|
169
|
+
## Resources
|
170
|
+
|
171
|
+
Machine learning is still a rapidly evolving field and research is ongoing on various aspects of it. This is just the tip of the iceberg, the field of machine learning is extremely complex, below are various resources for the average developer to get started:
|
172
|
+
|
173
|
+
ftp://ftp.sas.com/pub/neural/FAQ.html#questions
|
174
|
+
|
163
175
|
## Contributing
|
164
176
|
|
165
177
|
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/brains. 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.
|
data/brains.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.email = ["joseph.dayo@gmail.com"]
|
11
11
|
|
12
12
|
spec.summary = %q{A feedforward neural network library for JRuby}
|
13
|
-
spec.description = %q{A feedforward neural network library for JRuby}
|
13
|
+
spec.description = %q{A feedforward neural network library for JRuby. Aims to provide a quick way to get started on machine learning with ruby }
|
14
14
|
spec.homepage = "https://github.com/jedld/brains-jruby"
|
15
15
|
spec.license = "MIT"
|
16
16
|
spec.platform = "java"
|
data/example/colors.rb
CHANGED
@@ -3,43 +3,95 @@
|
|
3
3
|
require 'brains'
|
4
4
|
|
5
5
|
#This neural network will identify the main color name based on rgb values
|
6
|
+
RED = [0,0,1]
|
7
|
+
GREEN = [0,1,0]
|
8
|
+
BLUE = [1,0,0]
|
9
|
+
|
10
|
+
def color_value(color_value)
|
11
|
+
[
|
12
|
+
Integer(color_value[0..1], 16).to_f / 0xff.to_f,
|
13
|
+
Integer(color_value[2..3], 16).to_f / 0xff.to_f,
|
14
|
+
Integer(color_value[4..5], 16).to_f / 0xff.to_f,
|
15
|
+
]
|
16
|
+
end
|
17
|
+
|
18
|
+
def color_desc(result)
|
19
|
+
return "blue" if (result[0] > result[1] && result[0] > result[2])
|
20
|
+
return "green" if (result[1] > result[0] && result[1] > result[2])
|
21
|
+
return "red" if (result[2] > result[0] && result[2] > result[1])
|
22
|
+
end
|
6
23
|
|
7
24
|
label_encodings = {
|
8
|
-
"
|
25
|
+
"Blue" => [1, 0, 0],
|
9
26
|
"Green" => [0, 1, 0],
|
10
|
-
"
|
27
|
+
"Red" => [0, 0 ,1]
|
11
28
|
}
|
12
29
|
#0000ff
|
13
30
|
training_data = [
|
14
|
-
|
15
|
-
[
|
16
|
-
[
|
31
|
+
# red
|
32
|
+
[color_value('E32636'), RED],
|
33
|
+
[color_value('8B0000'), RED],
|
34
|
+
[color_value('800000'), RED],
|
35
|
+
[color_value('65000B'), RED],
|
36
|
+
[color_value('674846'), RED],
|
37
|
+
|
38
|
+
#green
|
39
|
+
[color_value('8F9779'), GREEN],
|
40
|
+
[color_value('568203'), GREEN],
|
41
|
+
[color_value('013220'), GREEN],
|
42
|
+
[color_value('00FF00'), GREEN],
|
43
|
+
[color_value('006400'), GREEN],
|
44
|
+
[color_value('00A877'), GREEN],
|
45
|
+
|
46
|
+
#blue
|
47
|
+
[color_value('89CFF0'), BLUE],
|
48
|
+
[color_value('ADD8E6'), BLUE],
|
49
|
+
[color_value('0000FF'), BLUE],
|
50
|
+
[color_value('0070BB'), BLUE],
|
51
|
+
[color_value('545AA7'), BLUE],
|
52
|
+
[color_value('4C516D'), BLUE],
|
17
53
|
]
|
18
54
|
|
19
|
-
nn = Brains::Net.create(
|
55
|
+
nn = Brains::Net.create(3, 3, 2, { neurons_per_layer: 5 })
|
56
|
+
|
57
|
+
# randomize weights before training
|
20
58
|
nn.randomize_weights
|
21
59
|
|
22
60
|
|
23
61
|
# test on untrained data
|
24
|
-
#0000ee
|
62
|
+
#0000ee #C41E3A
|
25
63
|
test_data = [
|
26
|
-
[
|
64
|
+
[color_value('0087BD') , 'blue'], # blue
|
65
|
+
[color_value('C80815') , 'red'], # venetian red
|
66
|
+
[color_value('009E60') , 'green'], # Shamrock green
|
67
|
+
[color_value('00FF00') , 'green'], # green
|
68
|
+
[color_value('333399') , 'blue'], # blue
|
27
69
|
]
|
28
70
|
|
29
|
-
|
30
|
-
|
71
|
+
correct = 0
|
72
|
+
test_data.each_with_index { |item , index|
|
73
|
+
c = color_desc(nn.feed(item[0]))
|
74
|
+
correct +=1 if (c == item[1])
|
75
|
+
puts c
|
31
76
|
}
|
32
77
|
|
33
|
-
|
78
|
+
puts "#{correct}/#{test_data.length}"
|
34
79
|
|
35
|
-
result = nn.optimize(training_data, 0.
|
80
|
+
result = nn.optimize(training_data, 0.005, 1_000_000, 100 ) { |i, error|
|
36
81
|
puts "#{i} #{error}"
|
37
82
|
}
|
38
83
|
|
84
|
+
p result
|
85
|
+
|
39
86
|
puts "after training"
|
40
87
|
|
41
|
-
|
42
|
-
|
88
|
+
correct = 0
|
89
|
+
test_data.each_with_index { |item , index|
|
90
|
+
c = color_desc(nn.feed(item[0]))
|
91
|
+
correct +=1 if (c == item[1])
|
92
|
+
puts c
|
43
93
|
}
|
44
94
|
|
45
|
-
|
95
|
+
puts "#{correct}/#{test_data.length}"
|
96
|
+
|
97
|
+
puts nn.to_json
|
data/example/iris.data
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
5.1,3.5,1.4,0.2,Iris-setosa
|
2
|
+
4.9,3.0,1.4,0.2,Iris-setosa
|
3
|
+
4.7,3.2,1.3,0.2,Iris-setosa
|
4
|
+
4.6,3.1,1.5,0.2,Iris-setosa
|
5
|
+
5.0,3.6,1.4,0.2,Iris-setosa
|
6
|
+
5.4,3.9,1.7,0.4,Iris-setosa
|
7
|
+
4.6,3.4,1.4,0.3,Iris-setosa
|
8
|
+
5.0,3.4,1.5,0.2,Iris-setosa
|
9
|
+
4.4,2.9,1.4,0.2,Iris-setosa
|
10
|
+
4.9,3.1,1.5,0.1,Iris-setosa
|
11
|
+
5.4,3.7,1.5,0.2,Iris-setosa
|
12
|
+
4.8,3.4,1.6,0.2,Iris-setosa
|
13
|
+
4.8,3.0,1.4,0.1,Iris-setosa
|
14
|
+
4.3,3.0,1.1,0.1,Iris-setosa
|
15
|
+
5.8,4.0,1.2,0.2,Iris-setosa
|
16
|
+
5.7,4.4,1.5,0.4,Iris-setosa
|
17
|
+
5.4,3.9,1.3,0.4,Iris-setosa
|
18
|
+
5.1,3.5,1.4,0.3,Iris-setosa
|
19
|
+
5.7,3.8,1.7,0.3,Iris-setosa
|
20
|
+
5.1,3.8,1.5,0.3,Iris-setosa
|
21
|
+
5.4,3.4,1.7,0.2,Iris-setosa
|
22
|
+
5.1,3.7,1.5,0.4,Iris-setosa
|
23
|
+
4.6,3.6,1.0,0.2,Iris-setosa
|
24
|
+
5.1,3.3,1.7,0.5,Iris-setosa
|
25
|
+
4.8,3.4,1.9,0.2,Iris-setosa
|
26
|
+
5.0,3.0,1.6,0.2,Iris-setosa
|
27
|
+
5.0,3.4,1.6,0.4,Iris-setosa
|
28
|
+
5.2,3.5,1.5,0.2,Iris-setosa
|
29
|
+
5.2,3.4,1.4,0.2,Iris-setosa
|
30
|
+
4.7,3.2,1.6,0.2,Iris-setosa
|
31
|
+
4.8,3.1,1.6,0.2,Iris-setosa
|
32
|
+
5.4,3.4,1.5,0.4,Iris-setosa
|
33
|
+
5.2,4.1,1.5,0.1,Iris-setosa
|
34
|
+
5.5,4.2,1.4,0.2,Iris-setosa
|
35
|
+
4.9,3.1,1.5,0.1,Iris-setosa
|
36
|
+
5.0,3.2,1.2,0.2,Iris-setosa
|
37
|
+
5.5,3.5,1.3,0.2,Iris-setosa
|
38
|
+
4.9,3.1,1.5,0.1,Iris-setosa
|
39
|
+
4.4,3.0,1.3,0.2,Iris-setosa
|
40
|
+
5.1,3.4,1.5,0.2,Iris-setosa
|
41
|
+
5.0,3.5,1.3,0.3,Iris-setosa
|
42
|
+
4.5,2.3,1.3,0.3,Iris-setosa
|
43
|
+
4.4,3.2,1.3,0.2,Iris-setosa
|
44
|
+
5.0,3.5,1.6,0.6,Iris-setosa
|
45
|
+
5.1,3.8,1.9,0.4,Iris-setosa
|
46
|
+
4.8,3.0,1.4,0.3,Iris-setosa
|
47
|
+
5.1,3.8,1.6,0.2,Iris-setosa
|
48
|
+
4.6,3.2,1.4,0.2,Iris-setosa
|
49
|
+
5.3,3.7,1.5,0.2,Iris-setosa
|
50
|
+
5.0,3.3,1.4,0.2,Iris-setosa
|
51
|
+
7.0,3.2,4.7,1.4,Iris-versicolor
|
52
|
+
6.4,3.2,4.5,1.5,Iris-versicolor
|
53
|
+
6.9,3.1,4.9,1.5,Iris-versicolor
|
54
|
+
5.5,2.3,4.0,1.3,Iris-versicolor
|
55
|
+
6.5,2.8,4.6,1.5,Iris-versicolor
|
56
|
+
5.7,2.8,4.5,1.3,Iris-versicolor
|
57
|
+
6.3,3.3,4.7,1.6,Iris-versicolor
|
58
|
+
4.9,2.4,3.3,1.0,Iris-versicolor
|
59
|
+
6.6,2.9,4.6,1.3,Iris-versicolor
|
60
|
+
5.2,2.7,3.9,1.4,Iris-versicolor
|
61
|
+
5.0,2.0,3.5,1.0,Iris-versicolor
|
62
|
+
5.9,3.0,4.2,1.5,Iris-versicolor
|
63
|
+
6.0,2.2,4.0,1.0,Iris-versicolor
|
64
|
+
6.1,2.9,4.7,1.4,Iris-versicolor
|
65
|
+
5.6,2.9,3.6,1.3,Iris-versicolor
|
66
|
+
6.7,3.1,4.4,1.4,Iris-versicolor
|
67
|
+
5.6,3.0,4.5,1.5,Iris-versicolor
|
68
|
+
5.8,2.7,4.1,1.0,Iris-versicolor
|
69
|
+
6.2,2.2,4.5,1.5,Iris-versicolor
|
70
|
+
5.6,2.5,3.9,1.1,Iris-versicolor
|
71
|
+
5.9,3.2,4.8,1.8,Iris-versicolor
|
72
|
+
6.1,2.8,4.0,1.3,Iris-versicolor
|
73
|
+
6.3,2.5,4.9,1.5,Iris-versicolor
|
74
|
+
6.1,2.8,4.7,1.2,Iris-versicolor
|
75
|
+
6.4,2.9,4.3,1.3,Iris-versicolor
|
76
|
+
6.6,3.0,4.4,1.4,Iris-versicolor
|
77
|
+
6.8,2.8,4.8,1.4,Iris-versicolor
|
78
|
+
6.7,3.0,5.0,1.7,Iris-versicolor
|
79
|
+
6.0,2.9,4.5,1.5,Iris-versicolor
|
80
|
+
5.7,2.6,3.5,1.0,Iris-versicolor
|
81
|
+
5.5,2.4,3.8,1.1,Iris-versicolor
|
82
|
+
5.5,2.4,3.7,1.0,Iris-versicolor
|
83
|
+
5.8,2.7,3.9,1.2,Iris-versicolor
|
84
|
+
6.0,2.7,5.1,1.6,Iris-versicolor
|
85
|
+
5.4,3.0,4.5,1.5,Iris-versicolor
|
86
|
+
6.0,3.4,4.5,1.6,Iris-versicolor
|
87
|
+
6.7,3.1,4.7,1.5,Iris-versicolor
|
88
|
+
6.3,2.3,4.4,1.3,Iris-versicolor
|
89
|
+
5.6,3.0,4.1,1.3,Iris-versicolor
|
90
|
+
5.5,2.5,4.0,1.3,Iris-versicolor
|
91
|
+
5.5,2.6,4.4,1.2,Iris-versicolor
|
92
|
+
6.1,3.0,4.6,1.4,Iris-versicolor
|
93
|
+
5.8,2.6,4.0,1.2,Iris-versicolor
|
94
|
+
5.0,2.3,3.3,1.0,Iris-versicolor
|
95
|
+
5.6,2.7,4.2,1.3,Iris-versicolor
|
96
|
+
5.7,3.0,4.2,1.2,Iris-versicolor
|
97
|
+
5.7,2.9,4.2,1.3,Iris-versicolor
|
98
|
+
6.2,2.9,4.3,1.3,Iris-versicolor
|
99
|
+
5.1,2.5,3.0,1.1,Iris-versicolor
|
100
|
+
5.7,2.8,4.1,1.3,Iris-versicolor
|
101
|
+
6.3,3.3,6.0,2.5,Iris-virginica
|
102
|
+
5.8,2.7,5.1,1.9,Iris-virginica
|
103
|
+
7.1,3.0,5.9,2.1,Iris-virginica
|
104
|
+
6.3,2.9,5.6,1.8,Iris-virginica
|
105
|
+
6.5,3.0,5.8,2.2,Iris-virginica
|
106
|
+
7.6,3.0,6.6,2.1,Iris-virginica
|
107
|
+
4.9,2.5,4.5,1.7,Iris-virginica
|
108
|
+
7.3,2.9,6.3,1.8,Iris-virginica
|
109
|
+
6.7,2.5,5.8,1.8,Iris-virginica
|
110
|
+
7.2,3.6,6.1,2.5,Iris-virginica
|
111
|
+
6.5,3.2,5.1,2.0,Iris-virginica
|
112
|
+
6.4,2.7,5.3,1.9,Iris-virginica
|
113
|
+
6.8,3.0,5.5,2.1,Iris-virginica
|
114
|
+
5.7,2.5,5.0,2.0,Iris-virginica
|
115
|
+
5.8,2.8,5.1,2.4,Iris-virginica
|
116
|
+
6.4,3.2,5.3,2.3,Iris-virginica
|
117
|
+
6.5,3.0,5.5,1.8,Iris-virginica
|
118
|
+
7.7,3.8,6.7,2.2,Iris-virginica
|
119
|
+
7.7,2.6,6.9,2.3,Iris-virginica
|
120
|
+
6.0,2.2,5.0,1.5,Iris-virginica
|
121
|
+
6.9,3.2,5.7,2.3,Iris-virginica
|
122
|
+
5.6,2.8,4.9,2.0,Iris-virginica
|
123
|
+
7.7,2.8,6.7,2.0,Iris-virginica
|
124
|
+
6.3,2.7,4.9,1.8,Iris-virginica
|
125
|
+
6.7,3.3,5.7,2.1,Iris-virginica
|
126
|
+
7.2,3.2,6.0,1.8,Iris-virginica
|
127
|
+
6.2,2.8,4.8,1.8,Iris-virginica
|
128
|
+
6.1,3.0,4.9,1.8,Iris-virginica
|
129
|
+
6.4,2.8,5.6,2.1,Iris-virginica
|
130
|
+
7.2,3.0,5.8,1.6,Iris-virginica
|
131
|
+
7.4,2.8,6.1,1.9,Iris-virginica
|
132
|
+
7.9,3.8,6.4,2.0,Iris-virginica
|
133
|
+
6.4,2.8,5.6,2.2,Iris-virginica
|
134
|
+
6.3,2.8,5.1,1.5,Iris-virginica
|
135
|
+
6.1,2.6,5.6,1.4,Iris-virginica
|
136
|
+
7.7,3.0,6.1,2.3,Iris-virginica
|
137
|
+
6.3,3.4,5.6,2.4,Iris-virginica
|
138
|
+
6.4,3.1,5.5,1.8,Iris-virginica
|
139
|
+
6.0,3.0,4.8,1.8,Iris-virginica
|
140
|
+
6.9,3.1,5.4,2.1,Iris-virginica
|
141
|
+
6.7,3.1,5.6,2.4,Iris-virginica
|
142
|
+
6.9,3.1,5.1,2.3,Iris-virginica
|
143
|
+
5.8,2.7,5.1,1.9,Iris-virginica
|
144
|
+
6.8,3.2,5.9,2.3,Iris-virginica
|
145
|
+
6.7,3.3,5.7,2.5,Iris-virginica
|
146
|
+
6.7,3.0,5.2,2.3,Iris-virginica
|
147
|
+
6.3,2.5,5.0,1.9,Iris-virginica
|
148
|
+
6.5,3.0,5.2,2.0,Iris-virginica
|
149
|
+
6.2,3.4,5.4,2.3,Iris-virginica
|
150
|
+
5.9,3.0,5.1,1.8,Iris-virginica
|
@@ -0,0 +1,53 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'brains'
|
4
|
+
|
5
|
+
# RNN to approximate a sine function sequence
|
6
|
+
|
7
|
+
def generate_sine_test_data(start_t, end_t)
|
8
|
+
inputs = []
|
9
|
+
outputs = []
|
10
|
+
|
11
|
+
(start_t...end_t).each do |t|
|
12
|
+
inputs << [Math.sin(t)]
|
13
|
+
outputs << [Math.sin(t + 1)]
|
14
|
+
end
|
15
|
+
|
16
|
+
[[inputs, outputs]]
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
training_data = generate_sine_test_data(0, 10)
|
22
|
+
|
23
|
+
testing_data = generate_sine_test_data(11, 20)
|
24
|
+
|
25
|
+
# input sequence
|
26
|
+
input_sequence = training_data[0][0].map { |a| a[0] }
|
27
|
+
test_input_sequence = testing_data[0][0].map { |a| a[0] }
|
28
|
+
test_output_sequence = testing_data[0][1].map { |a| a[0] }
|
29
|
+
|
30
|
+
nn = Brains::Net.create(1, 1, 1, { neurons_per_layer: 3,
|
31
|
+
learning_rate: 0.01,
|
32
|
+
recurrent: true,
|
33
|
+
output_function: :htan,
|
34
|
+
})
|
35
|
+
|
36
|
+
# randomize weights before training
|
37
|
+
nn.randomize_weights
|
38
|
+
|
39
|
+
results = nn.feed(testing_data[0][0])
|
40
|
+
results.each_with_index do |a, index|
|
41
|
+
puts "#{test_input_sequence[index]} => #{a[0]}"
|
42
|
+
end
|
43
|
+
|
44
|
+
result = nn.optimize_recurrent(training_data, 0.001, 100_000, 10_000 ) { |i, error|
|
45
|
+
puts "#{i} #{error}"
|
46
|
+
}
|
47
|
+
|
48
|
+
results = nn.feed(testing_data[0][0])
|
49
|
+
results.each_with_index do |a, index|
|
50
|
+
puts "#{test_input_sequence[index]} => #{a[0]} (#{test_output_sequence[index]})"
|
51
|
+
end
|
52
|
+
|
53
|
+
puts nn.to_json
|
data/lib/brains/brains.jar
CHANGED
Binary file
|
data/lib/brains/net.rb
CHANGED
@@ -13,10 +13,11 @@ module Brains
|
|
13
13
|
config.learningRate = opts[:learning_rate] || 0.1
|
14
14
|
config.neuronsPerLayer = neurons_per_layer
|
15
15
|
config.momentumFactor = opts[:momentum_factor] || 0.5
|
16
|
+
config.isRecurrent = opts[:recurrent] || false
|
16
17
|
config.backPropagationAlgorithm = opt_t_back_alg(opts[:train_method] || :standard)
|
17
18
|
config.activationFunctionType = opt_to_func(opts[:activation_function] || :htan)
|
18
|
-
config.outputActivationFunctionType = opt_to_func(opts[:
|
19
|
-
config.errorFormula = opt_to_error_func(opts[:
|
19
|
+
config.outputActivationFunctionType = opt_to_func(opts[:output_function] || :sigmoid)
|
20
|
+
config.errorFormula = opt_to_error_func(opts[:error] || :mean_squared)
|
20
21
|
nn = com.dayosoft.nn.NeuralNet.new(config);
|
21
22
|
|
22
23
|
Brains::Net.new.set_nn(nn).set_config(config)
|
@@ -55,8 +56,68 @@ module Brains
|
|
55
56
|
{ iterations: result.first, error: result.second }
|
56
57
|
end
|
57
58
|
|
59
|
+
def optimize_recurrent(test_cases, target_error = 0.01, max_epoch = 1_000_000_000,
|
60
|
+
callback_interval = 1000, max_layers = 0, &callback)
|
61
|
+
inputs = []
|
62
|
+
outputs = []
|
63
|
+
|
64
|
+
input_set = java.util.ArrayList.new
|
65
|
+
output_set = java.util.ArrayList.new
|
66
|
+
|
67
|
+
test_cases.each do |item|
|
68
|
+
|
69
|
+
inputs = java.util.ArrayList.new
|
70
|
+
outputs = java.util.ArrayList.new
|
71
|
+
|
72
|
+
item[0].each do |item|
|
73
|
+
inputs.add(item.to_java(Java::double))
|
74
|
+
end
|
75
|
+
|
76
|
+
item[1].each do |item|
|
77
|
+
outputs.add(item.to_java(Java::double))
|
78
|
+
end
|
79
|
+
|
80
|
+
input_set.add(inputs)
|
81
|
+
output_set.add(outputs)
|
82
|
+
end
|
83
|
+
|
84
|
+
result = @nn.optimizeRecurrent(input_set, output_set, target_error, max_layers, max_epoch,
|
85
|
+
callback_interval, callback)
|
86
|
+
|
87
|
+
{ iterations: result.first, error: result.second }
|
88
|
+
end
|
89
|
+
|
58
90
|
def feed(input)
|
59
|
-
|
91
|
+
# recurrent mode when array of array is passed.
|
92
|
+
if input && input.size > 0 && input[0].kind_of?(Array)
|
93
|
+
feed_recurrent(input)
|
94
|
+
else
|
95
|
+
result = @nn.feed(input.to_java(Java::double)).to_a
|
96
|
+
@nn.updatePreviousOutputs if config.isRecurrent
|
97
|
+
|
98
|
+
result
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# For a recurrent network, this resets hidden states back to zero
|
103
|
+
def reset
|
104
|
+
if config.isRecurrent
|
105
|
+
@nn.resetRecurrentStates
|
106
|
+
else
|
107
|
+
puts "Warning not a recurrent network. This does nothing"
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def feed_recurrent(inputs)
|
112
|
+
inputset = java.util.ArrayList.new
|
113
|
+
inputs.each do |input|
|
114
|
+
inputset.add(input.to_java(Java::double))
|
115
|
+
end
|
116
|
+
|
117
|
+
output_set = @nn.feed(inputset).to_a
|
118
|
+
output_set.collect do |output|
|
119
|
+
output.to_a
|
120
|
+
end
|
60
121
|
end
|
61
122
|
|
62
123
|
def to_json
|
@@ -101,6 +162,8 @@ module Brains
|
|
101
162
|
com.dayosoft.nn.NeuralNet::Config::STANDARD_BACKPROPAGATION
|
102
163
|
when :rprop
|
103
164
|
com.dayosoft.nn.NeuralNet::Config::RPROP_BACKPROPAGATION
|
165
|
+
else
|
166
|
+
raise "Invalid backpropagation method #{func}"
|
104
167
|
end
|
105
168
|
end
|
106
169
|
|
data/lib/brains/version.rb
CHANGED
metadata
CHANGED
@@ -1,58 +1,59 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: brains
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: java
|
6
6
|
authors:
|
7
7
|
- Joseph Emmanuel Dayo
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-03-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name: bundler
|
15
|
-
version_requirements: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '1.12'
|
20
14
|
requirement: !ruby/object:Gem::Requirement
|
21
15
|
requirements:
|
22
16
|
- - "~>"
|
23
17
|
- !ruby/object:Gem::Version
|
24
18
|
version: '1.12'
|
19
|
+
name: bundler
|
25
20
|
prerelease: false
|
26
21
|
type: :development
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: rake
|
29
22
|
version_requirements: !ruby/object:Gem::Requirement
|
30
23
|
requirements:
|
31
24
|
- - "~>"
|
32
25
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
34
28
|
requirement: !ruby/object:Gem::Requirement
|
35
29
|
requirements:
|
36
30
|
- - "~>"
|
37
31
|
- !ruby/object:Gem::Version
|
38
32
|
version: '10.0'
|
33
|
+
name: rake
|
39
34
|
prerelease: false
|
40
35
|
type: :development
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: rspec
|
43
36
|
version_requirements: !ruby/object:Gem::Requirement
|
44
37
|
requirements:
|
45
38
|
- - "~>"
|
46
39
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
48
42
|
requirement: !ruby/object:Gem::Requirement
|
49
43
|
requirements:
|
50
44
|
- - "~>"
|
51
45
|
- !ruby/object:Gem::Version
|
52
46
|
version: '3.0'
|
47
|
+
name: rspec
|
53
48
|
prerelease: false
|
54
49
|
type: :development
|
55
|
-
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: 'A feedforward neural network library for JRuby. Aims to provide a quick
|
56
|
+
way to get started on machine learning with ruby '
|
56
57
|
email:
|
57
58
|
- joseph.dayo@gmail.com
|
58
59
|
executables: []
|
@@ -62,6 +63,7 @@ files:
|
|
62
63
|
- ".gitignore"
|
63
64
|
- ".rspec"
|
64
65
|
- ".travis.yml"
|
66
|
+
- CHANGELOG.md
|
65
67
|
- CODE_OF_CONDUCT.md
|
66
68
|
- Gemfile
|
67
69
|
- LICENSE.txt
|
@@ -71,7 +73,9 @@ files:
|
|
71
73
|
- bin/setup
|
72
74
|
- brains.gemspec
|
73
75
|
- example/colors.rb
|
76
|
+
- example/iris.data
|
74
77
|
- example/iris.rb
|
78
|
+
- example/sine_function.rb
|
75
79
|
- example/xor.rb
|
76
80
|
- iris.data
|
77
81
|
- lib/brains.rb
|
@@ -102,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
106
|
version: '0'
|
103
107
|
requirements: []
|
104
108
|
rubyforge_project:
|
105
|
-
rubygems_version: 2.
|
109
|
+
rubygems_version: 2.6.8
|
106
110
|
signing_key:
|
107
111
|
specification_version: 4
|
108
112
|
summary: A feedforward neural network library for JRuby
|