ai4r 1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +48 -0
- data/examples/decision_trees/data_set.csv +121 -0
- data/examples/decision_trees/id3_example.rb +31 -0
- data/examples/decision_trees/results.txt +29 -0
- data/examples/genetic_algorithm/genetic_algorithm_example.rb +39 -0
- data/examples/genetic_algorithm/travel_cost.csv +16 -0
- data/examples/neural_network/backpropagation_example.rb +65 -0
- data/examples/neural_network/patterns_with_base_noise.rb +68 -0
- data/examples/neural_network/patterns_with_noise.rb +66 -0
- data/examples/neural_network/training_patterns.rb +68 -0
- data/lib/decision_tree/id3.rb +354 -0
- data/lib/genetic_algorithm/genetic_algorithm.rb +268 -0
- data/lib/neural_network/backpropagation.rb +259 -0
- data/site/build/site/en/broken-links.xml +2 -0
- data/site/build/site/en/downloads.html +187 -0
- data/site/build/site/en/downloads.pdf +151 -0
- data/site/build/site/en/geneticAlgorithms.html +564 -0
- data/site/build/site/en/geneticAlgorithms.pdf +911 -0
- data/site/build/site/en/images/ai4r-logo.png +0 -0
- data/site/build/site/en/images/built-with-forrest-button.png +0 -0
- data/site/build/site/en/images/c.png +0 -0
- data/site/build/site/en/images/c_wbn.png +0 -0
- data/site/build/site/en/images/c_wn.png +0 -0
- data/site/build/site/en/images/ero.gif +0 -0
- data/site/build/site/en/images/europe2.png +0 -0
- data/site/build/site/en/images/europe3.png +0 -0
- data/site/build/site/en/images/fitness.png +0 -0
- data/site/build/site/en/images/instruction_arrow.png +0 -0
- data/site/build/site/en/images/my_email.png +0 -0
- data/site/build/site/en/images/rubyforge.png +0 -0
- data/site/build/site/en/images/s.png +0 -0
- data/site/build/site/en/images/s_wbn.png +0 -0
- data/site/build/site/en/images/s_wn.png +0 -0
- data/site/build/site/en/images/sigmoid.png +0 -0
- data/site/build/site/en/images/t.png +0 -0
- data/site/build/site/en/images/t_wbn.png +0 -0
- data/site/build/site/en/images/t_wn.png +0 -0
- data/site/build/site/en/index.html +258 -0
- data/site/build/site/en/index.pdf +306 -0
- data/site/build/site/en/linkmap.html +231 -0
- data/site/build/site/en/linkmap.pdf +94 -0
- data/site/build/site/en/locationmap.xml +72 -0
- data/site/build/site/en/machineLearning.html +325 -0
- data/site/build/site/en/machineLearning.pdf +337 -0
- data/site/build/site/en/neuralNetworks.html +446 -0
- data/site/build/site/en/neuralNetworks.pdf +604 -0
- data/site/build/site/en/skin/CommonMessages_de.xml +23 -0
- data/site/build/site/en/skin/CommonMessages_en_US.xml +23 -0
- data/site/build/site/en/skin/CommonMessages_es.xml +23 -0
- data/site/build/site/en/skin/CommonMessages_fr.xml +23 -0
- data/site/build/site/en/skin/basic.css +166 -0
- data/site/build/site/en/skin/breadcrumbs-optimized.js +90 -0
- data/site/build/site/en/skin/breadcrumbs.js +237 -0
- data/site/build/site/en/skin/fontsize.js +166 -0
- data/site/build/site/en/skin/getBlank.js +40 -0
- data/site/build/site/en/skin/getMenu.js +45 -0
- data/site/build/site/en/skin/images/README.txt +1 -0
- data/site/build/site/en/skin/images/add.jpg +0 -0
- data/site/build/site/en/skin/images/built-with-forrest-button.png +0 -0
- data/site/build/site/en/skin/images/chapter.gif +0 -0
- data/site/build/site/en/skin/images/chapter_open.gif +0 -0
- data/site/build/site/en/skin/images/current.gif +0 -0
- data/site/build/site/en/skin/images/error.png +0 -0
- data/site/build/site/en/skin/images/external-link.gif +0 -0
- data/site/build/site/en/skin/images/fix.jpg +0 -0
- data/site/build/site/en/skin/images/forrest-credit-logo.png +0 -0
- data/site/build/site/en/skin/images/hack.jpg +0 -0
- data/site/build/site/en/skin/images/header_white_line.gif +0 -0
- data/site/build/site/en/skin/images/info.png +0 -0
- data/site/build/site/en/skin/images/instruction_arrow.png +0 -0
- data/site/build/site/en/skin/images/label.gif +0 -0
- data/site/build/site/en/skin/images/page.gif +0 -0
- data/site/build/site/en/skin/images/pdfdoc.gif +0 -0
- data/site/build/site/en/skin/images/poddoc.png +0 -0
- data/site/build/site/en/skin/images/printer.gif +0 -0
- data/site/build/site/en/skin/images/rc-b-l-15-1body-2menu-3menu.png +0 -0
- data/site/build/site/en/skin/images/rc-b-r-15-1body-2menu-3menu.png +0 -0
- data/site/build/site/en/skin/images/rc-b-r-5-1header-2tab-selected-3tab-selected.png +0 -0
- data/site/build/site/en/skin/images/rc-t-l-5-1header-2searchbox-3searchbox.png +0 -0
- data/site/build/site/en/skin/images/rc-t-l-5-1header-2tab-selected-3tab-selected.png +0 -0
- data/site/build/site/en/skin/images/rc-t-l-5-1header-2tab-unselected-3tab-unselected.png +0 -0
- data/site/build/site/en/skin/images/rc-t-r-15-1body-2menu-3menu.png +0 -0
- data/site/build/site/en/skin/images/rc-t-r-5-1header-2searchbox-3searchbox.png +0 -0
- data/site/build/site/en/skin/images/rc-t-r-5-1header-2tab-selected-3tab-selected.png +0 -0
- data/site/build/site/en/skin/images/rc-t-r-5-1header-2tab-unselected-3tab-unselected.png +0 -0
- data/site/build/site/en/skin/images/remove.jpg +0 -0
- data/site/build/site/en/skin/images/rss.png +0 -0
- data/site/build/site/en/skin/images/spacer.gif +0 -0
- data/site/build/site/en/skin/images/success.png +0 -0
- data/site/build/site/en/skin/images/txtdoc.png +0 -0
- data/site/build/site/en/skin/images/update.jpg +0 -0
- data/site/build/site/en/skin/images/valid-html401.png +0 -0
- data/site/build/site/en/skin/images/vcss.png +0 -0
- data/site/build/site/en/skin/images/warning.png +0 -0
- data/site/build/site/en/skin/images/xmldoc.gif +0 -0
- data/site/build/site/en/skin/menu.js +48 -0
- data/site/build/site/en/skin/note.txt +50 -0
- data/site/build/site/en/skin/print.css +54 -0
- data/site/build/site/en/skin/profile.css +163 -0
- data/site/build/site/en/skin/prototype.js +1257 -0
- data/site/build/site/en/skin/screen.css +587 -0
- data/site/build/site/en/svn.html +223 -0
- data/site/build/site/en/svn.pdf +239 -0
- data/site/build/site/en/wholesite.pdf +1686 -0
- data/site/build/tmp/brokenlinks.xml +2 -0
- data/site/build/tmp/build-info.xml +5 -0
- 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/input.xmap +32 -0
- data/site/build/tmp/internal.xmap +32 -0
- data/site/build/tmp/locationmap.xml +29 -0
- data/site/build/tmp/output.xmap +38 -0
- data/site/build/tmp/pluginlist2fetchbuild.xml +144 -0
- data/site/build/tmp/plugins-1.xml +212 -0
- data/site/build/tmp/plugins-2.xml +347 -0
- data/site/build/tmp/projfilters.properties +41 -0
- data/site/build/tmp/resources.xmap +32 -0
- data/site/build/webapp/WEB-INF/logs/access.log +0 -0
- data/site/build/webapp/WEB-INF/logs/core.log +788 -0
- data/site/build/webapp/WEB-INF/logs/debug.log +0 -0
- data/site/build/webapp/WEB-INF/logs/error.log +248 -0
- data/site/build/webapp/WEB-INF/logs/flow.log +0 -0
- data/site/build/webapp/WEB-INF/logs/idgen.log +0 -0
- data/site/build/webapp/WEB-INF/logs/linkrewriter.log +0 -0
- data/site/build/webapp/WEB-INF/logs/locationmap.log +0 -0
- data/site/build/webapp/WEB-INF/logs/sitemap.log +0 -0
- data/site/build/webapp/WEB-INF/logs/xmlform.log +0 -0
- data/site/forrest.properties +152 -0
- data/site/forrest.properties.dispatcher.properties +25 -0
- data/site/forrest.properties.xml +29 -0
- data/site/src/documentation/README.txt +7 -0
- data/site/src/documentation/classes/CatalogManager.properties +62 -0
- data/site/src/documentation/content/locationmap.xml +72 -0
- data/site/src/documentation/content/xdocs/downloads.html +9 -0
- data/site/src/documentation/content/xdocs/geneticAlgorithms.xml +280 -0
- data/site/src/documentation/content/xdocs/index.xml +73 -0
- data/site/src/documentation/content/xdocs/machineLearning.xml +129 -0
- data/site/src/documentation/content/xdocs/neuralNetworks.xml +218 -0
- data/site/src/documentation/content/xdocs/site.xml +51 -0
- data/site/src/documentation/content/xdocs/svn.xml +31 -0
- data/site/src/documentation/content/xdocs/tabs.xml +35 -0
- data/site/src/documentation/resources/images/ai4r-logo.png +0 -0
- data/site/src/documentation/resources/images/c.png +0 -0
- data/site/src/documentation/resources/images/c_wbn.png +0 -0
- data/site/src/documentation/resources/images/c_wn.png +0 -0
- data/site/src/documentation/resources/images/ellipse-2.svg +30 -0
- data/site/src/documentation/resources/images/ero.gif +0 -0
- data/site/src/documentation/resources/images/europe2.png +0 -0
- data/site/src/documentation/resources/images/europe3.png +0 -0
- data/site/src/documentation/resources/images/fitness.png +0 -0
- data/site/src/documentation/resources/images/icon-a.png +0 -0
- data/site/src/documentation/resources/images/icon-b.png +0 -0
- data/site/src/documentation/resources/images/icon.png +0 -0
- data/site/src/documentation/resources/images/my_email.png +0 -0
- data/site/src/documentation/resources/images/project-logo.png +0 -0
- data/site/src/documentation/resources/images/rubyforge.png +0 -0
- data/site/src/documentation/resources/images/s.png +0 -0
- data/site/src/documentation/resources/images/s_wbn.png +0 -0
- data/site/src/documentation/resources/images/s_wn.png +0 -0
- data/site/src/documentation/resources/images/sigmoid.png +0 -0
- data/site/src/documentation/resources/images/sub-dir/icon-c.png +0 -0
- data/site/src/documentation/resources/images/t.png +0 -0
- data/site/src/documentation/resources/images/t_wbn.png +0 -0
- data/site/src/documentation/resources/images/t_wn.png +0 -0
- data/site/src/documentation/resources/schema/catalog.xcat +29 -0
- data/site/src/documentation/resources/schema/hello-v10.dtd +51 -0
- data/site/src/documentation/resources/schema/symbols-project-v10.ent +26 -0
- data/site/src/documentation/resources/stylesheets/hello2document.xsl +33 -0
- data/site/src/documentation/sitemap.xmap +66 -0
- data/site/src/documentation/skinconf.xml +418 -0
- data/site/src/documentation/translations/langcode.xml +29 -0
- data/site/src/documentation/translations/languages_de.xml +24 -0
- data/site/src/documentation/translations/languages_en.xml +24 -0
- data/site/src/documentation/translations/languages_es.xml +22 -0
- data/site/src/documentation/translations/languages_fr.xml +24 -0
- data/site/src/documentation/translations/languages_nl.xml +24 -0
- data/site/src/documentation/translations/menu.xml +33 -0
- data/site/src/documentation/translations/menu_af.xml +33 -0
- data/site/src/documentation/translations/menu_de.xml +33 -0
- data/site/src/documentation/translations/menu_es.xml +33 -0
- data/site/src/documentation/translations/menu_fr.xml +33 -0
- data/site/src/documentation/translations/menu_it.xml +33 -0
- data/site/src/documentation/translations/menu_nl.xml +33 -0
- data/site/src/documentation/translations/menu_no.xml +33 -0
- data/site/src/documentation/translations/menu_ru.xml +33 -0
- data/site/src/documentation/translations/menu_sk.xml +33 -0
- data/site/src/documentation/translations/tabs.xml +22 -0
- data/site/src/documentation/translations/tabs_de.xml +22 -0
- data/site/src/documentation/translations/tabs_es.xml +22 -0
- data/site/src/documentation/translations/tabs_fr.xml +22 -0
- data/site/src/documentation/translations/tabs_nl.xml +22 -0
- data/test/decision_tree/id3_test.rb +209 -0
- data/test/genetic_algorithm/chromosome_test.rb +55 -0
- data/test/genetic_algorithm/genetic_algorithm_test.rb +78 -0
- data/test/neural_network/backpropagation_test.rb +44 -0
- metadata +274 -0
@@ -0,0 +1,129 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
|
3
|
+
<document>
|
4
|
+
<header>
|
5
|
+
<title>Machine Learning with ID3 Decision Trees in Ruby</title>
|
6
|
+
</header>
|
7
|
+
<body>
|
8
|
+
<section id="mach-intro">
|
9
|
+
<title>Introduction to ID3 algorithm</title>
|
10
|
+
<p>
|
11
|
+
This is an implementation of the ID3 algorithm (Quinlan).
|
12
|
+
Given a set of preclassified examples, it builds a top-down
|
13
|
+
induction of decision tree, biased by the information gain and
|
14
|
+
entropy measure.
|
15
|
+
</p>
|
16
|
+
<p>
|
17
|
+
The good thing about this learning method is that humans learns as well.
|
18
|
+
Unlike other ai techniques like nueral networks, this class can
|
19
|
+
generate ruby code with if / else sentences. You
|
20
|
+
can use this to evaluate parameters on realtime, copy paste them in a
|
21
|
+
code, or just read them to learn about your problem domain.
|
22
|
+
</p>
|
23
|
+
</section>
|
24
|
+
|
25
|
+
<section id="mach-HowTo">
|
26
|
+
<title>Marketing target strategy example using ID3 Decision Trees in Ruby</title>
|
27
|
+
<p>Let's suppose that you are writtng an application that must identify people as relevant marketing targets or not. The only information that you have es collection of examples:</p>
|
28
|
+
<source>
|
29
|
+
<![CDATA[
|
30
|
+
DATA_LABELS = [ 'city', 'age_range', 'gender', 'marketing_target' ]
|
31
|
+
|
32
|
+
DATA_SET = [
|
33
|
+
['New York', '<30', 'M', 'Y'],
|
34
|
+
['Chicago', '<30', 'M', 'Y'],
|
35
|
+
['Chicago', '<30', 'F', 'Y'],
|
36
|
+
['New York', '<30', 'M', 'Y'],
|
37
|
+
['New York', '<30', 'M', 'Y'],
|
38
|
+
['Chicago', '[30-50)', 'M', 'Y'],
|
39
|
+
['New York', '[30-50)', 'F', 'N'],
|
40
|
+
['Chicago', '[30-50)', 'F', 'Y'],
|
41
|
+
['New York', '[30-50)', 'F', 'N'],
|
42
|
+
['Chicago', '[50-80]', 'M', 'N'],
|
43
|
+
['New York', '[50-80]', 'F', 'N'],
|
44
|
+
['New York', '[50-80]', 'M', 'N'],
|
45
|
+
['Chicago', '[50-80]', 'M', 'N'],
|
46
|
+
['New York', '[50-80]', 'F', 'N'],
|
47
|
+
['Chicago', '>80', 'F', 'Y']
|
48
|
+
]
|
49
|
+
]]>
|
50
|
+
</source>
|
51
|
+
<p>You can create an ID3 Decision tree to do the dirty job for you:</p>
|
52
|
+
<source>
|
53
|
+
<![CDATA[
|
54
|
+
id3 = DecisionTree::ID3.new(DATA_SET, DATA_LABELS)
|
55
|
+
]]>
|
56
|
+
</source>
|
57
|
+
<p>The Decision tree will automatically create the "rules" to parse new data,
|
58
|
+
and identify new posible marketing targets:</p>
|
59
|
+
<source>
|
60
|
+
<![CDATA[
|
61
|
+
id3.to_s
|
62
|
+
# => if age_range=='<30' then marketing_target='Y'
|
63
|
+
elsif age_range=='[30-50)' and city=='Chicago' then marketing_target='Y'
|
64
|
+
elsif age_range=='[30-50)' and city=='New York' then marketing_target='N'
|
65
|
+
elsif age_range=='[50-80]' then marketing_target='N'
|
66
|
+
elsif age_range=='>80' then marketing_target='Y'
|
67
|
+
else raise 'There was not enough information during training to do a proper induction for this data element' end
|
68
|
+
|
69
|
+
id3.eval(['New York', '<30', 'M'])
|
70
|
+
# => 'Y'
|
71
|
+
]]>
|
72
|
+
</source>
|
73
|
+
</section>
|
74
|
+
|
75
|
+
<section id="mach-dataload">
|
76
|
+
<title>Better data loading</title>
|
77
|
+
<p>
|
78
|
+
In the real life you will use lot more data training examples,
|
79
|
+
with more attributes.
|
80
|
+
Consider moving your data to an external CSV (comma separate values) file.
|
81
|
+
</p>
|
82
|
+
<source>
|
83
|
+
<![CDATA[
|
84
|
+
data_set = []
|
85
|
+
CSV::Reader.parse(File.open("#{File.dirname(__FILE__)}/data_set.csv", 'r')) do |row|
|
86
|
+
data_set << row
|
87
|
+
end
|
88
|
+
data_labels = data_set.shift
|
89
|
+
|
90
|
+
id3 = DecisionTree::ID3.new(data_set, data_labels)
|
91
|
+
]]></source>
|
92
|
+
|
93
|
+
</section>
|
94
|
+
|
95
|
+
<section id="mach-eval">
|
96
|
+
<title>A good tip for data evaluation</title>
|
97
|
+
<p>
|
98
|
+
The ID3 class provides a method to evaluate new data.
|
99
|
+
</p>
|
100
|
+
<source>
|
101
|
+
<![CDATA[
|
102
|
+
id3.eval(['New York', '<30', 'M'])
|
103
|
+
# => 'Y'
|
104
|
+
]]></source>
|
105
|
+
<p>
|
106
|
+
But instead of going through the tree every time, you can take advantage of the fact that the method "to_s" generates proper ruby code!
|
107
|
+
</p>
|
108
|
+
<source>
|
109
|
+
<![CDATA[
|
110
|
+
id3 = DecisionTree::ID3.new(DATA_SET, DATA_LABELS)
|
111
|
+
age_range = '<30'
|
112
|
+
city = 'New York'
|
113
|
+
gender = 'M'
|
114
|
+
marketing_target = nil
|
115
|
+
eval id3.to_s
|
116
|
+
puts marketing_target
|
117
|
+
# => 'Y'
|
118
|
+
]]></source>
|
119
|
+
</section>
|
120
|
+
|
121
|
+
<section id="mach-more">
|
122
|
+
<title>More about ID3 and decision trees</title>
|
123
|
+
<p>
|
124
|
+
<a href="http://en.wikipedia.org/wiki/Decision_tree">Wikipedia article on Decision trees</a>
|
125
|
+
<a href="http://en.wikipedia.org/wiki/ID3_algorithm">Wikipedia article on ID3 Algorithm</a>
|
126
|
+
</p>
|
127
|
+
</section>
|
128
|
+
</body>
|
129
|
+
</document>
|
@@ -0,0 +1,218 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<!--
|
3
|
+
Licensed to the Apache Software Foundation (ASF) under one or more
|
4
|
+
contributor license agreements. See the NOTICE file distributed with
|
5
|
+
this work for additional information regarding copyright ownership.
|
6
|
+
The ASF licenses this file to You under the Apache License, Version 2.0
|
7
|
+
(the "License"); you may not use this file except in compliance with
|
8
|
+
the License. You may obtain a copy of the License at
|
9
|
+
|
10
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
|
12
|
+
Unless required by applicable law or agreed to in writing, software
|
13
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
See the License for the specific language governing permissions and
|
16
|
+
limitations under the License.
|
17
|
+
|
18
|
+
-->
|
19
|
+
<!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
|
20
|
+
<document>
|
21
|
+
<header>
|
22
|
+
<title>Backpropagation neural networks in ruby :: ai4r</title>
|
23
|
+
</header>
|
24
|
+
<body>
|
25
|
+
<section id="nn-Introduction">
|
26
|
+
<title>Introduction to Neural Networks in Ruby</title>
|
27
|
+
<p>
|
28
|
+
The utility of artificial neural network models lies in the fact that they can be used to infer a function from observations. This is particularly useful in applications where the complexity of the data or task makes the design of such a function by hand impractical. Neural Networks are being used in many businesses and applications. Their ability to learn by example makes them attractive in environments where the business rules are either not well defined or are hard to enumerate and define. Many people believe that Neural Networks can only solve toy problems. Give them a try, and let you decide if they are good enough to solve your needs.
|
29
|
+
</p>
|
30
|
+
<p>
|
31
|
+
In this module you will find an implementation of neural networks using the Backpropagation is a supervised learning technique (described by Paul Werbos in 1974, and further developed by David E. Rumelhart, Geoffrey E. Hinton and Ronald J. Williams in 1986)
|
32
|
+
</p>
|
33
|
+
</section>
|
34
|
+
<section id="nn-HowTo">
|
35
|
+
<title>How to use Backpropagation Neural Networks in Ruby</title>
|
36
|
+
<source>
|
37
|
+
<![CDATA[
|
38
|
+
# Create the network
|
39
|
+
net = Backpropagation.new([4, 3, 2]) # 4 inputs
|
40
|
+
# 1 hidden layer with 3 neurons,
|
41
|
+
# 4 outputs
|
42
|
+
# Train the network
|
43
|
+
1..upto(100) do |i|
|
44
|
+
net.train(example[i], result[i])
|
45
|
+
end
|
46
|
+
|
47
|
+
# Use it: Evaluate data with the trained network
|
48
|
+
net.eval([12, 48, 12, 25]) # => [34, 22]
|
49
|
+
]]>
|
50
|
+
</source>
|
51
|
+
</section>
|
52
|
+
|
53
|
+
<section id="nn-example">
|
54
|
+
<title>Example using ai4r Backpropagation network in ruby</title>
|
55
|
+
<p>
|
56
|
+
Let's imagine that we have to implement a program to identify simple patterns (triangles, squares, crosses, etc). The main problem is that this program must be resistant to random noise in the image (pixels with wrong
|
57
|
+
values) and line noise (similar to the unwanted direct current that we usually have in a signal).</p>
|
58
|
+
<p>
|
59
|
+
We can take an example of each pattern to be recognized, and train a neural network to identify them.
|
60
|
+
</p>
|
61
|
+
<p>
|
62
|
+
We create a network with the following architecture: 256 input neurons, 128 neurons in a hidden layer, 3 output neurons. We feed this network with 16x16 matrices (in fact will convert them to vectors of length 256). Each pixel is represented with a number from 0 (white pixel) to 10 (black pixel). The output of this network if a 3 vector of dimension 3, where ideally:</p>
|
63
|
+
<ul>
|
64
|
+
<li>(1, 0, 0) for triangles</li>
|
65
|
+
<li>(0, 1, 0) for squares</li>
|
66
|
+
<li>(0, 0, 1) for crosses</li>
|
67
|
+
</ul>
|
68
|
+
<p>We train our backpropagation neural network using the following examples:</p>
|
69
|
+
<table>
|
70
|
+
<caption>Training patterns</caption>
|
71
|
+
<tr>
|
72
|
+
<td><img alt="Triangule training example" src="/images/t.png" /></td>
|
73
|
+
<td><img alt="Square training example" src="/images/s.png" /></td>
|
74
|
+
<td><img alt="Cross training example" src="/images/c.png" /></td>
|
75
|
+
</tr>
|
76
|
+
</table>
|
77
|
+
<p>And we repeat the training 20 times.</p>
|
78
|
+
|
79
|
+
<p>
|
80
|
+
The results we got when we evaluate patterns with our trained network are:
|
81
|
+
</p>
|
82
|
+
<ul>
|
83
|
+
<li>Evaluating the training patterns with the trained network:
|
84
|
+
<ol>
|
85
|
+
<li> <img alt="Triangule training example" src="/images/t.png" />
|
86
|
+
[0.98, 0.03, 0.01] => TRIANGLE </li>
|
87
|
+
<li> <img alt="Square training example" src="/images/s.png" />
|
88
|
+
[0.00, 0.96, 0.03] => SQUARE </li>
|
89
|
+
<li> <img alt="Cross training example" src="/images/c.png" />
|
90
|
+
[0.00, 0.00, 0.99] => CROSS </li>
|
91
|
+
</ol>
|
92
|
+
</li>
|
93
|
+
<li>Evaluating the patterns with random noise with the trained network:
|
94
|
+
<ol>
|
95
|
+
<li> <img alt="Triangule pattern with random noise" src="/images/t_wn.png" /> [0.98, 0.01, 0.01] => TRIANGLE </li>
|
96
|
+
<li> <img alt="Square pattern with random noise" src="/images/s_wn.png" /> [0.00, 0.96, 0.02] => SQUARE </li>
|
97
|
+
<li> <img alt="Cross pattern with random noise" src="/images/c_wn.png" /> [0.00, 0.00, 0.98] => CROSS </li>
|
98
|
+
</ol>
|
99
|
+
</li>
|
100
|
+
<li>Evaluating the patterns with line noise with the trained network:
|
101
|
+
<ol>
|
102
|
+
<li> <img alt="Triangule pattern with line noise" src="/images/t_wbn.png" /> [0.62, 0.00, 0.02] => TRIANGLE </li>
|
103
|
+
<li> <img alt="Square pattern with line noise" src="/images/s_wbn.png" /> [0.00, 0.75, 0.01] => SQUARE </li>
|
104
|
+
<li> <img alt="Cross pattern with line noise" src="/images/c_wbn.png" /> [0.00, 0.00, 0.98] => CROSS </li>
|
105
|
+
</ol>
|
106
|
+
</li>
|
107
|
+
</ul>
|
108
|
+
<p>These results are very satisfactory. The network could sucessfully identify the patterns despite the noise introduced to them. In fact, one of the most popular uses of neural networks in business
|
109
|
+
applications is OCR (opticar character recognition).</p>
|
110
|
+
<p>This is the source code used to elaborate this example (You can find it inside the zip file):</p>
|
111
|
+
<source>
|
112
|
+
<![CDATA[
|
113
|
+
require File.dirname(__FILE__) + '/training_patterns'
|
114
|
+
require File.dirname(__FILE__) + '/patterns_with_noise'
|
115
|
+
require File.dirname(__FILE__) + '/patterns_with_base_noise'
|
116
|
+
require File.dirname(__FILE__) + '/../../lib/neural_network/backpropagation'
|
117
|
+
|
118
|
+
net = NeuralNetwork::Backpropagation.new([256, 128, 3])
|
119
|
+
|
120
|
+
tr_input = TRIANGLE.flatten.collect { |input| input.to_f / 10}
|
121
|
+
sq_input = SQUARE.flatten.collect { |input| input.to_f / 10}
|
122
|
+
cr_input = CROSS.flatten.collect { |input| input.to_f / 10}
|
123
|
+
|
124
|
+
tr_with_noise = TRIANGLE_WITH_NOISE.flatten.collect { |input| input.to_f / 10}
|
125
|
+
sq_with_noise = SQUARE_WITH_NOISE.flatten.collect { |input| input.to_f / 10}
|
126
|
+
cr_with_noise = CROSS_WITH_NOISE.flatten.collect { |input| input.to_f / 10}
|
127
|
+
|
128
|
+
tr_with_base_noise = TRIANGLE_WITH_BASE_NOISE.flatten.collect { |input| input.to_f / 10}
|
129
|
+
sq_with_base_noise = SQUARE_WITH_BASE_NOISE.flatten.collect { |input| input.to_f / 10}
|
130
|
+
cr_with_base_noise = CROSS_WITH_BASE_NOISE.flatten.collect { |input| input.to_f / 10}
|
131
|
+
|
132
|
+
puts "Training the network, please wait."
|
133
|
+
20.times do
|
134
|
+
net.train(tr_input, [1,0,0])
|
135
|
+
net.train(sq_input, [0,1,0])
|
136
|
+
net.train(cr_input, [0,0,1])
|
137
|
+
end
|
138
|
+
|
139
|
+
def result_label(result)
|
140
|
+
if result[0] > result[1] && result[0] > result[2]
|
141
|
+
"TRIANGLE"
|
142
|
+
elsif result[1] > result[2]
|
143
|
+
"SQUARE"
|
144
|
+
else
|
145
|
+
"CROSS"
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
puts "Training Examples"
|
150
|
+
puts "#{net.eval(tr_input).inspect} => #{result_label(net.eval(tr_input))}"
|
151
|
+
puts "#{net.eval(sq_input).inspect} => #{result_label(net.eval(sq_input))}"
|
152
|
+
puts "#{net.eval(cr_input).inspect} => #{result_label(net.eval(cr_input))}"
|
153
|
+
puts "Examples with noise"
|
154
|
+
puts "#{net.eval(tr_with_noise).inspect} => #{result_label(net.eval(tr_with_noise))}"
|
155
|
+
puts "#{net.eval(sq_with_noise).inspect} => #{result_label(net.eval(sq_with_noise))}"
|
156
|
+
puts "#{net.eval(cr_with_noise).inspect} => #{result_label(net.eval(cr_with_noise))}"
|
157
|
+
puts "Examples with base noise"
|
158
|
+
puts "#{net.eval(tr_with_base_noise).inspect} => #{result_label(net.eval(tr_with_base_noise))}"
|
159
|
+
puts "#{net.eval(sq_with_base_noise).inspect} => #{result_label(net.eval(sq_with_base_noise))}"
|
160
|
+
puts "#{net.eval(cr_with_base_noise).inspect} => #{result_label(net.eval(cr_with_base_noise))}"
|
161
|
+
]]>
|
162
|
+
</source>
|
163
|
+
</section>
|
164
|
+
|
165
|
+
<section id="nn-custom">
|
166
|
+
<title>Customizing your neural network in ai4r</title>
|
167
|
+
<p>Sometime for a given problem, you will have to "play around" with some parameters to
|
168
|
+
get to a solution. This parameters are:</p>
|
169
|
+
<p><strong>threshold</strong>: A real number which we will call Threshold. Experiments have shown that best values for q are between 0.25 and 1. You can optionally pass this parameter to the initialization method of your network.</p>
|
170
|
+
|
171
|
+
<p><strong>lambda</strong>: The Learning Rate: a real number, usually between 0.05 and 0.25. You can optionally pass this parameter to the initialization method of your network.</p>
|
172
|
+
|
173
|
+
<p><strong>momentum</strong>: A momentum will avoid oscillations during learning, converging to a solution in less iterations. You can optionally pass this parameter to the initialization method of your network.</p>
|
174
|
+
<p><strong>transference function</strong>: By default, f(x) = 1/(1 + e^(-x)).
|
175
|
+
This function is called "Sigmoid function". You can see it like a
|
176
|
+
"smoothed" version of the "Heaviside step function". It will always provide a
|
177
|
+
value between 0 and 1. </p>
|
178
|
+
<p><img src="images/sigmoid.png" alt="Sigmoid function" /></p>
|
179
|
+
<p>
|
180
|
+
Sometimes you will have better results with f(x) = x. You can change the transference function from the default sigmoidal function to the linear one, overriding NeuralNetwork::Neuron.f and NeuralNetwork::Neuron.f_prime (derived function
|
181
|
+
of f):</p>
|
182
|
+
<source>
|
183
|
+
<![CDATA[
|
184
|
+
class NeuralNetwork::Neuron
|
185
|
+
def self.f(x)
|
186
|
+
x
|
187
|
+
end
|
188
|
+
def self.f_prime(x)
|
189
|
+
1
|
190
|
+
end
|
191
|
+
end
|
192
|
+
]]>
|
193
|
+
</source>
|
194
|
+
</section>
|
195
|
+
|
196
|
+
<section id="nn-more">
|
197
|
+
<title>More about Neural Networks and Backpropagation</title>
|
198
|
+
|
199
|
+
<ul>
|
200
|
+
<li>
|
201
|
+
<a href="http://en.wikipedia.org/wiki/Artificial_neural_network">Wikipedia article on Artificial Neural Networks</a>
|
202
|
+
</li>
|
203
|
+
<li>
|
204
|
+
<a href="http://en.wikipedia.org/wiki/Backpropagation">Wikipedia article on Backpropagation Algorithm</a>
|
205
|
+
</li>
|
206
|
+
<li>
|
207
|
+
<a href="http://www.tek271.com/articles/neuralNet/IntoToNeuralNets.html">Neural Networks - An Introduction by Abdul Habra</a>
|
208
|
+
</li>
|
209
|
+
<li>
|
210
|
+
<a href="http://galaxy.agh.edu.pl/~vlsi/AI/backp_t_en/backprop.html">A graphical explanation of the
|
211
|
+
backpropagation algorithm by Mariusz Bernacki and Przemysław Włodarczyk</a>
|
212
|
+
</li>
|
213
|
+
</ul>
|
214
|
+
|
215
|
+
</section>
|
216
|
+
|
217
|
+
</body>
|
218
|
+
</document>
|
@@ -0,0 +1,51 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<!--
|
3
|
+
Licensed to the Apache Software Foundation (ASF) under one or more
|
4
|
+
contributor license agreements. See the NOTICE file distributed with
|
5
|
+
this work for additional information regarding copyright ownership.
|
6
|
+
The ASF licenses this file to You under the Apache License, Version 2.0
|
7
|
+
(the "License"); you may not use this file except in compliance with
|
8
|
+
the License. You may obtain a copy of the License at
|
9
|
+
|
10
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
|
12
|
+
Unless required by applicable law or agreed to in writing, software
|
13
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
See the License for the specific language governing permissions and
|
16
|
+
limitations under the License.
|
17
|
+
-->
|
18
|
+
<!--
|
19
|
+
Forrest site.xml
|
20
|
+
|
21
|
+
This file contains an outline of the site's information content. It is used to:
|
22
|
+
- Generate the website menus (though these can be overridden - see docs)
|
23
|
+
- Provide semantic, location-independent aliases for internal 'site:' URIs, eg
|
24
|
+
<link href="site:changes"> links to changes.html (or ../changes.html if in
|
25
|
+
subdir).
|
26
|
+
- Provide aliases for external URLs in the external-refs section. Eg, <link
|
27
|
+
href="ext:cocoon"> links to http://cocoon.apache.org/
|
28
|
+
|
29
|
+
See http://forrest.apache.org/docs/linking.html for more info
|
30
|
+
-->
|
31
|
+
<!-- The label attribute of the outer "site" element will only show
|
32
|
+
in the linkmap (linkmap.html).
|
33
|
+
Use elements project-name and group-name in skinconfig to change name of
|
34
|
+
your site or project that is usually shown at the top of page.
|
35
|
+
No matter what you configure for the href attribute, Forrest will
|
36
|
+
always use index.html when you request http://yourHost/
|
37
|
+
See FAQ: "How can I use a start-up-page other than index.html?"
|
38
|
+
-->
|
39
|
+
<site label="ai4r" href="" xmlns="http://apache.org/forrest/linkmap/1.0"
|
40
|
+
tab="">
|
41
|
+
<about label="Home">
|
42
|
+
<index label="Index" href="index.html" description="ai4r - Artificial Intelligence for Ruby"/>
|
43
|
+
<geneticAlgorithms label="Genetic Algorithms" href="geneticAlgorithms.html" description="Genetic Algorithms in ruby"/>
|
44
|
+
<machineLearning label="Machine Learning" href="machineLearning.html" description="ID3 Decision Trees in ruby"/>
|
45
|
+
<neuralNetworks label="Neural Networks" href="neuralNetworks.html" description="Backpropagation Neural Network in ruby" />
|
46
|
+
<download label="Downloads" href="downloads.html" description="ai4r - Download Files"/>
|
47
|
+
<svn label="Subversion (svn)" href="svn.html" description="ai4r Subversion repository"/>
|
48
|
+
<ai4r_site_pdf label="ai4r project PDF" href="wholesite.pdf"/>
|
49
|
+
</about>
|
50
|
+
|
51
|
+
</site>
|
@@ -0,0 +1,31 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
|
3
|
+
<document>
|
4
|
+
<header>
|
5
|
+
<title>Subversion repository for ai4r (Artificial Intelligence for Ruby)</title>
|
6
|
+
</header>
|
7
|
+
<body>
|
8
|
+
<section id="Anonymous">
|
9
|
+
<title>Anonymous Subversion Access</title>
|
10
|
+
<p>This project's SVN repository can be checked out through anonymous access with the following command(s).</p>
|
11
|
+
<ul>
|
12
|
+
<li>svn checkout http://ai4r.rubyforge.org/svn/trunk</li>
|
13
|
+
<li>svn checkout svn://rubyforge.org/var/svn/ai4r/trunk</li>
|
14
|
+
</ul>
|
15
|
+
</section>
|
16
|
+
|
17
|
+
<section id="Browse">
|
18
|
+
<title>Browse latest files</title>
|
19
|
+
<p>You can see the latest ai4r code directly in your browser: <a href="http://ai4r.rubyforge.org/svn/">http://ai4r.rubyforge.org/svn/</a>
|
20
|
+
</p>
|
21
|
+
<warning>The latest code in http://ai4r.rubyforge.org/svn/trunk will probably be a work in progess. If you
|
22
|
+
want something that works, please download the releases.</warning>
|
23
|
+
</section>
|
24
|
+
|
25
|
+
<section id="moreinfor">
|
26
|
+
<title>More info on Subversion (svn)</title>
|
27
|
+
<p>If you need help about using Subversion (svn), the (IMO) best documentation is available <a href="http://svnbook.red-bean.com/">here</a>.
|
28
|
+
</p>
|
29
|
+
</section>
|
30
|
+
</body>
|
31
|
+
</document>
|
@@ -0,0 +1,35 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<!--
|
3
|
+
Licensed to the Apache Software Foundation (ASF) under one or more
|
4
|
+
contributor license agreements. See the NOTICE file distributed with
|
5
|
+
this work for additional information regarding copyright ownership.
|
6
|
+
The ASF licenses this file to You under the Apache License, Version 2.0
|
7
|
+
(the "License"); you may not use this file except in compliance with
|
8
|
+
the License. You may obtain a copy of the License at
|
9
|
+
|
10
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
|
12
|
+
Unless required by applicable law or agreed to in writing, software
|
13
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
14
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15
|
+
See the License for the specific language governing permissions and
|
16
|
+
limitations under the License.
|
17
|
+
-->
|
18
|
+
<!DOCTYPE tabs PUBLIC "-//APACHE//DTD Cocoon Documentation Tab V1.1//EN" "http://forrest.apache.org/dtd/tab-cocoon-v11.dtd">
|
19
|
+
<tabs software="ai4r"
|
20
|
+
title="ai4r"
|
21
|
+
copyright="Sergio Fierens"
|
22
|
+
xmlns:xlink="http://www.w3.org/1999/xlink">
|
23
|
+
<!-- The rules for tabs are:
|
24
|
+
@dir will always have '/@indexfile' added.
|
25
|
+
@indexfile gets appended to @dir if the tab is selected. Defaults to 'index.html'
|
26
|
+
@href is not modified unless it is root-relative and obviously specifies a
|
27
|
+
directory (ends in '/'), in which case /index.html will be added
|
28
|
+
If @id's are present, site.xml entries with a matching @tab will be in that tab.
|
29
|
+
|
30
|
+
Tabs can be embedded to a depth of two. The second level of tabs will only
|
31
|
+
be displayed when their parent tab is selected.
|
32
|
+
-->
|
33
|
+
<tab id="" label="Home" dir="" indexfile="index.html"/>
|
34
|
+
|
35
|
+
</tabs>
|