ai4r 1.3 → 1.4
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.
- data/README.rdoc +6 -12
- data/examples/neural_network/backpropagation_example.rb +18 -16
- data/examples/neural_network/xor_example.rb +30 -20
- data/lib/ai4r/classifiers/classifier.rb +15 -4
- data/lib/ai4r/classifiers/id3.rb +31 -31
- data/lib/ai4r/clusterers/clusterer.rb +5 -24
- data/lib/ai4r/clusterers/k_means.rb +7 -38
- data/lib/ai4r/data/data_set.rb +4 -2
- data/lib/ai4r/data/parameterizable.rb +64 -0
- data/lib/ai4r/neural_network/backpropagation.rb +233 -210
- data/site/build/site/en/downloads.html +3 -3
- data/site/build/site/en/geneticAlgorithms.html +3 -3
- data/site/build/site/en/index.html +32 -15
- data/site/build/site/en/index.pdf +126 -100
- data/site/build/site/en/linkmap.html +7 -9
- data/site/build/site/en/linkmap.pdf +12 -12
- data/site/build/site/en/machineLearning.html +7 -6
- data/site/build/site/en/machineLearning.pdf +29 -29
- data/site/build/site/en/neuralNetworks.html +164 -127
- data/site/build/site/en/neuralNetworks.pdf +267 -200
- data/site/build/site/en/svn.html +4 -4
- data/site/build/tmp/cocoon-work/cache-dir/cocoon-ehcache-1.data +0 -0
- data/site/build/tmp/cocoon-work/cache-dir/cocoon-ehcache-1.index +0 -0
- data/site/build/tmp/projfilters.properties +1 -1
- data/site/build/webapp/WEB-INF/logs/core.log +670 -489
- data/site/build/webapp/WEB-INF/logs/error.log +213 -364
- data/site/build/webapp/WEB-INF/logs/sitemap.log +0 -368
- data/site/src/documentation/content/xdocs/index.xml +1 -1
- data/site/src/documentation/content/xdocs/neuralNetworks.xml +118 -90
- data/site/src/documentation/content/xdocs/site.xml +2 -3
- data/test/neural_network/backpropagation_test.rb +23 -0
- metadata +5 -7
- data/site/build/site/en/forum.html +0 -197
- data/site/build/site/en/forum.pdf +0 -151
- data/site/build/site/en/wholesite.pdf +0 -1915
@@ -115,13 +115,13 @@ document.write("Last Published: " + document.lastModified);
|
|
115
115
|
<a href="http://forum.jadeferret.com/viewforum.php?f=3" title="Ai4r Forum">Forum</a>
|
116
116
|
</div>
|
117
117
|
<div class="menuitem">
|
118
|
-
<a href="
|
118
|
+
<a href="svn.html" title="ai4r Subversion repository">Source Code repository</a>
|
119
119
|
</div>
|
120
120
|
<div class="menuitem">
|
121
|
-
<a href="
|
121
|
+
<a href="http://wiki.jadeferret.com/Category:AI4R" title="ai4r wiki with How-to guides and more documentation">AI4R Wiki</a>
|
122
122
|
</div>
|
123
123
|
<div class="menuitem">
|
124
|
-
<a href="
|
124
|
+
<a href="http://ai4r.rubyforge.org/rdoc/index.html" title="Ai4 RDoc docs">API doc (Rdoc)</a>
|
125
125
|
</div>
|
126
126
|
</div>
|
127
127
|
<div id="credit">
|
@@ -208,23 +208,21 @@ google_ad_height = 125;
|
|
208
208
|
|
209
209
|
<ul>
|
210
210
|
<li>
|
211
|
-
<a href="
|
211
|
+
<a href="svn.html">Source Code repository</a> ___________________ <em>svn</em> : ai4r Subversion repository</li>
|
212
212
|
</ul>
|
213
213
|
|
214
214
|
<ul>
|
215
215
|
<li>
|
216
|
-
<a href="
|
216
|
+
<a class="external" href="http://wiki.jadeferret.com/Category:AI4R">AI4R Wiki</a> ___________________ <em>wiki</em> : ai4r wiki with How-to guides and more documentation</li>
|
217
217
|
</ul>
|
218
218
|
|
219
219
|
<ul>
|
220
220
|
<li>
|
221
|
-
<a href="
|
222
|
-
</
|
223
|
-
</ul>
|
221
|
+
<a href="http://ai4r.rubyforge.org/rdoc/index.html">API doc (Rdoc)</a> ___________________ <em>rdoc</em> : Ai4 RDoc docs</li>
|
222
|
+
</ul>
|
224
223
|
|
225
224
|
</ul>
|
226
225
|
</ul>
|
227
|
-
|
228
226
|
|
229
227
|
</ul>
|
230
228
|
</ul>
|
@@ -5,10 +5,10 @@
|
|
5
5
|
/Producer (FOP 0.20.5) >>
|
6
6
|
endobj
|
7
7
|
5 0 obj
|
8
|
-
<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3qam<a0eGoh'7A(G3fp(.0_q;U&P*ilnHD`J'F"3R?DO?1[dqd0X6DlCY5bRX\OEY-d_DENe`LbK3)kYeQT1kb<iOk8mH$L=fgA=.D2:(FEAJpE8M`HM1_GVWo6/&4Ct8e-2O%J<,L6W4/1CT3ZYZ0r2P\qmT[dU4=(V0rS2"1%>nCPA&lp_ftR$'jIXZ=c\A(D'8m'D*:]m0WsdhUOq(A++!'jRN%<s,o)"M^?9(,N?5P`Ct__U6??Lhr<lH0??l~>
|
12
12
|
endstream
|
13
13
|
endobj
|
14
14
|
6 0 obj
|
@@ -72,17 +72,17 @@ endobj
|
|
72
72
|
xref
|
73
73
|
0 12
|
74
74
|
0000000000 65535 f
|
75
|
-
|
76
|
-
|
77
|
-
|
75
|
+
0000002030 00000 n
|
76
|
+
0000002088 00000 n
|
77
|
+
0000002138 00000 n
|
78
78
|
0000000015 00000 n
|
79
79
|
0000000071 00000 n
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
80
|
+
0000001369 00000 n
|
81
|
+
0000001475 00000 n
|
82
|
+
0000001587 00000 n
|
83
|
+
0000001696 00000 n
|
84
|
+
0000001806 00000 n
|
85
|
+
0000001914 00000 n
|
86
86
|
trailer
|
87
87
|
<<
|
88
88
|
/Size 12
|
@@ -90,5 +90,5 @@ trailer
|
|
90
90
|
/Info 4 0 R
|
91
91
|
>>
|
92
92
|
startxref
|
93
|
-
|
93
|
+
2258
|
94
94
|
%%EOF
|
@@ -115,13 +115,13 @@ document.write("Last Published: " + document.lastModified);
|
|
115
115
|
<a href="http://forum.jadeferret.com/viewforum.php?f=3" title="Ai4r Forum">Forum</a>
|
116
116
|
</div>
|
117
117
|
<div class="menuitem">
|
118
|
-
<a href="
|
118
|
+
<a href="svn.html" title="ai4r Subversion repository">Source Code repository</a>
|
119
119
|
</div>
|
120
120
|
<div class="menuitem">
|
121
|
-
<a href="
|
121
|
+
<a href="http://wiki.jadeferret.com/Category:AI4R" title="ai4r wiki with How-to guides and more documentation">AI4R Wiki</a>
|
122
122
|
</div>
|
123
123
|
<div class="menuitem">
|
124
|
-
<a href="
|
124
|
+
<a href="http://ai4r.rubyforge.org/rdoc/index.html" title="Ai4 RDoc docs">API doc (Rdoc)</a>
|
125
125
|
</div>
|
126
126
|
</div>
|
127
127
|
<div id="credit">
|
@@ -229,7 +229,7 @@ The only information that you have is a collection of examples, provided by a ma
|
|
229
229
|
and identify new posible marketing targets:</p>
|
230
230
|
<pre class="code">
|
231
231
|
|
232
|
-
id3.
|
232
|
+
id3.get_rules
|
233
233
|
# => if age_range=='<30' then marketing_target='Y'
|
234
234
|
elsif age_range=='[30-50)' and city=='Chicago' then marketing_target='Y'
|
235
235
|
elsif age_range=='[30-50)' and city=='New York' then marketing_target='N'
|
@@ -277,7 +277,8 @@ id3.eval(['New York', '<30', 'M'])
|
|
277
277
|
# => 'Y'
|
278
278
|
</pre>
|
279
279
|
<p>
|
280
|
-
But instead of going through the tree every time, you can take advantage of the
|
280
|
+
But instead of going through the tree every time, you can take advantage of the
|
281
|
+
fact that the method "get_rules" generates proper ruby code!
|
281
282
|
</p>
|
282
283
|
<pre class="code">
|
283
284
|
|
@@ -286,7 +287,7 @@ But instead of going through the tree every time, you can take advantage of the
|
|
286
287
|
city = 'New York'
|
287
288
|
gender = 'M'
|
288
289
|
marketing_target = nil
|
289
|
-
eval id3.
|
290
|
+
eval id3.get_rules
|
290
291
|
puts marketing_target
|
291
292
|
# => 'Y'
|
292
293
|
</pre>
|
@@ -95,10 +95,10 @@ endobj
|
|
95
95
|
>>
|
96
96
|
endobj
|
97
97
|
20 0 obj
|
98
|
-
<< /Length
|
98
|
+
<< /Length 1802 /Filter [ /ASCII85Decode /FlateDecode ]
|
99
99
|
>>
|
100
100
|
stream
|
101
|
-
Gau0DD/\/e&H;*)Tqrjt!!t0=H7b9]8X^>FdUGk.!C[tFN()
|
101
|
+
Gau0DD/\/e&H;*)Tqrjt!!t0=H7b9]8X^>FdUGk.!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"(sY.OKi*akYmJ8?U*:EMPfjMRd%Gjn*jWC$coY5soYZPGIdN0To1^fXW@jIjXZ0s?KlNDOo?)cT%9D].a:B3R7L*^o+?QQajUX`XV/)=.F(/>(tf35Ad,Uk81I0jBJc1F2VSHN5S'K%+(!]kj+)4u3A!8W'C&m!_Y?@3L?4`[r/7?0;A*.DPHo\7i,HDZjIN6UI:03Wo#00>HMr@!0'2`g(P,Btu=,ONqhe[X*a9WbTgp(/T[kOqL8]W^iSsCPgbOX4tp=cdC`6DLp3K82Q:@h>4P?cc&T</YSdVI#b:e\\t]-nh_H_RX<s6nq]tMhW!6\^;2Ucg>4!jB5&W@H-K<b5")\JeK?"69;u)=.1L>0@lkn!LQh8Mo_Nrod^3,f4?5R>]^EmB)Gjnpjj;de3X&Mbi@ZH@i+&>Kq7X!/qlZu-(H7Bf?DB(T0>c<DbjS3NpF[ae^q-_1SXbjfe]l40`2ij/irlrr]_J6(jjX:TAuecnIBHNQ"j@=0o*bpJY6\^iIJA_f/`WY*+W.W)>f,]&$<<.hc!ii52@C8b2tR>8MmMukAJW=:#[(phDBHA<8d3`LoVt5NCAYqnX!B@$Iur[]3lPg2AYDHm^G>0-$8NMk*4_pA-&;]NgKj`(O:O2]\5qO<O/bMB/DWFgGd/gH`".aAQatUBJAT$oY,,=?WM(tS\3hY*aBu)tL+*`%9;NonI8\oa8tqE:+^/JUh9-)s^:s]'id_"~>
|
102
102
|
endstream
|
103
103
|
endobj
|
104
104
|
21 0 obj
|
@@ -288,44 +288,44 @@ endobj
|
|
288
288
|
xref
|
289
289
|
0 39
|
290
290
|
0000000000 65535 f
|
291
|
-
|
292
|
-
|
293
|
-
|
291
|
+
0000009548 00000 n
|
292
|
+
0000009627 00000 n
|
293
|
+
0000009719 00000 n
|
294
294
|
0000000015 00000 n
|
295
295
|
0000000071 00000 n
|
296
296
|
0000000834 00000 n
|
297
297
|
0000000954 00000 n
|
298
298
|
0000001007 00000 n
|
299
|
-
|
299
|
+
0000009853 00000 n
|
300
300
|
0000001142 00000 n
|
301
|
-
|
301
|
+
0000009916 00000 n
|
302
302
|
0000001278 00000 n
|
303
|
-
|
303
|
+
0000009982 00000 n
|
304
304
|
0000001414 00000 n
|
305
|
-
|
305
|
+
0000010047 00000 n
|
306
306
|
0000001551 00000 n
|
307
|
-
|
307
|
+
0000010113 00000 n
|
308
308
|
0000001688 00000 n
|
309
309
|
0000003674 00000 n
|
310
310
|
0000003782 00000 n
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
311
|
+
0000005677 00000 n
|
312
|
+
0000005785 00000 n
|
313
|
+
0000006805 00000 n
|
314
|
+
0000006928 00000 n
|
315
|
+
0000006962 00000 n
|
316
|
+
0000007155 00000 n
|
317
|
+
0000010178 00000 n
|
318
|
+
0000007350 00000 n
|
319
|
+
0000007611 00000 n
|
320
|
+
0000008103 00000 n
|
321
|
+
0000008321 00000 n
|
322
|
+
0000008602 00000 n
|
323
|
+
0000008886 00000 n
|
324
|
+
0000008999 00000 n
|
325
|
+
0000009109 00000 n
|
326
|
+
0000009217 00000 n
|
327
|
+
0000009323 00000 n
|
328
|
+
0000009439 00000 n
|
329
329
|
trailer
|
330
330
|
<<
|
331
331
|
/Size 39
|
@@ -333,5 +333,5 @@ trailer
|
|
333
333
|
/Info 4 0 R
|
334
334
|
>>
|
335
335
|
startxref
|
336
|
-
|
336
|
+
10229
|
337
337
|
%%EOF
|
@@ -5,7 +5,7 @@
|
|
5
5
|
<meta content="Apache Forrest" name="Generator">
|
6
6
|
<meta name="Forrest-version" content="0.8">
|
7
7
|
<meta name="Forrest-skin-name" content="pelt">
|
8
|
-
<title>Backpropagation
|
8
|
+
<title>OCR example using Backpropagation networks in ruby :: ai4r</title>
|
9
9
|
<link type="text/css" href="skin/basic.css" rel="stylesheet">
|
10
10
|
<link media="screen" type="text/css" href="skin/screen.css" rel="stylesheet">
|
11
11
|
<link media="print" type="text/css" href="skin/print.css" rel="stylesheet">
|
@@ -115,13 +115,13 @@ document.write("Last Published: " + document.lastModified);
|
|
115
115
|
<a href="http://forum.jadeferret.com/viewforum.php?f=3" title="Ai4r Forum">Forum</a>
|
116
116
|
</div>
|
117
117
|
<div class="menuitem">
|
118
|
-
<a href="
|
118
|
+
<a href="svn.html" title="ai4r Subversion repository">Source Code repository</a>
|
119
119
|
</div>
|
120
120
|
<div class="menuitem">
|
121
|
-
<a href="
|
121
|
+
<a href="http://wiki.jadeferret.com/Category:AI4R" title="ai4r wiki with How-to guides and more documentation">AI4R Wiki</a>
|
122
122
|
</div>
|
123
123
|
<div class="menuitem">
|
124
|
-
<a href="
|
124
|
+
<a href="http://ai4r.rubyforge.org/rdoc/index.html" title="Ai4 RDoc docs">API doc (Rdoc)</a>
|
125
125
|
</div>
|
126
126
|
</div>
|
127
127
|
<div id="credit">
|
@@ -151,17 +151,20 @@ google_ad_height = 125;
|
|
151
151
|
<a class="dida" href="neuralNetworks.pdf"><img alt="PDF -icon" src="skin/images/pdfdoc.gif" class="skin"><br>
|
152
152
|
PDF</a>
|
153
153
|
</div>
|
154
|
-
<h1>Backpropagation
|
154
|
+
<h1>OCR example using Backpropagation networks in ruby :: ai4r</h1>
|
155
155
|
<div id="minitoc-area">
|
156
156
|
<ul class="minitoc">
|
157
157
|
<li>
|
158
|
-
<a href="#nn-Introduction">Introduction to Neural Networks
|
158
|
+
<a href="#nn-Introduction">Introduction to Neural Networks</a>
|
159
|
+
</li>
|
160
|
+
<li>
|
161
|
+
<a href="#nn-example">Modeling the OCR problem using Neural Networks networks</a>
|
159
162
|
</li>
|
160
163
|
<li>
|
161
|
-
<a href="#
|
164
|
+
<a href="#nn2-example">Implementing a basic OCR application using ruby and AI4R</a>
|
162
165
|
</li>
|
163
166
|
<li>
|
164
|
-
<a href="#
|
167
|
+
<a href="#nn3-example">Results obtained with the AI4R OCR algorithm</a>
|
165
168
|
</li>
|
166
169
|
<li>
|
167
170
|
<a href="#nn-custom">Customizing your neural network in ai4r</a>
|
@@ -173,7 +176,7 @@ google_ad_height = 125;
|
|
173
176
|
</div>
|
174
177
|
|
175
178
|
<a name="N1000D"></a><a name="nn-Introduction"></a>
|
176
|
-
<h2 class="boxed">Introduction to Neural Networks
|
179
|
+
<h2 class="boxed">Introduction to Neural Networks</h2>
|
177
180
|
<div class="section">
|
178
181
|
<p>
|
179
182
|
The utility of artificial neural network models lies in the fact
|
@@ -194,31 +197,9 @@ google_ad_height = 125;
|
|
194
197
|
Rumelhart, Geoffrey E. Hinton and Ronald J. Williams in 1986)
|
195
198
|
</p>
|
196
199
|
</div>
|
197
|
-
|
198
|
-
<a name="N1001A"></a><a name="nn-HowTo"></a>
|
199
|
-
<h2 class="boxed">How to use Backpropagation Neural Networks in Ruby</h2>
|
200
|
-
<div class="section">
|
201
|
-
<pre class="code">
|
202
|
-
|
203
|
-
# Create the network
|
204
|
-
net = Ai4r::NeuralNetwork::Backpropagation.new([4, 3, 2])
|
205
|
-
# 4 inputs
|
206
|
-
# 1 hidden layer with 3 neurons
|
207
|
-
# 2 outputs
|
208
|
-
# Train the network
|
209
|
-
1..upto(100) do |i|
|
210
|
-
net.train(example[i], result[i])
|
211
|
-
end
|
212
|
-
|
213
|
-
# Use it: Evaluate data with the trained network
|
214
|
-
net.eval([12, 48, 12, 25]) # => [0.89, 0.04]
|
215
|
-
|
216
|
-
</pre>
|
217
|
-
</div>
|
218
200
|
|
219
|
-
|
220
|
-
<
|
221
|
-
<h2 class="boxed">OCR example using Backpropagation networks in ruby</h2>
|
201
|
+
<a name="N1001A"></a><a name="nn-example"></a>
|
202
|
+
<h2 class="boxed">Modeling the OCR problem using Neural Networks networks</h2>
|
222
203
|
<div class="section">
|
223
204
|
<p>
|
224
205
|
Let's imagine that we have to implement a program to identify simple patterns
|
@@ -228,12 +209,14 @@ google_ad_height = 125;
|
|
228
209
|
signal).
|
229
210
|
</p>
|
230
211
|
<p>
|
231
|
-
|
232
|
-
neural network to identify
|
212
|
+
In order to solve this problem, we can take an example of each pattern
|
213
|
+
to be recognized, and train a neural network to identify similar patterns.
|
214
|
+
In fact, one of the most popular uses of neural networks in business
|
215
|
+
applications is OCR (opticar character recognition)
|
233
216
|
</p>
|
234
217
|
<p>
|
235
|
-
|
236
|
-
|
218
|
+
In our porposed solotion, we create a network with the following
|
219
|
+
architecture: 256 input neurons and 3 output neurons. We feed this network
|
237
220
|
with 16x16 matrices (in fact will convert them to vectors of length 256).
|
238
221
|
Each pixel is represented with a number from 0 (white pixel) to 10
|
239
222
|
(black pixel). The output of this network if a 3 vector of dimension 3,
|
@@ -262,81 +245,56 @@ google_ad_height = 125;
|
|
262
245
|
</tr>
|
263
246
|
|
264
247
|
</table>
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
</
|
269
|
-
<
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
<li>
|
275
|
-
<img alt="Triangule training example" src="/images/t.png">
|
276
|
-
[0.98, 0.03, 0.01] => TRIANGLE </li>
|
277
|
-
|
278
|
-
<li>
|
279
|
-
<img alt="Square training example" src="/images/s.png">
|
280
|
-
[0.00, 0.96, 0.03] => SQUARE </li>
|
281
|
-
|
282
|
-
<li>
|
283
|
-
<img alt="Cross training example" src="/images/c.png">
|
284
|
-
[0.00, 0.00, 0.99] => CROSS </li>
|
285
|
-
|
286
|
-
</ol>
|
287
|
-
|
288
|
-
</li>
|
289
|
-
|
290
|
-
<li>Evaluating the patterns with random noise with the trained network:
|
291
|
-
<ol>
|
292
|
-
|
293
|
-
<li>
|
294
|
-
<img alt="Triangule pattern with random noise" src="/images/t_wn.png"> [0.98, 0.01, 0.01] => TRIANGLE </li>
|
295
|
-
|
296
|
-
<li>
|
297
|
-
<img alt="Square pattern with random noise" src="/images/s_wn.png"> [0.00, 0.96, 0.02] => SQUARE </li>
|
298
|
-
|
299
|
-
<li>
|
300
|
-
<img alt="Cross pattern with random noise" src="/images/c_wn.png"> [0.00, 0.00, 0.98] => CROSS </li>
|
301
|
-
|
302
|
-
</ol>
|
303
|
-
|
304
|
-
</li>
|
305
|
-
|
306
|
-
<li>Evaluating the patterns with line noise with the trained network:
|
307
|
-
<ol>
|
308
|
-
|
309
|
-
<li>
|
310
|
-
<img alt="Triangule pattern with line noise" src="/images/t_wbn.png"> [0.62, 0.00, 0.02] => TRIANGLE </li>
|
311
|
-
|
312
|
-
<li>
|
313
|
-
<img alt="Square pattern with line noise" src="/images/s_wbn.png"> [0.00, 0.75, 0.01] => SQUARE </li>
|
314
|
-
|
315
|
-
<li>
|
316
|
-
<img alt="Cross pattern with line noise" src="/images/c_wbn.png"> [0.00, 0.00, 0.98] => CROSS </li>
|
317
|
-
|
318
|
-
</ol>
|
319
|
-
|
320
|
-
</li>
|
248
|
+
</div>
|
249
|
+
|
250
|
+
<a name="N10057"></a><a name="nn2-example"></a>
|
251
|
+
<h2 class="boxed">Implementing a basic OCR application using ruby and AI4R</h2>
|
252
|
+
<div class="section">
|
253
|
+
<p>The code bellow shows the basic steps to use Backpropagation Neural
|
254
|
+
Networks in AI4R:</p>
|
255
|
+
<pre class="code">
|
321
256
|
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
257
|
+
# Create the network with:
|
258
|
+
# 4 inputs
|
259
|
+
# 1 hidden layer with 3 neurons
|
260
|
+
# 2 outputs
|
261
|
+
net = Ai4r::NeuralNetwork::Backpropagation.new([4, 3, 2])
|
262
|
+
|
263
|
+
# Train the network
|
264
|
+
100.times do |i|
|
265
|
+
net.train(example[i], result[i])
|
266
|
+
end
|
267
|
+
|
268
|
+
# Use it: Evaluate data with the trained network
|
269
|
+
net.eval([12, 48, 12, 25]) # => [0.89, 0.04]
|
270
|
+
|
271
|
+
</pre>
|
272
|
+
<p>This is the source code used to elaborate this simple OCR application
|
273
|
+
(You can find it inside the AI4R zip file release):</p>
|
326
274
|
<pre class="code">
|
327
275
|
|
328
276
|
require "rubygems"
|
329
|
-
require "ai4r
|
277
|
+
require "ai4r"
|
330
278
|
require File.dirname(__FILE__) + '/training_patterns'
|
331
279
|
require File.dirname(__FILE__) + '/patterns_with_noise'
|
332
280
|
require File.dirname(__FILE__) + '/patterns_with_base_noise'
|
333
281
|
|
334
|
-
|
282
|
+
# Create a network with 256 inputs, and 3 outputs
|
283
|
+
net = Ai4r::NeuralNetwork::Backpropagation.new([256, 3])
|
335
284
|
|
285
|
+
# Load training data
|
336
286
|
tr_input = TRIANGLE.flatten.collect { |input| input.to_f / 10}
|
337
287
|
sq_input = SQUARE.flatten.collect { |input| input.to_f / 10}
|
338
288
|
cr_input = CROSS.flatten.collect { |input| input.to_f / 10}
|
289
|
+
# Train the network
|
290
|
+
puts "Training the network, please wait."
|
291
|
+
100.times do
|
292
|
+
net.train(tr_input, [1,0,0])
|
293
|
+
net.train(sq_input, [0,1,0])
|
294
|
+
net.train(cr_input, [0,0,1])
|
295
|
+
end
|
339
296
|
|
297
|
+
# Load test data with noise
|
340
298
|
tr_with_noise = TRIANGLE_WITH_NOISE.flatten.collect { |input| input.to_f / 10}
|
341
299
|
sq_with_noise = SQUARE_WITH_NOISE.flatten.collect { |input| input.to_f / 10}
|
342
300
|
cr_with_noise = CROSS_WITH_NOISE.flatten.collect { |input| input.to_f / 10}
|
@@ -345,12 +303,7 @@ tr_with_base_noise = TRIANGLE_WITH_BASE_NOISE.flatten.collect { |input| input.to
|
|
345
303
|
sq_with_base_noise = SQUARE_WITH_BASE_NOISE.flatten.collect { |input| input.to_f / 10}
|
346
304
|
cr_with_base_noise = CROSS_WITH_BASE_NOISE.flatten.collect { |input| input.to_f / 10}
|
347
305
|
|
348
|
-
|
349
|
-
20.times do
|
350
|
-
net.train(tr_input, [1,0,0])
|
351
|
-
net.train(sq_input, [0,1,0])
|
352
|
-
net.train(cr_input, [0,0,1])
|
353
|
-
end
|
306
|
+
# Print the evaluation results
|
354
307
|
|
355
308
|
def result_label(result)
|
356
309
|
if result[0] > result[1] && result[0] > result[2]
|
@@ -377,45 +330,129 @@ puts "#{net.eval(cr_with_base_noise).inspect} => #{result_label(net.eval(cr_w
|
|
377
330
|
|
378
331
|
</pre>
|
379
332
|
</div>
|
333
|
+
|
334
|
+
<a name="N1006C"></a><a name="nn3-example"></a>
|
335
|
+
<h2 class="boxed">Results obtained with the AI4R OCR algorithm</h2>
|
336
|
+
<div class="section">
|
337
|
+
<p>
|
338
|
+
The results we got when we evaluate patterns with our trained network are:
|
339
|
+
</p>
|
340
|
+
<ul>
|
341
|
+
|
342
|
+
<li>Evaluating the training patterns with the trained network:
|
343
|
+
<ol>
|
344
|
+
|
345
|
+
<li>
|
346
|
+
<img alt="Triangule training example" src="/images/t.png">
|
347
|
+
[0.98, 0.03, 0.01] => TRIANGLE </li>
|
348
|
+
|
349
|
+
<li>
|
350
|
+
<img alt="Square training example" src="/images/s.png">
|
351
|
+
[0.00, 0.96, 0.03] => SQUARE </li>
|
352
|
+
|
353
|
+
<li>
|
354
|
+
<img alt="Cross training example" src="/images/c.png">
|
355
|
+
[0.00, 0.00, 0.99] => CROSS </li>
|
356
|
+
|
357
|
+
</ol>
|
358
|
+
|
359
|
+
</li>
|
360
|
+
|
361
|
+
<li>Evaluating the patterns with random noise with the trained network:
|
362
|
+
<ol>
|
363
|
+
|
364
|
+
<li>
|
365
|
+
<img alt="Triangule pattern with random noise" src="/images/t_wn.png"> [0.98, 0.01, 0.01] => TRIANGLE </li>
|
366
|
+
|
367
|
+
<li>
|
368
|
+
<img alt="Square pattern with random noise" src="/images/s_wn.png"> [0.00, 0.96, 0.02] => SQUARE </li>
|
369
|
+
|
370
|
+
<li>
|
371
|
+
<img alt="Cross pattern with random noise" src="/images/c_wn.png"> [0.00, 0.00, 0.98] => CROSS </li>
|
372
|
+
|
373
|
+
</ol>
|
374
|
+
|
375
|
+
</li>
|
376
|
+
|
377
|
+
<li>Evaluating the patterns with line noise with the trained network:
|
378
|
+
<ol>
|
379
|
+
|
380
|
+
<li>
|
381
|
+
<img alt="Triangule pattern with line noise" src="/images/t_wbn.png"> [0.62, 0.00, 0.02] => TRIANGLE </li>
|
382
|
+
|
383
|
+
<li>
|
384
|
+
<img alt="Square pattern with line noise" src="/images/s_wbn.png"> [0.00, 0.75, 0.01] => SQUARE </li>
|
385
|
+
|
386
|
+
<li>
|
387
|
+
<img alt="Cross pattern with line noise" src="/images/c_wbn.png"> [0.00, 0.00, 0.98] => CROSS </li>
|
388
|
+
|
389
|
+
</ol>
|
390
|
+
|
391
|
+
</li>
|
392
|
+
|
393
|
+
</ul>
|
394
|
+
<p>These results are satisfactory. The network could sucessfully identify
|
395
|
+
the patterns despite the noise introduced to them.</p>
|
396
|
+
</div>
|
380
397
|
|
381
398
|
|
382
|
-
<a name="
|
399
|
+
<a name="N100CD"></a><a name="nn-custom"></a>
|
383
400
|
<h2 class="boxed">Customizing your neural network in ai4r</h2>
|
384
401
|
<div class="section">
|
385
402
|
<p>Sometime for a given problem, you will have to "play around" with some parameters to
|
386
403
|
get to a solution. This parameters are:</p>
|
387
404
|
<p>
|
388
|
-
|
389
|
-
<
|
390
|
-
|
405
|
+
|
406
|
+
<strong>Learning Rate</strong>: a real number, usually between 0.05
|
407
|
+
and 0.25.
|
408
|
+
</p>
|
391
409
|
<p>
|
392
|
-
|
410
|
+
|
411
|
+
<strong>Momentum</strong>: A momentum will avoid oscillations during
|
412
|
+
learning, converging to a solution in less iterations.
|
413
|
+
</p>
|
393
414
|
<p>
|
394
|
-
<strong>
|
395
|
-
This function is called
|
396
|
-
"
|
415
|
+
<strong>Propagation function</strong>: By default, f(x) = 1/(1 + e^(-x)).
|
416
|
+
This function is called
|
417
|
+
<a class="external" href="http://en.wikipedia.org/wiki/Sigmoid_function">
|
418
|
+
Sigmoid function
|
419
|
+
</a>. You can see it like a "smoothed" version of the
|
420
|
+
<a class="external" href="http://en.wikipedia.org/wiki/Heaviside_step_function">
|
421
|
+
Heaviside step function
|
422
|
+
</a>. It will always provide a
|
397
423
|
value between 0 and 1. </p>
|
398
424
|
<p>
|
399
425
|
<img alt="Sigmoid function" src="images/sigmoid.png"></p>
|
400
426
|
<p>
|
401
|
-
Sometimes you will have better results with f(x) = x
|
402
|
-
|
427
|
+
Sometimes you will have better results with f(x) = tanh(x), or even with
|
428
|
+
f(x) = x. If you modify the propagation function, you have to supply the
|
429
|
+
derivative function too (in terms of the propagation function result).
|
430
|
+
</p>
|
431
|
+
<p>To customize these parameters in AI4R, you can user the "set_parameters"
|
432
|
+
method:</p>
|
403
433
|
<pre class="code">
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
413
|
-
|
434
|
+
net.set_parameters(
|
435
|
+
:momentum => 0.15,
|
436
|
+
:learning_rate => 0.5,
|
437
|
+
:propagation_function => lambda { |x| Math.tanh(x) },
|
438
|
+
:derivative_propagation_function => lambda { |y| 1.0 - y**2 }
|
439
|
+
)
|
440
|
+
</pre>
|
441
|
+
<p>You can also use the attribute accesors:</p>
|
442
|
+
<pre class="code">
|
443
|
+
net.momentum = 0.15
|
444
|
+
net.learning_rate = 0.5
|
445
|
+
net.propagation_function = lambda { |x| Math.tanh(x) }
|
446
|
+
net.derivative_propagation_function = lambda { |y| 1.0 - y**2 }
|
414
447
|
</pre>
|
415
448
|
</div>
|
416
|
-
|
417
449
|
|
418
|
-
<
|
450
|
+
<div class="note">
|
451
|
+
<div class="label">Note</div>
|
452
|
+
<div class="content">Remember to set the custom parameters BEFORE training the network</div>
|
453
|
+
</div>
|
454
|
+
|
455
|
+
<a name="N1010B"></a><a name="nn-more"></a>
|
419
456
|
<h2 class="boxed">More about Neural Networks and Backpropagation</h2>
|
420
457
|
<div class="section">
|
421
458
|
<ul>
|