mirror of
https://github.com/TheAlgorithms/C
synced 2024-12-12 22:57:06 +03:00
783 lines
62 KiB
HTML
783 lines
62 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
|
|
<meta name="generator" content="Doxygen 1.8.18"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>Algorithms_in_C: Kohonen SOM topology algorithm</title>
|
|
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../jquery.js"></script>
|
|
<script type="text/javascript" src="../../dynsections.js"></script>
|
|
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../resize.js"></script>
|
|
<script type="text/javascript" src="../../navtreedata.js"></script>
|
|
<script type="text/javascript" src="../../navtree.js"></script>
|
|
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../search/searchdata.js"></script>
|
|
<script type="text/javascript" src="../../search/search.js"></script>
|
|
<script type="text/x-mathjax-config">
|
|
MathJax.Hub.Config({
|
|
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
|
|
jax: ["input/TeX","output/HTML-CSS"],
|
|
});
|
|
</script>
|
|
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
|
|
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
|
|
</head>
|
|
<body>
|
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
|
<div id="titlearea">
|
|
<table cellspacing="0" cellpadding="0">
|
|
<tbody>
|
|
<tr style="height: 56px;">
|
|
<td id="projectalign" style="padding-left: 0.5em;">
|
|
<div id="projectname">Algorithms_in_C
|
|
 <span id="projectnumber">1.0.0</span>
