mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
562 lines
35 KiB
HTML
562 lines
35 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" lang="en-US">
|
|
<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.5"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>Algorithms_in_C: Kohonen SOM trace/chain algorithm</title>
|
|
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../jquery.js"></script>
|
|
<script type="text/javascript" src="../../dynsections.js"></script>
|
|
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../resize.js"></script>
|
|
<script type="text/javascript" src="../../navtreedata.js"></script>
|
|
<script type="text/javascript" src="../../navtree.js"></script>
|
|
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../search/searchdata.js"></script>
|
|
<script type="text/javascript" src="../../search/search.js"></script>
|
|
<script type="text/x-mathjax-config">
|
|
MathJax.Hub.Config({
|
|
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
|
|
jax: ["input/TeX","output/HTML-CSS"],
|
|
});
|
|
</script>
|
|
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
|
|
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
|
|
</head>
|
|
<body>
|
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
|
<div id="titlearea">
|
|
<table cellspacing="0" cellpadding="0">
|
|
<tbody>
|
|
<tr id="projectrow">
|
|
<td id="projectalign">
|
|
<div id="projectname">Algorithms_in_C<span id="projectnumber"> 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.5 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
var searchBox = new SearchBox("searchBox", "../../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('d0/dcb/group__kohonen__1d.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">
|
|
<div id="MSearchResults">
|
|
<div class="SRPage">
|
|
<div id="SRIndex">
|
|
<div id="SRResults"></div>
|
|
<div class="SRStatus" id="Loading">Loading...</div>
|
|
<div class="SRStatus" id="Searching">Searching...</div>
|
|
<div class="SRStatus" id="NoMatches">No Matches</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="header">
|
|
<div class="summary">
|
|
<a href="#define-members">Macros</a> |
|
|
<a href="#func-members">Functions</a> </div>
|
|
<div class="headertitle"><div class="title">Kohonen SOM trace/chain algorithm<div class="ingroups"><a class="el" href="../../d9/d66/group__machine__learning.html">Machine learning algorithms</a></div></div></div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
<div class="dynheader">
|
|
Collaboration diagram for Kohonen SOM trace/chain algorithm:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d0/dcb/group__kohonen__1d.svg" width="418" height="51"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</div>
|
|
<table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="define-members" name="define-members"></a>
|
|
Macros</h2></td></tr>
|
|
<tr class="memitem:gaffe776513b24d84b39af8ab0930fef7f"><td class="memItemLeft" align="right" valign="top"><a id="gaffe776513b24d84b39af8ab0930fef7f" name="gaffe776513b24d84b39af8ab0930fef7f"></a>
|
|
#define </td><td class="memItemRight" valign="bottom"><b>max</b>(a, b)   (((a) > (b)) ? (a) : (b))</td></tr>
|
|
<tr class="memdesc:gaffe776513b24d84b39af8ab0930fef7f"><td class="mdescLeft"> </td><td class="mdescRight">shorthand for maximum value <br /></td></tr>
|
|
<tr class="separator:gaffe776513b24d84b39af8ab0930fef7f"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:gac6afabdc09a49a433ee19d8a9486056d"><td class="memItemLeft" align="right" valign="top"><a id="gac6afabdc09a49a433ee19d8a9486056d" name="gac6afabdc09a49a433ee19d8a9486056d"></a>
|
|
#define </td><td class="memItemRight" valign="bottom"><b>min</b>(a, b)   (((a) < (b)) ? (a) : (b))</td></tr>
|
|
<tr class="memdesc:gac6afabdc09a49a433ee19d8a9486056d"><td class="mdescLeft"> </td><td class="mdescRight">shorthand for minimum value <br /></td></tr>
|
|
<tr class="separator:gac6afabdc09a49a433ee19d8a9486056d"><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:gaf5ce14f026d6d231bef29161bac2b485"><td class="memItemLeft" align="right" valign="top">double </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/dcb/group__kohonen__1d.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a> (double a, double b)</td></tr>
|
|
<tr class="memdesc:gaf5ce14f026d6d231bef29161bac2b485"><td class="mdescLeft"> </td><td class="mdescRight">Helper function to generate a random number in a given interval. <a href="../../d0/dcb/group__kohonen__1d.html#gaf5ce14f026d6d231bef29161bac2b485">More...</a><br /></td></tr>
|
|
<tr class="separator:gaf5ce14f026d6d231bef29161bac2b485"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:ga7b84b14e60f47812b581d1f93057c85a"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/dcb/group__kohonen__1d.html#ga7b84b14e60f47812b581d1f93057c85a">save_nd_data</a> (const char *fname, double **X, int num_points, int num_features)</td></tr>
|
|
<tr class="memdesc:ga7b84b14e60f47812b581d1f93057c85a"><td class="mdescLeft"> </td><td class="mdescRight">Save a given n-dimensional data martix to file. <a href="../../d0/dcb/group__kohonen__1d.html#ga7b84b14e60f47812b581d1f93057c85a">More...</a><br /></td></tr>
|
|
<tr class="separator:ga7b84b14e60f47812b581d1f93057c85a"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:ga4a57a413a3cef286a7da6d4666575586"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/dcb/group__kohonen__1d.html#ga4a57a413a3cef286a7da6d4666575586">kohonen_get_min_1d</a> (double const *X, int N, double *val, int *idx)</td></tr>
|
|
<tr class="memdesc:ga4a57a413a3cef286a7da6d4666575586"><td class="mdescLeft"> </td><td class="mdescRight">Get minimum value and index of the value in a vector. <a href="../../d0/dcb/group__kohonen__1d.html#ga4a57a413a3cef286a7da6d4666575586">More...</a><br /></td></tr>
|
|
<tr class="separator:ga4a57a413a3cef286a7da6d4666575586"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:gae334493a0917a24736fe5ba82aa6f81f"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/dcb/group__kohonen__1d.html#gae334493a0917a24736fe5ba82aa6f81f">kohonen_update_weights</a> (double const *x, double *const *W, double *D, int num_out, int num_features, double alpha, int R)</td></tr>
|
|
<tr class="memdesc:gae334493a0917a24736fe5ba82aa6f81f"><td class="mdescLeft"> </td><td class="mdescRight">Update weights of the SOM using Kohonen algorithm. <a href="../../d0/dcb/group__kohonen__1d.html#gae334493a0917a24736fe5ba82aa6f81f">More...</a><br /></td></tr>
|
|
<tr class="separator:gae334493a0917a24736fe5ba82aa6f81f"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:gaeaeffbff2be4d5d15b0d4f10f846abde"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/dcb/group__kohonen__1d.html#gaeaeffbff2be4d5d15b0d4f10f846abde">kohonen_som_tracer</a> (double **X, double *const *W, int num_samples, int num_features, int num_out, double alpha_min)</td></tr>
|
|
<tr class="memdesc:gaeaeffbff2be4d5d15b0d4f10f846abde"><td class="mdescLeft"> </td><td class="mdescRight">Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given datset. <a href="../../d0/dcb/group__kohonen__1d.html#gaeaeffbff2be4d5d15b0d4f10f846abde">More...</a><br /></td></tr>
|
|
<tr class="separator:gaeaeffbff2be4d5d15b0d4f10f846abde"><td class="memSeparator" colspan="2"> </td></tr>
|
|
</table>
|
|
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
|
|
<h2 class="groupheader">Function Documentation</h2>
|
|
<a id="gaf5ce14f026d6d231bef29161bac2b485" name="gaf5ce14f026d6d231bef29161bac2b485"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#gaf5ce14f026d6d231bef29161bac2b485">◆ </a></span>_random()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">double _random </td>
|
|
<td>(</td>
|
|
<td class="paramtype">double </td>
|
|
<td class="paramname"><em>a</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double </td>
|
|
<td class="paramname"><em>b</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Helper function to generate a random number in a given interval. </p>
|
|
<p ><br />
|
|
Steps:</p><ol type="1">
|
|
<li><code>r1 = rand() % 100</code> gets a random number between 0 and 99</li>
|
|
<li><code>r2 = r1 / 100</code> converts random number to be between 0 and 0.99</li>
|
|
<li>scale and offset the random number to given range of \([a,b)\) <p class="formulaDsp">
|
|
\[
|
|
y = (b - a) \times \frac{\text{(random number between 0 and RAND_MAX)} \;
|
|
\text{mod}\; 100}{100} + a \]
|
|
</p>
|
|
</li>
|
|
</ol>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramname">a</td><td>lower limit </td></tr>
|
|
<tr><td class="paramname">b</td><td>upper limit </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<dl class="section return"><dt>Returns</dt><dd>random number in the range \([a,b)\) </dd></dl>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 55</span>{</div>
|
|
<div class="line"><span class="lineno"> 56</span> <span class="keywordtype">int</span> r = rand() % 100;</div>
|
|
<div class="line"><span class="lineno"> 57</span> <span class="keywordflow">return</span> ((b - a) * r / 100.f) + a;</div>
|
|
<div class="line"><span class="lineno"> 58</span>}</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="ga4a57a413a3cef286a7da6d4666575586" name="ga4a57a413a3cef286a7da6d4666575586"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ga4a57a413a3cef286a7da6d4666575586">◆ </a></span>kohonen_get_min_1d()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void kohonen_get_min_1d </td>
|
|
<td>(</td>
|
|
<td class="paramtype">double const * </td>
|
|
<td class="paramname"><em>X</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>N</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double * </td>
|
|
<td class="paramname"><em>val</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int * </td>
|
|
<td class="paramname"><em>idx</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Get minimum value and index of the value in a vector. </p>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">X</td><td>vector to search </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">N</td><td>number of points in the vector </td></tr>
|
|
<tr><td class="paramdir">[out]</td><td class="paramname">val</td><td>minimum value found </td></tr>
|
|
<tr><td class="paramdir">[out]</td><td class="paramname">idx</td><td>index where minimum value was found </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 105</span>{</div>
|
|
<div class="line"><span class="lineno"> 106</span> val[0] = INFINITY; <span class="comment">// initial min value</span></div>
|
|
<div class="line"><span class="lineno"> 107</span> </div>
|
|
<div class="line"><span class="lineno"> 108</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < N; i++) <span class="comment">// check each value</span></div>
|
|
<div class="line"><span class="lineno"> 109</span> {</div>
|
|
<div class="line"><span class="lineno"> 110</span> <span class="keywordflow">if</span> (X[i] < val[0]) <span class="comment">// if a lower value is found</span></div>
|
|
<div class="line"><span class="lineno"> 111</span> { <span class="comment">// save the value and its index</span></div>
|
|
<div class="line"><span class="lineno"> 112</span> idx[0] = i;</div>
|
|
<div class="line"><span class="lineno"> 113</span> val[0] = X[i];</div>
|
|
<div class="line"><span class="lineno"> 114</span> }</div>
|
|
<div class="line"><span class="lineno"> 115</span> }</div>
|
|
<div class="line"><span class="lineno"> 116</span>}</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="gaeaeffbff2be4d5d15b0d4f10f846abde" name="gaeaeffbff2be4d5d15b0d4f10f846abde"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#gaeaeffbff2be4d5d15b0d4f10f846abde">◆ </a></span>kohonen_som_tracer()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void kohonen_som_tracer </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">double *const * </td>
|
|
<td class="paramname"><em>W</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>num_samples</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>num_features</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>num_out</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double </td>
|
|
<td class="paramname"><em>alpha_min</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given datset. </p>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">X</td><td>data set </td></tr>
|
|
<tr><td class="paramdir">[in,out]</td><td class="paramname">W</td><td>weights matrix </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">num_samples</td><td>number of output points </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">num_features</td><td>number of features per input sample </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">num_out</td><td>number of output points </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">alpha_min</td><td>terminal value of alpha </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 181</span>{</div>
|
|
<div class="line"><span class="lineno"> 182</span> <span class="keywordtype">int</span> R = num_out >> 2, iter = 0;</div>
|
|
<div class="line"><span class="lineno"> 183</span> <span class="keywordtype">double</span> alpha = 1.f;</div>
|
|
<div class="line"><span class="lineno"> 184</span> <span class="keywordtype">double</span> *D = (<span class="keywordtype">double</span> *)<a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a725f50ecaf1959d96de79b36b4788fee">malloc</a>(num_out * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
|
|
<div class="line"><span class="lineno"> 185</span> </div>
|
|
<div class="line"><span class="lineno"> 186</span> <span class="comment">// Loop alpha from 1 to alpha_min</span></div>
|
|
<div class="line"><span class="lineno"> 187</span> <span class="keywordflow">for</span> (; alpha > alpha_min; alpha -= 0.01, iter++)</div>
|
|
<div class="line"><span class="lineno"> 188</span> {</div>
|
|
<div class="line"><span class="lineno"> 189</span> <span class="comment">// Loop for each sample pattern in the data set</span></div>
|
|
<div class="line"><span class="lineno"> 190</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> sample = 0; sample < num_samples; sample++)</div>
|
|
<div class="line"><span class="lineno"> 191</span> {</div>
|
|
<div class="line"><span class="lineno"> 192</span> <span class="keyword">const</span> <span class="keywordtype">double</span> *x = X[sample];</div>
|
|
<div class="line"><span class="lineno"> 193</span> <span class="comment">// update weights for the current input pattern sample</span></div>
|
|
<div class="line"><span class="lineno"> 194</span> <a class="code hl_function" href="../../d0/dcb/group__kohonen__1d.html#gae334493a0917a24736fe5ba82aa6f81f">kohonen_update_weights</a>(x, W, D, num_out, num_features, alpha, R);</div>
|
|
<div class="line"><span class="lineno"> 195</span> }</div>
|
|
<div class="line"><span class="lineno"> 196</span> </div>
|
|
<div class="line"><span class="lineno"> 197</span> <span class="comment">// every 10th iteration, reduce the neighborhood range</span></div>
|
|
<div class="line"><span class="lineno"> 198</span> <span class="keywordflow">if</span> (iter % 10 == 0 && R > 1)</div>
|
|
<div class="line"><span class="lineno"> 199</span> R--;</div>
|
|
<div class="line"><span class="lineno"> 200</span> }</div>
|
|
<div class="line"><span class="lineno"> 201</span> </div>
|
|
<div class="line"><span class="lineno"> 202</span> <a class="code hl_define" href="../../d2/ddd/malloc__dbg_8h.html#a9cc854374299a1dd933bf62029761768">free</a>(D);</div>
|
|
<div class="line"><span class="lineno"> 203</span>}</div>
|
|
<div class="ttc" id="agroup__kohonen__1d_html_gae334493a0917a24736fe5ba82aa6f81f"><div class="ttname"><a href="../../d0/dcb/group__kohonen__1d.html#gae334493a0917a24736fe5ba82aa6f81f">kohonen_update_weights</a></div><div class="ttdeci">void kohonen_update_weights(double const *x, double *const *W, double *D, int num_out, int num_features, double alpha, int R)</div><div class="ttdoc">Update weights of the SOM using Kohonen algorithm.</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:129</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><!-- 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="../../d0/dcb/group__kohonen__1d_gaeaeffbff2be4d5d15b0d4f10f846abde_cgraph.svg" width="564" height="36"><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="gae334493a0917a24736fe5ba82aa6f81f" name="gae334493a0917a24736fe5ba82aa6f81f"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#gae334493a0917a24736fe5ba82aa6f81f">◆ </a></span>kohonen_update_weights()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void kohonen_update_weights </td>
|
|
<td>(</td>
|
|
<td class="paramtype">double const * </td>
|
|
<td class="paramname"><em>x</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double *const * </td>
|
|
<td class="paramname"><em>W</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double * </td>
|
|
<td class="paramname"><em>D</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>num_out</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>num_features</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double </td>
|
|
<td class="paramname"><em>alpha</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>R</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Update weights of the SOM using Kohonen algorithm. </p>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">x</td><td>data point </td></tr>
|
|
<tr><td class="paramdir">[in,out]</td><td class="paramname">W</td><td>weights matrix </td></tr>
|
|
<tr><td class="paramdir">[in,out]</td><td class="paramname">D</td><td>temporary vector to store distances </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">num_out</td><td>number of output points </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">num_features</td><td>number of features per input sample </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">alpha</td><td>learning rate \(0<\alpha\le1\) </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">R</td><td>neighborhood range </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 131</span>{</div>
|
|
<div class="line"><span class="lineno"> 132</span> <span class="keywordtype">int</span> j, k;</div>
|
|
<div class="line"><span class="lineno"> 133</span> </div>
|
|
<div class="line"><span class="lineno"> 134</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><span class="lineno"> 135</span><span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><span class="lineno"> 136</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 137</span> <span class="comment">// step 1: for each output point</span></div>
|
|
<div class="line"><span class="lineno"> 138</span> <span class="keywordflow">for</span> (j = 0; j < num_out; j++)</div>
|
|
<div class="line"><span class="lineno"> 139</span> {</div>
|
|
<div class="line"><span class="lineno"> 140</span> D[j] = 0.f;</div>
|
|
<div class="line"><span class="lineno"> 141</span> <span class="comment">// compute Euclidian distance of each output</span></div>
|
|
<div class="line"><span class="lineno"> 142</span> <span class="comment">// point from the current sample</span></div>
|
|
<div class="line"><span class="lineno"> 143</span> <span class="keywordflow">for</span> (k = 0; k < num_features; k++)</div>
|
|
<div class="line"><span class="lineno"> 144</span> D[j] += (W[j][k] - x[k]) * (W[j][k] - x[k]);</div>
|
|
<div class="line"><span class="lineno"> 145</span> }</div>
|
|
<div class="line"><span class="lineno"> 146</span> </div>
|
|
<div class="line"><span class="lineno"> 147</span> <span class="comment">// step 2: get closest node i.e., node with smallest Euclidian distance to</span></div>
|
|
<div class="line"><span class="lineno"> 148</span> <span class="comment">// the current pattern</span></div>
|
|
<div class="line"><span class="lineno"> 149</span> <span class="keywordtype">int</span> d_min_idx;</div>
|
|
<div class="line"><span class="lineno"> 150</span> <span class="keywordtype">double</span> d_min;</div>
|
|
<div class="line"><span class="lineno"> 151</span> <a class="code hl_function" href="../../d0/dcb/group__kohonen__1d.html#ga4a57a413a3cef286a7da6d4666575586">kohonen_get_min_1d</a>(D, num_out, &d_min, &d_min_idx);</div>
|
|
<div class="line"><span class="lineno"> 152</span> </div>
|
|
<div class="line"><span class="lineno"> 153</span> <span class="comment">// step 3a: get the neighborhood range</span></div>
|
|
<div class="line"><span class="lineno"> 154</span> <span class="keywordtype">int</span> from_node = <a class="code hl_define" href="../../d0/dcb/group__kohonen__1d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(0, d_min_idx - R);</div>
|
|
<div class="line"><span class="lineno"> 155</span> <span class="keywordtype">int</span> to_node = <a class="code hl_define" href="../../d0/dcb/group__kohonen__1d.html#gac6afabdc09a49a433ee19d8a9486056d">min</a>(num_out, d_min_idx + R + 1);</div>
|
|
<div class="line"><span class="lineno"> 156</span> </div>
|
|
<div class="line"><span class="lineno"> 157</span> <span class="comment">// step 3b: update the weights of nodes in the</span></div>
|
|
<div class="line"><span class="lineno"> 158</span> <span class="comment">// neighborhood</span></div>
|
|
<div class="line"><span class="lineno"> 159</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><span class="lineno"> 160</span><span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><span class="lineno"> 161</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 162</span> <span class="keywordflow">for</span> (j = from_node; j < to_node; j++)</div>
|
|
<div class="line"><span class="lineno"> 163</span> <span class="keywordflow">for</span> (k = 0; k < num_features; k++)</div>
|
|
<div class="line"><span class="lineno"> 164</span> <span class="comment">// update weights of nodes in the neighborhood</span></div>
|
|
<div class="line"><span class="lineno"> 165</span> W[j][k] += alpha * (x[k] - W[j][k]);</div>
|
|
<div class="line"><span class="lineno"> 166</span>}</div>
|
|
<div class="ttc" id="agroup__kohonen__1d_html_ga4a57a413a3cef286a7da6d4666575586"><div class="ttname"><a href="../../d0/dcb/group__kohonen__1d.html#ga4a57a413a3cef286a7da6d4666575586">kohonen_get_min_1d</a></div><div class="ttdeci">void kohonen_get_min_1d(double const *X, int N, double *val, int *idx)</div><div class="ttdoc">Get minimum value and index of the value in a vector.</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:104</div></div>
|
|
<div class="ttc" id="agroup__kohonen__1d_html_gac6afabdc09a49a433ee19d8a9486056d"><div class="ttname"><a href="../../d0/dcb/group__kohonen__1d.html#gac6afabdc09a49a433ee19d8a9486056d">min</a></div><div class="ttdeci">#define min(a, b)</div><div class="ttdoc">shorthand for minimum value</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:36</div></div>
|
|
<div class="ttc" id="agroup__kohonen__1d_html_gaffe776513b24d84b39af8ab0930fef7f"><div class="ttname"><a href="../../d0/dcb/group__kohonen__1d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a></div><div class="ttdeci">#define max(a, b)</div><div class="ttdoc">shorthand for maximum value</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:32</div></div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d0/dcb/group__kohonen__1d_gae334493a0917a24736fe5ba82aa6f81f_cgraph.svg" width="374" height="36"><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="ga7b84b14e60f47812b581d1f93057c85a" name="ga7b84b14e60f47812b581d1f93057c85a"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ga7b84b14e60f47812b581d1f93057c85a">◆ </a></span>save_nd_data()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">int save_nd_data </td>
|
|
<td>(</td>
|
|
<td class="paramtype">const char * </td>
|
|
<td class="paramname"><em>fname</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">int </td>
|
|
<td class="paramname"><em>num_points</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>num_features</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Save a given n-dimensional data martix to file. </p>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">fname</td><td>filename to save in (gets overwriten without confirmation) </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">X</td><td>matrix to save </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">num_points</td><td>rows in the matrix = number of points </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">num_features</td><td>columns in the matrix = dimensions of points </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<dl class="section return"><dt>Returns</dt><dd>0 if all ok </dd>
|
|
<dd>
|
|
-1 if file creation failed </dd></dl>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 72</span>{</div>
|
|
<div class="line"><span class="lineno"> 73</span> FILE *fp = fopen(fname, <span class="stringliteral">"wt"</span>);</div>
|
|
<div class="line"><span class="lineno"> 74</span> <span class="keywordflow">if</span> (!fp) <span class="comment">// error with fopen</span></div>
|
|
<div class="line"><span class="lineno"> 75</span> {</div>
|
|
<div class="line"><span class="lineno"> 76</span> <span class="keywordtype">char</span> msg[120];</div>
|
|
<div class="line"><span class="lineno"> 77</span> sprintf(msg, <span class="stringliteral">"File error (%s): "</span>, fname);</div>
|
|
<div class="line"><span class="lineno"> 78</span> perror(msg);</div>
|
|
<div class="line"><span class="lineno"> 79</span> <span class="keywordflow">return</span> -1;</div>
|
|
<div class="line"><span class="lineno"> 80</span> }</div>
|
|
<div class="line"><span class="lineno"> 81</span> </div>
|
|
<div class="line"><span class="lineno"> 82</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < num_points; i++) <span class="comment">// for each point in the array</span></div>
|
|
<div class="line"><span class="lineno"> 83</span> {</div>
|
|
<div class="line"><span class="lineno"> 84</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j < num_features; j++) <span class="comment">// for each feature in the array</span></div>
|
|
<div class="line"><span class="lineno"> 85</span> {</div>
|
|
<div class="line"><span class="lineno"> 86</span> fprintf(fp, <span class="stringliteral">"%.4g"</span>, X[i][j]); <span class="comment">// print the feature value</span></div>
|
|
<div class="line"><span class="lineno"> 87</span> <span class="keywordflow">if</span> (j < num_features - 1) <span class="comment">// if not the last feature</span></div>
|
|
<div class="line"><span class="lineno"> 88</span> fprintf(fp, <span class="stringliteral">","</span>); <span class="comment">// suffix comma</span></div>
|
|
<div class="line"><span class="lineno"> 89</span> }</div>
|
|
<div class="line"><span class="lineno"> 90</span> <span class="keywordflow">if</span> (i < num_points - 1) <span class="comment">// if not the last row</span></div>
|
|
<div class="line"><span class="lineno"> 91</span> fprintf(fp, <span class="stringliteral">"\n"</span>); <span class="comment">// start a new line</span></div>
|
|
<div class="line"><span class="lineno"> 92</span> }</div>
|
|
<div class="line"><span class="lineno"> 93</span> fclose(fp);</div>
|
|
<div class="line"><span class="lineno"> 94</span> <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><span class="lineno"> 95</span>}</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
</div><!-- contents -->
|
|
</div><!-- doc-content -->
|
|
<!-- start footer part -->
|
|
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
|
<ul>
|
|
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.5 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|