paradiseo/trunk/paradiseo-eo/doc/html/qp_8h-source.html
legrand c3aec878e5 Paradiseo-eo sources added
git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@40 331e1502-861f-0410-8da2-ba01fb791d7f
2006-12-12 14:49:08 +00:00

266 lines
14 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: qp.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&nbsp;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&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="namespacemembers.html">Namespace&nbsp;Members</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a> | <a class="qindex" href="pages.html">Related&nbsp;Pages</a> | <span class="search"><u>S</u>earch&nbsp;for&nbsp;<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>&nbsp;/&nbsp;<a class="el" href="dir_000023.html">gprop</a></div>
<h1>qp.h</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">//-----------------------------------------------------------------------------</span>
00002 <span class="comment">// qp.h</span>
00003 <span class="comment">//-----------------------------------------------------------------------------</span>
00004
00005 <span class="preprocessor">#ifndef qp_h</span>
00006 <span class="preprocessor"></span><span class="preprocessor">#define qp_h</span>
00007 <span class="preprocessor"></span>
00008 <span class="comment">//-----------------------------------------------------------------------------</span>
00009
00010 <span class="preprocessor">#include &lt;iostream&gt;</span> <span class="comment">// istream ostream</span>
00011 <span class="preprocessor">#include &lt;algorithm&gt;</span> <span class="comment">// fill</span>
00012 <span class="preprocessor">#include &lt;vector&gt;</span> <span class="comment">// vector</span>
00013 <span class="preprocessor">#include &lt;utils/rnd_generators.h&gt;</span> <span class="comment">// uniform_generator</span>
00014 <span class="preprocessor">#include &lt;mlp.h&gt;</span> <span class="comment">// neuron layer net</span>
00015
00016 <span class="comment">//-----------------------------------------------------------------------------</span>
00017
00018 <span class="keyword">namespace </span>qp
00019 {
00020 <span class="comment">//---------------------------------------------------------------------------</span>
00021 <span class="comment">// useful typedefs</span>
00022 <span class="comment">//---------------------------------------------------------------------------</span>
00023
00024 <span class="keyword">using</span> mlp::real;
00025 <span class="keyword">using</span> mlp::vector;
00026
00027 <span class="keyword">using</span> mlp::max_real;
00028 <span class="keyword">using</span> mlp::min_real;
00029
00030 <span class="keyword">using</span> mlp::set;
00031
00032 <span class="comment">//---------------------------------------------------------------------------</span>
00033 <span class="comment">// useful constants</span>
00034 <span class="comment">//---------------------------------------------------------------------------</span>
00035
00036 <span class="keyword">const</span> real eta_default = 0.5;
00037 <span class="keyword">const</span> real eta_floor = 0.0001;
00038 <span class="keyword">const</span> real alpha_default = 0.9;
00039 <span class="keyword">const</span> real lambda_default = 0.5;
00040 <span class="keyword">const</span> real lambda0 = 0.1;
00041 <span class="keyword">const</span> real backtrack_step = 0.5;
00042 <span class="keyword">const</span> real me_floor = 0.0001;
00043 <span class="keyword">const</span> real mw_floor = 0.0001;
00044
00045
00046 <span class="comment">//---------------------------------------------------------------------------</span>
00047 <span class="comment">// neuron</span>
00048 <span class="comment">//---------------------------------------------------------------------------</span>
00049
00050 <span class="keyword">struct </span>neuron
00051 {
00052 mlp::neuron* n;
00053 real out, delta, ndelta, dbias1, dbias2;
00054 vector dweight1, dweight2, dxo;
00055
00056 neuron(mlp::neuron&amp; _n):
00057 n(&amp;_n), out(0), delta(0), ndelta(0), dbias1(0), dbias2(0),
00058 dweight1(n-&gt;weight.size(), 0),
00059 dweight2(n-&gt;weight.size(), 0),
00060 dxo(n-&gt;weight.size(), 0) {}
00061
00062 <span class="keywordtype">void</span> reset()
00063 {
00064 <span class="comment">// underlaying neuron</span>
00065 n-&gt;reset();
00066
00067 <span class="comment">// addons</span>
00068 out = delta = ndelta = dbias1 = dbias2 = 0;
00069 fill(dweight1.begin(), dweight1.end(), 0);
00070 fill(dweight2.begin(), dweight2.end(), 0);
00071 fill(dxo.begin(), dxo.end(), 0);
00072 }
00073
00074 real operator()(<span class="keyword">const</span> vector&amp; input)
00075 {
00076 <span class="keywordflow">return</span> out = mlp::sigmoid(n-&gt;bias + dbias1 +
00077 (n-&gt;weight + dweight1) * input);
00078 }
00079 };
00080
00081 ostream&amp; operator&lt;&lt;(ostream&amp; os, <span class="keyword">const</span> neuron&amp; n)
00082 {
00083 <span class="keywordflow">return</span> os &lt;&lt; *n.n &lt;&lt; <span class="stringliteral">" "</span> &lt;&lt; n.out &lt;&lt; <span class="stringliteral">" "</span> &lt;&lt; n.delta &lt;&lt; <span class="stringliteral">" "</span>
00084 &lt;&lt; n.ndelta &lt;&lt; <span class="stringliteral">" "</span> &lt;&lt; n.dbias1 &lt;&lt; <span class="stringliteral">" "</span> &lt;&lt; n.dbias2 &lt;&lt; <span class="stringliteral">" "</span>
00085 &lt;&lt; n.dweight1 &lt;&lt; <span class="stringliteral">" "</span> &lt;&lt; n.dweight2 &lt;&lt; <span class="stringliteral">" "</span> &lt;&lt; n.dxo;
00086 }
00087
00088
00089 <span class="comment">//---------------------------------------------------------------------------</span>
00090 <span class="comment">// layer</span>
00091 <span class="comment">//---------------------------------------------------------------------------</span>
00092
00093 <span class="keyword">class </span>layer: <span class="keyword">public</span> std::vector&lt;neuron&gt;
00094 {
00095 <span class="keyword">public</span>:
00096 layer(mlp::layer&amp; l)<span class="comment">//: std::vector&lt;neuron&gt;(l.begin(), l.end()) {}</span>
00097 {
00098 <span class="keywordflow">for</span> (mlp::layer::iterator n = l.begin(); n != l.end(); ++n)
00099 push_back(neuron(*n));
00100 }
00101
00102 <span class="keywordtype">void</span> reset()
00103 {
00104 <span class="keywordflow">for</span>(iterator n = begin(); n != end(); ++n)
00105 n-&gt;reset();
00106 }
00107
00108 vector operator()(<span class="keyword">const</span> vector&amp; input)
00109 {
00110 vector output(size());
00111
00112 <span class="keywordflow">for</span>(<span class="keywordtype">unsigned</span> i = 0; i &lt; output.size(); ++i)
00113 output[i] = (*this)[i](input);
00114
00115 <span class="keywordflow">return</span> output;
00116 }
00117 };
00118
00119
00120 <span class="comment">//---------------------------------------------------------------------------</span>
00121 <span class="comment">// net</span>
00122 <span class="comment">//---------------------------------------------------------------------------</span>
00123
00124 <span class="keyword">class </span>net: <span class="keyword">public</span> std::vector&lt;layer&gt;
00125 {
00126 <span class="keyword">public</span>:
00127 net(mlp::net&amp; n) <span class="comment">//: std::vector&lt;layer&gt;(n.begin(), n.end()) { reset(); }</span>
00128 {
00129 <span class="keywordflow">for</span> (mlp::net::iterator l = n.begin(); l != n.end(); ++l)
00130 push_back(*l);
00131 }
00132
00133 <span class="keyword">virtual</span> ~net() {}
00134
00135 <span class="keywordtype">void</span> reset()
00136 {
00137 <span class="keywordflow">for</span>(iterator l = begin(); l != end(); ++l)
00138 l-&gt;reset();
00139 }
00140
00141 real train(<span class="keyword">const</span> set&amp; ts,
00142 <span class="keywordtype">unsigned</span> epochs,
00143 real target_error,
00144 real tolerance,
00145 real eta = eta_default,
00146 real momentum = alpha_default,
00147 real lambda = lambda_default)
00148 {
00149 real error_ = max_real;
00150
00151 <span class="keywordflow">while</span> (epochs-- &amp;&amp; error_ &gt; target_error)
00152 {
00153 real last_error = error_;
00154
00155 init_delta();
00156
00157 error_ = error(ts);
00158
00159 <span class="keywordflow">if</span> (error_ &lt; last_error + tolerance)
00160 {
00161 coeff_adapt(eta, momentum, lambda);
00162 weight_update(ts.size(), <span class="keyword">true</span>, eta, momentum);
00163 }
00164 <span class="keywordflow">else</span>
00165 {
00166 eta *= backtrack_step;
00167 eta = max(eta, eta_floor);
00168 momentum = eta * lambda;
00169 weight_update(ts.size(), <span class="keyword">false</span>, eta, momentum);
00170 error_ = last_error;
00171 }
00172 }
00173
00174 <span class="keywordflow">return</span> error_;
00175 }
00176
00177 <span class="keyword">virtual</span> real error(<span class="keyword">const</span> set&amp; ts) = 0;
00178
00179 <span class="comment">// protected:</span>
00180 <span class="keywordtype">void</span> forward(vector input)
00181 {
00182 <span class="keywordflow">for</span> (iterator l = begin(); l != end(); ++l)
00183 {
00184 vector tmp = (*l)(input);
00185 input.swap(tmp);
00186 }
00187 }
00188
00189 <span class="comment">// private:</span>
00190 <span class="keywordtype">void</span> init_delta()
00191 {
00192 <span class="keywordflow">for</span> (iterator l = begin(); l != end(); ++l)
00193 <span class="keywordflow">for</span> (layer::iterator n = l-&gt;begin(); n != l-&gt;end(); ++n)
00194 fill(n-&gt;dxo.begin(), n-&gt;dxo.end(), n-&gt;ndelta = 0.0);
00195 }
00196
00197 <span class="keywordtype">void</span> coeff_adapt(real&amp; eta, real&amp; momentum, real&amp; lambda)
00198 {
00199 real me = 0, mw = 0, ew = 0;
00200
00201 <span class="keywordflow">for</span> (iterator l = begin(); l != end(); ++l)
00202 <span class="keywordflow">for</span> (layer::iterator n = l-&gt;begin(); n != l-&gt;end(); ++n)
00203 {
00204 me += n-&gt;dxo * n-&gt;dxo;
00205 mw += n-&gt;dweight1 * n-&gt;dweight1;
00206 ew += n-&gt;dxo * n-&gt;dweight1;
00207 }
00208
00209 me = max(static_cast&lt;real&gt;(sqrt(me)), me_floor);
00210 mw = max(static_cast&lt;real&gt;(sqrt(mw)), mw_floor);
00211 eta *= (1.0 + 0.5 * ew / ( me * mw));
00212 eta = max(eta, eta_floor);
00213 lambda = lambda0 * me / mw;
00214 momentum = eta * lambda;
00215 <span class="preprocessor">#ifdef DEBUG</span>
00216 <span class="preprocessor"></span> cout &lt;&lt; me &lt;&lt; <span class="stringliteral">" \t"</span> &lt;&lt; mw &lt;&lt; <span class="stringliteral">" \t"</span> &lt;&lt; ew &lt;&lt; <span class="stringliteral">" \t"</span>
00217 &lt;&lt; eta &lt;&lt; <span class="stringliteral">" \t"</span> &lt;&lt; momentum &lt;&lt; <span class="stringliteral">" \t"</span> &lt;&lt; lambda &lt;&lt; endl;
00218 <span class="preprocessor">#endif // DEBUG</span>
00219 <span class="preprocessor"></span> }
00220
00221 <span class="keywordtype">void</span> weight_update(<span class="keywordtype">unsigned</span> size, <span class="keywordtype">bool</span> fire, real eta, real momentum)
00222 {
00223 <span class="keywordflow">for</span> (iterator l = begin(); l != end(); ++l)
00224 <span class="keywordflow">for</span> (layer::iterator n = l-&gt;begin(); n != l-&gt;end(); ++n)
00225 {
00226 n-&gt;ndelta /= size;
00227 n-&gt;dxo /= size;
00228 <span class="keywordflow">if</span> (fire)
00229 {
00230 n-&gt;n-&gt;weight += n-&gt;dweight1;
00231 n-&gt;dweight2 = n-&gt;dweight1;
00232 n-&gt;n-&gt;bias += n-&gt;dbias1;
00233 n-&gt;dbias2 = n-&gt;dbias1;
00234 }
00235 n-&gt;dweight1 = eta * n-&gt;dxo + momentum * n-&gt;dweight2;
00236 n-&gt;dbias1 = eta * n-&gt;ndelta + momentum * n-&gt;dbias2;
00237 }
00238 }
00239 };
00240
00241 <span class="comment">//---------------------------------------------------------------------------</span>
00242
00243 } <span class="comment">// namespace qp</span>
00244
00245 <span class="comment">//-----------------------------------------------------------------------------</span>
00246
00247 <span class="preprocessor">#endif // qp_h</span>
00248 <span class="preprocessor"></span>
00249 <span class="comment">// Local Variables: </span>
00250 <span class="comment">// mode:C++ </span>
00251 <span class="comment">// End:</span>
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Thu Oct 19 05:06:42 2006 for EO by&nbsp;
<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>