Paradiseo-eo sources added
git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@40 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
parent
bc1f453978
commit
c3aec878e5
3609 changed files with 342772 additions and 0 deletions
266
trunk/paradiseo-eo/doc/html/qp_8h-source.html
Normal file
266
trunk/paradiseo-eo/doc/html/qp_8h-source.html
Normal file
|
|
@ -0,0 +1,266 @@
|
|||
<!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 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>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 <iostream></span> <span class="comment">// istream ostream</span>
|
||||
00011 <span class="preprocessor">#include <algorithm></span> <span class="comment">// fill</span>
|
||||
00012 <span class="preprocessor">#include <vector></span> <span class="comment">// vector</span>
|
||||
00013 <span class="preprocessor">#include <utils/rnd_generators.h></span> <span class="comment">// uniform_generator</span>
|
||||
00014 <span class="preprocessor">#include <mlp.h></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& _n):
|
||||
00057 n(&_n), out(0), delta(0), ndelta(0), dbias1(0), dbias2(0),
|
||||
00058 dweight1(n->weight.size(), 0),
|
||||
00059 dweight2(n->weight.size(), 0),
|
||||
00060 dxo(n->weight.size(), 0) {}
|
||||
00061
|
||||
00062 <span class="keywordtype">void</span> reset()
|
||||
00063 {
|
||||
00064 <span class="comment">// underlaying neuron</span>
|
||||
00065 n->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& input)
|
||||
00075 {
|
||||
00076 <span class="keywordflow">return</span> out = mlp::sigmoid(n->bias + dbias1 +
|
||||
00077 (n->weight + dweight1) * input);
|
||||
00078 }
|
||||
00079 };
|
||||
00080
|
||||
00081 ostream& operator<<(ostream& os, <span class="keyword">const</span> neuron& n)
|
||||
00082 {
|
||||
00083 <span class="keywordflow">return</span> os << *n.n << <span class="stringliteral">" "</span> << n.out << <span class="stringliteral">" "</span> << n.delta << <span class="stringliteral">" "</span>
|
||||
00084 << n.ndelta << <span class="stringliteral">" "</span> << n.dbias1 << <span class="stringliteral">" "</span> << n.dbias2 << <span class="stringliteral">" "</span>
|
||||
00085 << n.dweight1 << <span class="stringliteral">" "</span> << n.dweight2 << <span class="stringliteral">" "</span> << 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<neuron>
|
||||
00094 {
|
||||
00095 <span class="keyword">public</span>:
|
||||
00096 layer(mlp::layer& l)<span class="comment">//: std::vector<neuron>(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->reset();
|
||||
00106 }
|
||||
00107
|
||||
00108 vector operator()(<span class="keyword">const</span> vector& input)
|
||||
00109 {
|
||||
00110 vector output(size());
|
||||
00111
|
||||
00112 <span class="keywordflow">for</span>(<span class="keywordtype">unsigned</span> i = 0; i < 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<layer>
|
||||
00125 {
|
||||
00126 <span class="keyword">public</span>:
|
||||
00127 net(mlp::net& n) <span class="comment">//: std::vector<layer>(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->reset();
|
||||
00139 }
|
||||
00140
|
||||
00141 real train(<span class="keyword">const</span> set& 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-- && error_ > 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_ < 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& 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->begin(); n != l->end(); ++n)
|
||||
00194 fill(n->dxo.begin(), n->dxo.end(), n->ndelta = 0.0);
|
||||
00195 }
|
||||
00196
|
||||
00197 <span class="keywordtype">void</span> coeff_adapt(real& eta, real& momentum, real& 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->begin(); n != l->end(); ++n)
|
||||
00203 {
|
||||
00204 me += n->dxo * n->dxo;
|
||||
00205 mw += n->dweight1 * n->dweight1;
|
||||
00206 ew += n->dxo * n->dweight1;
|
||||
00207 }
|
||||
00208
|
||||
00209 me = max(static_cast<real>(sqrt(me)), me_floor);
|
||||
00210 mw = max(static_cast<real>(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 << me << <span class="stringliteral">" \t"</span> << mw << <span class="stringliteral">" \t"</span> << ew << <span class="stringliteral">" \t"</span>
|
||||
00217 << eta << <span class="stringliteral">" \t"</span> << momentum << <span class="stringliteral">" \t"</span> << lambda << 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->begin(); n != l->end(); ++n)
|
||||
00225 {
|
||||
00226 n->ndelta /= size;
|
||||
00227 n->dxo /= size;
|
||||
00228 <span class="keywordflow">if</span> (fire)
|
||||
00229 {
|
||||
00230 n->n->weight += n->dweight1;
|
||||
00231 n->dweight2 = n->dweight1;
|
||||
00232 n->n->bias += n->dbias1;
|
||||
00233 n->dbias2 = n->dbias1;
|
||||
00234 }
|
||||
00235 n->dweight1 = eta * n->dxo + momentum * n->dweight2;
|
||||
00236 n->dbias1 = eta * n->ndelta + momentum * n->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
|
||||
<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>
|
||||
Loading…
Add table
Add a link
Reference in a new issue