git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@40 331e1502-861f-0410-8da2-ba01fb791d7f
155 lines
8.6 KiB
HTML
155 lines
8.6 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
|
|
<title>EO: mse.h Source File</title>
|
|
<link href="doxygen.css" rel="stylesheet" type="text/css">
|
|
</head><body>
|
|
<!-- Generated by Doxygen 1.3.9.1 -->
|
|
<div class="qindex"> <form class="search" action="search.php" method="get">
|
|
<a class="qindex" href="main.html">Main Page</a> | <a class="qindex" href="modules.html">Modules</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Class List</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="namespacemembers.html">Namespace Members</a> | <a class="qindex" href="functions.html">Class Members</a> | <a class="qindex" href="pages.html">Related Pages</a> | <span class="search"><u>S</u>earch for <input class="search" type="text" name="query" value="" size="20" accesskey="s"/></span></form></div>
|
|
<div class="nav">
|
|
<a class="el" href="dir_000020.html">app</a> / <a class="el" href="dir_000023.html">gprop</a></div>
|
|
<h1>mse.h</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00002 <span class="comment">// mse.h</span>
|
|
00003 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00004
|
|
00005 <span class="preprocessor">#ifndef mse_h</span>
|
|
00006 <span class="preprocessor"></span><span class="preprocessor">#define mse_h</span>
|
|
00007 <span class="preprocessor"></span>
|
|
00008 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00009
|
|
00010 <span class="preprocessor">#include <qp.h></span> <span class="comment">// neuron layer net set</span>
|
|
00011
|
|
00012 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00013
|
|
00014 <span class="keyword">namespace </span>mse
|
|
00015 {
|
|
00016 <span class="comment">//---------------------------------------------------------------------------</span>
|
|
00017 <span class="comment">// useful typedefs</span>
|
|
00018 <span class="comment">//---------------------------------------------------------------------------</span>
|
|
00019
|
|
00020 <span class="keyword">using</span> qp::real;
|
|
00021 <span class="keyword">using</span> qp::vector;
|
|
00022 <span class="keyword">using</span> qp::max_real;
|
|
00023 <span class="keyword">using</span> qp::min_real;
|
|
00024 <span class="keyword">using</span> qp::set;
|
|
00025 <span class="keyword">using</span> qp::neuron;
|
|
00026 <span class="keyword">using</span> qp::layer;
|
|
00027
|
|
00028 <span class="comment">//---------------------------------------------------------------------------</span>
|
|
00029 <span class="comment">// error</span>
|
|
00030 <span class="comment">//---------------------------------------------------------------------------</span>
|
|
00031
|
|
00032 real error(<span class="keyword">const</span> mlp::net& net, <span class="keyword">const</span> set& ts)
|
|
00033 {
|
|
00034 real error_ = 0.0;
|
|
00035
|
|
00036 <span class="keywordflow">for</span> (set::const_iterator s = ts.begin(); s != ts.end(); ++s)
|
|
00037 {
|
|
00038 vector out = net(s->input);
|
|
00039
|
|
00040 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i = 0; i < out.size(); ++i)
|
|
00041 {
|
|
00042 real diff = s->output[i] - out[i];
|
|
00043 error_ += diff * diff;
|
|
00044 }
|
|
00045 }
|
|
00046
|
|
00047 <span class="keywordflow">return</span> error_ / ts.size();
|
|
00048 }
|
|
00049 <span class="comment">//-------------------------------------------------------------------------</span>
|
|
00050 <span class="comment">// mse</span>
|
|
00051 <span class="comment">//-------------------------------------------------------------------------</span>
|
|
00052
|
|
00053 <span class="keyword">class </span>net: <span class="keyword">public</span> qp::net
|
|
00054 {
|
|
00055 <span class="keyword">public</span>:
|
|
00056 net(mlp::net& n): qp::net(n) {}
|
|
00057
|
|
00058 real error(<span class="keyword">const</span> set& ts)
|
|
00059 {
|
|
00060 real error_ = 0;
|
|
00061
|
|
00062 <span class="keywordflow">for</span> (set::const_iterator s = ts.begin(); s != ts.end(); ++s)
|
|
00063 {
|
|
00064 forward(s->input);
|
|
00065 error_ += backward(s->input, s->output);
|
|
00066 }
|
|
00067 error_ /= ts.size();
|
|
00068
|
|
00069 <span class="keywordflow">return</span> error_;
|
|
00070 }
|
|
00071
|
|
00072 <span class="keyword">private</span>:
|
|
00073 real backward(<span class="keyword">const</span> vector& input, <span class="keyword">const</span> vector& output)
|
|
00074 {
|
|
00075 reverse_iterator current_layer = rbegin();
|
|
00076 reverse_iterator backward_layer = current_layer + 1;
|
|
00077 real error_ = 0;
|
|
00078
|
|
00079 <span class="comment">// output layer</span>
|
|
00080 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> j = 0; j < current_layer->size(); ++j)
|
|
00081 {
|
|
00082 neuron& n = (*current_layer)[j];
|
|
00083
|
|
00084 real diff = output[j] - n.out;
|
|
00085 n.ndelta += n.delta = diff * n.out * (1.0 - n.out);
|
|
00086
|
|
00087 <span class="keywordflow">if</span> (size() == 1) <span class="comment">// monolayer</span>
|
|
00088 n.dxo += n.delta * input;
|
|
00089 <span class="keywordflow">else</span> <span class="comment">// multilayer</span>
|
|
00090 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> k = 0; k < n.dxo.size(); ++k)
|
|
00091 n.dxo[k] += n.delta * (*backward_layer)[k].out;
|
|
00092
|
|
00093 error_ += diff * diff;
|
|
00094 }
|
|
00095
|
|
00096 <span class="comment">// hidden layers</span>
|
|
00097 <span class="keywordflow">while</span> (++current_layer != rend())
|
|
00098 {
|
|
00099 reverse_iterator forward_layer = current_layer - 1;
|
|
00100 reverse_iterator backward_layer = current_layer + 1;
|
|
00101
|
|
00102 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> j = 0; j < current_layer->size(); ++j)
|
|
00103 {
|
|
00104
|
|
00105 neuron& n = (*current_layer)[j];
|
|
00106 real sum = 0;
|
|
00107
|
|
00108 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> k = 0; k < forward_layer->size(); ++k)
|
|
00109 {
|
|
00110 neuron& nf = (*forward_layer)[k];
|
|
00111 sum += nf.delta * (nf.n->weight[j] + nf.dweight1[j]);
|
|
00112 }
|
|
00113
|
|
00114 n.delta = n.out * (1.0 - n.out) * sum;
|
|
00115 n.ndelta += n.delta;
|
|
00116
|
|
00117
|
|
00118 <span class="keywordflow">if</span> (backward_layer == rend()) <span class="comment">// first hidden layer</span>
|
|
00119 n.dxo += n.delta * input;
|
|
00120 <span class="keywordflow">else</span> <span class="comment">// rest of hidden layers</span>
|
|
00121 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> k = 0; k < n.dxo.size(); ++k)
|
|
00122 n.dxo[k] += n.delta * (*backward_layer)[k].out;
|
|
00123 }
|
|
00124 }
|
|
00125
|
|
00126 <span class="keywordflow">return</span> error_;
|
|
00127 }
|
|
00128 };
|
|
00129
|
|
00130 <span class="comment">//---------------------------------------------------------------------------</span>
|
|
00131
|
|
00132 } <span class="comment">// namespace mse</span>
|
|
00133
|
|
00134 <span class="comment">//-----------------------------------------------------------------------------</span>
|
|
00135
|
|
00136 <span class="preprocessor">#endif // mse_h</span>
|
|
00137 <span class="preprocessor"></span>
|
|
00138 <span class="comment">// Local Variables: </span>
|
|
00139 <span class="comment">// mode:C++ </span>
|
|
00140 <span class="comment">// End:</span>
|
|
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:41 2006 for EO by
|
|
<a href="http://www.doxygen.org/index.html">
|
|
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.3.9.1 </small></address>
|
|
</body>
|
|
</html>
|