|
|
</div>
|
|
<div id="projectbrief">Set of algorithms implemented in C.</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.8.18 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
|
|
/* @license-end */
|
|
</script>
|
|
<script type="text/javascript" src="../../menudata.js"></script>
|
|
<script type="text/javascript" src="../../menu.js"></script>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
$(function() {
|
|
initMenu('../../',true,false,'search.php','Search');
|
|
$(document).ready(function() { init_search(); });
|
|
});
|
|
/* @license-end */</script>
|
|
<div id="main-nav"></div>
|
|
</div><!-- top -->
|
|
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
|
<div id="nav-tree">
|
|
<div id="nav-tree-contents">
|
|
<div id="nav-sync" class="sync"></div>
|
|
</div>
|
|
</div>
|
|
<div id="splitbar" style="-moz-user-select:none;"
|
|
class="ui-resizable-handle">
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
$(document).ready(function(){initNavTree('d1/d6b/group__kohonen__2d.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">Kohonen SOM topology 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 topology algorithm:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d1/d6b/group__kohonen__2d.svg" width="403" 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 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="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a></td></tr>
|
|
<tr class="memdesc:"><td class="mdescLeft"> </td><td class="mdescRight">to store info regarding 3D arrays <a href="../../d8/db8/structkohonen__array__3d.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 name="define-members"></a>
|
|
Macros</h2></td></tr>
|
|
<tr class="memitem:gaffe776513b24d84b39af8ab0930fef7f"><td class="memItemLeft" align="right" valign="top"><a id="gaffe776513b24d84b39af8ab0930fef7f"></a>
|
|
#define </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(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"></a>
|
|
#define </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d6b/group__kohonen__2d.html#gac6afabdc09a49a433ee19d8a9486056d">min</a>(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 name="func-members"></a>
|
|
Functions</h2></td></tr>
|
|
<tr class="memitem:ga8df35f04c1762a01dcf108fa13b897d6"><td class="memItemLeft" align="right" valign="top">double * </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d6b/group__kohonen__2d.html#ga8df35f04c1762a01dcf108fa13b897d6">kohonen_data_3d</a> (const struct <a class="el" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> *arr, int x, int y, int z)</td></tr>
|
|
<tr class="memdesc:ga8df35f04c1762a01dcf108fa13b897d6"><td class="mdescLeft"> </td><td class="mdescRight">Function that returns the pointer to (x, y, z) ^th location in the linear 3D array given by: <a href="../../d1/d6b/group__kohonen__2d.html#ga8df35f04c1762a01dcf108fa13b897d6">More...</a><br /></td></tr>
|
|
<tr class="separator:ga8df35f04c1762a01dcf108fa13b897d6"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:gaf5ce14f026d6d231bef29161bac2b485"><td class="memItemLeft" align="right" valign="top">double </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d6b/group__kohonen__2d.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="../../d1/d6b/group__kohonen__2d.html#gaf5ce14f026d6d231bef29161bac2b485">More...</a><br /></td></tr>
|
|
<tr class="separator:gaf5ce14f026d6d231bef29161bac2b485"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:ga6824dc6d973eb3339af7aef5fea78b0c"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d6b/group__kohonen__2d.html#ga6824dc6d973eb3339af7aef5fea78b0c">save_2d_data</a> (const char *fname, double **X, int num_points, int num_features)</td></tr>
|
|
<tr class="memdesc:ga6824dc6d973eb3339af7aef5fea78b0c"><td class="mdescLeft"> </td><td class="mdescRight">Save a given n-dimensional data martix to file. <a href="../../d1/d6b/group__kohonen__2d.html#ga6824dc6d973eb3339af7aef5fea78b0c">More...</a><br /></td></tr>
|
|
<tr class="separator:ga6824dc6d973eb3339af7aef5fea78b0c"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:ga49d35f68f5d11d8ef6f8cce0d0e7bcba"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d6b/group__kohonen__2d.html#ga49d35f68f5d11d8ef6f8cce0d0e7bcba">save_u_matrix</a> (const char *fname, struct <a class="el" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> *W)</td></tr>
|
|
<tr class="memdesc:ga49d35f68f5d11d8ef6f8cce0d0e7bcba"><td class="mdescLeft"> </td><td class="mdescRight">Create the distance matrix or <a href="https://en.wikipedia.org/wiki/U-matrix">U-matrix</a> from the trained weights and save to disk. <a href="../../d1/d6b/group__kohonen__2d.html#ga49d35f68f5d11d8ef6f8cce0d0e7bcba">More...</a><br /></td></tr>
|
|
<tr class="separator:ga49d35f68f5d11d8ef6f8cce0d0e7bcba"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:gadc22d512c00a9f5799ee067f4fb90b4b"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d6b/group__kohonen__2d.html#gadc22d512c00a9f5799ee067f4fb90b4b">get_min_2d</a> (double **X, int N, double *val, int *x_idx, int *y_idx)</td></tr>
|
|
<tr class="memdesc:gadc22d512c00a9f5799ee067f4fb90b4b"><td class="mdescLeft"> </td><td class="mdescRight">Get minimum value and index of the value in a matrix. <a href="../../d1/d6b/group__kohonen__2d.html#gadc22d512c00a9f5799ee067f4fb90b4b">More...</a><br /></td></tr>
|
|
<tr class="separator:gadc22d512c00a9f5799ee067f4fb90b4b"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:ga83abb572c60d202e100595a989dfe123"><td class="memItemLeft" align="right" valign="top">double </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d6b/group__kohonen__2d.html#ga83abb572c60d202e100595a989dfe123">kohonen_update_weights</a> (const double *X, struct <a class="el" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> *W, double **D, int num_out, int num_features, double alpha, int R)</td></tr>
|
|
<tr class="memdesc:ga83abb572c60d202e100595a989dfe123"><td class="mdescLeft"> </td><td class="mdescRight">Update weights of the SOM using Kohonen algorithm. <a href="../../d1/d6b/group__kohonen__2d.html#ga83abb572c60d202e100595a989dfe123">More...</a><br /></td></tr>
|
|
<tr class="separator:ga83abb572c60d202e100595a989dfe123"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:gacb42eda8af6ebd6a141a34ab00a0b710"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d1/d6b/group__kohonen__2d.html#gacb42eda8af6ebd6a141a34ab00a0b710">kohonen_som</a> (double **X, struct <a class="el" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> *W, int num_samples, int num_features, int num_out, double alpha_min)</td></tr>
|
|
<tr class="memdesc:gacb42eda8af6ebd6a141a34ab00a0b710"><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="../../d1/d6b/group__kohonen__2d.html#gacb42eda8af6ebd6a141a34ab00a0b710">More...</a><br /></td></tr>
|
|
<tr class="separator:gacb42eda8af6ebd6a141a34ab00a0b710"><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"></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="paramdir">[in]</td><td class="paramname">a</td><td>lower limit </td></tr>
|
|
<tr><td class="paramdir">[in]</td><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"><a name="l00088"></a><span class="lineno"> 88</span> {</div>
|
|
<div class="line"><a name="l00089"></a><span class="lineno"> 89</span>  <span class="keywordflow">return</span> ((b - a) * (rand() % 100) / 100.f) + a;</div>
|
|
<div class="line"><a name="l00090"></a><span class="lineno"> 90</span> }</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="gadc22d512c00a9f5799ee067f4fb90b4b"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#gadc22d512c00a9f5799ee067f4fb90b4b">◆ </a></span>get_min_2d()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void get_min_2d </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>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>x_idx</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int * </td>
|
|
<td class="paramname"><em>y_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 matrix. </p>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">X</td><td>matrix 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">x_idx</td><td>x-index where minimum value was found </td></tr>
|
|
<tr><td class="paramdir">[out]</td><td class="paramname">y_idx</td><td>y-index where minimum value was found </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<div class="fragment"><div class="line"><a name="l00205"></a><span class="lineno"> 205</span> {</div>
|
|
<div class="line"><a name="l00206"></a><span class="lineno"> 206</span>  val[0] = INFINITY; <span class="comment">// initial min value</span></div>
|
|
<div class="line"><a name="l00207"></a><span class="lineno"> 207</span>  </div>
|
|
<div class="line"><a name="l00208"></a><span class="lineno"> 208</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < N; i++) <span class="comment">// traverse each x-index</span></div>
|
|
<div class="line"><a name="l00209"></a><span class="lineno"> 209</span>  {</div>
|
|
<div class="line"><a name="l00210"></a><span class="lineno"> 210</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j < N; j++) <span class="comment">// traverse each y-index</span></div>
|
|
<div class="line"><a name="l00211"></a><span class="lineno"> 211</span>  {</div>
|
|
<div class="line"><a name="l00212"></a><span class="lineno"> 212</span>  <span class="keywordflow">if</span> (X[i][j] < val[0]) <span class="comment">// if a lower value is found</span></div>
|
|
<div class="line"><a name="l00213"></a><span class="lineno"> 213</span>  { <span class="comment">// save the value and its index</span></div>
|
|
<div class="line"><a name="l00214"></a><span class="lineno"> 214</span>  x_idx[0] = i;</div>
|
|
<div class="line"><a name="l00215"></a><span class="lineno"> 215</span>  y_idx[0] = j;</div>
|
|
<div class="line"><a name="l00216"></a><span class="lineno"> 216</span>  val[0] = X[i][j];</div>
|
|
<div class="line"><a name="l00217"></a><span class="lineno"> 217</span>  }</div>
|
|
<div class="line"><a name="l00218"></a><span class="lineno"> 218</span>  }</div>
|
|
<div class="line"><a name="l00219"></a><span class="lineno"> 219</span>  }</div>
|
|
<div class="line"><a name="l00220"></a><span class="lineno"> 220</span> }</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="ga8df35f04c1762a01dcf108fa13b897d6"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ga8df35f04c1762a01dcf108fa13b897d6">◆ </a></span>kohonen_data_3d()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">double* kohonen_data_3d </td>
|
|
<td>(</td>
|
|
<td class="paramtype">const struct <a class="el" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> * </td>
|
|
<td class="paramname"><em>arr</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </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>y</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>z</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Function that returns the pointer to (x, y, z) ^th location in the linear 3D array given by: </p>
|
|
<p class="formulaDsp">
|
|
\[ X_{i,j,k} = i\times M\times N + j\times N + k \]
|
|
</p>
|
|
<p> where \(L\), \(M\) and \(N\) are the 3D matrix dimensions. </p><dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">arr</td><td>pointer to <a class="el" href="../../d8/db8/structkohonen__array__3d.html" title="to store info regarding 3D arrays">kohonen_array_3d</a> structure </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">x</td><td>first index </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">y</td><td>second index </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">z</td><td>third index </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<dl class="section return"><dt>Returns</dt><dd>pointer to (x,y,z)^th location of data </dd></dl>
|
|
<div class="fragment"><div class="line"><a name="l00068"></a><span class="lineno"> 68</span> {</div>
|
|
<div class="line"><a name="l00069"></a><span class="lineno"> 69</span>  <span class="keywordtype">int</span> offset = (x * arr-><a class="code" href="../../d8/db8/structkohonen__array__3d.html#a888d7e007b38c91c7933e12a9566af1d">dim2</a> * arr-><a class="code" href="../../d8/db8/structkohonen__array__3d.html#a160f14830bdfbbf9f422f382ee754dbf">dim3</a>) + (y * arr-><a class="code" href="../../d8/db8/structkohonen__array__3d.html#a160f14830bdfbbf9f422f382ee754dbf">dim3</a>) + z;</div>
|
|
<div class="line"><a name="l00070"></a><span class="lineno"> 70</span>  <span class="keywordflow">return</span> arr-><a class="code" href="../../d8/db8/structkohonen__array__3d.html#ad546baa2e81c6196d5f1dc0fe2e5bd59">data</a> + offset;</div>
|
|
<div class="line"><a name="l00071"></a><span class="lineno"> 71</span> }</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="gacb42eda8af6ebd6a141a34ab00a0b710"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#gacb42eda8af6ebd6a141a34ab00a0b710">◆ </a></span>kohonen_som()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void kohonen_som </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">struct <a class="el" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> * </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"><a name="l00316"></a><span class="lineno"> 316</span> {</div>
|
|
<div class="line"><a name="l00317"></a><span class="lineno"> 317</span>  <span class="keywordtype">int</span> R = num_out >> 2, iter = 0;</div>
|
|
<div class="line"><a name="l00318"></a><span class="lineno"> 318</span>  <span class="keywordtype">double</span> **D = (<span class="keywordtype">double</span> **)malloc(num_out * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
|
|
<div class="line"><a name="l00319"></a><span class="lineno"> 319</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < num_out; i++)</div>
|
|
<div class="line"><a name="l00320"></a><span class="lineno"> 320</span>  D[i] = (<span class="keywordtype">double</span> *)malloc(num_out * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
|
|
<div class="line"><a name="l00321"></a><span class="lineno"> 321</span>  </div>
|
|
<div class="line"><a name="l00322"></a><span class="lineno"> 322</span>  <span class="keywordtype">double</span> dmin = 1.f; <span class="comment">// average minimum distance of all samples</span></div>
|
|
<div class="line"><a name="l00323"></a><span class="lineno"> 323</span>  </div>
|
|
<div class="line"><a name="l00324"></a><span class="lineno"> 324</span>  <span class="comment">// Loop alpha from 1 to slpha_min</span></div>
|
|
<div class="line"><a name="l00325"></a><span class="lineno"> 325</span>  <span class="keywordflow">for</span> (<span class="keywordtype">double</span> alpha = 1.f; alpha > alpha_min && dmin > 1e-3;</div>
|
|
<div class="line"><a name="l00326"></a><span class="lineno"> 326</span>  alpha -= 0.001, iter++)</div>
|
|
<div class="line"><a name="l00327"></a><span class="lineno"> 327</span>  {</div>
|
|
<div class="line"><a name="l00328"></a><span class="lineno"> 328</span>  dmin = 0.f;</div>
|
|
<div class="line"><a name="l00329"></a><span class="lineno"> 329</span>  <span class="comment">// Loop for each sample pattern in the data set</span></div>
|
|
<div class="line"><a name="l00330"></a><span class="lineno"> 330</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> sample = 0; sample < num_samples; sample++)</div>
|
|
<div class="line"><a name="l00331"></a><span class="lineno"> 331</span>  {</div>
|
|
<div class="line"><a name="l00332"></a><span class="lineno"> 332</span>  <span class="comment">// update weights for the current input pattern sample</span></div>
|
|
<div class="line"><a name="l00333"></a><span class="lineno"> 333</span>  dmin += <a class="code" href="../../d1/d6b/group__kohonen__2d.html#ga83abb572c60d202e100595a989dfe123">kohonen_update_weights</a>(X[sample], W, D, num_out,</div>
|
|
<div class="line"><a name="l00334"></a><span class="lineno"> 334</span>  num_features, alpha, R);</div>
|
|
<div class="line"><a name="l00335"></a><span class="lineno"> 335</span>  }</div>
|
|
<div class="line"><a name="l00336"></a><span class="lineno"> 336</span>  </div>
|
|
<div class="line"><a name="l00337"></a><span class="lineno"> 337</span>  <span class="comment">// every 20th iteration, reduce the neighborhood range</span></div>
|
|
<div class="line"><a name="l00338"></a><span class="lineno"> 338</span>  <span class="keywordflow">if</span> (iter % 100 == 0 && R > 1)</div>
|
|
<div class="line"><a name="l00339"></a><span class="lineno"> 339</span>  R--;</div>
|
|
<div class="line"><a name="l00340"></a><span class="lineno"> 340</span>  </div>
|
|
<div class="line"><a name="l00341"></a><span class="lineno"> 341</span>  dmin /= num_samples;</div>
|
|
<div class="line"><a name="l00342"></a><span class="lineno"> 342</span>  printf(<span class="stringliteral">"iter: %5d\t alpha: %.4g\t R: %d\td_min: %.4g\r"</span>, iter, alpha, R,</div>
|
|
<div class="line"><a name="l00343"></a><span class="lineno"> 343</span>  dmin);</div>
|
|
<div class="line"><a name="l00344"></a><span class="lineno"> 344</span>  }</div>
|
|
<div class="line"><a name="l00345"></a><span class="lineno"> 345</span>  putchar(<span class="charliteral">'\n'</span>);</div>
|
|
<div class="line"><a name="l00346"></a><span class="lineno"> 346</span>  </div>
|
|
<div class="line"><a name="l00347"></a><span class="lineno"> 347</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < num_out; i++) free(D[i]);</div>
|
|
<div class="line"><a name="l00348"></a><span class="lineno"> 348</span>  free(D);</div>
|
|
<div class="line"><a name="l00349"></a><span class="lineno"> 349</span> }</div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d1/d6b/group__kohonen__2d_gacb42eda8af6ebd6a141a34ab00a0b710_cgraph.svg" width="503" height="88"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<a id="ga83abb572c60d202e100595a989dfe123"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ga83abb572c60d202e100595a989dfe123">◆ </a></span>kohonen_update_weights()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">double kohonen_update_weights </td>
|
|
<td>(</td>
|
|
<td class="paramtype">const double * </td>
|
|
<td class="paramname"><em>X</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">struct <a class="el" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> * </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>
|
|
<dl class="section return"><dt>Returns</dt><dd>minimum distance of sample and trained weights </dd></dl>
|
|
<div class="fragment"><div class="line"><a name="l00237"></a><span class="lineno"> 237</span> {</div>
|
|
<div class="line"><a name="l00238"></a><span class="lineno"> 238</span>  <span class="keywordtype">int</span> x, y, k;</div>
|
|
<div class="line"><a name="l00239"></a><span class="lineno"> 239</span>  <span class="keywordtype">double</span> d_min = 0.f;</div>
|
|
<div class="line"><a name="l00240"></a><span class="lineno"> 240</span>  </div>
|
|
<div class="line"><a name="l00241"></a><span class="lineno"> 241</span> <span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><a name="l00242"></a><span class="lineno"> 242</span> <span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><a name="l00243"></a><span class="lineno"> 243</span> <span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a name="l00244"></a><span class="lineno"> 244</span>  <span class="comment">// step 1: for each 2D output point</span></div>
|
|
<div class="line"><a name="l00245"></a><span class="lineno"> 245</span>  <span class="keywordflow">for</span> (x = 0; x < num_out; x++)</div>
|
|
<div class="line"><a name="l00246"></a><span class="lineno"> 246</span>  {</div>
|
|
<div class="line"><a name="l00247"></a><span class="lineno"> 247</span>  <span class="keywordflow">for</span> (y = 0; y < num_out; y++)</div>
|
|
<div class="line"><a name="l00248"></a><span class="lineno"> 248</span>  {</div>
|
|
<div class="line"><a name="l00249"></a><span class="lineno"> 249</span>  D[x][y] = 0.f;</div>
|
|
<div class="line"><a name="l00250"></a><span class="lineno"> 250</span>  <span class="comment">// compute Euclidian distance of each output</span></div>
|
|
<div class="line"><a name="l00251"></a><span class="lineno"> 251</span>  <span class="comment">// point from the current sample</span></div>
|
|
<div class="line"><a name="l00252"></a><span class="lineno"> 252</span>  <span class="keywordflow">for</span> (k = 0; k < num_features; k++)</div>
|
|
<div class="line"><a name="l00253"></a><span class="lineno"> 253</span>  {</div>
|
|
<div class="line"><a name="l00254"></a><span class="lineno"> 254</span>  <span class="keywordtype">double</span> *w = <a class="code" href="../../d1/d6b/group__kohonen__2d.html#ga8df35f04c1762a01dcf108fa13b897d6">kohonen_data_3d</a>(W, x, y, k);</div>
|
|
<div class="line"><a name="l00255"></a><span class="lineno"> 255</span>  D[x][y] += (w[0] - X[k]) * (w[0] - X[k]);</div>
|
|
<div class="line"><a name="l00256"></a><span class="lineno"> 256</span>  }</div>
|
|
<div class="line"><a name="l00257"></a><span class="lineno"> 257</span>  D[x][y] = sqrt(D[x][y]);</div>
|
|
<div class="line"><a name="l00258"></a><span class="lineno"> 258</span>  }</div>
|
|
<div class="line"><a name="l00259"></a><span class="lineno"> 259</span>  }</div>
|
|
<div class="line"><a name="l00260"></a><span class="lineno"> 260</span>  </div>
|
|
<div class="line"><a name="l00261"></a><span class="lineno"> 261</span>  <span class="comment">// step 2: get closest node i.e., node with smallest Euclidian distance to</span></div>
|
|
<div class="line"><a name="l00262"></a><span class="lineno"> 262</span>  <span class="comment">// the current pattern</span></div>
|
|
<div class="line"><a name="l00263"></a><span class="lineno"> 263</span>  <span class="keywordtype">int</span> d_min_x, d_min_y;</div>
|
|
<div class="line"><a name="l00264"></a><span class="lineno"> 264</span>  <a class="code" href="../../d1/d6b/group__kohonen__2d.html#gadc22d512c00a9f5799ee067f4fb90b4b">get_min_2d</a>(D, num_out, &d_min, &d_min_x, &d_min_y);</div>
|
|
<div class="line"><a name="l00265"></a><span class="lineno"> 265</span>  </div>
|
|
<div class="line"><a name="l00266"></a><span class="lineno"> 266</span>  <span class="comment">// step 3a: get the neighborhood range</span></div>
|
|
<div class="line"><a name="l00267"></a><span class="lineno"> 267</span>  <span class="keywordtype">int</span> from_x = <a class="code" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(0, d_min_x - R);</div>
|
|
<div class="line"><a name="l00268"></a><span class="lineno"> 268</span>  <span class="keywordtype">int</span> to_x = <a class="code" href="../../d1/d6b/group__kohonen__2d.html#gac6afabdc09a49a433ee19d8a9486056d">min</a>(num_out, d_min_x + R + 1);</div>
|
|
<div class="line"><a name="l00269"></a><span class="lineno"> 269</span>  <span class="keywordtype">int</span> from_y = <a class="code" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(0, d_min_y - R);</div>
|
|
<div class="line"><a name="l00270"></a><span class="lineno"> 270</span>  <span class="keywordtype">int</span> to_y = <a class="code" href="../../d1/d6b/group__kohonen__2d.html#gac6afabdc09a49a433ee19d8a9486056d">min</a>(num_out, d_min_y + R + 1);</div>
|
|
<div class="line"><a name="l00271"></a><span class="lineno"> 271</span>  </div>
|
|
<div class="line"><a name="l00272"></a><span class="lineno"> 272</span>  <span class="comment">// step 3b: update the weights of nodes in the</span></div>
|
|
<div class="line"><a name="l00273"></a><span class="lineno"> 273</span>  <span class="comment">// neighborhood</span></div>
|
|
<div class="line"><a name="l00274"></a><span class="lineno"> 274</span> <span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><a name="l00275"></a><span class="lineno"> 275</span> <span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><a name="l00276"></a><span class="lineno"> 276</span> <span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a name="l00277"></a><span class="lineno"> 277</span>  <span class="keywordflow">for</span> (x = from_x; x < to_x; x++)</div>
|
|
<div class="line"><a name="l00278"></a><span class="lineno"> 278</span>  {</div>
|
|
<div class="line"><a name="l00279"></a><span class="lineno"> 279</span>  <span class="keywordflow">for</span> (y = from_y; y < to_y; y++)</div>
|
|
<div class="line"><a name="l00280"></a><span class="lineno"> 280</span>  {</div>
|
|
<div class="line"><a name="l00281"></a><span class="lineno"> 281</span>  <span class="comment">/* you can enable the following normalization if needed.</span></div>
|
|
<div class="line"><a name="l00282"></a><span class="lineno"> 282</span> <span class="comment"> personally, I found it detrimental to convergence */</span></div>
|
|
<div class="line"><a name="l00283"></a><span class="lineno"> 283</span>  <span class="comment">// const double s2pi = sqrt(2.f * M_PI);</span></div>
|
|
<div class="line"><a name="l00284"></a><span class="lineno"> 284</span>  <span class="comment">// double normalize = 1.f / (alpha * s2pi);</span></div>
|
|
<div class="line"><a name="l00285"></a><span class="lineno"> 285</span>  </div>
|
|
<div class="line"><a name="l00286"></a><span class="lineno"> 286</span>  <span class="comment">/* apply scaling inversely proportional to distance from the</span></div>
|
|
<div class="line"><a name="l00287"></a><span class="lineno"> 287</span> <span class="comment"> current node */</span></div>
|
|
<div class="line"><a name="l00288"></a><span class="lineno"> 288</span>  <span class="keywordtype">double</span> d2 =</div>
|
|
<div class="line"><a name="l00289"></a><span class="lineno"> 289</span>  (d_min_x - x) * (d_min_x - x) + (d_min_y - y) * (d_min_y - y);</div>
|
|
<div class="line"><a name="l00290"></a><span class="lineno"> 290</span>  <span class="keywordtype">double</span> scale_factor = exp(-d2 / (2.f * alpha * alpha));</div>
|
|
<div class="line"><a name="l00291"></a><span class="lineno"> 291</span>  </div>
|
|
<div class="line"><a name="l00292"></a><span class="lineno"> 292</span>  <span class="keywordflow">for</span> (k = 0; k < num_features; k++)</div>
|
|
<div class="line"><a name="l00293"></a><span class="lineno"> 293</span>  {</div>
|
|
<div class="line"><a name="l00294"></a><span class="lineno"> 294</span>  <span class="keywordtype">double</span> *w = <a class="code" href="../../d1/d6b/group__kohonen__2d.html#ga8df35f04c1762a01dcf108fa13b897d6">kohonen_data_3d</a>(W, x, y, k);</div>
|
|
<div class="line"><a name="l00295"></a><span class="lineno"> 295</span>  <span class="comment">// update weights of nodes in the neighborhood</span></div>
|
|
<div class="line"><a name="l00296"></a><span class="lineno"> 296</span>  w[0] += alpha * scale_factor * (X[k] - w[0]);</div>
|
|
<div class="line"><a name="l00297"></a><span class="lineno"> 297</span>  }</div>
|
|
<div class="line"><a name="l00298"></a><span class="lineno"> 298</span>  }</div>
|
|
<div class="line"><a name="l00299"></a><span class="lineno"> 299</span>  }</div>
|
|
<div class="line"><a name="l00300"></a><span class="lineno"> 300</span>  <span class="keywordflow">return</span> d_min;</div>
|
|
<div class="line"><a name="l00301"></a><span class="lineno"> 301</span> }</div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d1/d6b/group__kohonen__2d_ga83abb572c60d202e100595a989dfe123_cgraph.svg" width="352" height="88"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<a id="ga6824dc6d973eb3339af7aef5fea78b0c"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ga6824dc6d973eb3339af7aef5fea78b0c">◆ </a></span>save_2d_data()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">int save_2d_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 overwritten 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"><a name="l00104"></a><span class="lineno"> 104</span> {</div>
|
|
<div class="line"><a name="l00105"></a><span class="lineno"> 105</span>  FILE *fp = fopen(fname, <span class="stringliteral">"wt"</span>);</div>
|
|
<div class="line"><a name="l00106"></a><span class="lineno"> 106</span>  <span class="keywordflow">if</span> (!fp) <span class="comment">// error with fopen</span></div>
|
|
<div class="line"><a name="l00107"></a><span class="lineno"> 107</span>  {</div>
|
|
<div class="line"><a name="l00108"></a><span class="lineno"> 108</span>  <span class="keywordtype">char</span> msg[120];</div>
|
|
<div class="line"><a name="l00109"></a><span class="lineno"> 109</span>  sprintf(msg, <span class="stringliteral">"File error (%s): "</span>, fname);</div>
|
|
<div class="line"><a name="l00110"></a><span class="lineno"> 110</span>  perror(msg);</div>
|
|
<div class="line"><a name="l00111"></a><span class="lineno"> 111</span>  <span class="keywordflow">return</span> -1;</div>
|
|
<div class="line"><a name="l00112"></a><span class="lineno"> 112</span>  }</div>
|
|
<div class="line"><a name="l00113"></a><span class="lineno"> 113</span>  </div>
|
|
<div class="line"><a name="l00114"></a><span class="lineno"> 114</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"><a name="l00115"></a><span class="lineno"> 115</span>  {</div>
|
|
<div class="line"><a name="l00116"></a><span class="lineno"> 116</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"><a name="l00117"></a><span class="lineno"> 117</span>  {</div>
|
|
<div class="line"><a name="l00118"></a><span class="lineno"> 118</span>  fprintf(fp, <span class="stringliteral">"%.4g"</span>, X[i][j]); <span class="comment">// print the feature value</span></div>
|
|
<div class="line"><a name="l00119"></a><span class="lineno"> 119</span>  <span class="keywordflow">if</span> (j < num_features - 1) <span class="comment">// if not the last feature</span></div>
|
|
<div class="line"><a name="l00120"></a><span class="lineno"> 120</span>  fputc(<span class="charliteral">','</span>, fp); <span class="comment">// suffix comma</span></div>
|
|
<div class="line"><a name="l00121"></a><span class="lineno"> 121</span>  }</div>
|
|
<div class="line"><a name="l00122"></a><span class="lineno"> 122</span>  <span class="keywordflow">if</span> (i < num_points - 1) <span class="comment">// if not the last row</span></div>
|
|
<div class="line"><a name="l00123"></a><span class="lineno"> 123</span>  fputc(<span class="charliteral">'\n'</span>, fp); <span class="comment">// start a new line</span></div>
|
|
<div class="line"><a name="l00124"></a><span class="lineno"> 124</span>  }</div>
|
|
<div class="line"><a name="l00125"></a><span class="lineno"> 125</span>  fclose(fp);</div>
|
|
<div class="line"><a name="l00126"></a><span class="lineno"> 126</span>  <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><a name="l00127"></a><span class="lineno"> 127</span> }</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="ga49d35f68f5d11d8ef6f8cce0d0e7bcba"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ga49d35f68f5d11d8ef6f8cce0d0e7bcba">◆ </a></span>save_u_matrix()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">int save_u_matrix </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">struct <a class="el" href="../../d8/db8/structkohonen__array__3d.html">kohonen_array_3d</a> * </td>
|
|
<td class="paramname"><em>W</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Create the distance matrix or <a href="https://en.wikipedia.org/wiki/U-matrix">U-matrix</a> from the trained weights and save to disk. </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">W</td><td>model matrix to save </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"><a name="l00140"></a><span class="lineno"> 140</span> {</div>
|
|
<div class="line"><a name="l00141"></a><span class="lineno"> 141</span>  FILE *fp = fopen(fname, <span class="stringliteral">"wt"</span>);</div>
|
|
<div class="line"><a name="l00142"></a><span class="lineno"> 142</span>  <span class="keywordflow">if</span> (!fp) <span class="comment">// error with fopen</span></div>
|
|
<div class="line"><a name="l00143"></a><span class="lineno"> 143</span>  {</div>
|
|
<div class="line"><a name="l00144"></a><span class="lineno"> 144</span>  <span class="keywordtype">char</span> msg[120];</div>
|
|
<div class="line"><a name="l00145"></a><span class="lineno"> 145</span>  sprintf(msg, <span class="stringliteral">"File error (%s): "</span>, fname);</div>
|
|
<div class="line"><a name="l00146"></a><span class="lineno"> 146</span>  perror(msg);</div>
|
|
<div class="line"><a name="l00147"></a><span class="lineno"> 147</span>  <span class="keywordflow">return</span> -1;</div>
|
|
<div class="line"><a name="l00148"></a><span class="lineno"> 148</span>  }</div>
|
|
<div class="line"><a name="l00149"></a><span class="lineno"> 149</span>  </div>
|
|
<div class="line"><a name="l00150"></a><span class="lineno"> 150</span>  <span class="keywordtype">int</span> R = <a class="code" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(W-><a class="code" href="../../d8/db8/structkohonen__array__3d.html#a16720581653fa9a34d1029e7229a7377">dim1</a> >> 3, 2); <span class="comment">/* neighborhood range */</span></div>
|
|
<div class="line"><a name="l00151"></a><span class="lineno"> 151</span>  </div>
|
|
<div class="line"><a name="l00152"></a><span class="lineno"> 152</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < W-><a class="code" href="../../d8/db8/structkohonen__array__3d.html#a16720581653fa9a34d1029e7229a7377">dim1</a>; i++) <span class="comment">// for each x</span></div>
|
|
<div class="line"><a name="l00153"></a><span class="lineno"> 153</span>  {</div>
|
|
<div class="line"><a name="l00154"></a><span class="lineno"> 154</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j < W-><a class="code" href="../../d8/db8/structkohonen__array__3d.html#a888d7e007b38c91c7933e12a9566af1d">dim2</a>; j++) <span class="comment">// for each y</span></div>
|
|
<div class="line"><a name="l00155"></a><span class="lineno"> 155</span>  {</div>
|
|
<div class="line"><a name="l00156"></a><span class="lineno"> 156</span>  <span class="keywordtype">double</span> distance = 0.f;</div>
|
|
<div class="line"><a name="l00157"></a><span class="lineno"> 157</span>  <span class="keywordtype">int</span> k;</div>
|
|
<div class="line"><a name="l00158"></a><span class="lineno"> 158</span>  </div>
|
|
<div class="line"><a name="l00159"></a><span class="lineno"> 159</span>  <span class="keywordtype">int</span> from_x = <a class="code" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(0, i - R);</div>
|
|
<div class="line"><a name="l00160"></a><span class="lineno"> 160</span>  <span class="keywordtype">int</span> to_x = <a class="code" href="../../d1/d6b/group__kohonen__2d.html#gac6afabdc09a49a433ee19d8a9486056d">min</a>(W-><a class="code" href="../../d8/db8/structkohonen__array__3d.html#a16720581653fa9a34d1029e7229a7377">dim1</a>, i + R + 1);</div>
|
|
<div class="line"><a name="l00161"></a><span class="lineno"> 161</span>  <span class="keywordtype">int</span> from_y = <a class="code" href="../../d1/d6b/group__kohonen__2d.html#gaffe776513b24d84b39af8ab0930fef7f">max</a>(0, j - R);</div>
|
|
<div class="line"><a name="l00162"></a><span class="lineno"> 162</span>  <span class="keywordtype">int</span> to_y = <a class="code" href="../../d1/d6b/group__kohonen__2d.html#gac6afabdc09a49a433ee19d8a9486056d">min</a>(W-><a class="code" href="../../d8/db8/structkohonen__array__3d.html#a888d7e007b38c91c7933e12a9566af1d">dim2</a>, j + R + 1);</div>
|
|
<div class="line"><a name="l00163"></a><span class="lineno"> 163</span>  <span class="keywordtype">int</span> l;</div>
|
|
<div class="line"><a name="l00164"></a><span class="lineno"> 164</span> <span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><a name="l00165"></a><span class="lineno"> 165</span> <span class="preprocessor">#pragma omp parallel for reduction(+ : distance)</span></div>
|
|
<div class="line"><a name="l00166"></a><span class="lineno"> 166</span> <span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a name="l00167"></a><span class="lineno"> 167</span>  <span class="keywordflow">for</span> (l = from_x; l < to_x; l++) <span class="comment">// scan neighborhoor in x</span></div>
|
|
<div class="line"><a name="l00168"></a><span class="lineno"> 168</span>  {</div>
|
|
<div class="line"><a name="l00169"></a><span class="lineno"> 169</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> m = from_y; m < to_y; m++) <span class="comment">// scan neighborhood in y</span></div>
|
|
<div class="line"><a name="l00170"></a><span class="lineno"> 170</span>  {</div>
|
|
<div class="line"><a name="l00171"></a><span class="lineno"> 171</span>  <span class="keywordtype">double</span> d = 0.f;</div>
|
|
<div class="line"><a name="l00172"></a><span class="lineno"> 172</span>  <span class="keywordflow">for</span> (k = 0; k < W-><a class="code" href="../../d8/db8/structkohonen__array__3d.html#a160f14830bdfbbf9f422f382ee754dbf">dim3</a>; k++) <span class="comment">// for each feature</span></div>
|
|
<div class="line"><a name="l00173"></a><span class="lineno"> 173</span>  {</div>
|
|
<div class="line"><a name="l00174"></a><span class="lineno"> 174</span>  <span class="keywordtype">double</span> *w1 = <a class="code" href="../../d1/d6b/group__kohonen__2d.html#ga8df35f04c1762a01dcf108fa13b897d6">kohonen_data_3d</a>(W, i, j, k);</div>
|
|
<div class="line"><a name="l00175"></a><span class="lineno"> 175</span>  <span class="keywordtype">double</span> *w2 = <a class="code" href="../../d1/d6b/group__kohonen__2d.html#ga8df35f04c1762a01dcf108fa13b897d6">kohonen_data_3d</a>(W, l, m, k);</div>
|
|
<div class="line"><a name="l00176"></a><span class="lineno"> 176</span>  d += (w1[0] - w2[0]) * (w1[0] - w2[0]);</div>
|
|
<div class="line"><a name="l00177"></a><span class="lineno"> 177</span>  <span class="comment">// distance += w1[0] * w1[0];</span></div>
|
|
<div class="line"><a name="l00178"></a><span class="lineno"> 178</span>  }</div>
|
|
<div class="line"><a name="l00179"></a><span class="lineno"> 179</span>  distance += sqrt(d);</div>
|
|
<div class="line"><a name="l00180"></a><span class="lineno"> 180</span>  <span class="comment">// distance += d;</span></div>
|
|
<div class="line"><a name="l00181"></a><span class="lineno"> 181</span>  }</div>
|
|
<div class="line"><a name="l00182"></a><span class="lineno"> 182</span>  }</div>
|
|
<div class="line"><a name="l00183"></a><span class="lineno"> 183</span>  </div>
|
|
<div class="line"><a name="l00184"></a><span class="lineno"> 184</span>  distance /= R * R; <span class="comment">// mean distance from neighbors</span></div>
|
|
<div class="line"><a name="l00185"></a><span class="lineno"> 185</span>  fprintf(fp, <span class="stringliteral">"%.4g"</span>, distance); <span class="comment">// print the mean separation</span></div>
|
|
<div class="line"><a name="l00186"></a><span class="lineno"> 186</span>  <span class="keywordflow">if</span> (j < W->dim2 - 1) <span class="comment">// if not the last column</span></div>
|
|
<div class="line"><a name="l00187"></a><span class="lineno"> 187</span>  fputc(<span class="charliteral">','</span>, fp); <span class="comment">// suffix comma</span></div>
|
|
<div class="line"><a name="l00188"></a><span class="lineno"> 188</span>  }</div>
|
|
<div class="line"><a name="l00189"></a><span class="lineno"> 189</span>  <span class="keywordflow">if</span> (i < W->dim1 - 1) <span class="comment">// if not the last row</span></div>
|
|
<div class="line"><a name="l00190"></a><span class="lineno"> 190</span>  fputc(<span class="charliteral">'\n'</span>, fp); <span class="comment">// start a new line</span></div>
|
|
<div class="line"><a name="l00191"></a><span class="lineno"> 191</span>  }</div>
|
|
<div class="line"><a name="l00192"></a><span class="lineno"> 192</span>  fclose(fp);</div>
|
|
<div class="line"><a name="l00193"></a><span class="lineno"> 193</span>  <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><a name="l00194"></a><span class="lineno"> 194</span> }</div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d1/d6b/group__kohonen__2d_ga49d35f68f5d11d8ef6f8cce0d0e7bcba_cgraph.svg" width="290" height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
</div><!-- contents -->
|
|
</div><!-- doc-content -->
|
|
<div class="ttc" id="astructkohonen__array__3d_html_ad546baa2e81c6196d5f1dc0fe2e5bd59"><div class="ttname"><a href="../../d8/db8/structkohonen__array__3d.html#ad546baa2e81c6196d5f1dc0fe2e5bd59">kohonen_array_3d::data</a></div><div class="ttdeci">double * data</div><div class="ttdoc">pointer to data</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:52</div></div>
|
|
<div class="ttc" id="astructkohonen__array__3d_html_a888d7e007b38c91c7933e12a9566af1d"><div class="ttname"><a href="../../d8/db8/structkohonen__array__3d.html#a888d7e007b38c91c7933e12a9566af1d">kohonen_array_3d::dim2</a></div><div class="ttdeci">int dim2</div><div class="ttdoc">lengths of second dimension</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:50</div></div>
|
|
<div class="ttc" id="astructkohonen__array__3d_html_a160f14830bdfbbf9f422f382ee754dbf"><div class="ttname"><a href="../../d8/db8/structkohonen__array__3d.html#a160f14830bdfbbf9f422f382ee754dbf">kohonen_array_3d::dim3</a></div><div class="ttdeci">int dim3</div><div class="ttdoc">lengths of thirddimension</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:51</div></div>
|
|
<div class="ttc" id="agroup__kohonen__2d_html_gadc22d512c00a9f5799ee067f4fb90b4b"><div class="ttname"><a href="../../d1/d6b/group__kohonen__2d.html#gadc22d512c00a9f5799ee067f4fb90b4b">get_min_2d</a></div><div class="ttdeci">void get_min_2d(double **X, int N, double *val, int *x_idx, int *y_idx)</div><div class="ttdoc">Get minimum value and index of the value in a matrix.</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:204</div></div>
|
|
<div class="ttc" id="agroup__kohonen__2d_html_gac6afabdc09a49a433ee19d8a9486056d"><div class="ttname"><a href="../../d1/d6b/group__kohonen__2d.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_topology.c:43</div></div>
|
|
<div class="ttc" id="agroup__kohonen__2d_html_gaffe776513b24d84b39af8ab0930fef7f"><div class="ttname"><a href="../../d1/d6b/group__kohonen__2d.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_topology.c:39</div></div>
|
|
<div class="ttc" id="astructkohonen__array__3d_html_a16720581653fa9a34d1029e7229a7377"><div class="ttname"><a href="../../d8/db8/structkohonen__array__3d.html#a16720581653fa9a34d1029e7229a7377">kohonen_array_3d::dim1</a></div><div class="ttdeci">int dim1</div><div class="ttdoc">lengths of first dimension</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:49</div></div>
|
|
<div class="ttc" id="agroup__kohonen__2d_html_ga83abb572c60d202e100595a989dfe123"><div class="ttname"><a href="../../d1/d6b/group__kohonen__2d.html#ga83abb572c60d202e100595a989dfe123">kohonen_update_weights</a></div><div class="ttdeci">double kohonen_update_weights(const double *X, struct kohonen_array_3d *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_topology.c:234</div></div>
|
|
<div class="ttc" id="agroup__kohonen__2d_html_ga8df35f04c1762a01dcf108fa13b897d6"><div class="ttname"><a href="../../d1/d6b/group__kohonen__2d.html#ga8df35f04c1762a01dcf108fa13b897d6">kohonen_data_3d</a></div><div class="ttdeci">double * kohonen_data_3d(const struct kohonen_array_3d *arr, int x, int y, int z)</div><div class="ttdoc">Function that returns the pointer to (x, y, z) ^th location in the linear 3D array given by:</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.c:67</div></div>
|
|
<!-- start footer part -->
|
|
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
|
<ul>
|
|
<li class="footer">Generated by
|
|
<a href="http://www.doxygen.org/index.html">
|
|
<img class="footer" src="../../doxygen.png" alt="doxygen"/></a> 1.8.18 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|