TheAlgorithms-C/da/d2a/group__adaline.html
2021-02-17 22:41:16 +00:00

550 lines
43 KiB
HTML

<!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=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<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 style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">Algorithms_in_C
&#160;<span id="projectnumber">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.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "../../search",false,'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:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(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:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(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 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 name="define-members"></a>
Macros</h2></td></tr>
<tr class="memitem:ga555ba960994e9bccb2029764588f694f"><td class="memItemLeft" align="right" valign="top"><a id="ga555ba960994e9bccb2029764588f694f"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d2a/group__adaline.html#ga555ba960994e9bccb2029764588f694f">MAX_ADALINE_ITER</a>&#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"></a>
#define&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d2a/group__adaline.html#gab4d49d73dec94c092b7ffadba55fb020">ADALINE_ACCURACY</a>&#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 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">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:ga251695a79baa885cafdcf6d8ed4ac120"><td class="memItemLeft" align="right" valign="top">char *&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d2a/group__adaline.html#ga251695a79baa885cafdcf6d8ed4ac120">adaline_get_weights_str</a> (const struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> *ada)</td></tr>
<tr class="memdesc:ga251695a79baa885cafdcf6d8ed4ac120"><td class="mdescLeft">&#160;</td><td class="mdescRight">Operator to print the weights of the model. <a href="../../da/d2a/group__adaline.html#ga251695a79baa885cafdcf6d8ed4ac120">More...</a><br /></td></tr>
<tr class="separator:ga251695a79baa885cafdcf6d8ed4ac120"><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"></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">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"><a name="l00105"></a><span class="lineno"> 105</span>&#160;{ <span class="keywordflow">return</span> x &gt; 0 ? 1 : -1; }</div>
</div><!-- fragment -->
</div>
</div>
<a id="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"><a name="l00185"></a><span class="lineno"> 185</span>&#160;{</div>
<div class="line"><a name="l00186"></a><span class="lineno"> 186</span>&#160; <span class="keywordtype">double</span> avg_pred_error = 1.f;</div>
<div class="line"><a name="l00187"></a><span class="lineno"> 187</span>&#160; </div>
<div class="line"><a name="l00188"></a><span class="lineno"> 188</span>&#160; <span class="keywordtype">int</span> iter;</div>
<div class="line"><a name="l00189"></a><span class="lineno"> 189</span>&#160; <span class="keywordflow">for</span> (iter = 0;</div>
<div class="line"><a name="l00190"></a><span class="lineno"> 190</span>&#160; (iter &lt; <a class="code" href="../../da/d2a/group__adaline.html#ga555ba960994e9bccb2029764588f694f">MAX_ADALINE_ITER</a>) &amp;&amp; (avg_pred_error &gt; <a class="code" href="../../da/d2a/group__adaline.html#gab4d49d73dec94c092b7ffadba55fb020">ADALINE_ACCURACY</a>);</div>
<div class="line"><a name="l00191"></a><span class="lineno"> 191</span>&#160; iter++)</div>
<div class="line"><a name="l00192"></a><span class="lineno"> 192</span>&#160; {</div>
<div class="line"><a name="l00193"></a><span class="lineno"> 193</span>&#160; avg_pred_error = 0.f;</div>
<div class="line"><a name="l00194"></a><span class="lineno"> 194</span>&#160; </div>
<div class="line"><a name="l00195"></a><span class="lineno"> 195</span>&#160; <span class="comment">// perform fit for each sample</span></div>
<div class="line"><a name="l00196"></a><span class="lineno"> 196</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; N; i++)</div>
<div class="line"><a name="l00197"></a><span class="lineno"> 197</span>&#160; {</div>
<div class="line"><a name="l00198"></a><span class="lineno"> 198</span>&#160; <span class="keywordtype">double</span> err = <a class="code" href="../../da/d2a/group__adaline.html#ga20d3642e0a87f36fdb7bf91b023cd166">adaline_fit_sample</a>(ada, X[i], y[i]);</div>
<div class="line"><a name="l00199"></a><span class="lineno"> 199</span>&#160; avg_pred_error += fabs(err);</div>
<div class="line"><a name="l00200"></a><span class="lineno"> 200</span>&#160; }</div>
<div class="line"><a name="l00201"></a><span class="lineno"> 201</span>&#160; avg_pred_error /= N;</div>
<div class="line"><a name="l00202"></a><span class="lineno"> 202</span>&#160; </div>
<div class="line"><a name="l00203"></a><span class="lineno"> 203</span>&#160; <span class="comment">// Print updates every 200th iteration</span></div>
<div class="line"><a name="l00204"></a><span class="lineno"> 204</span>&#160; <span class="comment">// if (iter % 100 == 0)</span></div>
<div class="line"><a name="l00205"></a><span class="lineno"> 205</span>&#160; printf(<span class="stringliteral">&quot;\tIter %3d: Training weights: %s\tAvg error: %.4f\n&quot;</span>, iter,</div>
<div class="line"><a name="l00206"></a><span class="lineno"> 206</span>&#160; <a class="code" href="../../da/d2a/group__adaline.html#ga251695a79baa885cafdcf6d8ed4ac120">adaline_get_weights_str</a>(ada), avg_pred_error);</div>
<div class="line"><a name="l00207"></a><span class="lineno"> 207</span>&#160; }</div>
<div class="line"><a name="l00208"></a><span class="lineno"> 208</span>&#160; </div>
<div class="line"><a name="l00209"></a><span class="lineno"> 209</span>&#160; <span class="keywordflow">if</span> (iter &lt; <a class="code" href="../../da/d2a/group__adaline.html#ga555ba960994e9bccb2029764588f694f">MAX_ADALINE_ITER</a>)</div>
<div class="line"><a name="l00210"></a><span class="lineno"> 210</span>&#160; printf(<span class="stringliteral">&quot;Converged after %d iterations.\n&quot;</span>, iter);</div>
<div class="line"><a name="l00211"></a><span class="lineno"> 211</span>&#160; <span class="keywordflow">else</span></div>
<div class="line"><a name="l00212"></a><span class="lineno"> 212</span>&#160; printf(<span class="stringliteral">&quot;Did not converged after %d iterations.\n&quot;</span>, iter);</div>
<div class="line"><a name="l00213"></a><span class="lineno"> 213</span>&#160;}</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_ga251695a79baa885cafdcf6d8ed4ac120"><div class="ttname"><a href="../../da/d2a/group__adaline.html#ga251695a79baa885cafdcf6d8ed4ac120">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 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><!-- 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"></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"><a name="l00159"></a><span class="lineno"> 159</span>&#160;{</div>
<div class="line"><a name="l00160"></a><span class="lineno"> 160</span>&#160; <span class="comment">/* output of the model with current weights */</span></div>
<div class="line"><a name="l00161"></a><span class="lineno"> 161</span>&#160; <span class="keywordtype">int</span> p = <a class="code" href="../../da/d2a/group__adaline.html#gac70b578aee679005fd336073969c3d94">adaline_predict</a>(ada, x, NULL);</div>
<div class="line"><a name="l00162"></a><span class="lineno"> 162</span>&#160; <span class="keywordtype">int</span> prediction_error = y - p; <span class="comment">// error in estimation</span></div>
<div class="line"><a name="l00163"></a><span class="lineno"> 163</span>&#160; <span class="keywordtype">double</span> correction_factor = ada-&gt;<a class="code" href="../../d2/daa/structadaline.html#a85dbd7cce6195d11ebb388220b96bde2">eta</a> * prediction_error;</div>
<div class="line"><a name="l00164"></a><span class="lineno"> 164</span>&#160; </div>
<div class="line"><a name="l00165"></a><span class="lineno"> 165</span>&#160; <span class="comment">/* update each weight, the last weight is the bias term */</span></div>
<div class="line"><a name="l00166"></a><span class="lineno"> 166</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; ada-&gt;<a class="code" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a> - 1; i++)</div>
<div class="line"><a name="l00167"></a><span class="lineno"> 167</span>&#160; {</div>
<div class="line"><a name="l00168"></a><span class="lineno"> 168</span>&#160; ada-&gt;<a class="code" href="../../d2/daa/structadaline.html#a32e58c03fd9258709eae6138ad0ec657">weights</a>[i] += correction_factor * x[i];</div>
<div class="line"><a name="l00169"></a><span class="lineno"> 169</span>&#160; }</div>
<div class="line"><a name="l00170"></a><span class="lineno"> 170</span>&#160; ada-&gt;<a class="code" href="../../d2/daa/structadaline.html#a32e58c03fd9258709eae6138ad0ec657">weights</a>[ada-&gt;<a class="code" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a> - 1] += correction_factor; <span class="comment">// update bias</span></div>
<div class="line"><a name="l00171"></a><span class="lineno"> 171</span>&#160; </div>
<div class="line"><a name="l00172"></a><span class="lineno"> 172</span>&#160; <span class="keywordflow">return</span> correction_factor;</div>
<div class="line"><a name="l00173"></a><span class="lineno"> 173</span>&#160;}</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="ga251695a79baa885cafdcf6d8ed4ac120"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ga251695a79baa885cafdcf6d8ed4ac120">&#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"><a name="l00113"></a><span class="lineno"> 113</span>&#160;{</div>
<div class="line"><a name="l00114"></a><span class="lineno"> 114</span>&#160; <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"><a name="l00115"></a><span class="lineno"> 115</span>&#160; </div>
<div class="line"><a name="l00116"></a><span class="lineno"> 116</span>&#160; sprintf(out, <span class="stringliteral">&quot;&lt;&quot;</span>);</div>
<div class="line"><a name="l00117"></a><span class="lineno"> 117</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; ada-&gt;<a class="code" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a>; i++)</div>
<div class="line"><a name="l00118"></a><span class="lineno"> 118</span>&#160; {</div>
<div class="line"><a name="l00119"></a><span class="lineno"> 119</span>&#160; sprintf(out, <span class="stringliteral">&quot;%s%.4g&quot;</span>, out, ada-&gt;<a class="code" href="../../d2/daa/structadaline.html#a32e58c03fd9258709eae6138ad0ec657">weights</a>[i]);</div>
<div class="line"><a name="l00120"></a><span class="lineno"> 120</span>&#160; <span class="keywordflow">if</span> (i &lt; ada-&gt;num_weights - 1)</div>
<div class="line"><a name="l00121"></a><span class="lineno"> 121</span>&#160; sprintf(out, <span class="stringliteral">&quot;%s, &quot;</span>, out);</div>
<div class="line"><a name="l00122"></a><span class="lineno"> 122</span>&#160; }</div>
<div class="line"><a name="l00123"></a><span class="lineno"> 123</span>&#160; sprintf(out, <span class="stringliteral">&quot;%s&gt;&quot;</span>, out);</div>
<div class="line"><a name="l00124"></a><span class="lineno"> 124</span>&#160; <span class="keywordflow">return</span> out;</div>
<div class="line"><a name="l00125"></a><span class="lineno"> 125</span>&#160;}</div>
</div><!-- fragment -->
</div>
</div>
<a id="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"><a name="l00137"></a><span class="lineno"> 137</span>&#160;{</div>
<div class="line"><a name="l00138"></a><span class="lineno"> 138</span>&#160; <span class="keywordtype">double</span> y = ada-&gt;<a class="code" href="../../d2/daa/structadaline.html#a32e58c03fd9258709eae6138ad0ec657">weights</a>[ada-&gt;<a class="code" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a> - 1]; <span class="comment">// assign bias value</span></div>
<div class="line"><a name="l00139"></a><span class="lineno"> 139</span>&#160; </div>
<div class="line"><a name="l00140"></a><span class="lineno"> 140</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; ada-&gt;<a class="code" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a> - 1; i++) y += x[i] * ada-&gt;<a class="code" href="../../d2/daa/structadaline.html#a32e58c03fd9258709eae6138ad0ec657">weights</a>[i];</div>
<div class="line"><a name="l00141"></a><span class="lineno"> 141</span>&#160; </div>
<div class="line"><a name="l00142"></a><span class="lineno"> 142</span>&#160; if (out) <span class="comment">// if out variable is not NULL</span></div>
<div class="line"><a name="l00143"></a><span class="lineno"> 143</span>&#160; *out = y;</div>
<div class="line"><a name="l00144"></a><span class="lineno"> 144</span>&#160; </div>
<div class="line"><a name="l00145"></a><span class="lineno"> 145</span>&#160; <span class="comment">// quantizer: apply ADALINE threshold function</span></div>
<div class="line"><a name="l00146"></a><span class="lineno"> 146</span>&#160; <span class="keywordflow">return</span> <a class="code" href="../../da/d2a/group__adaline.html#ga43576566b020c4157d4fb28f0dd45cfa">adaline_activation</a>(y);</div>
<div class="line"><a name="l00147"></a><span class="lineno"> 147</span>&#160;}</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"></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"><a name="l00090"></a><span class="lineno"> 90</span>&#160;{</div>
<div class="line"><a name="l00091"></a><span class="lineno"> 91</span>&#160; <span class="keywordflow">if</span> (ada == NULL)</div>
<div class="line"><a name="l00092"></a><span class="lineno"> 92</span>&#160; <span class="keywordflow">return</span>;</div>
<div class="line"><a name="l00093"></a><span class="lineno"> 93</span>&#160; </div>
<div class="line"><a name="l00094"></a><span class="lineno"> 94</span>&#160; <a class="code" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(ada-&gt;<a class="code" href="../../d2/daa/structadaline.html#a32e58c03fd9258709eae6138ad0ec657">weights</a>);</div>
<div class="line"><a name="l00095"></a><span class="lineno"> 95</span>&#160;};</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"></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"><a name="l00060"></a><span class="lineno"> 60</span>&#160;{</div>
<div class="line"><a name="l00061"></a><span class="lineno"> 61</span>&#160; <span class="keywordflow">if</span> (eta &lt;= 0.f || eta &gt;= 1.f)</div>
<div class="line"><a name="l00062"></a><span class="lineno"> 62</span>&#160; {</div>
<div class="line"><a name="l00063"></a><span class="lineno"> 63</span>&#160; fprintf(stderr, <span class="stringliteral">&quot;learning rate should be &gt; 0 and &lt; 1\n&quot;</span>);</div>
<div class="line"><a name="l00064"></a><span class="lineno"> 64</span>&#160; exit(EXIT_FAILURE);</div>
<div class="line"><a name="l00065"></a><span class="lineno"> 65</span>&#160; }</div>
<div class="line"><a name="l00066"></a><span class="lineno"> 66</span>&#160; </div>
<div class="line"><a name="l00067"></a><span class="lineno"> 67</span>&#160; <span class="comment">// additional weight is for the constant bias term</span></div>
<div class="line"><a name="l00068"></a><span class="lineno"> 68</span>&#160; <span class="keywordtype">int</span> num_weights = num_features + 1;</div>
<div class="line"><a name="l00069"></a><span class="lineno"> 69</span>&#160; <span class="keyword">struct </span><a class="code" href="../../d2/daa/structadaline.html">adaline</a> ada;</div>
<div class="line"><a name="l00070"></a><span class="lineno"> 70</span>&#160; ada.<a class="code" href="../../d2/daa/structadaline.html#a85dbd7cce6195d11ebb388220b96bde2">eta</a> = <a class="code" href="../../d2/daa/structadaline.html#a85dbd7cce6195d11ebb388220b96bde2">eta</a>;</div>
<div class="line"><a name="l00071"></a><span class="lineno"> 71</span>&#160; ada.num_weights = <a class="code" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a>;</div>
<div class="line"><a name="l00072"></a><span class="lineno"> 72</span>&#160; ada.weights = (<span class="keywordtype">double</span> *)<a class="code" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(<a class="code" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a> * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
<div class="line"><a name="l00073"></a><span class="lineno"> 73</span>&#160; <span class="keywordflow">if</span> (!ada.weights)</div>
<div class="line"><a name="l00074"></a><span class="lineno"> 74</span>&#160; {</div>
<div class="line"><a name="l00075"></a><span class="lineno"> 75</span>&#160; perror(<span class="stringliteral">&quot;Unable to allocate error for weights!&quot;</span>);</div>
<div class="line"><a name="l00076"></a><span class="lineno"> 76</span>&#160; <span class="keywordflow">return</span> ada;</div>
<div class="line"><a name="l00077"></a><span class="lineno"> 77</span>&#160; }</div>
<div class="line"><a name="l00078"></a><span class="lineno"> 78</span>&#160; </div>
<div class="line"><a name="l00079"></a><span class="lineno"> 79</span>&#160; <span class="comment">// initialize with random weights in the range [-50, 49]</span></div>
<div class="line"><a name="l00080"></a><span class="lineno"> 80</span>&#160; <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; <a class="code" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a>; i++) ada.weights[i] = 1.f;</div>
<div class="line"><a name="l00081"></a><span class="lineno"> 81</span>&#160; <span class="comment">// ada.weights[i] = (double)(rand() % 100) - 50);</span></div>
<div class="line"><a name="l00082"></a><span class="lineno"> 82</span>&#160; </div>
<div class="line"><a name="l00083"></a><span class="lineno"> 83</span>&#160; <span class="keywordflow">return</span> ada;</div>
<div class="line"><a name="l00084"></a><span class="lineno"> 84</span>&#160;}</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.1 </li>
</ul>
</div>
</body>
</html>