TheAlgorithms-C/da/d2a/group__adaline.html

549 lines
41 KiB
HTML
Raw Normal View History

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.3"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C: Adaline learning algorithm</title>
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr id="projectrow">
<td id="projectalign">
<div id="projectname">Algorithms_in_C<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.3 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search",'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('da/d2a/group__adaline.html','../../'); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="summary">
<a href="#nested-classes">Data Structures</a> &#124;
<a href="#define-members">Macros</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle"><div class="title">Adaline learning algorithm<div class="ingroups"><a class="el" href="../../d9/d66/group__machine__learning.html">Machine learning algorithms</a></div></div></div>
</div><!--header-->
<div class="contents">
<div class="dynheader">
Collaboration diagram for Adaline learning algorithm:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/d2a/group__adaline.svg" width="414" height="36"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="nested-classes" name="nested-classes"></a>
Data Structures</h2></td></tr>
<tr class="memitem:"><td class="memItemLeft" align="right" valign="top">struct &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d2/daa/structadaline.html">adaline</a></td></tr>
<tr class="memdesc:"><td class="mdescLeft">&#160;</td><td class="mdescRight">structure to hold adaline model parameters <a href="../../d2/daa/structadaline.html#details">More...</a><br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="define-members" name="define-members"></a>
Macros</h2></td></tr>
<tr class="memitem:ga555ba960994e9bccb2029764588f694f"><td class="memItemLeft" align="right" valign="top"><a id="ga555ba960994e9bccb2029764588f694f" name="ga555ba960994e9bccb2029764588f694f"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><b>MAX_ADALINE_ITER</b>&#160;&#160;&#160;500</td></tr>
<tr class="memdesc:ga555ba960994e9bccb2029764588f694f"><td class="mdescLeft">&#160;</td><td class="mdescRight">Maximum number of iterations to learn. <br /></td></tr>
<tr class="separator:ga555ba960994e9bccb2029764588f694f"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:gab4d49d73dec94c092b7ffadba55fb020"><td class="memItemLeft" align="right" valign="top"><a id="gab4d49d73dec94c092b7ffadba55fb020" name="gab4d49d73dec94c092b7ffadba55fb020"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><b>ADALINE_ACCURACY</b>&#160;&#160;&#160;1e-5</td></tr>
<tr class="memdesc:gab4d49d73dec94c092b7ffadba55fb020"><td class="mdescLeft">&#160;</td><td class="mdescRight">convergence accuracy \(=1\times10^{-5}\) <br /></td></tr>
<tr class="separator:gab4d49d73dec94c092b7ffadba55fb020"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:gacd88962c5f6341e43cbc69b4a7d3485b"><td class="memItemLeft" align="right" valign="top">struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d2a/group__adaline.html#gacd88962c5f6341e43cbc69b4a7d3485b">new_adaline</a> (const int num_features, const double eta)</td></tr>
<tr class="memdesc:gacd88962c5f6341e43cbc69b4a7d3485b"><td class="mdescLeft">&#160;</td><td class="mdescRight">Default constructor. <a href="../../da/d2a/group__adaline.html#gacd88962c5f6341e43cbc69b4a7d3485b">More...</a><br /></td></tr>
<tr class="separator:gacd88962c5f6341e43cbc69b4a7d3485b"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga6f35caa3084772cc126ac7b20f67f665"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d2a/group__adaline.html#ga6f35caa3084772cc126ac7b20f67f665">delete_adaline</a> (struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> *ada)</td></tr>
<tr class="memdesc:ga6f35caa3084772cc126ac7b20f67f665"><td class="mdescLeft">&#160;</td><td class="mdescRight">delete dynamically allocated memory <a href="../../da/d2a/group__adaline.html#ga6f35caa3084772cc126ac7b20f67f665">More...</a><br /></td></tr>
<tr class="separator:ga6f35caa3084772cc126ac7b20f67f665"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga43576566b020c4157d4fb28f0dd45cfa"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d2a/group__adaline.html#ga43576566b020c4157d4fb28f0dd45cfa">adaline_activation</a> (double x)</td></tr>
<tr class="memdesc:ga43576566b020c4157d4fb28f0dd45cfa"><td class="mdescLeft">&#160;</td><td class="mdescRight"><a href="https://en.wikipedia.org/wiki/Heaviside_step_function" target="_blank">Heaviside activation function</a> <img src="https://upload.wikimedia.org/wikipedia/commons/d/d9/Dirac_distribution_CDF.svg" alt="" style="pointer-events: none;" width="200px" class="inline"/> <a href="../../da/d2a/group__adaline.html#ga43576566b020c4157d4fb28f0dd45cfa">More...</a><br /></td></tr>
<tr class="separator:ga43576566b020c4157d4fb28f0dd45cfa"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:gadb1a7acb555ad62daaf3806bf2fed036"><td class="memItemLeft" align="right" valign="top">char *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d2a/group__adaline.html#gadb1a7acb555ad62daaf3806bf2fed036">adaline_get_weights_str</a> (const struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> *ada)</td></tr>
<tr class="memdesc:gadb1a7acb555ad62daaf3806bf2fed036"><td class="mdescLeft">&#160;</td><td class="mdescRight">Operator to print the weights of the model. <a href="../../da/d2a/group__adaline.html#gadb1a7acb555ad62daaf3806bf2fed036">More...</a><br /></td></tr>
<tr class="separator:gadb1a7acb555ad62daaf3806bf2fed036"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:gac70b578aee679005fd336073969c3d94"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d2a/group__adaline.html#gac70b578aee679005fd336073969c3d94">adaline_predict</a> (struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> *ada, const double *x, double *out)</td></tr>
<tr class="memdesc:gac70b578aee679005fd336073969c3d94"><td class="mdescLeft">&#160;</td><td class="mdescRight">predict the output of the model for given set of features <a href="../../da/d2a/group__adaline.html#gac70b578aee679005fd336073969c3d94">More...</a><br /></td></tr>
<tr class="separator:gac70b578aee679005fd336073969c3d94"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ga20d3642e0a87f36fdb7bf91b023cd166"><td class="memItemLeft" align="right" valign="top">double&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d2a/group__adaline.html#ga20d3642e0a87f36fdb7bf91b023cd166">adaline_fit_sample</a> (struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> *ada, const double *x, const int y)</td></tr>
<tr class="memdesc:ga20d3642e0a87f36fdb7bf91b023cd166"><td class="mdescLeft">&#160;</td><td class="mdescRight">Update the weights of the model using supervised learning for one feature vector. <a href="../../da/d2a/group__adaline.html#ga20d3642e0a87f36fdb7bf91b023cd166">More...</a><br /></td></tr>
<tr class="separator:ga20d3642e0a87f36fdb7bf91b023cd166"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:gaa52120912e32d2893fe1c6d78da5befd"><td class="memItemLeft" align="right" valign="top">void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d2a/group__adaline.html#gaa52120912e32d2893fe1c6d78da5befd">adaline_fit</a> (struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> *ada, double **X, const int *y, const int N)</td></tr>
<tr class="memdesc:gaa52120912e32d2893fe1c6d78da5befd"><td class="mdescLeft">&#160;</td><td class="mdescRight">Update the weights of the model using supervised learning for an array of vectors. <a href="../../da/d2a/group__adaline.html#gaa52120912e32d2893fe1c6d78da5befd">More...</a><br /></td></tr>
<tr class="separator:gaa52120912e32d2893fe1c6d78da5befd"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<h2 class="groupheader">Function Documentation</h2>
<a id="ga43576566b020c4157d4fb28f0dd45cfa" name="ga43576566b020c4157d4fb28f0dd45cfa"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga43576566b020c4157d4fb28f0dd45cfa">&#9670;&nbsp;</a></span>adaline_activation()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int adaline_activation </td>
<td>(</td>
<td class="paramtype">double&#160;</td>
<td class="paramname"><em>x</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p><a href="https://en.wikipedia.org/wiki/Heaviside_step_function" target="_blank">Heaviside activation function</a> <img src="https://upload.wikimedia.org/wikipedia/commons/d/d9/Dirac_distribution_CDF.svg" alt="" style="pointer-events: none;" width="200px" class="inline"/> </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">x</td><td>activation function input </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>\(f(x)= \begin{cases}1 &amp; \forall\; x &gt; 0\\ -1 &amp; \forall\; x \le0 \end{cases}\) </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 105</span>{ <span class="keywordflow">return</span> x &gt; 0 ? 1 : -1; }</div>
</div><!-- fragment -->
</div>
</div>
<a id="gaa52120912e32d2893fe1c6d78da5befd" name="gaa52120912e32d2893fe1c6d78da5befd"></a>
<h2 class="memtitle"><span class="permalink"><a href="#gaa52120912e32d2893fe1c6d78da5befd">&#9670;&nbsp;</a></span>adaline_fit()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void adaline_fit </td>
<td>(</td>
<td class="paramtype">struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> *&#160;</td>
<td class="paramname"><em>ada</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double **&#160;</td>
<td class="paramname"><em>X</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const int *&#160;</td>
<td class="paramname"><em>y</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const int&#160;</td>
<td class="paramname"><em>N</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Update the weights of the model using supervised learning for an array of vectors. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">ada</td><td>adaline model to train </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">X</td><td>array of feature vector </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">y</td><td>known output value for each feature vector </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">N</td><td>number of training samples </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 185</span>{</div>
<div class="line"><span class="lineno"> 186</span> <span class="keywordtype">double</span> avg_pred_error = 1.f;</div>
<div class="line"><span class="lineno"> 187</span> </div>
<div class="line"><span class="lineno"> 188</span> <span class="keywordtype">int</span> iter;</div>
<div class="line"><span class="lineno"> 189</span> <span class="keywordflow">for</span> (iter = 0;</div>
<div class="line"><span class="lineno"> 190</span> (iter &lt; <a class="code hl_define" href="../../da/d2a/group__adaline.html#ga555ba960994e9bccb2029764588f694f">MAX_ADALINE_ITER</a>) &amp;&amp; (avg_pred_error &gt; <a class="code hl_define" href="../../da/d2a/group__adaline.html#gab4d49d73dec94c092b7ffadba55fb020">ADALINE_ACCURACY</a>);</div>
<div class="line"><span class="lineno"> 191</span> iter++)</div>
<div class="line"><span class="lineno"> 192</span> {</div>
<div class="line"><span class="lineno"> 193</span> avg_pred_error = 0.f;</div>
<div class="line"><span class="lineno"> 194</span> </div>
<div class="line"><span class="lineno"> 195</span> <span class="comment">// perform fit for each sample</span></div>
<div class="line"><span class="lineno"> 196</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; N; i++)</div>
<div class="line"><span class="lineno"> 197</span> {</div>
<div class="line"><span class="lineno"> 198</span> <span class="keywordtype">double</span> err = <a class="code hl_function" href="../../da/d2a/group__adaline.html#ga20d3642e0a87f36fdb7bf91b023cd166">adaline_fit_sample</a>(ada, X[i], y[i]);</div>
<div class="line"><span class="lineno"> 199</span> avg_pred_error += fabs(err);</div>
<div class="line"><span class="lineno"> 200</span> }</div>
<div class="line"><span class="lineno"> 201</span> avg_pred_error /= N;</div>
<div class="line"><span class="lineno"> 202</span> </div>
<div class="line"><span class="lineno"> 203</span> <span class="comment">// Print updates every 200th iteration</span></div>
<div class="line"><span class="lineno"> 204</span> <span class="comment">// if (iter % 100 == 0)</span></div>
<div class="line"><span class="lineno"> 205</span> printf(<span class="stringliteral">&quot;\tIter %3d: Training weights: %s\tAvg error: %.4f\n&quot;</span>, iter,</div>
<div class="line"><span class="lineno"> 206</span> <a class="code hl_function" href="../../da/d2a/group__adaline.html#gadb1a7acb555ad62daaf3806bf2fed036">adaline_get_weights_str</a>(ada), avg_pred_error);</div>
<div class="line"><span class="lineno"> 207</span> }</div>
<div class="line"><span class="lineno"> 208</span> </div>
<div class="line"><span class="lineno"> 209</span> <span class="keywordflow">if</span> (iter &lt; <a class="code hl_define" href="../../da/d2a/group__adaline.html#ga555ba960994e9bccb2029764588f694f">MAX_ADALINE_ITER</a>)</div>
<div class="line"><span class="lineno"> 210</span> printf(<span class="stringliteral">&quot;Converged after %d iterations.\n&quot;</span>, iter);</div>
<div class="line"><span class="lineno"> 211</span> <span class="keywordflow">else</span></div>
<div class="line"><span class="lineno"> 212</span> printf(<span class="stringliteral">&quot;Did not converged after %d iterations.\n&quot;</span>, iter);</div>
<div class="line"><span class="lineno"> 213</span>}</div>
<div class="ttc" id="agroup__adaline_html_ga20d3642e0a87f36fdb7bf91b023cd166"><div class="ttname"><a href="../../da/d2a/group__adaline.html#ga20d3642e0a87f36fdb7bf91b023cd166">adaline_fit_sample</a></div><div class="ttdeci">double adaline_fit_sample(struct adaline *ada, const double *x, const int y)</div><div class="ttdoc">Update the weights of the model using supervised learning for one feature vector.</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:158</div></div>
<div class="ttc" id="agroup__adaline_html_ga555ba960994e9bccb2029764588f694f"><div class="ttname"><a href="../../da/d2a/group__adaline.html#ga555ba960994e9bccb2029764588f694f">MAX_ADALINE_ITER</a></div><div class="ttdeci">#define MAX_ADALINE_ITER</div><div class="ttdoc">Maximum number of iterations to learn.</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:40</div></div>
<div class="ttc" id="agroup__adaline_html_gab4d49d73dec94c092b7ffadba55fb020"><div class="ttname"><a href="../../da/d2a/group__adaline.html#gab4d49d73dec94c092b7ffadba55fb020">ADALINE_ACCURACY</a></div><div class="ttdeci">#define ADALINE_ACCURACY</div><div class="ttdoc">convergence accuracy</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:51</div></div>
<div class="ttc" id="agroup__adaline_html_gadb1a7acb555ad62daaf3806bf2fed036"><div class="ttname"><a href="../../da/d2a/group__adaline.html#gadb1a7acb555ad62daaf3806bf2fed036">adaline_get_weights_str</a></div><div class="ttdeci">char * adaline_get_weights_str(const struct adaline *ada)</div><div class="ttdoc">Operator to print the weights of the model.</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:112</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/d2a/group__adaline_gaa52120912e32d2893fe1c6d78da5befd_cgraph.svg" width="635" height="88"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="ga20d3642e0a87f36fdb7bf91b023cd166" name="ga20d3642e0a87f36fdb7bf91b023cd166"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga20d3642e0a87f36fdb7bf91b023cd166">&#9670;&nbsp;</a></span>adaline_fit_sample()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">double adaline_fit_sample </td>
<td>(</td>
<td class="paramtype">struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> *&#160;</td>
<td class="paramname"><em>ada</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const double *&#160;</td>
<td class="paramname"><em>x</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const int&#160;</td>
<td class="paramname"><em>y</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Update the weights of the model using supervised learning for one feature vector. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">ada</td><td>adaline model to fit </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">x</td><td>feature vector </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">y</td><td>known output value </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>correction factor </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 159</span>{</div>
<div class="line"><span class="lineno"> 160</span> <span class="comment">/* output of the model with current weights */</span></div>
<div class="line"><span class="lineno"> 161</span> <span class="keywordtype">int</span> p = <a class="code hl_function" href="../../da/d2a/group__adaline.html#gac70b578aee679005fd336073969c3d94">adaline_predict</a>(ada, x, NULL);</div>
<div class="line"><span class="lineno"> 162</span> <span class="keywordtype">int</span> prediction_error = y - p; <span class="comment">// error in estimation</span></div>
<div class="line"><span class="lineno"> 163</span> <span class="keywordtype">double</span> correction_factor = ada-&gt;<a class="code hl_variable" href="../../d2/daa/structadaline.html#a85dbd7cce6195d11ebb388220b96bde2">eta</a> * prediction_error;</div>
<div class="line"><span class="lineno"> 164</span> </div>
<div class="line"><span class="lineno"> 165</span> <span class="comment">/* update each weight, the last weight is the bias term */</span></div>
<div class="line"><span class="lineno"> 166</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; ada-&gt;<a class="code hl_variable" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a> - 1; i++)</div>
<div class="line"><span class="lineno"> 167</span> {</div>
<div class="line"><span class="lineno"> 168</span> ada-&gt;<a class="code hl_variable" href="../../d2/daa/structadaline.html#a32e58c03fd9258709eae6138ad0ec657">weights</a>[i] += correction_factor * x[i];</div>
<div class="line"><span class="lineno"> 169</span> }</div>
<div class="line"><span class="lineno"> 170</span> ada-&gt;<a class="code hl_variable" href="../../d2/daa/structadaline.html#a32e58c03fd9258709eae6138ad0ec657">weights</a>[ada-&gt;<a class="code hl_variable" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a> - 1] += correction_factor; <span class="comment">// update bias</span></div>
<div class="line"><span class="lineno"> 171</span> </div>
<div class="line"><span class="lineno"> 172</span> <span class="keywordflow">return</span> correction_factor;</div>
<div class="line"><span class="lineno"> 173</span>}</div>
<div class="ttc" id="agroup__adaline_html_gac70b578aee679005fd336073969c3d94"><div class="ttname"><a href="../../da/d2a/group__adaline.html#gac70b578aee679005fd336073969c3d94">adaline_predict</a></div><div class="ttdeci">int adaline_predict(struct adaline *ada, const double *x, double *out)</div><div class="ttdoc">predict the output of the model for given set of features</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:136</div></div>
<div class="ttc" id="astructadaline_html_a32e58c03fd9258709eae6138ad0ec657"><div class="ttname"><a href="../../d2/daa/structadaline.html#a32e58c03fd9258709eae6138ad0ec657">adaline::weights</a></div><div class="ttdeci">double * weights</div><div class="ttdoc">weights of the neural network</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:46</div></div>
<div class="ttc" id="astructadaline_html_a53314e737a0a5ff4552a03bcc9dafbc1"><div class="ttname"><a href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">adaline::num_weights</a></div><div class="ttdeci">int num_weights</div><div class="ttdoc">number of weights of the neural network</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:47</div></div>
<div class="ttc" id="astructadaline_html_a85dbd7cce6195d11ebb388220b96bde2"><div class="ttname"><a href="../../d2/daa/structadaline.html#a85dbd7cce6195d11ebb388220b96bde2">adaline::eta</a></div><div class="ttdeci">double eta</div><div class="ttdoc">learning rate of the algorithm</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:45</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/d2a/group__adaline_ga20d3642e0a87f36fdb7bf91b023cd166_cgraph.svg" width="474" height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="gadb1a7acb555ad62daaf3806bf2fed036" name="gadb1a7acb555ad62daaf3806bf2fed036"></a>
<h2 class="memtitle"><span class="permalink"><a href="#gadb1a7acb555ad62daaf3806bf2fed036">&#9670;&nbsp;</a></span>adaline_get_weights_str()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">char * adaline_get_weights_str </td>
<td>(</td>
<td class="paramtype">const struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> *&#160;</td>
<td class="paramname"><em>ada</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Operator to print the weights of the model. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">ada</td><td>model for which the values to print </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>pointer to a NULL terminated string of formatted weights </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 113</span>{</div>
<div class="line"><span class="lineno"> 114</span> <span class="keyword">static</span> <span class="keywordtype">char</span> out[100]; <span class="comment">// static so the value is persistent</span></div>
<div class="line"><span class="lineno"> 115</span> </div>
<div class="line"><span class="lineno"> 116</span> sprintf(out, <span class="stringliteral">&quot;&lt;&quot;</span>);</div>
<div class="line"><span class="lineno"> 117</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; ada-&gt;<a class="code hl_variable" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a>; i++)</div>
<div class="line"><span class="lineno"> 118</span> {</div>
<div class="line"><span class="lineno"> 119</span> sprintf(out, <span class="stringliteral">&quot;%s%.4g&quot;</span>, out, ada-&gt;<a class="code hl_variable" href="../../d2/daa/structadaline.html#a32e58c03fd9258709eae6138ad0ec657">weights</a>[i]);</div>
<div class="line"><span class="lineno"> 120</span> <span class="keywordflow">if</span> (i &lt; ada-&gt;num_weights - 1)</div>
<div class="line"><span class="lineno"> 121</span> sprintf(out, <span class="stringliteral">&quot;%s, &quot;</span>, out);</div>
<div class="line"><span class="lineno"> 122</span> }</div>
<div class="line"><span class="lineno"> 123</span> sprintf(out, <span class="stringliteral">&quot;%s&gt;&quot;</span>, out);</div>
<div class="line"><span class="lineno"> 124</span> <span class="keywordflow">return</span> out;</div>
<div class="line"><span class="lineno"> 125</span>}</div>
</div><!-- fragment -->
</div>
</div>
<a id="gac70b578aee679005fd336073969c3d94" name="gac70b578aee679005fd336073969c3d94"></a>
<h2 class="memtitle"><span class="permalink"><a href="#gac70b578aee679005fd336073969c3d94">&#9670;&nbsp;</a></span>adaline_predict()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int adaline_predict </td>
<td>(</td>
<td class="paramtype">struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> *&#160;</td>
<td class="paramname"><em>ada</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const double *&#160;</td>
<td class="paramname"><em>x</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">double *&#160;</td>
<td class="paramname"><em>out</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>predict the output of the model for given set of features </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">ada</td><td>adaline model to predict </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">x</td><td>input vector </td></tr>
<tr><td class="paramdir">[out]</td><td class="paramname">out</td><td>optional argument to return neuron output before applying activation function (<code>NULL</code> to ignore) </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>model prediction output </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 137</span>{</div>
<div class="line"><span class="lineno"> 138</span> <span class="keywordtype">double</span> y = ada-&gt;<a class="code hl_variable" href="../../d2/daa/structadaline.html#a32e58c03fd9258709eae6138ad0ec657">weights</a>[ada-&gt;<a class="code hl_variable" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a> - 1]; <span class="comment">// assign bias value</span></div>
<div class="line"><span class="lineno"> 139</span> </div>
<div class="line"><span class="lineno"> 140</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; ada-&gt;<a class="code hl_variable" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a> - 1; i++) y += x[i] * ada-&gt;<a class="code hl_variable" href="../../d2/daa/structadaline.html#a32e58c03fd9258709eae6138ad0ec657">weights</a>[i];</div>
<div class="line"><span class="lineno"> 141</span> </div>
<div class="line"><span class="lineno"> 142</span> if (out) <span class="comment">// if out variable is not NULL</span></div>
<div class="line"><span class="lineno"> 143</span> *out = y;</div>
<div class="line"><span class="lineno"> 144</span> </div>
<div class="line"><span class="lineno"> 145</span> <span class="comment">// quantizer: apply ADALINE threshold function</span></div>
<div class="line"><span class="lineno"> 146</span> <span class="keywordflow">return</span> <a class="code hl_function" href="../../da/d2a/group__adaline.html#ga43576566b020c4157d4fb28f0dd45cfa">adaline_activation</a>(y);</div>
<div class="line"><span class="lineno"> 147</span>}</div>
<div class="ttc" id="agroup__adaline_html_ga43576566b020c4157d4fb28f0dd45cfa"><div class="ttname"><a href="../../da/d2a/group__adaline.html#ga43576566b020c4157d4fb28f0dd45cfa">adaline_activation</a></div><div class="ttdeci">int adaline_activation(double x)</div><div class="ttdoc">Heaviside activation function</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:105</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/d2a/group__adaline_gac70b578aee679005fd336073969c3d94_cgraph.svg" width="295" height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="ga6f35caa3084772cc126ac7b20f67f665" name="ga6f35caa3084772cc126ac7b20f67f665"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga6f35caa3084772cc126ac7b20f67f665">&#9670;&nbsp;</a></span>delete_adaline()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">void delete_adaline </td>
<td>(</td>
<td class="paramtype">struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> *&#160;</td>
<td class="paramname"><em>ada</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>delete dynamically allocated memory </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">ada</td><td>model from which the memory is to be freed. </td></tr>
</table>
</dd>
</dl>
<div class="fragment"><div class="line"><span class="lineno"> 90</span>{</div>
<div class="line"><span class="lineno"> 91</span> <span class="keywordflow">if</span> (ada == NULL)</div>
<div class="line"><span class="lineno"> 92</span> <span class="keywordflow">return</span>;</div>
<div class="line"><span class="lineno"> 93</span> </div>
<div class="line"><span class="lineno"> 94</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(ada-&gt;<a class="code hl_variable" href="../../d2/daa/structadaline.html#a32e58c03fd9258709eae6138ad0ec657">weights</a>);</div>
<div class="line"><span class="lineno"> 95</span>};</div>
<div class="ttc" id="amalloc__dbg_8h_html_a9cc854374299a1dd933bf62029761768"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a></div><div class="ttdeci">#define free(ptr)</div><div class="ttdoc">This macro replace the standard free function with free_dbg.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.h:26</div></div>
</div><!-- fragment -->
</div>
</div>
<a id="gacd88962c5f6341e43cbc69b4a7d3485b" name="gacd88962c5f6341e43cbc69b4a7d3485b"></a>
<h2 class="memtitle"><span class="permalink"><a href="#gacd88962c5f6341e43cbc69b4a7d3485b">&#9670;&nbsp;</a></span>new_adaline()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> new_adaline </td>
<td>(</td>
<td class="paramtype">const int&#160;</td>
<td class="paramname"><em>num_features</em>, </td>
</tr>
<tr>
<td class="paramkey"></td>
<td></td>
<td class="paramtype">const double&#160;</td>
<td class="paramname"><em>eta</em>&#160;</td>
</tr>
<tr>
<td></td>
<td>)</td>
<td></td><td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Default constructor. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramdir">[in]</td><td class="paramname">num_features</td><td>number of features present </td></tr>
<tr><td class="paramdir">[in]</td><td class="paramname">eta</td><td>learning rate (optional, default=0.1) </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>new adaline model </dd></dl>
<div class="fragment"><div class="line"><span class="lineno"> 60</span>{</div>
<div class="line"><span class="lineno"> 61</span> <span class="keywordflow">if</span> (eta &lt;= 0.f || eta &gt;= 1.f)</div>
<div class="line"><span class="lineno"> 62</span> {</div>
<div class="line"><span class="lineno"> 63</span> fprintf(stderr, <span class="stringliteral">&quot;learning rate should be &gt; 0 and &lt; 1\n&quot;</span>);</div>
<div class="line"><span class="lineno"> 64</span> exit(EXIT_FAILURE);</div>
<div class="line"><span class="lineno"> 65</span> }</div>
<div class="line"><span class="lineno"> 66</span> </div>
<div class="line"><span class="lineno"> 67</span> <span class="comment">// additional weight is for the constant bias term</span></div>
<div class="line"><span class="lineno"> 68</span> <span class="keywordtype">int</span> num_weights = num_features + 1;</div>
<div class="line"><span class="lineno"> 69</span> <span class="keyword">struct </span><a class="code hl_struct" href="../../d2/daa/structadaline.html">adaline</a> ada;</div>
<div class="line"><span class="lineno"> 70</span> ada.<a class="code hl_variable" href="../../d2/daa/structadaline.html#a85dbd7cce6195d11ebb388220b96bde2">eta</a> = <a class="code hl_variable" href="../../d2/daa/structadaline.html#a85dbd7cce6195d11ebb388220b96bde2">eta</a>;</div>
<div class="line"><span class="lineno"> 71</span> ada.num_weights = <a class="code hl_variable" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a>;</div>
<div class="line"><span class="lineno"> 72</span> ada.weights = (<span class="keywordtype">double</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(<a class="code hl_variable" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a> * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
<div class="line"><span class="lineno"> 73</span> <span class="keywordflow">if</span> (!ada.weights)</div>
<div class="line"><span class="lineno"> 74</span> {</div>
<div class="line"><span class="lineno"> 75</span> perror(<span class="stringliteral">&quot;Unable to allocate error for weights!&quot;</span>);</div>
<div class="line"><span class="lineno"> 76</span> <span class="keywordflow">return</span> ada;</div>
<div class="line"><span class="lineno"> 77</span> }</div>
<div class="line"><span class="lineno"> 78</span> </div>
<div class="line"><span class="lineno"> 79</span> <span class="comment">// initialize with random weights in the range [-50, 49]</span></div>
<div class="line"><span class="lineno"> 80</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code hl_variable" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a>; i++) ada.weights[i] = 1.f;</div>
<div class="line"><span class="lineno"> 81</span> <span class="comment">// ada.weights[i] = (double)(rand() % 100) - 50);</span></div>
<div class="line"><span class="lineno"> 82</span> </div>
<div class="line"><span class="lineno"> 83</span> <span class="keywordflow">return</span> ada;</div>
<div class="line"><span class="lineno"> 84</span>}</div>
<div class="ttc" id="amalloc__dbg_8h_html_a725f50ecaf1959d96de79b36b4788fee"><div class="ttname"><a href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a></div><div class="ttdeci">#define malloc(bytes)</div><div class="ttdoc">This macro replace the standard malloc function with malloc_dbg.</div><div class="ttdef"><b>Definition:</b> malloc_dbg.h:18</div></div>
<div class="ttc" id="astructadaline_html"><div class="ttname"><a href="../../d2/daa/structadaline.html">adaline</a></div><div class="ttdoc">structure to hold adaline model parameters</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:44</div></div>
</div><!-- fragment -->
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.3 </li>
</ul>
</div>
</body>
</html>