mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
503 lines
57 KiB
HTML
503 lines
57 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=11"/>
|
|
<meta name="generator" content="Doxygen 1.9.2"/>
|
|
<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.9.2 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
var searchBox = new SearchBox("searchBox", "../../search",'Search','.html');
|
|
/* @license-end */
|
|
</script>
|
|
<script type="text/javascript" src="../../menudata.js"></script>
|
|
<script type="text/javascript" src="../../menu.js"></script>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(function() {
|
|
initMenu('../../',true,false,'search.php','Search');
|
|
$(document).ready(function() { init_search(); });
|
|
});
|
|
/* @license-end */
|
|
</script>
|
|
<div id="main-nav"></div>
|
|
</div><!-- top -->
|
|
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
|
<div id="nav-tree">
|
|
<div id="nav-tree-contents">
|
|
<div id="nav-sync" class="sync"></div>
|
|
</div>
|
|
</div>
|
|
<div id="splitbar" style="-moz-user-select:none;"
|
|
class="ui-resizable-handle">
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(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" target="_blank">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="612" 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 id="nested-classes" 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="memdesc:"><td class="mdescLeft"> </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"> </td></tr>
|
|
</table><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="define-members" name="define-members"></a>
|
|
Macros</h2></td></tr>
|
|
<tr class="memitem:ga555ba960994e9bccb2029764588f694f"><td class="memItemLeft" align="right" valign="top">
|
|
#define </td><td class="memItemRight" valign="bottom"><b>MAX_ADALINE_ITER</b>   500</td></tr>
|
|
<tr class="memdesc:ga555ba960994e9bccb2029764588f694f"><td class="mdescLeft"> </td><td class="mdescRight">Maximum number of iterations to learn. <br /></td></tr>
|
|
<tr class="separator:ga555ba960994e9bccb2029764588f694f"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:gab4d49d73dec94c092b7ffadba55fb020"><td class="memItemLeft" align="right" valign="top">
|
|
#define </td><td class="memItemRight" valign="bottom"><b>ADALINE_ACCURACY</b>   1e-5</td></tr>
|
|
<tr class="memdesc:gab4d49d73dec94c092b7ffadba55fb020"><td class="mdescLeft"> </td><td class="mdescRight">convergence accuracy \(=1\times10^{-5}\) <br /></td></tr>
|
|
<tr class="separator:gab4d49d73dec94c092b7ffadba55fb020"><td class="memSeparator" colspan="2"> </td></tr>
|
|
</table><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
|
|
Functions</h2></td></tr>
|
|
<tr class="memitem:gacd88962c5f6341e43cbc69b4a7d3485b"><td class="memItemLeft" align="right" valign="top">struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> </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"> </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"> </td></tr>
|
|
<tr class="memitem:ga6f35caa3084772cc126ac7b20f67f665"><td class="memItemLeft" align="right" valign="top">void </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"> </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"> </td></tr>
|
|
<tr class="memitem:ga43576566b020c4157d4fb28f0dd45cfa"><td class="memItemLeft" align="right" valign="top">int </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"> </td><td class="mdescRight"><a href="https://en.wikipedia.org/wiki/Heaviside_step_function" target="_blank">Heaviside activation function</a> <img src="https://upload.wikimedia.org/wikipedia/commons/d/d9/Dirac_distribution_CDF.svg" alt="" style="pointer-events: none;" width="200px" class="inline"/> <a href="../../da/d2a/group__adaline.html#ga43576566b020c4157d4fb28f0dd45cfa">More...</a><br /></td></tr>
|
|
<tr class="separator:ga43576566b020c4157d4fb28f0dd45cfa"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:gadb1a7acb555ad62daaf3806bf2fed036"><td class="memItemLeft" align="right" valign="top">char * </td><td class="memItemRight" valign="bottom"><a class="el" href="../../da/d2a/group__adaline.html#gadb1a7acb555ad62daaf3806bf2fed036">adaline_get_weights_str</a> (const struct <a class="el" href="../../d2/daa/structadaline.html">adaline</a> *ada)</td></tr>
|
|
<tr class="memdesc:gadb1a7acb555ad62daaf3806bf2fed036"><td class="mdescLeft"> </td><td class="mdescRight">Operator to print the weights of the model. <a href="../../da/d2a/group__adaline.html#gadb1a7acb555ad62daaf3806bf2fed036">More...</a><br /></td></tr>
|
|
<tr class="separator:gadb1a7acb555ad62daaf3806bf2fed036"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:gac70b578aee679005fd336073969c3d94"><td class="memItemLeft" align="right" valign="top">int </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"> </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"> </td></tr>
|
|
<tr class="memitem:ga20d3642e0a87f36fdb7bf91b023cd166"><td class="memItemLeft" align="right" valign="top">double </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"> </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"> </td></tr>
|
|
<tr class="memitem:gaa52120912e32d2893fe1c6d78da5befd"><td class="memItemLeft" align="right" valign="top">void </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"> </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"> </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="memdesc:ab4ecb3accf5d9e0263087e7265bbe3a9"><td class="mdescLeft"> </td><td class="mdescRight">test function to predict points in a 2D coordinate system above the line \(x=y\) as +1 and others as -1. <a href="../../dd/d8c/adaline__learning_8c.html#ab4ecb3accf5d9e0263087e7265bbe3a9">More...</a><br /></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="memdesc:a05cc9a0acb524fde727a4d7b4a747ee6"><td class="mdescLeft"> </td><td class="mdescRight">test function to predict points in a 2D coordinate system above the line \(x+3y=-1\) as +1 and others as -1. <a href="../../dd/d8c/adaline__learning_8c.html#a05cc9a0acb524fde727a4d7b4a747ee6">More...</a><br /></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="memdesc:a3f37b9f073f7e57fd0b39d70718af1b1"><td class="mdescLeft"> </td><td class="mdescRight">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. <a href="../../dd/d8c/adaline__learning_8c.html#a3f37b9f073f7e57fd0b39d70718af1b1">More...</a><br /></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="memdesc:a3c04138a5bfe5d72780bb7e82a18e627"><td class="mdescLeft"> </td><td class="mdescRight">Main function. <a href="../../dd/d8c/adaline__learning_8c.html#a3c04138a5bfe5d72780bb7e82a18e627">More...</a><br /></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" target="_blank">Adaptive Linear Neuron (ADALINE)</a> implementation </p>
|
|
<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" target="_blank">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><dl class="section author"><dt>Author</dt><dd><a href="https://github.com/kvedala" target="_blank">Krishna Vedala</a> </dd></dl>
|
|
</div><h2 class="groupheader">Function Documentation</h2>
|
|
<a id="a3c04138a5bfe5d72780bb7e82a18e627" name="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 id="l00399" name="l00399"></a><span class="lineno"> 399</span>{</div>
|
|
<div class="line"><a id="l00400" name="l00400"></a><span class="lineno"> 400</span> srand(time(NULL)); <span class="comment">// initialize random number generator</span></div>
|
|
<div class="line"><a id="l00401" name="l00401"></a><span class="lineno"> 401</span> </div>
|
|
<div class="line"><a id="l00402" name="l00402"></a><span class="lineno"> 402</span> <span class="keywordtype">double</span> eta = 0.1; <span class="comment">// default value of eta</span></div>
|
|
<div class="line"><a id="l00403" name="l00403"></a><span class="lineno"> 403</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 id="l00404" name="l00404"></a><span class="lineno"> 404</span> eta = strtof(argv[1], NULL);</div>
|
|
<div class="line"><a id="l00405" name="l00405"></a><span class="lineno"> 405</span> </div>
|
|
<div class="line"><a id="l00406" name="l00406"></a><span class="lineno"> 406</span> <a class="code hl_function" href="../../dd/d8c/adaline__learning_8c.html#ab4ecb3accf5d9e0263087e7265bbe3a9">test1</a>(eta);</div>
|
|
<div class="line"><a id="l00407" name="l00407"></a><span class="lineno"> 407</span> </div>
|
|
<div class="line"><a id="l00408" name="l00408"></a><span class="lineno"> 408</span> printf(<span class="stringliteral">"Press ENTER to continue...\n"</span>);</div>
|
|
<div class="line"><a id="l00409" name="l00409"></a><span class="lineno"> 409</span> getchar();</div>
|
|
<div class="line"><a id="l00410" name="l00410"></a><span class="lineno"> 410</span> </div>
|
|
<div class="line"><a id="l00411" name="l00411"></a><span class="lineno"> 411</span> <a class="code hl_function" href="../../dd/d8c/adaline__learning_8c.html#a05cc9a0acb524fde727a4d7b4a747ee6">test2</a>(eta);</div>
|
|
<div class="line"><a id="l00412" name="l00412"></a><span class="lineno"> 412</span> </div>
|
|
<div class="line"><a id="l00413" name="l00413"></a><span class="lineno"> 413</span> printf(<span class="stringliteral">"Press ENTER to continue...\n"</span>);</div>
|
|
<div class="line"><a id="l00414" name="l00414"></a><span class="lineno"> 414</span> getchar();</div>
|
|
<div class="line"><a id="l00415" name="l00415"></a><span class="lineno"> 415</span> </div>
|
|
<div class="line"><a id="l00416" name="l00416"></a><span class="lineno"> 416</span> <a class="code hl_function" href="../../dd/d8c/adaline__learning_8c.html#a3f37b9f073f7e57fd0b39d70718af1b1">test3</a>(eta);</div>
|
|
<div class="line"><a id="l00417" name="l00417"></a><span class="lineno"> 417</span> </div>
|
|
<div class="line"><a id="l00418" name="l00418"></a><span class="lineno"> 418</span> <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><a id="l00419" name="l00419"></a><span class="lineno"> 419</span>}</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="ttdoc">test function to predict points in a 2D coordinate system above the line as +1 and others as -1.</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:273</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="ttdoc">test function to predict points in a 3D coordinate system lying within the sphere of radius 1 and cen...</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:336</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="ttdoc">test function to predict points in a 2D coordinate system above the line as +1 and others as -1.</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:225</div></div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><div class="zoom"><iframe scrolling="no" frameborder="0" src="../../dd/d8c/adaline__learning_8c_a3c04138a5bfe5d72780bb7e82a18e627_cgraph.svg" width="100%" height="511"><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>
|
|
<a id="ab4ecb3accf5d9e0263087e7265bbe3a9" name="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. </p>
|
|
<p >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 id="l00226" name="l00226"></a><span class="lineno"> 226</span>{</div>
|
|
<div class="line"><a id="l00227" name="l00227"></a><span class="lineno"> 227</span> <span class="keyword">struct </span><a class="code hl_struct" href="../../d2/daa/structadaline.html">adaline</a> ada = <a class="code hl_function" href="../../da/d2a/group__adaline.html#gacd88962c5f6341e43cbc69b4a7d3485b">new_adaline</a>(2, <a class="code hl_variable" href="../../d2/daa/structadaline.html#a85dbd7cce6195d11ebb388220b96bde2">eta</a>); <span class="comment">// 2 features</span></div>
|
|
<div class="line"><a id="l00228" name="l00228"></a><span class="lineno"> 228</span> </div>
|
|
<div class="line"><a id="l00229" name="l00229"></a><span class="lineno"> 229</span> <span class="keyword">const</span> <span class="keywordtype">int</span> N = 10; <span class="comment">// number of sample points</span></div>
|
|
<div class="line"><a id="l00230" name="l00230"></a><span class="lineno"> 230</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 id="l00231" name="l00231"></a><span class="lineno"> 231</span> {4, 1}, {6, -5}, {-7, -3}, {-8, 5},</div>
|
|
<div class="line"><a id="l00232" name="l00232"></a><span class="lineno"> 232</span> {-9, 2}, {-10, -15}};</div>
|
|
<div class="line"><a id="l00233" name="l00233"></a><span class="lineno"> 233</span> </div>
|
|
<div class="line"><a id="l00234" name="l00234"></a><span class="lineno"> 234</span> <span class="keywordtype">double</span> **X = (<span class="keywordtype">double</span> **)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(N * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
|
|
<div class="line"><a id="l00235" name="l00235"></a><span class="lineno"> 235</span> <span class="keyword">const</span> <span class="keywordtype">int</span> Y[10] = {1, -1, 1, -1, -1,</div>
|
|
<div class="line"><a id="l00236" name="l00236"></a><span class="lineno"> 236</span> -1, 1, 1, 1, -1}; <span class="comment">// corresponding y-values</span></div>
|
|
<div class="line"><a id="l00237" name="l00237"></a><span class="lineno"> 237</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < N; i++)</div>
|
|
<div class="line"><a id="l00238" name="l00238"></a><span class="lineno"> 238</span> {</div>
|
|
<div class="line"><a id="l00239" name="l00239"></a><span class="lineno"> 239</span> X[i] = (<span class="keywordtype">double</span> *)saved_X[i];</div>
|
|
<div class="line"><a id="l00240" name="l00240"></a><span class="lineno"> 240</span> }</div>
|
|
<div class="line"><a id="l00241" name="l00241"></a><span class="lineno"> 241</span> </div>
|
|
<div class="line"><a id="l00242" name="l00242"></a><span class="lineno"> 242</span> printf(<span class="stringliteral">"------- Test 1 -------\n"</span>);</div>
|
|
<div class="line"><a id="l00243" name="l00243"></a><span class="lineno"> 243</span> printf(<span class="stringliteral">"Model before fit: %s\n"</span>, <a class="code hl_function" href="../../da/d2a/group__adaline.html#gadb1a7acb555ad62daaf3806bf2fed036">adaline_get_weights_str</a>(&ada));</div>
|
|
<div class="line"><a id="l00244" name="l00244"></a><span class="lineno"> 244</span> </div>
|
|
<div class="line"><a id="l00245" name="l00245"></a><span class="lineno"> 245</span> <a class="code hl_function" href="../../da/d2a/group__adaline.html#gaa52120912e32d2893fe1c6d78da5befd">adaline_fit</a>(&ada, X, Y, N);</div>
|
|
<div class="line"><a id="l00246" name="l00246"></a><span class="lineno"> 246</span> printf(<span class="stringliteral">"Model after fit: %s\n"</span>, <a class="code hl_function" href="../../da/d2a/group__adaline.html#gadb1a7acb555ad62daaf3806bf2fed036">adaline_get_weights_str</a>(&ada));</div>
|
|
<div class="line"><a id="l00247" name="l00247"></a><span class="lineno"> 247</span> </div>
|
|
<div class="line"><a id="l00248" name="l00248"></a><span class="lineno"> 248</span> <span class="keywordtype">double</span> test_x[] = {5, -3};</div>
|
|
<div class="line"><a id="l00249" name="l00249"></a><span class="lineno"> 249</span> <span class="keywordtype">int</span> pred = <a class="code hl_function" href="../../da/d2a/group__adaline.html#gac70b578aee679005fd336073969c3d94">adaline_predict</a>(&ada, test_x, NULL);</div>
|
|
<div class="line"><a id="l00250" name="l00250"></a><span class="lineno"> 250</span> printf(<span class="stringliteral">"Predict for x=(5,-3): % d\n"</span>, pred);</div>
|
|
<div class="line"><a id="l00251" name="l00251"></a><span class="lineno"> 251</span> assert(pred == -1);</div>
|
|
<div class="line"><a id="l00252" name="l00252"></a><span class="lineno"> 252</span> printf(<span class="stringliteral">" ...passed\n"</span>);</div>
|
|
<div class="line"><a id="l00253" name="l00253"></a><span class="lineno"> 253</span> </div>
|
|
<div class="line"><a id="l00254" name="l00254"></a><span class="lineno"> 254</span> <span class="keywordtype">double</span> test_x2[] = {5, 8};</div>
|
|
<div class="line"><a id="l00255" name="l00255"></a><span class="lineno"> 255</span> pred = <a class="code hl_function" href="../../da/d2a/group__adaline.html#gac70b578aee679005fd336073969c3d94">adaline_predict</a>(&ada, test_x2, NULL);</div>
|
|
<div class="line"><a id="l00256" name="l00256"></a><span class="lineno"> 256</span> printf(<span class="stringliteral">"Predict for x=(5, 8): % d\n"</span>, pred);</div>
|
|
<div class="line"><a id="l00257" name="l00257"></a><span class="lineno"> 257</span> assert(pred == 1);</div>
|
|
<div class="line"><a id="l00258" name="l00258"></a><span class="lineno"> 258</span> printf(<span class="stringliteral">" ...passed\n"</span>);</div>
|
|
<div class="line"><a id="l00259" name="l00259"></a><span class="lineno"> 259</span> </div>
|
|
<div class="line"><a id="l00260" name="l00260"></a><span class="lineno"> 260</span> <span class="comment">// for (int i = 0; i < N; i++)</span></div>
|
|
<div class="line"><a id="l00261" name="l00261"></a><span class="lineno"> 261</span> <span class="comment">// free(X[i]);</span></div>
|
|
<div class="line"><a id="l00262" name="l00262"></a><span class="lineno"> 262</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(X);</div>
|
|
<div class="line"><a id="l00263" name="l00263"></a><span class="lineno"> 263</span> <a class="code hl_function" href="../../da/d2a/group__adaline.html#ga6f35caa3084772cc126ac7b20f67f665">delete_adaline</a>(&ada);</div>
|
|
<div class="line"><a id="l00264" name="l00264"></a><span class="lineno"> 264</span>}</div>
|
|
<div class="ttc" id="agroup__adaline_html_ga6f35caa3084772cc126ac7b20f67f665"><div class="ttname"><a href="../../da/d2a/group__adaline.html#ga6f35caa3084772cc126ac7b20f67f665">delete_adaline</a></div><div class="ttdeci">void delete_adaline(struct adaline *ada)</div><div class="ttdoc">delete dynamically allocated memory</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:89</div></div>
|
|
<div class="ttc" id="agroup__adaline_html_gaa52120912e32d2893fe1c6d78da5befd"><div class="ttname"><a href="../../da/d2a/group__adaline.html#gaa52120912e32d2893fe1c6d78da5befd">adaline_fit</a></div><div class="ttdeci">void adaline_fit(struct adaline *ada, double **X, const int *y, const int N)</div><div class="ttdoc">Update the weights of the model using supervised learning for an array of vectors.</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:184</div></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="agroup__adaline_html_gacd88962c5f6341e43cbc69b4a7d3485b"><div class="ttname"><a href="../../da/d2a/group__adaline.html#gacd88962c5f6341e43cbc69b4a7d3485b">new_adaline</a></div><div class="ttdeci">struct adaline new_adaline(const int num_features, const double eta)</div><div class="ttdoc">Default constructor.</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:59</div></div>
|
|
<div class="ttc" id="agroup__adaline_html_gadb1a7acb555ad62daaf3806bf2fed036"><div class="ttname"><a href="../../da/d2a/group__adaline.html#gadb1a7acb555ad62daaf3806bf2fed036">adaline_get_weights_str</a></div><div class="ttdeci">char * adaline_get_weights_str(const struct adaline *ada)</div><div class="ttdoc">Operator to print the weights of the model.</div><div class="ttdef"><b>Definition:</b> adaline_learning.c:112</div></div>
|
|
<div 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="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 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 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"><div class="zoom"><iframe scrolling="no" frameborder="0" src="../../dd/d8c/adaline__learning_8c_ab4ecb3accf5d9e0263087e7265bbe3a9_cgraph.svg" width="100%" height="480"><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>
|
|
<a id="a05cc9a0acb524fde727a4d7b4a747ee6" name="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. </p>
|
|
<p >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 id="l00274" name="l00274"></a><span class="lineno"> 274</span>{</div>
|
|
<div class="line"><a id="l00275" name="l00275"></a><span class="lineno"> 275</span> <span class="keyword">struct </span><a class="code hl_struct" href="../../d2/daa/structadaline.html">adaline</a> ada = <a class="code hl_function" href="../../da/d2a/group__adaline.html#gacd88962c5f6341e43cbc69b4a7d3485b">new_adaline</a>(2, <a class="code hl_variable" href="../../d2/daa/structadaline.html#a85dbd7cce6195d11ebb388220b96bde2">eta</a>); <span class="comment">// 2 features</span></div>
|
|
<div class="line"><a id="l00276" name="l00276"></a><span class="lineno"> 276</span> </div>
|
|
<div class="line"><a id="l00277" name="l00277"></a><span class="lineno"> 277</span> <span class="keyword">const</span> <span class="keywordtype">int</span> N = 50; <span class="comment">// number of sample points</span></div>
|
|
<div class="line"><a id="l00278" name="l00278"></a><span class="lineno"> 278</span> </div>
|
|
<div class="line"><a id="l00279" name="l00279"></a><span class="lineno"> 279</span> <span class="keywordtype">double</span> **X = (<span class="keywordtype">double</span> **)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(N * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
|
|
<div class="line"><a id="l00280" name="l00280"></a><span class="lineno"> 280</span> <span class="keywordtype">int</span> *Y = (<span class="keywordtype">int</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(N * <span class="keyword">sizeof</span>(<span class="keywordtype">int</span>)); <span class="comment">// corresponding y-values</span></div>
|
|
<div class="line"><a id="l00281" name="l00281"></a><span class="lineno"> 281</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < N; i++) X[i] = (<span class="keywordtype">double</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(2 * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
|
|
<div class="line"><a id="l00282" name="l00282"></a><span class="lineno"> 282</span> </div>
|
|
<div class="line"><a id="l00283" name="l00283"></a><span class="lineno"> 283</span> <span class="comment">// generate sample points in the interval</span></div>
|
|
<div class="line"><a id="l00284" name="l00284"></a><span class="lineno"> 284</span> <span class="comment">// [-range2/100 , (range2-1)/100]</span></div>
|
|
<div class="line"><a id="l00285" name="l00285"></a><span class="lineno"> 285</span> <span class="keywordtype">int</span> <a class="code hl_struct" href="../../df/dea/structdata.html">range</a> = 500; <span class="comment">// sample points full-range</span></div>
|
|
<div class="line"><a id="l00286" name="l00286"></a><span class="lineno"> 286</span> <span class="keywordtype">int</span> range2 = <a class="code hl_struct" href="../../df/dea/structdata.html">range</a> >> 1; <span class="comment">// sample points half-range</span></div>
|
|
<div class="line"><a id="l00287" name="l00287"></a><span class="lineno"> 287</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < N; i++)</div>
|
|
<div class="line"><a id="l00288" name="l00288"></a><span class="lineno"> 288</span> {</div>
|
|
<div class="line"><a id="l00289" name="l00289"></a><span class="lineno"> 289</span> <span class="keywordtype">double</span> x0 = ((rand() % <a class="code hl_struct" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a id="l00290" name="l00290"></a><span class="lineno"> 290</span> <span class="keywordtype">double</span> x1 = ((rand() % <a class="code hl_struct" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a id="l00291" name="l00291"></a><span class="lineno"> 291</span> X[i][0] = x0;</div>
|
|
<div class="line"><a id="l00292" name="l00292"></a><span class="lineno"> 292</span> X[i][1] = x1;</div>
|
|
<div class="line"><a id="l00293" name="l00293"></a><span class="lineno"> 293</span> Y[i] = (x0 + 3. * x1) > -1 ? 1 : -1;</div>
|
|
<div class="line"><a id="l00294" name="l00294"></a><span class="lineno"> 294</span> }</div>
|
|
<div class="line"><a id="l00295" name="l00295"></a><span class="lineno"> 295</span> </div>
|
|
<div class="line"><a id="l00296" name="l00296"></a><span class="lineno"> 296</span> printf(<span class="stringliteral">"------- Test 2 -------\n"</span>);</div>
|
|
<div class="line"><a id="l00297" name="l00297"></a><span class="lineno"> 297</span> printf(<span class="stringliteral">"Model before fit: %s\n"</span>, <a class="code hl_function" href="../../da/d2a/group__adaline.html#gadb1a7acb555ad62daaf3806bf2fed036">adaline_get_weights_str</a>(&ada));</div>
|
|
<div class="line"><a id="l00298" name="l00298"></a><span class="lineno"> 298</span> </div>
|
|
<div class="line"><a id="l00299" name="l00299"></a><span class="lineno"> 299</span> <a class="code hl_function" href="../../da/d2a/group__adaline.html#gaa52120912e32d2893fe1c6d78da5befd">adaline_fit</a>(&ada, X, Y, N);</div>
|
|
<div class="line"><a id="l00300" name="l00300"></a><span class="lineno"> 300</span> printf(<span class="stringliteral">"Model after fit: %s\n"</span>, <a class="code hl_function" href="../../da/d2a/group__adaline.html#gadb1a7acb555ad62daaf3806bf2fed036">adaline_get_weights_str</a>(&ada));</div>
|
|
<div class="line"><a id="l00301" name="l00301"></a><span class="lineno"> 301</span> </div>
|
|
<div class="line"><a id="l00302" name="l00302"></a><span class="lineno"> 302</span> <span class="keywordtype">int</span> N_test_cases = 5;</div>
|
|
<div class="line"><a id="l00303" name="l00303"></a><span class="lineno"> 303</span> <span class="keywordtype">double</span> test_x[2];</div>
|
|
<div class="line"><a id="l00304" name="l00304"></a><span class="lineno"> 304</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < N_test_cases; i++)</div>
|
|
<div class="line"><a id="l00305" name="l00305"></a><span class="lineno"> 305</span> {</div>
|
|
<div class="line"><a id="l00306" name="l00306"></a><span class="lineno"> 306</span> <span class="keywordtype">double</span> x0 = ((rand() % <a class="code hl_struct" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a id="l00307" name="l00307"></a><span class="lineno"> 307</span> <span class="keywordtype">double</span> x1 = ((rand() % <a class="code hl_struct" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a id="l00308" name="l00308"></a><span class="lineno"> 308</span> </div>
|
|
<div class="line"><a id="l00309" name="l00309"></a><span class="lineno"> 309</span> test_x[0] = x0;</div>
|
|
<div class="line"><a id="l00310" name="l00310"></a><span class="lineno"> 310</span> test_x[1] = x1;</div>
|
|
<div class="line"><a id="l00311" name="l00311"></a><span class="lineno"> 311</span> <span class="keywordtype">int</span> pred = <a class="code hl_function" href="../../da/d2a/group__adaline.html#gac70b578aee679005fd336073969c3d94">adaline_predict</a>(&ada, test_x, NULL);</div>
|
|
<div class="line"><a id="l00312" name="l00312"></a><span class="lineno"> 312</span> printf(<span class="stringliteral">"Predict for x=(% 3.2f,% 3.2f): % d\n"</span>, x0, x1, pred);</div>
|
|
<div class="line"><a id="l00313" name="l00313"></a><span class="lineno"> 313</span> </div>
|
|
<div class="line"><a id="l00314" name="l00314"></a><span class="lineno"> 314</span> <span class="keywordtype">int</span> expected_val = (x0 + 3. * x1) > -1 ? 1 : -1;</div>
|
|
<div class="line"><a id="l00315" name="l00315"></a><span class="lineno"> 315</span> assert(pred == expected_val);</div>
|
|
<div class="line"><a id="l00316" name="l00316"></a><span class="lineno"> 316</span> printf(<span class="stringliteral">" ...passed\n"</span>);</div>
|
|
<div class="line"><a id="l00317" name="l00317"></a><span class="lineno"> 317</span> }</div>
|
|
<div class="line"><a id="l00318" name="l00318"></a><span class="lineno"> 318</span> </div>
|
|
<div class="line"><a id="l00319" name="l00319"></a><span class="lineno"> 319</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < N; i++) <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(X[i]);</div>
|
|
<div class="line"><a id="l00320" name="l00320"></a><span class="lineno"> 320</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(X);</div>
|
|
<div class="line"><a id="l00321" name="l00321"></a><span class="lineno"> 321</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(Y);</div>
|
|
<div class="line"><a id="l00322" name="l00322"></a><span class="lineno"> 322</span> <a class="code hl_function" href="../../da/d2a/group__adaline.html#ga6f35caa3084772cc126ac7b20f67f665">delete_adaline</a>(&ada);</div>
|
|
<div class="line"><a id="l00323" name="l00323"></a><span class="lineno"> 323</span>}</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><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><div class="zoom"><iframe scrolling="no" frameborder="0" src="../../dd/d8c/adaline__learning_8c_a05cc9a0acb524fde727a4d7b4a747ee6_cgraph.svg" width="100%" height="480"><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>
|
|
<a id="a3f37b9f073f7e57fd0b39d70718af1b1" name="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. </p>
|
|
<p >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 id="l00337" name="l00337"></a><span class="lineno"> 337</span>{</div>
|
|
<div class="line"><a id="l00338" name="l00338"></a><span class="lineno"> 338</span> <span class="keyword">struct </span><a class="code hl_struct" href="../../d2/daa/structadaline.html">adaline</a> ada = <a class="code hl_function" href="../../da/d2a/group__adaline.html#gacd88962c5f6341e43cbc69b4a7d3485b">new_adaline</a>(6, <a class="code hl_variable" href="../../d2/daa/structadaline.html#a85dbd7cce6195d11ebb388220b96bde2">eta</a>); <span class="comment">// 2 features</span></div>
|
|
<div class="line"><a id="l00339" name="l00339"></a><span class="lineno"> 339</span> </div>
|
|
<div class="line"><a id="l00340" name="l00340"></a><span class="lineno"> 340</span> <span class="keyword">const</span> <span class="keywordtype">int</span> N = 50; <span class="comment">// number of sample points</span></div>
|
|
<div class="line"><a id="l00341" name="l00341"></a><span class="lineno"> 341</span> </div>
|
|
<div class="line"><a id="l00342" name="l00342"></a><span class="lineno"> 342</span> <span class="keywordtype">double</span> **X = (<span class="keywordtype">double</span> **)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(N * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
|
|
<div class="line"><a id="l00343" name="l00343"></a><span class="lineno"> 343</span> <span class="keywordtype">int</span> *Y = (<span class="keywordtype">int</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(N * <span class="keyword">sizeof</span>(<span class="keywordtype">int</span>)); <span class="comment">// corresponding y-values</span></div>
|
|
<div class="line"><a id="l00344" name="l00344"></a><span class="lineno"> 344</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < N; i++) X[i] = (<span class="keywordtype">double</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(6 * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
|
|
<div class="line"><a id="l00345" name="l00345"></a><span class="lineno"> 345</span> </div>
|
|
<div class="line"><a id="l00346" name="l00346"></a><span class="lineno"> 346</span> <span class="comment">// generate sample points in the interval</span></div>
|
|
<div class="line"><a id="l00347" name="l00347"></a><span class="lineno"> 347</span> <span class="comment">// [-range2/100 , (range2-1)/100]</span></div>
|
|
<div class="line"><a id="l00348" name="l00348"></a><span class="lineno"> 348</span> <span class="keywordtype">int</span> <a class="code hl_struct" href="../../df/dea/structdata.html">range</a> = 200; <span class="comment">// sample points full-range</span></div>
|
|
<div class="line"><a id="l00349" name="l00349"></a><span class="lineno"> 349</span> <span class="keywordtype">int</span> range2 = <a class="code hl_struct" href="../../df/dea/structdata.html">range</a> >> 1; <span class="comment">// sample points half-range</span></div>
|
|
<div class="line"><a id="l00350" name="l00350"></a><span class="lineno"> 350</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < N; i++)</div>
|
|
<div class="line"><a id="l00351" name="l00351"></a><span class="lineno"> 351</span> {</div>
|
|
<div class="line"><a id="l00352" name="l00352"></a><span class="lineno"> 352</span> <span class="keywordtype">double</span> x0 = ((rand() % <a class="code hl_struct" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a id="l00353" name="l00353"></a><span class="lineno"> 353</span> <span class="keywordtype">double</span> x1 = ((rand() % <a class="code hl_struct" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a id="l00354" name="l00354"></a><span class="lineno"> 354</span> <span class="keywordtype">double</span> x2 = ((rand() % <a class="code hl_struct" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a id="l00355" name="l00355"></a><span class="lineno"> 355</span> X[i][0] = x0;</div>
|
|
<div class="line"><a id="l00356" name="l00356"></a><span class="lineno"> 356</span> X[i][1] = x1;</div>
|
|
<div class="line"><a id="l00357" name="l00357"></a><span class="lineno"> 357</span> X[i][2] = x2;</div>
|
|
<div class="line"><a id="l00358" name="l00358"></a><span class="lineno"> 358</span> X[i][3] = x0 * x0;</div>
|
|
<div class="line"><a id="l00359" name="l00359"></a><span class="lineno"> 359</span> X[i][4] = x1 * x1;</div>
|
|
<div class="line"><a id="l00360" name="l00360"></a><span class="lineno"> 360</span> X[i][5] = x2 * x2;</div>
|
|
<div class="line"><a id="l00361" name="l00361"></a><span class="lineno"> 361</span> Y[i] = (x0 * x0 + x1 * x1 + x2 * x2) <= 1 ? 1 : -1;</div>
|
|
<div class="line"><a id="l00362" name="l00362"></a><span class="lineno"> 362</span> }</div>
|
|
<div class="line"><a id="l00363" name="l00363"></a><span class="lineno"> 363</span> </div>
|
|
<div class="line"><a id="l00364" name="l00364"></a><span class="lineno"> 364</span> printf(<span class="stringliteral">"------- Test 3 -------\n"</span>);</div>
|
|
<div class="line"><a id="l00365" name="l00365"></a><span class="lineno"> 365</span> printf(<span class="stringliteral">"Model before fit: %s\n"</span>, <a class="code hl_function" href="../../da/d2a/group__adaline.html#gadb1a7acb555ad62daaf3806bf2fed036">adaline_get_weights_str</a>(&ada));</div>
|
|
<div class="line"><a id="l00366" name="l00366"></a><span class="lineno"> 366</span> </div>
|
|
<div class="line"><a id="l00367" name="l00367"></a><span class="lineno"> 367</span> <a class="code hl_function" href="../../da/d2a/group__adaline.html#gaa52120912e32d2893fe1c6d78da5befd">adaline_fit</a>(&ada, X, Y, N);</div>
|
|
<div class="line"><a id="l00368" name="l00368"></a><span class="lineno"> 368</span> printf(<span class="stringliteral">"Model after fit: %s\n"</span>, <a class="code hl_function" href="../../da/d2a/group__adaline.html#gadb1a7acb555ad62daaf3806bf2fed036">adaline_get_weights_str</a>(&ada));</div>
|
|
<div class="line"><a id="l00369" name="l00369"></a><span class="lineno"> 369</span> </div>
|
|
<div class="line"><a id="l00370" name="l00370"></a><span class="lineno"> 370</span> <span class="keywordtype">int</span> N_test_cases = 5;</div>
|
|
<div class="line"><a id="l00371" name="l00371"></a><span class="lineno"> 371</span> <span class="keywordtype">double</span> test_x[6];</div>
|
|
<div class="line"><a id="l00372" name="l00372"></a><span class="lineno"> 372</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < N_test_cases; i++)</div>
|
|
<div class="line"><a id="l00373" name="l00373"></a><span class="lineno"> 373</span> {</div>
|
|
<div class="line"><a id="l00374" name="l00374"></a><span class="lineno"> 374</span> <span class="keywordtype">double</span> x0 = ((rand() % <a class="code hl_struct" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a id="l00375" name="l00375"></a><span class="lineno"> 375</span> <span class="keywordtype">double</span> x1 = ((rand() % <a class="code hl_struct" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a id="l00376" name="l00376"></a><span class="lineno"> 376</span> <span class="keywordtype">double</span> x2 = ((rand() % <a class="code hl_struct" href="../../df/dea/structdata.html">range</a>) - range2) / 100.f;</div>
|
|
<div class="line"><a id="l00377" name="l00377"></a><span class="lineno"> 377</span> test_x[0] = x0;</div>
|
|
<div class="line"><a id="l00378" name="l00378"></a><span class="lineno"> 378</span> test_x[1] = x1;</div>
|
|
<div class="line"><a id="l00379" name="l00379"></a><span class="lineno"> 379</span> test_x[2] = x2;</div>
|
|
<div class="line"><a id="l00380" name="l00380"></a><span class="lineno"> 380</span> test_x[3] = x0 * x0;</div>
|
|
<div class="line"><a id="l00381" name="l00381"></a><span class="lineno"> 381</span> test_x[4] = x1 * x1;</div>
|
|
<div class="line"><a id="l00382" name="l00382"></a><span class="lineno"> 382</span> test_x[5] = x2 * x2;</div>
|
|
<div class="line"><a id="l00383" name="l00383"></a><span class="lineno"> 383</span> <span class="keywordtype">int</span> pred = <a class="code hl_function" href="../../da/d2a/group__adaline.html#gac70b578aee679005fd336073969c3d94">adaline_predict</a>(&ada, test_x, NULL);</div>
|
|
<div class="line"><a id="l00384" name="l00384"></a><span class="lineno"> 384</span> printf(<span class="stringliteral">"Predict for x=(% 3.2f,% 3.2f): % d\n"</span>, x0, x1, pred);</div>
|
|
<div class="line"><a id="l00385" name="l00385"></a><span class="lineno"> 385</span> </div>
|
|
<div class="line"><a id="l00386" name="l00386"></a><span class="lineno"> 386</span> <span class="keywordtype">int</span> expected_val = (x0 * x0 + x1 * x1 + x2 * x2) <= 1 ? 1 : -1;</div>
|
|
<div class="line"><a id="l00387" name="l00387"></a><span class="lineno"> 387</span> assert(pred == expected_val);</div>
|
|
<div class="line"><a id="l00388" name="l00388"></a><span class="lineno"> 388</span> printf(<span class="stringliteral">" ...passed\n"</span>);</div>
|
|
<div class="line"><a id="l00389" name="l00389"></a><span class="lineno"> 389</span> }</div>
|
|
<div class="line"><a id="l00390" name="l00390"></a><span class="lineno"> 390</span> </div>
|
|
<div class="line"><a id="l00391" name="l00391"></a><span class="lineno"> 391</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < N; i++) <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(X[i]);</div>
|
|
<div class="line"><a id="l00392" name="l00392"></a><span class="lineno"> 392</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(X);</div>
|
|
<div class="line"><a id="l00393" name="l00393"></a><span class="lineno"> 393</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(Y);</div>
|
|
<div class="line"><a id="l00394" name="l00394"></a><span class="lineno"> 394</span> <a class="code hl_function" href="../../da/d2a/group__adaline.html#ga6f35caa3084772cc126ac7b20f67f665">delete_adaline</a>(&ada);</div>
|
|
<div class="line"><a id="l00395" name="l00395"></a><span class="lineno"> 395</span>}</div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><div class="zoom"><iframe scrolling="no" frameborder="0" src="../../dd/d8c/adaline__learning_8c_a3f37b9f073f7e57fd0b39d70718af1b1_cgraph.svg" width="100%" height="480"><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>
|
|
</div><!-- contents -->
|
|
</div><!-- doc-content -->
|
|
<!-- 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="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.2 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|