mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
836 lines
77 KiB
HTML
836 lines
77 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.8.18"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>Algorithms_in_C: machine_learning/adaline_learning.c File Reference</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
|
|
 <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.8.18 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
|
|
/* @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&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&dn=gpl-2.0.txt GPL-v2 */
|
|
$(document).ready(function(){initNavTree('dd/d8c/adaline__learning_8c.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> |
|
|
<a href="#define-members">Macros</a> |
|
|
<a href="#func-members">Functions</a> </div>
|
|
<div class="headertitle">
|
|
<div class="title">adaline_learning.c File Reference</div> </div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
|
|
<p><a href="https://en.wikipedia.org/wiki/ADALINE">Adaptive Linear Neuron (ADALINE)</a> implementation
|
|
<a href="#details">More...</a></p>
|
|
<div class="textblock"><code>#include <assert.h></code><br />
|
|
<code>#include <limits.h></code><br />
|
|
<code>#include <math.h></code><br />
|
|
<code>#include <stdbool.h></code><br />
|
|
<code>#include <stdio.h></code><br />
|
|
<code>#include <stdlib.h></code><br />
|
|
<code>#include <time.h></code><br />
|
|
</div><div class="textblock"><div class="dynheader">
|
|
Include dependency graph for adaline_learning.c:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d9/dcc/adaline__learning_8c__incl.svg" width="611" height="127"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</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  </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d2/daa/structadaline.html">adaline</a></td></tr>
|
|
<tr class="separator:"><td class="memSeparator" colspan="2"> </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:acd517c6f195c75b9dd0f3aad65326f3b"><td class="memItemLeft" align="right" valign="top"><a id="acd517c6f195c75b9dd0f3aad65326f3b"></a>
|
|
#define </td><td class="memItemRight" valign="bottom"><b>MAX_ITER</b>   500</td></tr>
|
|
<tr class="separator:acd517c6f195c75b9dd0f3aad65326f3b"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:af270a96662132d0385cb6b4637c5a689"><td class="memItemLeft" align="right" valign="top"><a id="af270a96662132d0385cb6b4637c5a689"></a>
|
|
#define </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d8c/adaline__learning_8c.html#af270a96662132d0385cb6b4637c5a689">ACCURACY</a>   1e-5</td></tr>
|
|
<tr class="memdesc:af270a96662132d0385cb6b4637c5a689"><td class="mdescLeft"> </td><td class="mdescRight">convergence accuracy \(=1\times10^{-5}\) <br /></td></tr>
|
|
<tr class="separator:af270a96662132d0385cb6b4637c5a689"><td class="memSeparator" colspan="2"> </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:acd88962c5f6341e43cbc69b4a7d3485b"><td class="memItemLeft" align="right" valign="top">struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d8c/adaline__learning_8c.html#acd88962c5f6341e43cbc69b4a7d3485b">new_adaline</a> (const int num_features, const double eta)</td></tr>
|
|
<tr class="separator:acd88962c5f6341e43cbc69b4a7d3485b"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a6f35caa3084772cc126ac7b20f67f665"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d8c/adaline__learning_8c.html#a6f35caa3084772cc126ac7b20f67f665">delete_adaline</a> (struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> *ada)</td></tr>
|
|
<tr class="separator:a6f35caa3084772cc126ac7b20f67f665"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a7b12b6513314c975303b5a698608322f"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d8c/adaline__learning_8c.html#a7b12b6513314c975303b5a698608322f">activation</a> (double x)</td></tr>
|
|
<tr class="separator:a7b12b6513314c975303b5a698608322f"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a00dc6134ca22a12e0fd9cac54d601c2d"><td class="memItemLeft" align="right" valign="top">char * </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d8c/adaline__learning_8c.html#a00dc6134ca22a12e0fd9cac54d601c2d">get_weights_str</a> (struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> *ada)</td></tr>
|
|
<tr class="separator:a00dc6134ca22a12e0fd9cac54d601c2d"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a4db1ba00a7f282100ea31a94e32bd7a3"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d8c/adaline__learning_8c.html#a4db1ba00a7f282100ea31a94e32bd7a3">predict</a> (struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> *ada, const double *x, double *out)</td></tr>
|
|
<tr class="separator:a4db1ba00a7f282100ea31a94e32bd7a3"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:afa953e811eaae199d121a7ddb619d604"><td class="memItemLeft" align="right" valign="top">double </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d8c/adaline__learning_8c.html#afa953e811eaae199d121a7ddb619d604">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="separator:afa953e811eaae199d121a7ddb619d604"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a355fda53c238a0c0c07f03dcc021caed"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d8c/adaline__learning_8c.html#a355fda53c238a0c0c07f03dcc021caed">fit</a> (struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> *ada, double **X, const int *y, const int <a class="el" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>)</td></tr>
|
|
<tr class="separator:a355fda53c238a0c0c07f03dcc021caed"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:ab4ecb3accf5d9e0263087e7265bbe3a9"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d8c/adaline__learning_8c.html#ab4ecb3accf5d9e0263087e7265bbe3a9">test1</a> (double eta)</td></tr>
|
|
<tr class="separator:ab4ecb3accf5d9e0263087e7265bbe3a9"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a05cc9a0acb524fde727a4d7b4a747ee6"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d8c/adaline__learning_8c.html#a05cc9a0acb524fde727a4d7b4a747ee6">test2</a> (double eta)</td></tr>
|
|
<tr class="separator:a05cc9a0acb524fde727a4d7b4a747ee6"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a3f37b9f073f7e57fd0b39d70718af1b1"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d8c/adaline__learning_8c.html#a3f37b9f073f7e57fd0b39d70718af1b1">test3</a> (double eta)</td></tr>
|
|
<tr class="separator:a3f37b9f073f7e57fd0b39d70718af1b1"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="../../dd/d8c/adaline__learning_8c.html#a3c04138a5bfe5d72780bb7e82a18e627">main</a> (int argc, char **argv)</td></tr>
|
|
<tr class="separator:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memSeparator" colspan="2"> </td></tr>
|
|
</table>
|
|
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
|
|
<div class="textblock"><p><a href="https://en.wikipedia.org/wiki/ADALINE">Adaptive Linear Neuron (ADALINE)</a> implementation </p>
|
|
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/kvedala">Krishna Vedala</a></dd></dl>
|
|
<p><img src="https://upload.wikimedia.org/wikipedia/commons/b/be/Adaline_flow_chart.gif" alt="" width="200px" class="inline"/> <a href="https://commons.wikimedia.org/wiki/File:Adaline_flow_chart.gif">source</a> ADALINE is one of the first and simplest single layer artificial neural network. The algorithm essentially implements a linear function </p><p class="formulaDsp">
|
|
\[ f\left(x_0,x_1,x_2,\ldots\right) = \sum_j x_jw_j+\theta \]
|
|
</p>
|
|
<p> where \(x_j\) are the input features of a sample, \(w_j\) are the coefficients of the linear function and \(\theta\) is a constant. If we know the \(w_j\), then for any given set of features, \(y\) can be computed. Computing the \(w_j\) is a supervised learning algorithm wherein a set of features and their corresponding outputs are given and weights are computed using stochastic gradient descent method. </p>
|
|
</div><h2 class="groupheader">Function Documentation</h2>
|
|
<a id="a7b12b6513314c975303b5a698608322f"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a7b12b6513314c975303b5a698608322f">◆ </a></span>activation()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">int activation </td>
|
|
<td>(</td>
|
|
<td class="paramtype">double </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>
|
|
<div class="fragment"><div class="line"><a name="l00094"></a><span class="lineno"> 94</span> { <span class="keywordflow">return</span> x > 0 ? 1 : -1; }</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="a6f35caa3084772cc126ac7b20f67f665"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a6f35caa3084772cc126ac7b20f67f665">◆ </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> * </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 freeed. </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<div class="fragment"><div class="line"><a name="l00082"></a><span class="lineno"> 82</span> {</div>
|
|
<div class="line"><a name="l00083"></a><span class="lineno"> 83</span>  <span class="keywordflow">if</span> (ada == NULL)</div>
|
|
<div class="line"><a name="l00084"></a><span class="lineno"> 84</span>  <span class="keywordflow">return</span>;</div>
|
|
<div class="line"><a name="l00085"></a><span class="lineno"> 85</span>  </div>
|
|
<div class="line"><a name="l00086"></a><span class="lineno"> 86</span>  free(ada-><a class="code" href="../../d2/daa/structadaline.html#a32e58c03fd9258709eae6138ad0ec657">weights</a>);</div>
|
|
<div class="line"><a name="l00087"></a><span class="lineno"> 87</span> };</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="a355fda53c238a0c0c07f03dcc021caed"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a355fda53c238a0c0c07f03dcc021caed">◆ </a></span>fit()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void fit </td>
|
|
<td>(</td>
|
|
<td class="paramtype">struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> * </td>
|
|
<td class="paramname"><em>ada</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double ** </td>
|
|
<td class="paramname"><em>X</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">const int * </td>
|
|
<td class="paramname"><em>y</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">const int </td>
|
|
<td class="paramname"><em>N</em> </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="l00171"></a><span class="lineno"> 171</span> {</div>
|
|
<div class="line"><a name="l00172"></a><span class="lineno"> 172</span>  <span class="keywordtype">double</span> avg_pred_error = 1.f;</div>
|
|
<div class="line"><a name="l00173"></a><span class="lineno"> 173</span>  </div>
|
|
<div class="line"><a name="l00174"></a><span class="lineno"> 174</span>  <span class="keywordtype">int</span> iter;</div>
|
|
<div class="line"><a name="l00175"></a><span class="lineno"> 175</span>  <span class="keywordflow">for</span> (iter = 0; (iter < MAX_ITER) && (avg_pred_error > <a class="code" href="../../dd/d8c/adaline__learning_8c.html#af270a96662132d0385cb6b4637c5a689">ACCURACY</a>); iter++)</div>
|
|
<div class="line"><a name="l00176"></a><span class="lineno"> 176</span>  {</div>
|
|
<div class="line"><a name="l00177"></a><span class="lineno"> 177</span>  avg_pred_error = 0.f;</div>
|
|
<div class="line"><a name="l00178"></a><span class="lineno"> 178</span>  </div>
|
|
<div class="line"><a name="l00179"></a><span class="lineno"> 179</span>  <span class="comment">// perform fit for each sample</span></div>
|
|
<div class="line"><a name="l00180"></a><span class="lineno"> 180</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++)</div>
|
|
<div class="line"><a name="l00181"></a><span class="lineno"> 181</span>  {</div>
|
|
<div class="line"><a name="l00182"></a><span class="lineno"> 182</span>  <span class="keywordtype">double</span> err = <a class="code" href="../../dd/d8c/adaline__learning_8c.html#afa953e811eaae199d121a7ddb619d604">fit_sample</a>(ada, X[i], y[i]);</div>
|
|
<div class="line"><a name="l00183"></a><span class="lineno"> 183</span>  avg_pred_error += fabs(err);</div>
|
|
<div class="line"><a name="l00184"></a><span class="lineno"> 184</span>  }</div>
|
|
<div class="line"><a name="l00185"></a><span class="lineno"> 185</span>  avg_pred_error /= <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>;</div>
|
|
<div class="line"><a name="l00186"></a><span class="lineno"> 186</span>  </div>
|
|
<div class="line"><a name="l00187"></a><span class="lineno"> 187</span>  <span class="comment">// Print updates every 200th iteration</span></div>
|
|
<div class="line"><a name="l00188"></a><span class="lineno"> 188</span>  <span class="comment">// if (iter % 100 == 0)</span></div>
|
|
<div class="line"><a name="l00189"></a><span class="lineno"> 189</span>  printf(<span class="stringliteral">"\tIter %3d: Training weights: %s\tAvg error: %.4f\n"</span>, iter,</div>
|
|
<div class="line"><a name="l00190"></a><span class="lineno"> 190</span>  <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a00dc6134ca22a12e0fd9cac54d601c2d">get_weights_str</a>(ada), avg_pred_error);</div>
|
|
<div class="line"><a name="l00191"></a><span class="lineno"> 191</span>  }</div>
|
|
<div class="line"><a name="l00192"></a><span class="lineno"> 192</span>  </div>
|
|
<div class="line"><a name="l00193"></a><span class="lineno"> 193</span>  <span class="keywordflow">if</span> (iter < MAX_ITER)</div>
|
|
<div class="line"><a name="l00194"></a><span class="lineno"> 194</span>  printf(<span class="stringliteral">"Converged after %d iterations.\n"</span>, iter);</div>
|
|
<div class="line"><a name="l00195"></a><span class="lineno"> 195</span>  <span class="keywordflow">else</span></div>
|
|
<div class="line"><a name="l00196"></a><span class="lineno"> 196</span>  printf(<span class="stringliteral">"Did not converged after %d iterations.\n"</span>, iter);</div>
|
|
<div class="line"><a name="l00197"></a><span class="lineno"> 197</span> }</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="afa953e811eaae199d121a7ddb619d604"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#afa953e811eaae199d121a7ddb619d604">◆ </a></span>fit_sample()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">double fit_sample </td>
|
|
<td>(</td>
|
|
<td class="paramtype">struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> * </td>
|
|
<td class="paramname"><em>ada</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">const double * </td>
|
|
<td class="paramname"><em>x</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">const int </td>
|
|
<td class="paramname"><em>y</em> </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="l00145"></a><span class="lineno"> 145</span> {</div>
|
|
<div class="line"><a name="l00146"></a><span class="lineno"> 146</span>  <span class="comment">/* output of the model with current weights */</span></div>
|
|
<div class="line"><a name="l00147"></a><span class="lineno"> 147</span>  <span class="keywordtype">int</span> p = <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a4db1ba00a7f282100ea31a94e32bd7a3">predict</a>(ada, x, NULL);</div>
|
|
<div class="line"><a name="l00148"></a><span class="lineno"> 148</span>  <span class="keywordtype">int</span> prediction_error = y - p; <span class="comment">// error in estimation</span></div>
|
|
<div class="line"><a name="l00149"></a><span class="lineno"> 149</span>  <span class="keywordtype">double</span> correction_factor = ada-><a class="code" href="../../d2/daa/structadaline.html#a85dbd7cce6195d11ebb388220b96bde2">eta</a> * prediction_error;</div>
|
|
<div class="line"><a name="l00150"></a><span class="lineno"> 150</span>  </div>
|
|
<div class="line"><a name="l00151"></a><span class="lineno"> 151</span>  <span class="comment">/* update each weight, the last weight is the bias term */</span></div>
|
|
<div class="line"><a name="l00152"></a><span class="lineno"> 152</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < ada-><a class="code" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a> - 1; i++)</div>
|
|
<div class="line"><a name="l00153"></a><span class="lineno"> 153</span>  {</div>
|
|
<div class="line"><a name="l00154"></a><span class="lineno"> 154</span>  ada-><a class="code" href="../../d2/daa/structadaline.html#a32e58c03fd9258709eae6138ad0ec657">weights</a>[i] += correction_factor * x[i];</div>
|
|
<div class="line"><a name="l00155"></a><span class="lineno"> 155</span>  }</div>
|
|
<div class="line"><a name="l00156"></a><span class="lineno"> 156</span>  ada-><a class="code" href="../../d2/daa/structadaline.html#a32e58c03fd9258709eae6138ad0ec657">weights</a>[ada-><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="l00157"></a><span class="lineno"> 157</span>  </div>
|
|
<div class="line"><a name="l00158"></a><span class="lineno"> 158</span>  <span class="keywordflow">return</span> correction_factor;</div>
|
|
<div class="line"><a name="l00159"></a><span class="lineno"> 159</span> }</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="../../dd/d8c/adaline__learning_8c_afa953e811eaae199d121a7ddb619d604_cgraph.svg" width="330" 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="a00dc6134ca22a12e0fd9cac54d601c2d"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a00dc6134ca22a12e0fd9cac54d601c2d">◆ </a></span>get_weights_str()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">char* get_weights_str </td>
|
|
<td>(</td>
|
|
<td class="paramtype">struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> * </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>
|
|
<div class="fragment"><div class="line"><a name="l00100"></a><span class="lineno"> 100</span> {</div>
|
|
<div class="line"><a name="l00101"></a><span class="lineno"> 101</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"><a name="l00102"></a><span class="lineno"> 102</span>  </div>
|
|
<div class="line"><a name="l00103"></a><span class="lineno"> 103</span>  sprintf(out, <span class="stringliteral">"<"</span>);</div>
|
|
<div class="line"><a name="l00104"></a><span class="lineno"> 104</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < ada-><a class="code" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a>; i++)</div>
|
|
<div class="line"><a name="l00105"></a><span class="lineno"> 105</span>  {</div>
|
|
<div class="line"><a name="l00106"></a><span class="lineno"> 106</span>  sprintf(out, <span class="stringliteral">"%s%.4g"</span>, out, ada-><a class="code" href="../../d2/daa/structadaline.html#a32e58c03fd9258709eae6138ad0ec657">weights</a>[i]);</div>
|
|
<div class="line"><a name="l00107"></a><span class="lineno"> 107</span>  <span class="keywordflow">if</span> (i < ada->num_weights - 1)</div>
|
|
<div class="line"><a name="l00108"></a><span class="lineno"> 108</span>  sprintf(out, <span class="stringliteral">"%s, "</span>, out);</div>
|
|
<div class="line"><a name="l00109"></a><span class="lineno"> 109</span>  }</div>
|
|
<div class="line"><a name="l00110"></a><span class="lineno"> 110</span>  sprintf(out, <span class="stringliteral">"%s>"</span>, out);</div>
|
|
<div class="line"><a name="l00111"></a><span class="lineno"> 111</span>  <span class="keywordflow">return</span> out;</div>
|
|
<div class="line"><a name="l00112"></a><span class="lineno"> 112</span> }</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="a3c04138a5bfe5d72780bb7e82a18e627"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a3c04138a5bfe5d72780bb7e82a18e627">◆ </a></span>main()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">int main </td>
|
|
<td>(</td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>argc</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">char ** </td>
|
|
<td class="paramname"><em>argv</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Main function </p>
|
|
<div class="fragment"><div class="line"><a name="l00379"></a><span class="lineno"> 379</span> {</div>
|
|
<div class="line"><a name="l00380"></a><span class="lineno"> 380</span>  srand(time(NULL)); <span class="comment">// initialize random number generator</span></div>
|
|
<div class="line"><a name="l00381"></a><span class="lineno"> 381</span>  </div>
|
|
<div class="line"><a name="l00382"></a><span class="lineno"> 382</span>  <span class="keywordtype">double</span> eta = 0.1; <span class="comment">// default value of eta</span></div>
|
|
<div class="line"><a name="l00383"></a><span class="lineno"> 383</span>  <span class="keywordflow">if</span> (argc == 2) <span class="comment">// read eta value from commandline argument if present</span></div>
|
|
<div class="line"><a name="l00384"></a><span class="lineno"> 384</span>  eta = strtof(argv[1], NULL);</div>
|
|
<div class="line"><a name="l00385"></a><span class="lineno"> 385</span>  </div>
|
|
<div class="line"><a name="l00386"></a><span class="lineno"> 386</span>  <a class="code" href="../../dd/d8c/adaline__learning_8c.html#ab4ecb3accf5d9e0263087e7265bbe3a9">test1</a>(eta);</div>
|
|
<div class="line"><a name="l00387"></a><span class="lineno"> 387</span>  </div>
|
|
<div class="line"><a name="l00388"></a><span class="lineno"> 388</span>  printf(<span class="stringliteral">"Press ENTER to continue...\n"</span>);</div>
|
|
<div class="line"><a name="l00389"></a><span class="lineno"> 389</span>  getchar();</div>
|
|
<div class="line"><a name="l00390"></a><span class="lineno"> 390</span>  </div>
|
|
<div class="line"><a name="l00391"></a><span class="lineno"> 391</span>  <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a05cc9a0acb524fde727a4d7b4a747ee6">test2</a>(eta);</div>
|
|
<div class="line"><a name="l00392"></a><span class="lineno"> 392</span>  </div>
|
|
<div class="line"><a name="l00393"></a><span class="lineno"> 393</span>  printf(<span class="stringliteral">"Press ENTER to continue...\n"</span>);</div>
|
|
<div class="line"><a name="l00394"></a><span class="lineno"> 394</span>  getchar();</div>
|
|
<div class="line"><a name="l00395"></a><span class="lineno"> 395</span>  </div>
|
|
<div class="line"><a name="l00396"></a><span class="lineno"> 396</span>  <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a3f37b9f073f7e57fd0b39d70718af1b1">test3</a>(eta);</div>
|
|
<div class="line"><a name="l00397"></a><span class="lineno"> 397</span>  </div>
|
|
<div class="line"><a name="l00398"></a><span class="lineno"> 398</span>  <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><a name="l00399"></a><span class="lineno"> 399</span> }</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="../../dd/d8c/adaline__learning_8c_a3c04138a5bfe5d72780bb7e82a18e627_cgraph.svg" width="450" height="240"><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="acd88962c5f6341e43cbc69b4a7d3485b"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#acd88962c5f6341e43cbc69b4a7d3485b">◆ </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 </td>
|
|
<td class="paramname"><em>num_features</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">const double </td>
|
|
<td class="paramname"><em>eta</em> </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="l00052"></a><span class="lineno"> 52</span> {</div>
|
|
<div class="line"><a name="l00053"></a><span class="lineno"> 53</span>  <span class="keywordflow">if</span> (eta <= 0.f || eta >= 1.f)</div>
|
|
<div class="line"><a name="l00054"></a><span class="lineno"> 54</span>  {</div>
|
|
<div class="line"><a name="l00055"></a><span class="lineno"> 55</span>  fprintf(stderr, <span class="stringliteral">"learning rate should be > 0 and < 1\n"</span>);</div>
|
|
<div class="line"><a name="l00056"></a><span class="lineno"> 56</span>  exit(EXIT_FAILURE);</div>
|
|
<div class="line"><a name="l00057"></a><span class="lineno"> 57</span>  }</div>
|
|
<div class="line"><a name="l00058"></a><span class="lineno"> 58</span>  </div>
|
|
<div class="line"><a name="l00059"></a><span class="lineno"> 59</span>  <span class="comment">// additional weight is for the constant bias term</span></div>
|
|
<div class="line"><a name="l00060"></a><span class="lineno"> 60</span>  <span class="keywordtype">int</span> num_weights = num_features + 1;</div>
|
|
<div class="line"><a name="l00061"></a><span class="lineno"> 61</span>  <span class="keyword">struct </span><a class="code" href="../../d2/daa/structadaline.html">adaline</a> ada;</div>
|
|
<div class="line"><a name="l00062"></a><span class="lineno"> 62</span>  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="l00063"></a><span class="lineno"> 63</span>  ada.num_weights = <a class="code" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a>;</div>
|
|
<div class="line"><a name="l00064"></a><span class="lineno"> 64</span>  ada.weights = (<span class="keywordtype">double</span> *)malloc(<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="l00065"></a><span class="lineno"> 65</span>  <span class="keywordflow">if</span> (!ada.weights)</div>
|
|
<div class="line"><a name="l00066"></a><span class="lineno"> 66</span>  {</div>
|
|
<div class="line"><a name="l00067"></a><span class="lineno"> 67</span>  perror(<span class="stringliteral">"Unable to allocate error for weights!"</span>);</div>
|
|
<div class="line"><a name="l00068"></a><span class="lineno"> 68</span>  <span class="keywordflow">return</span> ada;</div>
|
|
<div class="line"><a name="l00069"></a><span class="lineno"> 69</span>  }</div>
|
|
<div class="line"><a name="l00070"></a><span class="lineno"> 70</span>  </div>
|
|
<div class="line"><a name="l00071"></a><span class="lineno"> 71</span>  <span class="comment">// initialize with random weights in the range [-50, 49]</span></div>
|
|
<div class="line"><a name="l00072"></a><span class="lineno"> 72</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <a class="code" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a>; i++) ada.weights[i] = 1.f;</div>
|
|
<div class="line"><a name="l00073"></a><span class="lineno"> 73</span>  <span class="comment">// ada.weights[i] = (double)(rand() % 100) - 50);</span></div>
|
|
<div class="line"><a name="l00074"></a><span class="lineno"> 74</span>  </div>
|
|
<div class="line"><a name="l00075"></a><span class="lineno"> 75</span>  <span class="keywordflow">return</span> ada;</div>
|
|
<div class="line"><a name="l00076"></a><span class="lineno"> 76</span> }</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="a4db1ba00a7f282100ea31a94e32bd7a3"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a4db1ba00a7f282100ea31a94e32bd7a3">◆ </a></span>predict()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">int predict </td>
|
|
<td>(</td>
|
|
<td class="paramtype">struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> * </td>
|
|
<td class="paramname"><em>ada</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">const double * </td>
|
|
<td class="paramname"><em>x</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double * </td>
|
|
<td class="paramname"><em>out</em> </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="l00124"></a><span class="lineno"> 124</span> {</div>
|
|
<div class="line"><a name="l00125"></a><span class="lineno"> 125</span>  <span class="keywordtype">double</span> y = ada-><a class="code" href="../../d2/daa/structadaline.html#a32e58c03fd9258709eae6138ad0ec657">weights</a>[ada-><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="l00126"></a><span class="lineno"> 126</span>  </div>
|
|
<div class="line"><a name="l00127"></a><span class="lineno"> 127</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < ada-><a class="code" href="../../d2/daa/structadaline.html#a53314e737a0a5ff4552a03bcc9dafbc1">num_weights</a> - 1; i++) y += x[i] * ada-><a class="code" href="../../d2/daa/structadaline.html#a32e58c03fd9258709eae6138ad0ec657">weights</a>[i];</div>
|
|
<div class="line"><a name="l00128"></a><span class="lineno"> 128</span>  </div>
|
|
<div class="line"><a name="l00129"></a><span class="lineno"> 129</span>  if (out) <span class="comment">// if out variable is not NULL</span></div>
|
|
<div class="line"><a name="l00130"></a><span class="lineno"> 130</span>  *out = y;</div>
|
|
<div class="line"><a name="l00131"></a><span class="lineno"> 131</span>  </div>
|
|
<div class="line"><a name="l00132"></a><span class="lineno"> 132</span>  <span class="keywordflow">return</span> <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a7b12b6513314c975303b5a698608322f">activation</a>(y); <span class="comment">// quantizer: apply ADALINE threshold function</span></div>
|
|
<div class="line"><a name="l00133"></a><span class="lineno"> 133</span> }</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="../../dd/d8c/adaline__learning_8c_a4db1ba00a7f282100ea31a94e32bd7a3_cgraph.svg" width="199" 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="ab4ecb3accf5d9e0263087e7265bbe3a9"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ab4ecb3accf5d9e0263087e7265bbe3a9">◆ </a></span>test1()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void test1 </td>
|
|
<td>(</td>
|
|
<td class="paramtype">double </td>
|
|
<td class="paramname"><em>eta</em></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>test function to predict points in a 2D coordinate system above the line \(x=y\) as +1 and others as -1. Note that each point is defined by 2 values or 2 features. </p><dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">eta</td><td>learning rate (optional, default=0.01) </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<div class="fragment"><div class="line"><a name="l00206"></a><span class="lineno"> 206</span> {</div>
|
|
<div class="line"><a name="l00207"></a><span class="lineno"> 207</span>  <span class="keyword">struct </span><a class="code" href="../../d2/daa/structadaline.html">adaline</a> ada = <a class="code" href="../../dd/d8c/adaline__learning_8c.html#acd88962c5f6341e43cbc69b4a7d3485b">new_adaline</a>(2, <a class="code" href="../../d2/daa/structadaline.html#a85dbd7cce6195d11ebb388220b96bde2">eta</a>); <span class="comment">// 2 features</span></div>
|
|
<div class="line"><a name="l00208"></a><span class="lineno"> 208</span>  </div>
|
|
<div class="line"><a name="l00209"></a><span class="lineno"> 209</span>  <span class="keyword">const</span> <span class="keywordtype">int</span> <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a> = 10; <span class="comment">// number of sample points</span></div>
|
|
<div class="line"><a name="l00210"></a><span class="lineno"> 210</span>  <span class="keyword">const</span> <span class="keywordtype">double</span> saved_X[10][2] = {{0, 1}, {1, -2}, {2, 3}, {3, -1},</div>
|
|
<div class="line"><a name="l00211"></a><span class="lineno"> 211</span>  {4, 1}, {6, -5}, {-7, -3}, {-8, 5},</div>
|
|
<div class="line"><a name="l00212"></a><span class="lineno"> 212</span>  {-9, 2}, {-10, -15}};</div>
|
|
<div class="line"><a name="l00213"></a><span class="lineno"> 213</span>  </div>
|
|
<div class="line"><a name="l00214"></a><span class="lineno"> 214</span>  <span class="keywordtype">double</span> **X = (<span class="keywordtype">double</span> **)malloc(<a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a> * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
|
|
<div class="line"><a name="l00215"></a><span class="lineno"> 215</span>  <span class="keyword">const</span> <span class="keywordtype">int</span> Y[10] = {1, -1, 1, -1, -1,</div>
|
|
<div class="line"><a name="l00216"></a><span class="lineno"> 216</span>  -1, 1, 1, 1, -1}; <span class="comment">// corresponding y-values</span></div>
|
|
<div class="line"><a name="l00217"></a><span class="lineno"> 217</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++)</div>
|
|
<div class="line"><a name="l00218"></a><span class="lineno"> 218</span>  {</div>
|
|
<div class="line"><a name="l00219"></a><span class="lineno"> 219</span>  X[i] = (<span class="keywordtype">double</span> *)saved_X[i];</div>
|
|
<div class="line"><a name="l00220"></a><span class="lineno"> 220</span>  }</div>
|
|
<div class="line"><a name="l00221"></a><span class="lineno"> 221</span>  </div>
|
|
<div class="line"><a name="l00222"></a><span class="lineno"> 222</span>  printf(<span class="stringliteral">"------- Test 1 -------\n"</span>);</div>
|
|
<div class="line"><a name="l00223"></a><span class="lineno"> 223</span>  printf(<span class="stringliteral">"Model before fit: %s"</span>, <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a00dc6134ca22a12e0fd9cac54d601c2d">get_weights_str</a>(&ada));</div>
|
|
<div class="line"><a name="l00224"></a><span class="lineno"> 224</span>  </div>
|
|
<div class="line"><a name="l00225"></a><span class="lineno"> 225</span>  <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a355fda53c238a0c0c07f03dcc021caed">fit</a>(&ada, X, Y, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>);</div>
|
|
<div class="line"><a name="l00226"></a><span class="lineno"> 226</span>  printf(<span class="stringliteral">"Model after fit: %s\n"</span>, <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a00dc6134ca22a12e0fd9cac54d601c2d">get_weights_str</a>(&ada));</div>
|
|
<div class="line"><a name="l00227"></a><span class="lineno"> 227</span>  </div>
|
|
<div class="line"><a name="l00228"></a><span class="lineno"> 228</span>  <span class="keywordtype">double</span> test_x[] = {5, -3};</div>
|
|
<div class="line"><a name="l00229"></a><span class="lineno"> 229</span>  <span class="keywordtype">int</span> pred = <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a4db1ba00a7f282100ea31a94e32bd7a3">predict</a>(&ada, test_x, NULL);</div>
|
|
<div class="line"><a name="l00230"></a><span class="lineno"> 230</span>  printf(<span class="stringliteral">"Predict for x=(5,-3): % d"</span>, pred);</div>
|
|
<div class="line"><a name="l00231"></a><span class="lineno"> 231</span>  assert(pred == -1);</div>
|
|
<div class="line"><a name="l00232"></a><span class="lineno"> 232</span>  printf(<span class="stringliteral">" ...passed\n"</span>);</div>
|
|
<div class="line"><a name="l00233"></a><span class="lineno"> 233</span>  </div>
|
|
<div class="line"><a name="l00234"></a><span class="lineno"> 234</span>  <span class="keywordtype">double</span> test_x2[] = {5, 8};</div>
|
|
<div class="line"><a name="l00235"></a><span class="lineno"> 235</span>  pred = <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a4db1ba00a7f282100ea31a94e32bd7a3">predict</a>(&ada, test_x2, NULL);</div>
|
|
<div class="line"><a name="l00236"></a><span class="lineno"> 236</span>  printf(<span class="stringliteral">"Predict for x=(5, 8): % d"</span>, pred);</div>
|
|
<div class="line"><a name="l00237"></a><span class="lineno"> 237</span>  assert(pred == 1);</div>
|
|
<div class="line"><a name="l00238"></a><span class="lineno"> 238</span>  printf(<span class="stringliteral">" ...passed\n"</span>);</div>
|
|
<div class="line"><a name="l00239"></a><span class="lineno"> 239</span>  </div>
|
|
<div class="line"><a name="l00240"></a><span class="lineno"> 240</span>  <span class="comment">// for (int i = 0; i < N; i++)</span></div>
|
|
<div class="line"><a name="l00241"></a><span class="lineno"> 241</span>  <span class="comment">// free(X[i]);</span></div>
|
|
<div class="line"><a name="l00242"></a><span class="lineno"> 242</span>  free(X);</div>
|
|
<div class="line"><a name="l00243"></a><span class="lineno"> 243</span>  <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a6f35caa3084772cc126ac7b20f67f665">delete_adaline</a>(&ada);</div>
|
|
<div class="line"><a name="l00244"></a><span class="lineno"> 244</span> }</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="../../dd/d8c/adaline__learning_8c_ab4ecb3accf5d9e0263087e7265bbe3a9_cgraph.svg" width="351" height="240"><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="a05cc9a0acb524fde727a4d7b4a747ee6"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a05cc9a0acb524fde727a4d7b4a747ee6">◆ </a></span>test2()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void test2 </td>
|
|
<td>(</td>
|
|
<td class="paramtype">double </td>
|
|
<td class="paramname"><em>eta</em></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>test function to predict points in a 2D coordinate system above the line \(x+3y=-1\) as +1 and others as -1. Note that each point is defined by 2 values or 2 features. The function will create random sample points for training and test purposes. </p><dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">eta</td><td>learning rate (optional, default=0.01) </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<div class="fragment"><div class="line"><a name="l00254"></a><span class="lineno"> 254</span> {</div>
|
|
<div class="line"><a name="l00255"></a><span class="lineno"> 255</span>  <span class="keyword">struct </span><a class="code" href="../../d2/daa/structadaline.html">adaline</a> ada = <a class="code" href="../../dd/d8c/adaline__learning_8c.html#acd88962c5f6341e43cbc69b4a7d3485b">new_adaline</a>(2, <a class="code" href="../../d2/daa/structadaline.html#a85dbd7cce6195d11ebb388220b96bde2">eta</a>); <span class="comment">// 2 features</span></div>
|
|
<div class="line"><a name="l00256"></a><span class="lineno"> 256</span>  </div>
|
|
<div class="line"><a name="l00257"></a><span class="lineno"> 257</span>  <span class="keyword">const</span> <span class="keywordtype">int</span> <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a> = 50; <span class="comment">// number of sample points</span></div>
|
|
<div class="line"><a name="l00258"></a><span class="lineno"> 258</span>  </div>
|
|
<div class="line"><a name="l00259"></a><span class="lineno"> 259</span>  <span class="keywordtype">double</span> **X = (<span class="keywordtype">double</span> **)malloc(<a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a> * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
|
|
<div class="line"><a name="l00260"></a><span class="lineno"> 260</span>  <span class="keywordtype">int</span> *Y = (<span class="keywordtype">int</span> *)malloc(<a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a> * <span class="keyword">sizeof</span>(<span class="keywordtype">int</span>)); <span class="comment">// corresponding y-values</span></div>
|
|
<div class="line"><a name="l00261"></a><span class="lineno"> 261</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++) X[i] = (<span class="keywordtype">double</span> *)malloc(2 * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
|
|
<div class="line"><a name="l00262"></a><span class="lineno"> 262</span>  </div>
|
|
<div class="line"><a name="l00263"></a><span class="lineno"> 263</span>  <span class="comment">// generate sample points in the interval</span></div>
|
|
<div class="line"><a name="l00264"></a><span class="lineno"> 264</span>  <span class="comment">// [-range2/100 , (range2-1)/100]</span></div>
|
|
<div class="line"><a name="l00265"></a><span class="lineno"> 265</span>  <span class="keywordtype">int</span> <a class="code" href="../../df/dea/structdata.html">range</a> = 500; <span class="comment">// sample points full-range</span></div>
|
|
<div class="line"><a name="l00266"></a><span class="lineno"> 266</span>  <span class="keywordtype">int</span> range2 = <a class="code" href="../../df/dea/structdata.html">range</a> >> 1; <span class="comment">// sample points half-range</span></div>
|
|
<div class="line"><a name="l00267"></a><span class="lineno"> 267</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++)</div>
|
|
<div class="line"><a name="l00268"></a><span class="lineno"> 268</span>  {</div>
|
|
<div class="line"><a name="l00269"></a><span class="lineno"> 269</span>  <span class="keywordtype">double</span> x0 = ((rand() % <a class="code" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a name="l00270"></a><span class="lineno"> 270</span>  <span class="keywordtype">double</span> x1 = ((rand() % <a class="code" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a name="l00271"></a><span class="lineno"> 271</span>  X[i][0] = x0;</div>
|
|
<div class="line"><a name="l00272"></a><span class="lineno"> 272</span>  X[i][1] = x1;</div>
|
|
<div class="line"><a name="l00273"></a><span class="lineno"> 273</span>  Y[i] = (x0 + 3. * x1) > -1 ? 1 : -1;</div>
|
|
<div class="line"><a name="l00274"></a><span class="lineno"> 274</span>  }</div>
|
|
<div class="line"><a name="l00275"></a><span class="lineno"> 275</span>  </div>
|
|
<div class="line"><a name="l00276"></a><span class="lineno"> 276</span>  printf(<span class="stringliteral">"------- Test 2 -------\n"</span>);</div>
|
|
<div class="line"><a name="l00277"></a><span class="lineno"> 277</span>  printf(<span class="stringliteral">"Model before fit: %s"</span>, <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a00dc6134ca22a12e0fd9cac54d601c2d">get_weights_str</a>(&ada));</div>
|
|
<div class="line"><a name="l00278"></a><span class="lineno"> 278</span>  </div>
|
|
<div class="line"><a name="l00279"></a><span class="lineno"> 279</span>  <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a355fda53c238a0c0c07f03dcc021caed">fit</a>(&ada, X, Y, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>);</div>
|
|
<div class="line"><a name="l00280"></a><span class="lineno"> 280</span>  printf(<span class="stringliteral">"Model after fit: %s\n"</span>, <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a00dc6134ca22a12e0fd9cac54d601c2d">get_weights_str</a>(&ada));</div>
|
|
<div class="line"><a name="l00281"></a><span class="lineno"> 281</span>  </div>
|
|
<div class="line"><a name="l00282"></a><span class="lineno"> 282</span>  <span class="keywordtype">int</span> N_test_cases = 5;</div>
|
|
<div class="line"><a name="l00283"></a><span class="lineno"> 283</span>  <span class="keywordtype">double</span> test_x[2];</div>
|
|
<div class="line"><a name="l00284"></a><span class="lineno"> 284</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < N_test_cases; i++)</div>
|
|
<div class="line"><a name="l00285"></a><span class="lineno"> 285</span>  {</div>
|
|
<div class="line"><a name="l00286"></a><span class="lineno"> 286</span>  <span class="keywordtype">double</span> x0 = ((rand() % <a class="code" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a name="l00287"></a><span class="lineno"> 287</span>  <span class="keywordtype">double</span> x1 = ((rand() % <a class="code" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a name="l00288"></a><span class="lineno"> 288</span>  </div>
|
|
<div class="line"><a name="l00289"></a><span class="lineno"> 289</span>  test_x[0] = x0;</div>
|
|
<div class="line"><a name="l00290"></a><span class="lineno"> 290</span>  test_x[1] = x1;</div>
|
|
<div class="line"><a name="l00291"></a><span class="lineno"> 291</span>  <span class="keywordtype">int</span> pred = <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a4db1ba00a7f282100ea31a94e32bd7a3">predict</a>(&ada, test_x, NULL);</div>
|
|
<div class="line"><a name="l00292"></a><span class="lineno"> 292</span>  printf(<span class="stringliteral">"Predict for x=(% 3.2f,% 3.2f): % d"</span>, x0, x1, pred);</div>
|
|
<div class="line"><a name="l00293"></a><span class="lineno"> 293</span>  </div>
|
|
<div class="line"><a name="l00294"></a><span class="lineno"> 294</span>  <span class="keywordtype">int</span> expected_val = (x0 + 3. * x1) > -1 ? 1 : -1;</div>
|
|
<div class="line"><a name="l00295"></a><span class="lineno"> 295</span>  assert(pred == expected_val);</div>
|
|
<div class="line"><a name="l00296"></a><span class="lineno"> 296</span>  printf(<span class="stringliteral">" ...passed\n"</span>);</div>
|
|
<div class="line"><a name="l00297"></a><span class="lineno"> 297</span>  }</div>
|
|
<div class="line"><a name="l00298"></a><span class="lineno"> 298</span>  </div>
|
|
<div class="line"><a name="l00299"></a><span class="lineno"> 299</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++) free(X[i]);</div>
|
|
<div class="line"><a name="l00300"></a><span class="lineno"> 300</span>  free(X);</div>
|
|
<div class="line"><a name="l00301"></a><span class="lineno"> 301</span>  free(Y);</div>
|
|
<div class="line"><a name="l00302"></a><span class="lineno"> 302</span>  <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a6f35caa3084772cc126ac7b20f67f665">delete_adaline</a>(&ada);</div>
|
|
<div class="line"><a name="l00303"></a><span class="lineno"> 303</span> }</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="../../dd/d8c/adaline__learning_8c_a05cc9a0acb524fde727a4d7b4a747ee6_cgraph.svg" width="351" height="240"><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="a3f37b9f073f7e57fd0b39d70718af1b1"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a3f37b9f073f7e57fd0b39d70718af1b1">◆ </a></span>test3()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void test3 </td>
|
|
<td>(</td>
|
|
<td class="paramtype">double </td>
|
|
<td class="paramname"><em>eta</em></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>test function to predict points in a 3D coordinate system lying within the sphere of radius 1 and centre at origin as +1 and others as -1. Note that each point is defined by 3 values but we use 6 features. The function will create random sample points for training and test purposes. The sphere centred at origin and radius 1 is defined as: \(x^2+y^2+z^2=r^2=1\) and if the \(r^2<1\), point lies within the sphere else, outside.</p>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">eta</td><td>learning rate (optional, default=0.01) </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<div class="fragment"><div class="line"><a name="l00317"></a><span class="lineno"> 317</span> {</div>
|
|
<div class="line"><a name="l00318"></a><span class="lineno"> 318</span>  <span class="keyword">struct </span><a class="code" href="../../d2/daa/structadaline.html">adaline</a> ada = <a class="code" href="../../dd/d8c/adaline__learning_8c.html#acd88962c5f6341e43cbc69b4a7d3485b">new_adaline</a>(6, <a class="code" href="../../d2/daa/structadaline.html#a85dbd7cce6195d11ebb388220b96bde2">eta</a>); <span class="comment">// 2 features</span></div>
|
|
<div class="line"><a name="l00319"></a><span class="lineno"> 319</span>  </div>
|
|
<div class="line"><a name="l00320"></a><span class="lineno"> 320</span>  <span class="keyword">const</span> <span class="keywordtype">int</span> <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a> = 50; <span class="comment">// number of sample points</span></div>
|
|
<div class="line"><a name="l00321"></a><span class="lineno"> 321</span>  </div>
|
|
<div class="line"><a name="l00322"></a><span class="lineno"> 322</span>  <span class="keywordtype">double</span> **X = (<span class="keywordtype">double</span> **)malloc(<a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a> * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
|
|
<div class="line"><a name="l00323"></a><span class="lineno"> 323</span>  <span class="keywordtype">int</span> *Y = (<span class="keywordtype">int</span> *)malloc(<a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a> * <span class="keyword">sizeof</span>(<span class="keywordtype">int</span>)); <span class="comment">// corresponding y-values</span></div>
|
|
<div class="line"><a name="l00324"></a><span class="lineno"> 324</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++) X[i] = (<span class="keywordtype">double</span> *)malloc(6 * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
|
|
<div class="line"><a name="l00325"></a><span class="lineno"> 325</span>  </div>
|
|
<div class="line"><a name="l00326"></a><span class="lineno"> 326</span>  <span class="comment">// generate sample points in the interval</span></div>
|
|
<div class="line"><a name="l00327"></a><span class="lineno"> 327</span>  <span class="comment">// [-range2/100 , (range2-1)/100]</span></div>
|
|
<div class="line"><a name="l00328"></a><span class="lineno"> 328</span>  <span class="keywordtype">int</span> <a class="code" href="../../df/dea/structdata.html">range</a> = 200; <span class="comment">// sample points full-range</span></div>
|
|
<div class="line"><a name="l00329"></a><span class="lineno"> 329</span>  <span class="keywordtype">int</span> range2 = <a class="code" href="../../df/dea/structdata.html">range</a> >> 1; <span class="comment">// sample points half-range</span></div>
|
|
<div class="line"><a name="l00330"></a><span class="lineno"> 330</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++)</div>
|
|
<div class="line"><a name="l00331"></a><span class="lineno"> 331</span>  {</div>
|
|
<div class="line"><a name="l00332"></a><span class="lineno"> 332</span>  <span class="keywordtype">double</span> x0 = ((rand() % <a class="code" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a name="l00333"></a><span class="lineno"> 333</span>  <span class="keywordtype">double</span> x1 = ((rand() % <a class="code" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a name="l00334"></a><span class="lineno"> 334</span>  <span class="keywordtype">double</span> x2 = ((rand() % <a class="code" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a name="l00335"></a><span class="lineno"> 335</span>  X[i][0] = x0;</div>
|
|
<div class="line"><a name="l00336"></a><span class="lineno"> 336</span>  X[i][1] = x1;</div>
|
|
<div class="line"><a name="l00337"></a><span class="lineno"> 337</span>  X[i][2] = x2;</div>
|
|
<div class="line"><a name="l00338"></a><span class="lineno"> 338</span>  X[i][3] = x0 * x0;</div>
|
|
<div class="line"><a name="l00339"></a><span class="lineno"> 339</span>  X[i][4] = x1 * x1;</div>
|
|
<div class="line"><a name="l00340"></a><span class="lineno"> 340</span>  X[i][5] = x2 * x2;</div>
|
|
<div class="line"><a name="l00341"></a><span class="lineno"> 341</span>  Y[i] = (x0 * x0 + x1 * x1 + x2 * x2) <= 1 ? 1 : -1;</div>
|
|
<div class="line"><a name="l00342"></a><span class="lineno"> 342</span>  }</div>
|
|
<div class="line"><a name="l00343"></a><span class="lineno"> 343</span>  </div>
|
|
<div class="line"><a name="l00344"></a><span class="lineno"> 344</span>  printf(<span class="stringliteral">"------- Test 3 -------\n"</span>);</div>
|
|
<div class="line"><a name="l00345"></a><span class="lineno"> 345</span>  printf(<span class="stringliteral">"Model before fit: %s"</span>, <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a00dc6134ca22a12e0fd9cac54d601c2d">get_weights_str</a>(&ada));</div>
|
|
<div class="line"><a name="l00346"></a><span class="lineno"> 346</span>  </div>
|
|
<div class="line"><a name="l00347"></a><span class="lineno"> 347</span>  <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a355fda53c238a0c0c07f03dcc021caed">fit</a>(&ada, X, Y, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>);</div>
|
|
<div class="line"><a name="l00348"></a><span class="lineno"> 348</span>  printf(<span class="stringliteral">"Model after fit: %s\n"</span>, <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a00dc6134ca22a12e0fd9cac54d601c2d">get_weights_str</a>(&ada));</div>
|
|
<div class="line"><a name="l00349"></a><span class="lineno"> 349</span>  </div>
|
|
<div class="line"><a name="l00350"></a><span class="lineno"> 350</span>  <span class="keywordtype">int</span> N_test_cases = 5;</div>
|
|
<div class="line"><a name="l00351"></a><span class="lineno"> 351</span>  <span class="keywordtype">double</span> test_x[6];</div>
|
|
<div class="line"><a name="l00352"></a><span class="lineno"> 352</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < N_test_cases; i++)</div>
|
|
<div class="line"><a name="l00353"></a><span class="lineno"> 353</span>  {</div>
|
|
<div class="line"><a name="l00354"></a><span class="lineno"> 354</span>  <span class="keywordtype">double</span> x0 = ((rand() % <a class="code" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a name="l00355"></a><span class="lineno"> 355</span>  <span class="keywordtype">double</span> x1 = ((rand() % <a class="code" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a name="l00356"></a><span class="lineno"> 356</span>  <span class="keywordtype">double</span> x2 = ((rand() % <a class="code" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a name="l00357"></a><span class="lineno"> 357</span>  test_x[0] = x0;</div>
|
|
<div class="line"><a name="l00358"></a><span class="lineno"> 358</span>  test_x[1] = x1;</div>
|
|
<div class="line"><a name="l00359"></a><span class="lineno"> 359</span>  test_x[2] = x2;</div>
|
|
<div class="line"><a name="l00360"></a><span class="lineno"> 360</span>  test_x[3] = x0 * x0;</div>
|
|
<div class="line"><a name="l00361"></a><span class="lineno"> 361</span>  test_x[4] = x1 * x1;</div>
|
|
<div class="line"><a name="l00362"></a><span class="lineno"> 362</span>  test_x[5] = x2 * x2;</div>
|
|
<div class="line"><a name="l00363"></a><span class="lineno"> 363</span>  <span class="keywordtype">int</span> pred = <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a4db1ba00a7f282100ea31a94e32bd7a3">predict</a>(&ada, test_x, NULL);</div>
|
|
<div class="line"><a name="l00364"></a><span class="lineno"> 364</span>  printf(<span class="stringliteral">"Predict for x=(% 3.2f,% 3.2f): % d"</span>, x0, x1, pred);</div>
|
|
<div class="line"><a name="l00365"></a><span class="lineno"> 365</span>  </div>
|
|
<div class="line"><a name="l00366"></a><span class="lineno"> 366</span>  <span class="keywordtype">int</span> expected_val = (x0 * x0 + x1 * x1 + x2 * x2) <= 1 ? 1 : -1;</div>
|
|
<div class="line"><a name="l00367"></a><span class="lineno"> 367</span>  assert(pred == expected_val);</div>
|
|
<div class="line"><a name="l00368"></a><span class="lineno"> 368</span>  printf(<span class="stringliteral">" ...passed\n"</span>);</div>
|
|
<div class="line"><a name="l00369"></a><span class="lineno"> 369</span>  }</div>
|
|
<div class="line"><a name="l00370"></a><span class="lineno"> 370</span>  </div>
|
|
<div class="line"><a name="l00371"></a><span class="lineno"> 371</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++) free(X[i]);</div>
|
|
<div class="line"><a name="l00372"></a><span class="lineno"> 372</span>  free(X);</div>
|
|
<div class="line"><a name="l00373"></a><span class="lineno"> 373</span>  free(Y);</div>
|
|
<div class="line"><a name="l00374"></a><span class="lineno"> 374</span>  <a class="code" href="../../dd/d8c/adaline__learning_8c.html#a6f35caa3084772cc126ac7b20f67f665">delete_adaline</a>(&ada);</div>
|
|
<div class="line"><a name="l00375"></a><span class="lineno"> 375</span> }</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="../../dd/d8c/adaline__learning_8c_a3f37b9f073f7e57fd0b39d70718af1b1_cgraph.svg" width="351" height="240"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
</div><!-- contents -->
|
|
</div><!-- doc-content -->
|
|
<div class="ttc" id="aadaline__learning_8c_html_a00dc6134ca22a12e0fd9cac54d601c2d"><div class="ttname"><a href="../../dd/d8c/adaline__learning_8c.html#a00dc6134ca22a12e0fd9cac54d601c2d">get_weights_str</a></div><div class="ttdeci">char * get_weights_str(struct adaline *ada)</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:99</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:39</div></div>
|
|
<div class="ttc" id="aadaline__learning_8c_html_a6f35caa3084772cc126ac7b20f67f665"><div class="ttname"><a href="../../dd/d8c/adaline__learning_8c.html#a6f35caa3084772cc126ac7b20f67f665">delete_adaline</a></div><div class="ttdeci">void delete_adaline(struct adaline *ada)</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:81</div></div>
|
|
<div class="ttc" id="astructdata_html"><div class="ttname"><a href="../../df/dea/structdata.html">data</a></div><div class="ttdef"><b>Definition:</b> prime_factoriziation.c:25</div></div>
|
|
<div class="ttc" id="aproblem__13_2sol1_8c_html_a0240ac851181b84ac374872dc5434ee4"><div class="ttname"><a href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a></div><div class="ttdeci">#define N</div><div class="ttdef"><b>Definition:</b> sol1.c:109</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:38</div></div>
|
|
<div class="ttc" id="aadaline__learning_8c_html_a4db1ba00a7f282100ea31a94e32bd7a3"><div class="ttname"><a href="../../dd/d8c/adaline__learning_8c.html#a4db1ba00a7f282100ea31a94e32bd7a3">predict</a></div><div class="ttdeci">int predict(struct adaline *ada, const double *x, double *out)</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:123</div></div>
|
|
<div class="ttc" id="aadaline__learning_8c_html_acd88962c5f6341e43cbc69b4a7d3485b"><div class="ttname"><a href="../../dd/d8c/adaline__learning_8c.html#acd88962c5f6341e43cbc69b4a7d3485b">new_adaline</a></div><div class="ttdeci">struct adaline new_adaline(const int num_features, const double eta)</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:51</div></div>
|
|
<div class="ttc" id="aadaline__learning_8c_html_a3f37b9f073f7e57fd0b39d70718af1b1"><div class="ttname"><a href="../../dd/d8c/adaline__learning_8c.html#a3f37b9f073f7e57fd0b39d70718af1b1">test3</a></div><div class="ttdeci">void test3(double eta)</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:316</div></div>
|
|
<div class="ttc" id="aadaline__learning_8c_html_afa953e811eaae199d121a7ddb619d604"><div class="ttname"><a href="../../dd/d8c/adaline__learning_8c.html#afa953e811eaae199d121a7ddb619d604">fit_sample</a></div><div class="ttdeci">double fit_sample(struct adaline *ada, const double *x, const int y)</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:144</div></div>
|
|
<div class="ttc" id="aadaline__learning_8c_html_a7b12b6513314c975303b5a698608322f"><div class="ttname"><a href="../../dd/d8c/adaline__learning_8c.html#a7b12b6513314c975303b5a698608322f">activation</a></div><div class="ttdeci">int activation(double x)</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:94</div></div>
|
|
<div class="ttc" id="aadaline__learning_8c_html_ab4ecb3accf5d9e0263087e7265bbe3a9"><div class="ttname"><a href="../../dd/d8c/adaline__learning_8c.html#ab4ecb3accf5d9e0263087e7265bbe3a9">test1</a></div><div class="ttdeci">void test1(double eta)</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:205</div></div>
|
|
<div class="ttc" id="aadaline__learning_8c_html_a05cc9a0acb524fde727a4d7b4a747ee6"><div class="ttname"><a href="../../dd/d8c/adaline__learning_8c.html#a05cc9a0acb524fde727a4d7b4a747ee6">test2</a></div><div class="ttdeci">void test2(double eta)</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:253</div></div>
|
|
<div class="ttc" id="aadaline__learning_8c_html_a355fda53c238a0c0c07f03dcc021caed"><div class="ttname"><a href="../../dd/d8c/adaline__learning_8c.html#a355fda53c238a0c0c07f03dcc021caed">fit</a></div><div class="ttdeci">void fit(struct adaline *ada, double **X, const int *y, const int N)</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:170</div></div>
|
|
<div class="ttc" id="astructadaline_html"><div class="ttname"><a href="../../d2/daa/structadaline.html">adaline</a></div><div class="ttdef"><b>Definition:</b> adaline_learning.c:37</div></div>
|
|
<div class="ttc" id="aadaline__learning_8c_html_af270a96662132d0385cb6b4637c5a689"><div class="ttname"><a href="../../dd/d8c/adaline__learning_8c.html#af270a96662132d0385cb6b4637c5a689">ACCURACY</a></div><div class="ttdeci">#define ACCURACY</div><div class="ttdoc">convergence accuracy</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:43</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:40</div></div>
|
|
<!-- start footer part -->
|
|
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
|
<ul>
|
|
<li class="navelem"><a class="el" href="../../dir_3343723ae086de42ee4ca9774da3a13f.html">machine_learning</a></li><li class="navelem"><a class="el" href="../../dd/d8c/adaline__learning_8c.html">adaline_learning.c</a></li>
|
|
<li class="footer">Generated by
|
|
<a href="http://www.doxygen.org/index.html">
|
|
<img class="footer" src="../../doxygen.png" alt="doxygen"/></a> 1.8.18 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|