mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-03-23 21:32:09 +08:00
816 lines
80 KiB
HTML
816 lines
80 KiB
HTML
<!-- HTML header for doxygen 1.12.0-->
|
|
<!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.14.0"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>TheAlgorithms/C++: machine_learning/kohonen_som_topology.cpp File Reference</title>
|
|
<link rel="icon" href="../../favicon.svg" type="image/x-icon" />
|
|
<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>
|
|
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@xpack-3rd-party/doxygen-awesome-css@2.2.0-1/doxygen-awesome-darkmode-toggle.js"></script>
|
|
<script type="text/javascript">
|
|
DoxygenAwesomeDarkModeToggle.init()
|
|
</script>
|
|
<script type="text/javascript" src="../../clipboard.js"></script>
|
|
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../navtreedata.js"></script>
|
|
<script type="text/javascript" src="../../navtree.js"></script>
|
|
<script type="text/javascript" src="../../cookie.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/javascript">
|
|
window.MathJax = {
|
|
options: {
|
|
ignoreHtmlClass: 'tex2jax_ignore',
|
|
processHtmlClass: 'tex2jax_process'
|
|
},
|
|
loader: {
|
|
load: ['[tex]/ams']
|
|
},
|
|
tex: {
|
|
macros: {},
|
|
packages: ['base','configmacros','ams']
|
|
}
|
|
};
|
|
</script>
|
|
<script type="text/javascript" id="MathJax-script" async="async" src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js"></script>
|
|
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
|
|
<link href="../../doxygen-awesome.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="projectlogo"><img alt="Logo" src="../../project_logo.png"/></td>
|
|
<td id="projectalign">
|
|
<div id="projectname">TheAlgorithms/C++<span id="projectnumber"> 1.0.0</span>
|
|
</div>
|
|
<div id="projectbrief">All the algorithms implemented in C++</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.14.0 -->
|
|
<script type="text/javascript">
|
|
var searchBox = new SearchBox("searchBox", "../../search/",'.html');
|
|
</script>
|
|
<script type="text/javascript">
|
|
$(function() { codefold.init(); });
|
|
</script>
|
|
<script type="text/javascript" src="../../menudata.js"></script>
|
|
<script type="text/javascript" src="../../menu.js"></script>
|
|
<script type="text/javascript">
|
|
$(function() {
|
|
initMenu('../../',true,false,'search.php','Search',true);
|
|
$(function() { init_search(); });
|
|
});
|
|
</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">
|
|
$(function(){initNavTree('d4/def/kohonen__som__topology_8cpp.html','../../',''); });
|
|
</script>
|
|
<div id="container">
|
|
<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="headertitle"><div class="title">kohonen_som_topology.cpp File Reference<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">
|
|
|
|
<p><a href="https://en.wikipedia.org/wiki/Self-organizing_map" target="_blank">Kohonen self organizing map</a> (topological map)
|
|
<a href="#details">More...</a></p>
|
|
<div class="textblock"><code>#include <algorithm></code><br />
|
|
<code>#include <array></code><br />
|
|
<code>#include <cerrno></code><br />
|
|
<code>#include <cmath></code><br />
|
|
<code>#include <cstdlib></code><br />
|
|
<code>#include <cstring></code><br />
|
|
<code>#include <ctime></code><br />
|
|
<code>#include <fstream></code><br />
|
|
<code>#include <iostream></code><br />
|
|
<code>#include <valarray></code><br />
|
|
<code>#include <vector></code><br />
|
|
</div><div class="textblock"><div class="dynheader">
|
|
Include dependency graph for kohonen_som_topology.cpp:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><div class="zoom"><iframe scrolling="no" loading="lazy" frameborder="0" src="../../d5/dcd/kohonen__som__topology_8cpp__incl.svg" width="100%" height="394"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div></div>
|
|
</div>
|
|
</div>
|
|
<p><a href="../../d4/def/kohonen__som__topology_8cpp_source.html">Go to the source code of this file.</a></p>
|
|
<table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 id="header-namespaces" class="groupheader"><a id="namespaces" name="namespaces"></a>
|
|
Namespaces</h2></td></tr>
|
|
<tr class="memitem:machine_5Flearning" id="r_machine_5Flearning"><td class="memItemLeft" align="right" valign="top">namespace  </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d8/d77/namespacemachine__learning.html">machine_learning</a></td></tr>
|
|
<tr class="memdesc:d8/d77/namespacemachine__learning"><td class="mdescLeft"> </td><td class="mdescRight"><a href="https://en.wikipedia.org/wiki/A*_search_algorithm" target="_blank">A* search algorithm</a> <br /></td></tr>
|
|
</table><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 id="header-func-members" class="groupheader"><a id="func-members" name="func-members"></a>
|
|
Functions</h2></td></tr>
|
|
<tr class="memitem:gaf5ce14f026d6d231bef29161bac2b485" id="r_gaf5ce14f026d6d231bef29161bac2b485"><td class="memItemLeft" align="right" valign="top">double </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d9/d66/group__machine__learning.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a> (double a, double b)</td></tr>
|
|
<tr class="memitem:gabc90175770bf0d5853c466e14993a08c" id="r_gabc90175770bf0d5853c466e14993a08c"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d9/d66/group__machine__learning.html#gabc90175770bf0d5853c466e14993a08c">save_2d_data</a> (const char *fname, const std::vector< std::valarray< double > > &X)</td></tr>
|
|
<tr class="memitem:ga60f9186ccb682724a8792a2bf81e9b9e" id="r_ga60f9186ccb682724a8792a2bf81e9b9e"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d9/d66/group__machine__learning.html#ga60f9186ccb682724a8792a2bf81e9b9e">get_min_2d</a> (const std::vector< std::valarray< double > > &X, double *val, int *x_idx, int *y_idx)</td></tr>
|
|
<tr class="memitem:aa72a53c88203fde278f1fe6c3afe5b07" id="r_aa72a53c88203fde278f1fe6c3afe5b07"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d8/d77/namespacemachine__learning.html#aa72a53c88203fde278f1fe6c3afe5b07">machine_learning::save_u_matrix</a> (const char *fname, const std::vector< std::vector< std::valarray< double > > > &W)</td></tr>
|
|
<tr class="memitem:ae868ad43698a1d69ba46ea3827d7d2c3" id="r_ae868ad43698a1d69ba46ea3827d7d2c3"><td class="memItemLeft" align="right" valign="top">double </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d8/d77/namespacemachine__learning.html#ae868ad43698a1d69ba46ea3827d7d2c3">machine_learning::update_weights</a> (const std::valarray< double > &X, std::vector< std::vector< std::valarray< double > > > *W, std::vector< std::valarray< double > > *D, double alpha, int R)</td></tr>
|
|
<tr class="memitem:ac43d294e21a0c4fa33c53757df054576" id="r_ac43d294e21a0c4fa33c53757df054576"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d8/d77/namespacemachine__learning.html#ac43d294e21a0c4fa33c53757df054576">machine_learning::kohonen_som</a> (const std::vector< std::valarray< double > > &X, std::vector< std::vector< std::valarray< double > > > *W, double alpha_min)</td></tr>
|
|
<tr class="memitem:a48efb079040c7aaa3a4917a0e486cba9" id="r_a48efb079040c7aaa3a4917a0e486cba9"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="#a48efb079040c7aaa3a4917a0e486cba9">test_2d_classes</a> (std::vector< std::valarray< double > > *<a class="el" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>)</td></tr>
|
|
<tr class="memitem:a1440a7779ac56f47a3f355ce4a8c7da0" id="r_a1440a7779ac56f47a3f355ce4a8c7da0"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="#a1440a7779ac56f47a3f355ce4a8c7da0">test1</a> ()</td></tr>
|
|
<tr class="memitem:a1302662a56ebf67a21249270b017297e" id="r_a1302662a56ebf67a21249270b017297e"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="#a1302662a56ebf67a21249270b017297e">test_3d_classes1</a> (std::vector< std::valarray< double > > *<a class="el" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>)</td></tr>
|
|
<tr class="memitem:a0283886819c7c140a023582b7269e2d0" id="r_a0283886819c7c140a023582b7269e2d0"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="#a0283886819c7c140a023582b7269e2d0">test2</a> ()</td></tr>
|
|
<tr class="memitem:a4b7ab643f6a5002f991837de46f70653" id="r_a4b7ab643f6a5002f991837de46f70653"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="#a4b7ab643f6a5002f991837de46f70653">test_3d_classes2</a> (std::vector< std::valarray< double > > *<a class="el" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>)</td></tr>
|
|
<tr class="memitem:a6d0455dd5c30adda100e95f0423c786e" id="r_a6d0455dd5c30adda100e95f0423c786e"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="#a6d0455dd5c30adda100e95f0423c786e">test3</a> ()</td></tr>
|
|
<tr class="memitem:a2256c10b16edba377b64a44b6c656908" id="r_a2256c10b16edba377b64a44b6c656908"><td class="memItemLeft" align="right" valign="top">double </td><td class="memItemRight" valign="bottom"><a class="el" href="#a2256c10b16edba377b64a44b6c656908">get_clock_diff</a> (clock_t start_t, clock_t end_t)</td></tr>
|
|
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4" id="r_ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
|
|
<tr class="memitem:ac43d294e21a0c4fa33c53757df054576" id="r_ac43d294e21a0c4fa33c53757df054576"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="#ac43d294e21a0c4fa33c53757df054576">kohonen_som</a> (const std::vector< std::valarray< double > > &X, std::vector< std::vector< std::valarray< double > > > *W, double alpha_min)</td></tr>
|
|
<tr class="memitem:aa72a53c88203fde278f1fe6c3afe5b07" id="r_aa72a53c88203fde278f1fe6c3afe5b07"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="#aa72a53c88203fde278f1fe6c3afe5b07">save_u_matrix</a> (const char *fname, const std::vector< std::vector< std::valarray< double > > > &W)</td></tr>
|
|
</table><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 id="header-var-members" class="groupheader"><a id="var-members" name="var-members"></a>
|
|
Variables</h2></td></tr>
|
|
<tr class="memitem:a7220dbb7fa896d83bfb7a50e4fce1786" id="r_a7220dbb7fa896d83bfb7a50e4fce1786"><td class="memItemLeft" align="right" valign="top">constexpr double </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d8/d77/namespacemachine__learning.html#a7220dbb7fa896d83bfb7a50e4fce1786">machine_learning::MIN_DISTANCE</a> = 1e-4</td></tr>
|
|
</table>
|
|
<a name="details" id="details"></a><h2 id="header-details" class="groupheader">Detailed Description</h2>
|
|
<div class="textblock"><p><a href="https://en.wikipedia.org/wiki/Self-organizing_map" target="_blank">Kohonen self organizing map</a> (topological map) </p>
|
|
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/kvedala" target="_blank">Krishna Vedala</a></dd></dl>
|
|
<p>This example implements a powerful unsupervised learning algorithm called as a self organizing map. The algorithm creates a connected network of weights that closely follows the given data points. This thus creates a topological map of the given data i.e., it maintains the relationship between varipus data points in a much higher dimesional space by creating an equivalent in a 2-dimensional space. <img src="https://raw.githubusercontent.com/TheAlgorithms/C-Plus-Plus/docs/images/machine_learning/2D_Kohonen_SOM.svg" alt="Trained topological maps for the test cases in the program" style="pointer-events: none;" class="inline"/> </p><dl class="section note"><dt>Note</dt><dd>This C++ version of the program is considerable slower than its <a href="https://github.com/kvedala/C/blob/master/machine_learning/kohonen_som_trace.c" target="_blank">C counterpart</a> </dd>
|
|
<dd>
|
|
The compiled code is much slower when compiled with MS Visual C++ 2019 than with GCC on windows </dd></dl>
|
|
<dl class="section see"><dt>See also</dt><dd><a class="el" href="../../d9/d49/kohonen__som__trace_8cpp.html" title="Kohonen self organizing map (data tracing)">kohonen_som_trace.cpp</a> </dd></dl>
|
|
|
|
<p class="definition">Definition in file <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html">kohonen_som_topology.cpp</a>.</p>
|
|
</div><a name="doc-func-members" id="doc-func-members"></a><h2 id="header-doc-func-members" class="groupheader">Function Documentation</h2>
|
|
<a id="a2256c10b16edba377b64a44b6c656908" name="a2256c10b16edba377b64a44b6c656908"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a2256c10b16edba377b64a44b6c656908">◆ </a></span>get_clock_diff()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">double get_clock_diff </td>
|
|
<td>(</td>
|
|
<td class="paramtype">clock_t</td> <td class="paramname"><span class="paramname"><em>start_t</em></span>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">clock_t</td> <td class="paramname"><span class="paramname"><em>end_t</em></span> )</td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Convert clock cycle difference to time in seconds</p>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">start_t</td><td>start clock </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">end_t</td><td>end clock </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<dl class="section return"><dt>Returns</dt><dd>time difference in seconds </dd></dl>
|
|
|
|
<p class="definition">Definition at line <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00577">577</a> of file <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html">kohonen_som_topology.cpp</a>.</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 577</span> {</div>
|
|
<div class="line"><span class="lineno"> 578</span> <span class="keywordflow">return</span> <span class="keyword">static_cast<</span><span class="keywordtype">double</span><span class="keyword">></span>(end_t - start_t) / CLOCKS_PER_SEC;</div>
|
|
<div class="line"><span class="lineno"> 579</span>}</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="ac43d294e21a0c4fa33c53757df054576" name="ac43d294e21a0c4fa33c53757df054576"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ac43d294e21a0c4fa33c53757df054576">◆ </a></span>kohonen_som()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void machine_learning::kohonen_som </td>
|
|
<td>(</td>
|
|
<td class="paramtype">const std::vector< std::valarray< double > > &</td> <td class="paramname"><span class="paramname"><em>X</em></span>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">std::vector< std::vector< std::valarray< double > > > *</td> <td class="paramname"><span class="paramname"><em>W</em></span>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double</td> <td class="paramname"><span class="paramname"><em>alpha_min</em></span> )</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">alpha_min</td><td>terminal value of alpha </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
|
|
<p class="definition">Definition at line <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00269">269</a> of file <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html">kohonen_som_topology.cpp</a>.</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 271</span> {</div>
|
|
<div class="line"><span class="lineno"> 272</span> <span class="keywordtype">size_t</span> num_samples = X.size(); <span class="comment">// number of rows</span></div>
|
|
<div class="line"><span class="lineno"> 273</span> <span class="comment">// size_t num_features = X[0].size(); // number of columns</span></div>
|
|
<div class="line"><span class="lineno"> 274</span> <span class="keywordtype">size_t</span> num_out = W->size(); <span class="comment">// output matrix size</span></div>
|
|
<div class="line"><span class="lineno"> 275</span> <span class="keywordtype">size_t</span> R = num_out >> 2, iter = 0;</div>
|
|
<div class="line"><span class="lineno"> 276</span> <span class="keywordtype">double</span> alpha = 1.f;</div>
|
|
<div class="line"><span class="lineno"> 277</span> </div>
|
|
<div class="line"><span class="lineno"> 278</span> std::vector<std::valarray<double>> D(num_out);</div>
|
|
<div class="line"><span class="lineno"> 279</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < num_out; i++) D[i] = std::valarray<double>(num_out);</div>
|
|
<div class="line"><span class="lineno"> 280</span> </div>
|
|
<div class="line"><span class="lineno"> 281</span> <span class="keywordtype">double</span> dmin = 1.f; <span class="comment">// average minimum distance of all samples</span></div>
|
|
<div class="line"><span class="lineno"> 282</span> <span class="keywordtype">double</span> past_dmin = 1.f; <span class="comment">// average minimum distance of all samples</span></div>
|
|
<div class="line"><span class="lineno"> 283</span> <span class="keywordtype">double</span> dmin_ratio = 1.f; <span class="comment">// change per step</span></div>
|
|
<div class="line"><span class="lineno"> 284</span> </div>
|
|
<div class="line"><span class="lineno"> 285</span> <span class="comment">// Loop alpha from 1 to slpha_min</span></div>
|
|
<div class="line"><span class="lineno"> 286</span> <span class="keywordflow">for</span> (; alpha > 0 && dmin_ratio > 1e-5; alpha -= 1e-4, iter++) {</div>
|
|
<div class="line"><span class="lineno"> 287</span> <span class="comment">// Loop for each sample pattern in the data set</span></div>
|
|
<div class="line"><span class="lineno"> 288</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> sample = 0; sample < num_samples; sample++) {</div>
|
|
<div class="line"><span class="lineno"> 289</span> <span class="comment">// update weights for the current input pattern sample</span></div>
|
|
<div class="line"><span class="lineno"> 290</span> dmin += <a class="code hl_function" href="../../d8/d77/namespacemachine__learning.html#ae868ad43698a1d69ba46ea3827d7d2c3">update_weights</a>(X[sample], W, &D, alpha, R);</div>
|
|
<div class="line"><span class="lineno"> 291</span> }</div>
|
|
<div class="line"><span class="lineno"> 292</span> </div>
|
|
<div class="line"><span class="lineno"> 293</span> <span class="comment">// every 100th iteration, reduce the neighborhood range</span></div>
|
|
<div class="line"><span class="lineno"> 294</span> <span class="keywordflow">if</span> (iter % 300 == 0 && R > 1) {</div>
|
|
<div class="line"><span class="lineno"> 295</span> R--;</div>
|
|
<div class="line"><span class="lineno"> 296</span> }</div>
|
|
<div class="line"><span class="lineno"> 297</span> </div>
|
|
<div class="line"><span class="lineno"> 298</span> dmin /= num_samples;</div>
|
|
<div class="line"><span class="lineno"> 299</span> </div>
|
|
<div class="line"><span class="lineno"> 300</span> <span class="comment">// termination condition variable -> % change in minimum distance</span></div>
|
|
<div class="line"><span class="lineno"> 301</span> dmin_ratio = (past_dmin - dmin) / past_dmin;</div>
|
|
<div class="line"><span class="lineno"> 302</span> <span class="keywordflow">if</span> (dmin_ratio < 0) {</div>
|
|
<div class="line"><span class="lineno"> 303</span> dmin_ratio = 1.f;</div>
|
|
<div class="line"><span class="lineno"> 304</span> }</div>
|
|
<div class="line"><span class="lineno"> 305</span> past_dmin = dmin;</div>
|
|
<div class="line"><span class="lineno"> 306</span> </div>
|
|
<div class="line"><span class="lineno"> 307</span> std::cout << <span class="stringliteral">"iter: "</span> << iter << <span class="stringliteral">"\t alpha: "</span> << alpha << <span class="stringliteral">"\t R: "</span> << R</div>
|
|
<div class="line"><span class="lineno"> 308</span> << <span class="stringliteral">"\t d_min: "</span> << dmin_ratio << <span class="stringliteral">"\r"</span>;</div>
|
|
<div class="line"><span class="lineno"> 309</span> }</div>
|
|
<div class="line"><span class="lineno"> 310</span> </div>
|
|
<div class="line"><span class="lineno"> 311</span> std::cout << <span class="stringliteral">"\n"</span>;</div>
|
|
<div class="line"><span class="lineno"> 312</span>}</div>
|
|
<div class="ttc" id="anamespacemachine__learning_html_ae868ad43698a1d69ba46ea3827d7d2c3"><div class="ttname"><a href="../../d8/d77/namespacemachine__learning.html#ae868ad43698a1d69ba46ea3827d7d2c3">machine_learning::update_weights</a></div><div class="ttdeci">double update_weights(const std::valarray< double > &X, std::vector< std::vector< std::valarray< double > > > *W, std::vector< std::valarray< double > > *D, double alpha, int R)</div><div class="ttdef"><b>Definition</b> <a href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00200">kohonen_som_topology.cpp:200</a></div></div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">◆ </a></span>main()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">int main </td>
|
|
<td>(</td>
|
|
<td class="paramtype">void</td> <td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Main function </p>
|
|
|
|
<p class="definition">Definition at line <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00582">582</a> of file <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html">kohonen_som_topology.cpp</a>.</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 582</span> {</div>
|
|
<div class="line"><span class="lineno"> 583</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><span class="lineno"> 584</span> std::cout << <span class="stringliteral">"Using OpenMP based parallelization\n"</span>;</div>
|
|
<div class="line"><span class="lineno"> 585</span><span class="preprocessor">#else</span></div>
|
|
<div class="line"><span class="lineno"> 586</span> std::cout << <span class="stringliteral">"NOT using OpenMP based parallelization\n"</span>;</div>
|
|
<div class="line"><span class="lineno"> 587</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 588</span> </div>
|
|
<div class="line"><span class="lineno"> 589</span> std::srand(std::time(<span class="keyword">nullptr</span>));</div>
|
|
<div class="line"><span class="lineno"> 590</span> </div>
|
|
<div class="line"><span class="lineno"> 591</span> std::clock_t start_clk = std::clock();</div>
|
|
<div class="line"><span class="lineno"> 592</span> <a class="code hl_function" href="#a1440a7779ac56f47a3f355ce4a8c7da0">test1</a>();</div>
|
|
<div class="line"><span class="lineno"> 593</span> <span class="keyword">auto</span> end_clk = std::clock();</div>
|
|
<div class="line"><span class="lineno"> 594</span> std::cout << <span class="stringliteral">"Test 1 completed in "</span> << <a class="code hl_function" href="#a2256c10b16edba377b64a44b6c656908">get_clock_diff</a>(start_clk, end_clk)</div>
|
|
<div class="line"><span class="lineno"> 595</span> << <span class="stringliteral">" sec\n"</span>;</div>
|
|
<div class="line"><span class="lineno"> 596</span> </div>
|
|
<div class="line"><span class="lineno"> 597</span> start_clk = std::clock();</div>
|
|
<div class="line"><span class="lineno"> 598</span> <a class="code hl_function" href="#a0283886819c7c140a023582b7269e2d0">test2</a>();</div>
|
|
<div class="line"><span class="lineno"> 599</span> end_clk = std::clock();</div>
|
|
<div class="line"><span class="lineno"> 600</span> std::cout << <span class="stringliteral">"Test 2 completed in "</span> << <a class="code hl_function" href="#a2256c10b16edba377b64a44b6c656908">get_clock_diff</a>(start_clk, end_clk)</div>
|
|
<div class="line"><span class="lineno"> 601</span> << <span class="stringliteral">" sec\n"</span>;</div>
|
|
<div class="line"><span class="lineno"> 602</span> </div>
|
|
<div class="line"><span class="lineno"> 603</span> start_clk = std::clock();</div>
|
|
<div class="line"><span class="lineno"> 604</span> <a class="code hl_function" href="#a6d0455dd5c30adda100e95f0423c786e">test3</a>();</div>
|
|
<div class="line"><span class="lineno"> 605</span> end_clk = std::clock();</div>
|
|
<div class="line"><span class="lineno"> 606</span> std::cout << <span class="stringliteral">"Test 3 completed in "</span> << <a class="code hl_function" href="#a2256c10b16edba377b64a44b6c656908">get_clock_diff</a>(start_clk, end_clk)</div>
|
|
<div class="line"><span class="lineno"> 607</span> << <span class="stringliteral">" sec\n"</span>;</div>
|
|
<div class="line"><span class="lineno"> 608</span> </div>
|
|
<div class="line"><span class="lineno"> 609</span> std::cout</div>
|
|
<div class="line"><span class="lineno"> 610</span> << <span class="stringliteral">"(Note: Calculated times include: creating test sets, training "</span></div>
|
|
<div class="line"><span class="lineno"> 611</span> <span class="stringliteral">"model and writing files to disk.)\n\n"</span>;</div>
|
|
<div class="line"><span class="lineno"> 612</span> <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><span class="lineno"> 613</span>}</div>
|
|
<div class="ttc" id="akohonen__som__topology_8cpp_html_a0283886819c7c140a023582b7269e2d0"><div class="ttname"><a href="#a0283886819c7c140a023582b7269e2d0">test2</a></div><div class="ttdeci">void test2()</div><div class="ttdef"><b>Definition</b> <a href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00451">kohonen_som_topology.cpp:451</a></div></div>
|
|
<div class="ttc" id="akohonen__som__topology_8cpp_html_a1440a7779ac56f47a3f355ce4a8c7da0"><div class="ttname"><a href="#a1440a7779ac56f47a3f355ce4a8c7da0">test1</a></div><div class="ttdeci">void test1()</div><div class="ttdef"><b>Definition</b> <a href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00369">kohonen_som_topology.cpp:369</a></div></div>
|
|
<div class="ttc" id="akohonen__som__topology_8cpp_html_a2256c10b16edba377b64a44b6c656908"><div class="ttname"><a href="#a2256c10b16edba377b64a44b6c656908">get_clock_diff</a></div><div class="ttdeci">double get_clock_diff(clock_t start_t, clock_t end_t)</div><div class="ttdef"><b>Definition</b> <a href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00577">kohonen_som_topology.cpp:577</a></div></div>
|
|
<div class="ttc" id="akohonen__som__topology_8cpp_html_a6d0455dd5c30adda100e95f0423c786e"><div class="ttname"><a href="#a6d0455dd5c30adda100e95f0423c786e">test3</a></div><div class="ttdeci">void test3()</div><div class="ttdef"><b>Definition</b> <a href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00537">kohonen_som_topology.cpp:537</a></div></div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="aa72a53c88203fde278f1fe6c3afe5b07" name="aa72a53c88203fde278f1fe6c3afe5b07"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#aa72a53c88203fde278f1fe6c3afe5b07">◆ </a></span>save_u_matrix()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">int machine_learning::save_u_matrix </td>
|
|
<td>(</td>
|
|
<td class="paramtype">const char *</td> <td class="paramname"><span class="paramname"><em>fname</em></span>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">const std::vector< std::vector< std::valarray< double > > > &</td> <td class="paramname"><span class="paramname"><em>W</em></span> )</td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Create the distance matrix or <a href="https://en.wikipedia.org/wiki/U-matrix" target="_blank">U-matrix</a> from the trained 3D weiths matrix 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>
|
|
|
|
<p class="definition">Definition at line <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00142">142</a> of file <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html">kohonen_som_topology.cpp</a>.</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 143</span> {</div>
|
|
<div class="line"><span class="lineno"> 144</span> std::ofstream fp(fname);</div>
|
|
<div class="line"><span class="lineno"> 145</span> <span class="keywordflow">if</span> (!fp) { <span class="comment">// error with fopen</span></div>
|
|
<div class="line"><span class="lineno"> 146</span> std::cerr << <span class="stringliteral">"File error ("</span> << fname << <span class="stringliteral">"): "</span> << std::strerror(errno)</div>
|
|
<div class="line"><span class="lineno"> 147</span> << std::endl;</div>
|
|
<div class="line"><span class="lineno"> 148</span> <span class="keywordflow">return</span> -1;</div>
|
|
<div class="line"><span class="lineno"> 149</span> }</div>
|
|
<div class="line"><span class="lineno"> 150</span> </div>
|
|
<div class="line"><span class="lineno"> 151</span> <span class="comment">// neighborhood range</span></div>
|
|
<div class="line"><span class="lineno"> 152</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> R = 1;</div>
|
|
<div class="line"><span class="lineno"> 153</span> </div>
|
|
<div class="line"><span class="lineno"> 154</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < W.size(); i++) { <span class="comment">// for each x</span></div>
|
|
<div class="line"><span class="lineno"> 155</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j < W[0].size(); j++) { <span class="comment">// for each y</span></div>
|
|
<div class="line"><span class="lineno"> 156</span> <span class="keywordtype">double</span> distance = 0.f;</div>
|
|
<div class="line"><span class="lineno"> 157</span> </div>
|
|
<div class="line"><span class="lineno"> 158</span> <span class="keywordtype">int</span> from_x = std::max<int>(0, i - R);</div>
|
|
<div class="line"><span class="lineno"> 159</span> <span class="keywordtype">int</span> to_x = std::min<int>(W.size(), i + R + 1);</div>
|
|
<div class="line"><span class="lineno"> 160</span> <span class="keywordtype">int</span> from_y = std::max<int>(0, j - R);</div>
|
|
<div class="line"><span class="lineno"> 161</span> <span class="keywordtype">int</span> to_y = std::min<int>(W[0].size(), j + R + 1);</div>
|
|
<div class="line"><span class="lineno"> 162</span> <span class="keywordtype">int</span> <a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a6d8df83a6f26ce24a75d3b358b7f5b8a">l</a> = 0, m = 0;</div>
|
|
<div class="line"><span class="lineno"> 163</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><span class="lineno"> 164</span><span class="preprocessor">#pragma omp parallel for reduction(+ : distance)</span></div>
|
|
<div class="line"><span class="lineno"> 165</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 166</span> <span class="keywordflow">for</span> (l = from_x; <a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a6d8df83a6f26ce24a75d3b358b7f5b8a">l</a> < to_x; <a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a6d8df83a6f26ce24a75d3b358b7f5b8a">l</a>++) { <span class="comment">// scan neighborhoor in x</span></div>
|
|
<div class="line"><span class="lineno"> 167</span> <span class="keywordflow">for</span> (m = from_y; m < to_y; m++) { <span class="comment">// scan neighborhood in y</span></div>
|
|
<div class="line"><span class="lineno"> 168</span> <span class="keyword">auto</span> d = W[i][j] - W[<a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a6d8df83a6f26ce24a75d3b358b7f5b8a">l</a>][m];</div>
|
|
<div class="line"><span class="lineno"> 169</span> <span class="keywordtype">double</span> d2 = std::pow(d, 2).sum();</div>
|
|
<div class="line"><span class="lineno"> 170</span> distance += std::sqrt(d2);</div>
|
|
<div class="line"><span class="lineno"> 171</span> <span class="comment">// distance += d2;</span></div>
|
|
<div class="line"><span class="lineno"> 172</span> }</div>
|
|
<div class="line"><span class="lineno"> 173</span> }</div>
|
|
<div class="line"><span class="lineno"> 174</span> </div>
|
|
<div class="line"><span class="lineno"> 175</span> distance /= R * R; <span class="comment">// mean distance from neighbors</span></div>
|
|
<div class="line"><span class="lineno"> 176</span> fp << distance; <span class="comment">// print the mean separation</span></div>
|
|
<div class="line"><span class="lineno"> 177</span> <span class="keywordflow">if</span> (j < W[0].size() - 1) { <span class="comment">// if not the last column</span></div>
|
|
<div class="line"><span class="lineno"> 178</span> fp << <span class="charliteral">','</span>; <span class="comment">// suffix comma</span></div>
|
|
<div class="line"><span class="lineno"> 179</span> }</div>
|
|
<div class="line"><span class="lineno"> 180</span> }</div>
|
|
<div class="line"><span class="lineno"> 181</span> <span class="keywordflow">if</span> (i < W.size() - 1) { <span class="comment">// if not the last row</span></div>
|
|
<div class="line"><span class="lineno"> 182</span> fp << <span class="charliteral">'\n'</span>; <span class="comment">// start a new line</span></div>
|
|
<div class="line"><span class="lineno"> 183</span> }</div>
|
|
<div class="line"><span class="lineno"> 184</span> }</div>
|
|
<div class="line"><span class="lineno"> 185</span> </div>
|
|
<div class="line"><span class="lineno"> 186</span> fp.close();</div>
|
|
<div class="line"><span class="lineno"> 187</span> <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><span class="lineno"> 188</span>}</div>
|
|
<div class="ttc" id="acomposite__simpson__rule_8cpp_html_a6d8df83a6f26ce24a75d3b358b7f5b8a"><div class="ttname"><a href="../../d4/d18/composite__simpson__rule_8cpp.html#a6d8df83a6f26ce24a75d3b358b7f5b8a">numerical_methods::simpson_method::l</a></div><div class="ttdeci">double l(double x)</div><div class="ttdoc">Another test function.</div><div class="ttdef"><b>Definition</b> <a href="../../d4/d18/composite__simpson__rule_8cpp_source.html#l00119">composite_simpson_rule.cpp:119</a></div></div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="a1440a7779ac56f47a3f355ce4a8c7da0" name="a1440a7779ac56f47a3f355ce4a8c7da0"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a1440a7779ac56f47a3f355ce4a8c7da0">◆ </a></span>test1()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void test1 </td>
|
|
<td>(</td>
|
|
<td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Test that creates a random set of points distributed in four clusters in circumference of a circle and trains an SOM that finds that circular pattern. The following <a href="https://en.wikipedia.org/wiki/Comma-separated_values" target="_blank">CSV</a> files are created to validate the execution:</p><ul>
|
|
<li><span class="tt">test1.csv</span>: random test samples points with a circular pattern</li>
|
|
<li><span class="tt">w11.csv</span>: initial random map</li>
|
|
<li><span class="tt">w12.csv</span>: trained SOM map </li>
|
|
</ul>
|
|
|
|
<p class="definition">Definition at line <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00369">369</a> of file <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html">kohonen_som_topology.cpp</a>.</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 369</span> {</div>
|
|
<div class="line"><span class="lineno"> 370</span> <span class="keywordtype">int</span> j = 0, N = 300;</div>
|
|
<div class="line"><span class="lineno"> 371</span> <span class="keywordtype">int</span> features = 2;</div>
|
|
<div class="line"><span class="lineno"> 372</span> <span class="keywordtype">int</span> num_out = 30;</div>
|
|
<div class="line"><span class="lineno"> 373</span> std::vector<std::valarray<double>> X(N);</div>
|
|
<div class="line"><span class="lineno"> 374</span> std::vector<std::vector<std::valarray<double>>> W(num_out);</div>
|
|
<div class="line"><span class="lineno"> 375</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < std::max(num_out, N); i++) {</div>
|
|
<div class="line"><span class="lineno"> 376</span> <span class="comment">// loop till max(N, num_out)</span></div>
|
|
<div class="line"><span class="lineno"> 377</span> <span class="keywordflow">if</span> (i < N) { <span class="comment">// only add new arrays if i < N</span></div>
|
|
<div class="line"><span class="lineno"> 378</span> X[i] = std::valarray<double>(features);</div>
|
|
<div class="line"><span class="lineno"> 379</span> }</div>
|
|
<div class="line"><span class="lineno"> 380</span> <span class="keywordflow">if</span> (i < num_out) { <span class="comment">// only add new arrays if i < num_out</span></div>
|
|
<div class="line"><span class="lineno"> 381</span> W[i] = std::vector<std::valarray<double>>(num_out);</div>
|
|
<div class="line"><span class="lineno"> 382</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> k = 0; <a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a1b74d828b33760094906797042b89442">k</a> < num_out; <a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a1b74d828b33760094906797042b89442">k</a>++) {</div>
|
|
<div class="line"><span class="lineno"> 383</span> W[i][<a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a1b74d828b33760094906797042b89442">k</a>] = std::valarray<double>(features);</div>
|
|
<div class="line"><span class="lineno"> 384</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><span class="lineno"> 385</span><span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><span class="lineno"> 386</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 387</span> <span class="keywordflow">for</span> (j = 0; j < features; j++) {</div>
|
|
<div class="line"><span class="lineno"> 388</span> <span class="comment">// preallocate with random initial weights</span></div>
|
|
<div class="line"><span class="lineno"> 389</span> W[i][<a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a1b74d828b33760094906797042b89442">k</a>][j] = <a class="code hl_function" href="../../d9/d66/group__machine__learning.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(-10, 10);</div>
|
|
<div class="line"><span class="lineno"> 390</span> }</div>
|
|
<div class="line"><span class="lineno"> 391</span> }</div>
|
|
<div class="line"><span class="lineno"> 392</span> }</div>
|
|
<div class="line"><span class="lineno"> 393</span> }</div>
|
|
<div class="line"><span class="lineno"> 394</span> </div>
|
|
<div class="line"><span class="lineno"> 395</span> <a class="code hl_function" href="#a48efb079040c7aaa3a4917a0e486cba9">test_2d_classes</a>(&X); <span class="comment">// create test data around circumference of a circle</span></div>
|
|
<div class="line"><span class="lineno"> 396</span> <a class="code hl_function" href="../../d9/d66/group__machine__learning.html#gabc90175770bf0d5853c466e14993a08c">save_2d_data</a>(<span class="stringliteral">"test1.csv"</span>, X); <span class="comment">// save test data points</span></div>
|
|
<div class="line"><span class="lineno"> 397</span> <a class="code hl_function" href="#aa72a53c88203fde278f1fe6c3afe5b07">save_u_matrix</a>(<span class="stringliteral">"w11.csv"</span>, W); <span class="comment">// save initial random weights</span></div>
|
|
<div class="line"><span class="lineno"> 398</span> <a class="code hl_function" href="#ac43d294e21a0c4fa33c53757df054576">kohonen_som</a>(X, &W, 1e-4); <span class="comment">// train the SOM</span></div>
|
|
<div class="line"><span class="lineno"> 399</span> <a class="code hl_function" href="#aa72a53c88203fde278f1fe6c3afe5b07">save_u_matrix</a>(<span class="stringliteral">"w12.csv"</span>, W); <span class="comment">// save the resultant weights</span></div>
|
|
<div class="line"><span class="lineno"> 400</span>}</div>
|
|
<div class="ttc" id="acomposite__simpson__rule_8cpp_html_a1b74d828b33760094906797042b89442"><div class="ttname"><a href="../../d4/d18/composite__simpson__rule_8cpp.html#a1b74d828b33760094906797042b89442">numerical_methods::simpson_method::k</a></div><div class="ttdeci">double k(double x)</div><div class="ttdoc">Another test function.</div><div class="ttdef"><b>Definition</b> <a href="../../d4/d18/composite__simpson__rule_8cpp_source.html#l00117">composite_simpson_rule.cpp:117</a></div></div>
|
|
<div class="ttc" id="agroup__machine__learning_html_gabc90175770bf0d5853c466e14993a08c"><div class="ttname"><a href="../../d9/d66/group__machine__learning.html#gabc90175770bf0d5853c466e14993a08c">save_2d_data</a></div><div class="ttdeci">int save_2d_data(const char *fname, const std::vector< std::valarray< double > > &X)</div><div class="ttdef"><b>Definition</b> <a href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00065">kohonen_som_topology.cpp:65</a></div></div>
|
|
<div class="ttc" id="agroup__machine__learning_html_gaf5ce14f026d6d231bef29161bac2b485"><div class="ttname"><a href="../../d9/d66/group__machine__learning.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a></div><div class="ttdeci">double _random(double a, double b)</div><div class="ttdef"><b>Definition</b> <a href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00053">kohonen_som_topology.cpp:53</a></div></div>
|
|
<div class="ttc" id="akohonen__som__topology_8cpp_html_a48efb079040c7aaa3a4917a0e486cba9"><div class="ttname"><a href="#a48efb079040c7aaa3a4917a0e486cba9">test_2d_classes</a></div><div class="ttdeci">void test_2d_classes(std::vector< std::valarray< double > > *data)</div><div class="ttdef"><b>Definition</b> <a href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00330">kohonen_som_topology.cpp:330</a></div></div>
|
|
<div class="ttc" id="akohonen__som__topology_8cpp_html_aa72a53c88203fde278f1fe6c3afe5b07"><div class="ttname"><a href="#aa72a53c88203fde278f1fe6c3afe5b07">save_u_matrix</a></div><div class="ttdeci">int save_u_matrix(const char *fname, const std::vector< std::vector< std::valarray< double > > > &W)</div><div class="ttdef"><b>Definition</b> <a href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00142">kohonen_som_topology.cpp:142</a></div></div>
|
|
<div class="ttc" id="akohonen__som__topology_8cpp_html_ac43d294e21a0c4fa33c53757df054576"><div class="ttname"><a href="#ac43d294e21a0c4fa33c53757df054576">kohonen_som</a></div><div class="ttdeci">void kohonen_som(const std::vector< std::valarray< double > > &X, std::vector< std::vector< std::valarray< double > > > *W, double alpha_min)</div><div class="ttdef"><b>Definition</b> <a href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00269">kohonen_som_topology.cpp:269</a></div></div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="a0283886819c7c140a023582b7269e2d0" name="a0283886819c7c140a023582b7269e2d0"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a0283886819c7c140a023582b7269e2d0">◆ </a></span>test2()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void test2 </td>
|
|
<td>(</td>
|
|
<td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Test that creates a random set of points distributed in 4 clusters in 3D space and trains an SOM that finds the topological pattern. The following <a href="https://en.wikipedia.org/wiki/Comma-separated_values" target="_blank">CSV</a> files are created to validate the execution:</p><ul>
|
|
<li><span class="tt">test2.csv</span>: random test samples points with a lamniscate pattern</li>
|
|
<li><span class="tt">w21.csv</span>: initial random map</li>
|
|
<li><span class="tt">w22.csv</span>: trained SOM map </li>
|
|
</ul>
|
|
|
|
<p class="definition">Definition at line <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00451">451</a> of file <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html">kohonen_som_topology.cpp</a>.</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 451</span> {</div>
|
|
<div class="line"><span class="lineno"> 452</span> <span class="keywordtype">int</span> j = 0, N = 300;</div>
|
|
<div class="line"><span class="lineno"> 453</span> <span class="keywordtype">int</span> features = 3;</div>
|
|
<div class="line"><span class="lineno"> 454</span> <span class="keywordtype">int</span> num_out = 30;</div>
|
|
<div class="line"><span class="lineno"> 455</span> std::vector<std::valarray<double>> X(N);</div>
|
|
<div class="line"><span class="lineno"> 456</span> std::vector<std::vector<std::valarray<double>>> W(num_out);</div>
|
|
<div class="line"><span class="lineno"> 457</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < std::max(num_out, N); i++) {</div>
|
|
<div class="line"><span class="lineno"> 458</span> <span class="comment">// loop till max(N, num_out)</span></div>
|
|
<div class="line"><span class="lineno"> 459</span> <span class="keywordflow">if</span> (i < N) { <span class="comment">// only add new arrays if i < N</span></div>
|
|
<div class="line"><span class="lineno"> 460</span> X[i] = std::valarray<double>(features);</div>
|
|
<div class="line"><span class="lineno"> 461</span> }</div>
|
|
<div class="line"><span class="lineno"> 462</span> <span class="keywordflow">if</span> (i < num_out) { <span class="comment">// only add new arrays if i < num_out</span></div>
|
|
<div class="line"><span class="lineno"> 463</span> W[i] = std::vector<std::valarray<double>>(num_out);</div>
|
|
<div class="line"><span class="lineno"> 464</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> k = 0; <a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a1b74d828b33760094906797042b89442">k</a> < num_out; <a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a1b74d828b33760094906797042b89442">k</a>++) {</div>
|
|
<div class="line"><span class="lineno"> 465</span> W[i][<a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a1b74d828b33760094906797042b89442">k</a>] = std::valarray<double>(features);</div>
|
|
<div class="line"><span class="lineno"> 466</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><span class="lineno"> 467</span><span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><span class="lineno"> 468</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 469</span> <span class="keywordflow">for</span> (j = 0; j < features; j++) {</div>
|
|
<div class="line"><span class="lineno"> 470</span> <span class="comment">// preallocate with random initial weights</span></div>
|
|
<div class="line"><span class="lineno"> 471</span> W[i][<a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a1b74d828b33760094906797042b89442">k</a>][j] = <a class="code hl_function" href="../../d9/d66/group__machine__learning.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(-10, 10);</div>
|
|
<div class="line"><span class="lineno"> 472</span> }</div>
|
|
<div class="line"><span class="lineno"> 473</span> }</div>
|
|
<div class="line"><span class="lineno"> 474</span> }</div>
|
|
<div class="line"><span class="lineno"> 475</span> }</div>
|
|
<div class="line"><span class="lineno"> 476</span> </div>
|
|
<div class="line"><span class="lineno"> 477</span> <a class="code hl_function" href="#a1302662a56ebf67a21249270b017297e">test_3d_classes1</a>(&X); <span class="comment">// create test data around circumference of a circle</span></div>
|
|
<div class="line"><span class="lineno"> 478</span> <a class="code hl_function" href="../../d9/d66/group__machine__learning.html#gabc90175770bf0d5853c466e14993a08c">save_2d_data</a>(<span class="stringliteral">"test2.csv"</span>, X); <span class="comment">// save test data points</span></div>
|
|
<div class="line"><span class="lineno"> 479</span> <a class="code hl_function" href="#aa72a53c88203fde278f1fe6c3afe5b07">save_u_matrix</a>(<span class="stringliteral">"w21.csv"</span>, W); <span class="comment">// save initial random weights</span></div>
|
|
<div class="line"><span class="lineno"> 480</span> <a class="code hl_function" href="#ac43d294e21a0c4fa33c53757df054576">kohonen_som</a>(X, &W, 1e-4); <span class="comment">// train the SOM</span></div>
|
|
<div class="line"><span class="lineno"> 481</span> <a class="code hl_function" href="#aa72a53c88203fde278f1fe6c3afe5b07">save_u_matrix</a>(<span class="stringliteral">"w22.csv"</span>, W); <span class="comment">// save the resultant weights</span></div>
|
|
<div class="line"><span class="lineno"> 482</span>}</div>
|
|
<div class="ttc" id="akohonen__som__topology_8cpp_html_a1302662a56ebf67a21249270b017297e"><div class="ttname"><a href="#a1302662a56ebf67a21249270b017297e">test_3d_classes1</a></div><div class="ttdeci">void test_3d_classes1(std::vector< std::valarray< double > > *data)</div><div class="ttdef"><b>Definition</b> <a href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00411">kohonen_som_topology.cpp:411</a></div></div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="a6d0455dd5c30adda100e95f0423c786e" name="a6d0455dd5c30adda100e95f0423c786e"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a6d0455dd5c30adda100e95f0423c786e">◆ </a></span>test3()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void test3 </td>
|
|
<td>(</td>
|
|
<td class="paramname"><span class="paramname"><em></em></span></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Test that creates a random set of points distributed in eight clusters in 3D space and trains an SOM that finds the topological pattern. The following <a href="https://en.wikipedia.org/wiki/Comma-separated_values" target="_blank">CSV</a> files are created to validate the execution:</p><ul>
|
|
<li><span class="tt">test3.csv</span>: random test samples points with a circular pattern</li>
|
|
<li><span class="tt">w31.csv</span>: initial random map</li>
|
|
<li><span class="tt">w32.csv</span>: trained SOM map </li>
|
|
</ul>
|
|
|
|
<p class="definition">Definition at line <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00537">537</a> of file <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html">kohonen_som_topology.cpp</a>.</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 537</span> {</div>
|
|
<div class="line"><span class="lineno"> 538</span> <span class="keywordtype">int</span> j = 0, N = 500;</div>
|
|
<div class="line"><span class="lineno"> 539</span> <span class="keywordtype">int</span> features = 3;</div>
|
|
<div class="line"><span class="lineno"> 540</span> <span class="keywordtype">int</span> num_out = 30;</div>
|
|
<div class="line"><span class="lineno"> 541</span> std::vector<std::valarray<double>> X(N);</div>
|
|
<div class="line"><span class="lineno"> 542</span> std::vector<std::vector<std::valarray<double>>> W(num_out);</div>
|
|
<div class="line"><span class="lineno"> 543</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < std::max(num_out, N); i++) {</div>
|
|
<div class="line"><span class="lineno"> 544</span> <span class="comment">// loop till max(N, num_out)</span></div>
|
|
<div class="line"><span class="lineno"> 545</span> <span class="keywordflow">if</span> (i < N) { <span class="comment">// only add new arrays if i < N</span></div>
|
|
<div class="line"><span class="lineno"> 546</span> X[i] = std::valarray<double>(features);</div>
|
|
<div class="line"><span class="lineno"> 547</span> }</div>
|
|
<div class="line"><span class="lineno"> 548</span> <span class="keywordflow">if</span> (i < num_out) { <span class="comment">// only add new arrays if i < num_out</span></div>
|
|
<div class="line"><span class="lineno"> 549</span> W[i] = std::vector<std::valarray<double>>(num_out);</div>
|
|
<div class="line"><span class="lineno"> 550</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> k = 0; <a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a1b74d828b33760094906797042b89442">k</a> < num_out; <a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a1b74d828b33760094906797042b89442">k</a>++) {</div>
|
|
<div class="line"><span class="lineno"> 551</span> W[i][<a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a1b74d828b33760094906797042b89442">k</a>] = std::valarray<double>(features);</div>
|
|
<div class="line"><span class="lineno"> 552</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><span class="lineno"> 553</span><span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><span class="lineno"> 554</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 555</span> <span class="keywordflow">for</span> (j = 0; j < features; j++) {</div>
|
|
<div class="line"><span class="lineno"> 556</span> <span class="comment">// preallocate with random initial weights</span></div>
|
|
<div class="line"><span class="lineno"> 557</span> W[i][<a class="code hl_function" href="../../d4/d18/composite__simpson__rule_8cpp.html#a1b74d828b33760094906797042b89442">k</a>][j] = <a class="code hl_function" href="../../d9/d66/group__machine__learning.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(-10, 10);</div>
|
|
<div class="line"><span class="lineno"> 558</span> }</div>
|
|
<div class="line"><span class="lineno"> 559</span> }</div>
|
|
<div class="line"><span class="lineno"> 560</span> }</div>
|
|
<div class="line"><span class="lineno"> 561</span> }</div>
|
|
<div class="line"><span class="lineno"> 562</span> </div>
|
|
<div class="line"><span class="lineno"> 563</span> <a class="code hl_function" href="#a4b7ab643f6a5002f991837de46f70653">test_3d_classes2</a>(&X); <span class="comment">// create test data around circumference of a circle</span></div>
|
|
<div class="line"><span class="lineno"> 564</span> <a class="code hl_function" href="../../d9/d66/group__machine__learning.html#gabc90175770bf0d5853c466e14993a08c">save_2d_data</a>(<span class="stringliteral">"test3.csv"</span>, X); <span class="comment">// save test data points</span></div>
|
|
<div class="line"><span class="lineno"> 565</span> <a class="code hl_function" href="#aa72a53c88203fde278f1fe6c3afe5b07">save_u_matrix</a>(<span class="stringliteral">"w31.csv"</span>, W); <span class="comment">// save initial random weights</span></div>
|
|
<div class="line"><span class="lineno"> 566</span> <a class="code hl_function" href="#ac43d294e21a0c4fa33c53757df054576">kohonen_som</a>(X, &W, 1e-4); <span class="comment">// train the SOM</span></div>
|
|
<div class="line"><span class="lineno"> 567</span> <a class="code hl_function" href="#aa72a53c88203fde278f1fe6c3afe5b07">save_u_matrix</a>(<span class="stringliteral">"w32.csv"</span>, W); <span class="comment">// save the resultant weights</span></div>
|
|
<div class="line"><span class="lineno"> 568</span>}</div>
|
|
<div class="ttc" id="akohonen__som__topology_8cpp_html_a4b7ab643f6a5002f991837de46f70653"><div class="ttname"><a href="#a4b7ab643f6a5002f991837de46f70653">test_3d_classes2</a></div><div class="ttdeci">void test_3d_classes2(std::vector< std::valarray< double > > *data)</div><div class="ttdef"><b>Definition</b> <a href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00493">kohonen_som_topology.cpp:493</a></div></div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="a48efb079040c7aaa3a4917a0e486cba9" name="a48efb079040c7aaa3a4917a0e486cba9"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a48efb079040c7aaa3a4917a0e486cba9">◆ </a></span>test_2d_classes()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void test_2d_classes </td>
|
|
<td>(</td>
|
|
<td class="paramtype">std::vector< std::valarray< double > > *</td> <td class="paramname"><span class="paramname"><em>data</em></span></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Creates a random set of points distributed in four clusters in 3D space with centroids at the points</p><ul>
|
|
<li>\((0,5, 0.5, 0.5)\)</li>
|
|
<li>\((0,5,-0.5, -0.5)\)</li>
|
|
<li>\((-0,5, 0.5, 0.5)\)</li>
|
|
<li>\((-0,5,-0.5, -0.5)\)</li>
|
|
</ul>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramdir">[out]</td><td class="paramname">data</td><td>matrix to store data in </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
|
|
<p class="definition">Definition at line <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00330">330</a> of file <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html">kohonen_som_topology.cpp</a>.</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 330</span> {</div>
|
|
<div class="line"><span class="lineno"> 331</span> <span class="keyword">const</span> <span class="keywordtype">int</span> N = <a class="code hl_variable" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>->size();</div>
|
|
<div class="line"><span class="lineno"> 332</span> <span class="keyword">const</span> <span class="keywordtype">double</span> R = 0.3; <span class="comment">// radius of cluster</span></div>
|
|
<div class="line"><span class="lineno"> 333</span> <span class="keywordtype">int</span> i = 0;</div>
|
|
<div class="line"><span class="lineno"> 334</span> <span class="keyword">const</span> <span class="keywordtype">int</span> num_classes = 4;</div>
|
|
<div class="line"><span class="lineno"> 335</span> std::array<std::array<double, 2>, num_classes> centres = {</div>
|
|
<div class="line"><span class="lineno"> 336</span> <span class="comment">// centres of each class cluster</span></div>
|
|
<div class="line"><span class="lineno"> 337</span> std::array<double, 2>({.5, .5}), <span class="comment">// centre of class 1</span></div>
|
|
<div class="line"><span class="lineno"> 338</span> std::array<double, 2>({.5, -.5}), <span class="comment">// centre of class 2</span></div>
|
|
<div class="line"><span class="lineno"> 339</span> std::array<double, 2>({-.5, .5}), <span class="comment">// centre of class 3</span></div>
|
|
<div class="line"><span class="lineno"> 340</span> std::array<double, 2>({-.5, -.5}) <span class="comment">// centre of class 4</span></div>
|
|
<div class="line"><span class="lineno"> 341</span> };</div>
|
|
<div class="line"><span class="lineno"> 342</span> </div>
|
|
<div class="line"><span class="lineno"> 343</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><span class="lineno"> 344</span><span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><span class="lineno"> 345</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 346</span> <span class="keywordflow">for</span> (i = 0; i < N; i++) {</div>
|
|
<div class="line"><span class="lineno"> 347</span> <span class="comment">// select a random class for the point</span></div>
|
|
<div class="line"><span class="lineno"> 348</span> <span class="keywordtype">int</span> cls = std::rand() % num_classes;</div>
|
|
<div class="line"><span class="lineno"> 349</span> </div>
|
|
<div class="line"><span class="lineno"> 350</span> <span class="comment">// create random coordinates (x,y,z) around the centre of the class</span></div>
|
|
<div class="line"><span class="lineno"> 351</span> <a class="code hl_variable" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>[0][i][0] = <a class="code hl_function" href="../../d9/d66/group__machine__learning.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(centres[cls][0] - R, centres[cls][0] + R);</div>
|
|
<div class="line"><span class="lineno"> 352</span> <a class="code hl_variable" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>[0][i][1] = <a class="code hl_function" href="../../d9/d66/group__machine__learning.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(centres[cls][1] - R, centres[cls][1] + R);</div>
|
|
<div class="line"><span class="lineno"> 353</span> </div>
|
|
<div class="line"><span class="lineno"> 354</span> <span class="comment">/* The follosing can also be used</span></div>
|
|
<div class="line"><span class="lineno"> 355</span><span class="comment"> for (int j = 0; j < 2; j++)</span></div>
|
|
<div class="line"><span class="lineno"> 356</span><span class="comment"> data[i][j] = _random(centres[class][j] - R, centres[class][j] + R);</span></div>
|
|
<div class="line"><span class="lineno"> 357</span><span class="comment"> */</span></div>
|
|
<div class="line"><span class="lineno"> 358</span> }</div>
|
|
<div class="line"><span class="lineno"> 359</span>}</div>
|
|
<div class="ttc" id="ahash__search_8cpp_html_a6e1a77282bc65ad359d753d25df23243"><div class="ttname"><a href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a></div><div class="ttdeci">int data[MAX]</div><div class="ttdoc">test data</div><div class="ttdef"><b>Definition</b> <a href="../../d1/df3/hash__search_8cpp_source.html#l00024">hash_search.cpp:24</a></div></div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="a1302662a56ebf67a21249270b017297e" name="a1302662a56ebf67a21249270b017297e"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a1302662a56ebf67a21249270b017297e">◆ </a></span>test_3d_classes1()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void test_3d_classes1 </td>
|
|
<td>(</td>
|
|
<td class="paramtype">std::vector< std::valarray< double > > *</td> <td class="paramname"><span class="paramname"><em>data</em></span></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Creates a random set of points distributed in four clusters in 3D space with centroids at the points</p><ul>
|
|
<li>\((0,5, 0.5, 0.5)\)</li>
|
|
<li>\((0,5,-0.5, -0.5)\)</li>
|
|
<li>\((-0,5, 0.5, 0.5)\)</li>
|
|
<li>\((-0,5,-0.5, -0.5)\)</li>
|
|
</ul>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramdir">[out]</td><td class="paramname">data</td><td>matrix to store data in </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
|
|
<p class="definition">Definition at line <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00411">411</a> of file <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html">kohonen_som_topology.cpp</a>.</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 411</span> {</div>
|
|
<div class="line"><span class="lineno"> 412</span> <span class="keyword">const</span> <span class="keywordtype">size_t</span> N = <a class="code hl_variable" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>->size();</div>
|
|
<div class="line"><span class="lineno"> 413</span> <span class="keyword">const</span> <span class="keywordtype">double</span> R = 0.3; <span class="comment">// radius of cluster</span></div>
|
|
<div class="line"><span class="lineno"> 414</span> <span class="keywordtype">int</span> i = 0;</div>
|
|
<div class="line"><span class="lineno"> 415</span> <span class="keyword">const</span> <span class="keywordtype">int</span> num_classes = 4;</div>
|
|
<div class="line"><span class="lineno"> 416</span> <span class="keyword">const</span> std::array<std::array<double, 3>, num_classes> centres = {</div>
|
|
<div class="line"><span class="lineno"> 417</span> <span class="comment">// centres of each class cluster</span></div>
|
|
<div class="line"><span class="lineno"> 418</span> std::array<double, 3>({.5, .5, .5}), <span class="comment">// centre of class 1</span></div>
|
|
<div class="line"><span class="lineno"> 419</span> std::array<double, 3>({.5, -.5, -.5}), <span class="comment">// centre of class 2</span></div>
|
|
<div class="line"><span class="lineno"> 420</span> std::array<double, 3>({-.5, .5, .5}), <span class="comment">// centre of class 3</span></div>
|
|
<div class="line"><span class="lineno"> 421</span> std::array<double, 3>({-.5, -.5 - .5}) <span class="comment">// centre of class 4</span></div>
|
|
<div class="line"><span class="lineno"> 422</span> };</div>
|
|
<div class="line"><span class="lineno"> 423</span> </div>
|
|
<div class="line"><span class="lineno"> 424</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><span class="lineno"> 425</span><span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><span class="lineno"> 426</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 427</span> <span class="keywordflow">for</span> (i = 0; i < N; i++) {</div>
|
|
<div class="line"><span class="lineno"> 428</span> <span class="comment">// select a random class for the point</span></div>
|
|
<div class="line"><span class="lineno"> 429</span> <span class="keywordtype">int</span> cls = std::rand() % num_classes;</div>
|
|
<div class="line"><span class="lineno"> 430</span> </div>
|
|
<div class="line"><span class="lineno"> 431</span> <span class="comment">// create random coordinates (x,y,z) around the centre of the class</span></div>
|
|
<div class="line"><span class="lineno"> 432</span> <a class="code hl_variable" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>[0][i][0] = <a class="code hl_function" href="../../d9/d66/group__machine__learning.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(centres[cls][0] - R, centres[cls][0] + R);</div>
|
|
<div class="line"><span class="lineno"> 433</span> <a class="code hl_variable" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>[0][i][1] = <a class="code hl_function" href="../../d9/d66/group__machine__learning.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(centres[cls][1] - R, centres[cls][1] + R);</div>
|
|
<div class="line"><span class="lineno"> 434</span> <a class="code hl_variable" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>[0][i][2] = <a class="code hl_function" href="../../d9/d66/group__machine__learning.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(centres[cls][2] - R, centres[cls][2] + R);</div>
|
|
<div class="line"><span class="lineno"> 435</span> </div>
|
|
<div class="line"><span class="lineno"> 436</span> <span class="comment">/* The follosing can also be used</span></div>
|
|
<div class="line"><span class="lineno"> 437</span><span class="comment"> for (int j = 0; j < 3; j++)</span></div>
|
|
<div class="line"><span class="lineno"> 438</span><span class="comment"> data[i][j] = _random(centres[class][j] - R, centres[class][j] + R);</span></div>
|
|
<div class="line"><span class="lineno"> 439</span><span class="comment"> */</span></div>
|
|
<div class="line"><span class="lineno"> 440</span> }</div>
|
|
<div class="line"><span class="lineno"> 441</span>}</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="a4b7ab643f6a5002f991837de46f70653" name="a4b7ab643f6a5002f991837de46f70653"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a4b7ab643f6a5002f991837de46f70653">◆ </a></span>test_3d_classes2()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void test_3d_classes2 </td>
|
|
<td>(</td>
|
|
<td class="paramtype">std::vector< std::valarray< double > > *</td> <td class="paramname"><span class="paramname"><em>data</em></span></td><td>)</td>
|
|
<td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p>Creates a random set of points distributed in four clusters in 3D space with centroids at the points</p><ul>
|
|
<li>\((0,5, 0.5, 0.5)\)</li>
|
|
<li>\((0,5,-0.5, -0.5)\)</li>
|
|
<li>\((-0,5, 0.5, 0.5)\)</li>
|
|
<li>\((-0,5,-0.5, -0.5)\)</li>
|
|
</ul>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramdir">[out]</td><td class="paramname">data</td><td>matrix to store data in </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
|
|
<p class="definition">Definition at line <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html#l00493">493</a> of file <a class="el" href="../../d4/def/kohonen__som__topology_8cpp_source.html">kohonen_som_topology.cpp</a>.</p>
|
|
<div class="fragment"><div class="line"><span class="lineno"> 493</span> {</div>
|
|
<div class="line"><span class="lineno"> 494</span> <span class="keyword">const</span> <span class="keywordtype">size_t</span> N = <a class="code hl_variable" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>->size();</div>
|
|
<div class="line"><span class="lineno"> 495</span> <span class="keyword">const</span> <span class="keywordtype">double</span> R = 0.2; <span class="comment">// radius of cluster</span></div>
|
|
<div class="line"><span class="lineno"> 496</span> <span class="keywordtype">int</span> i = 0;</div>
|
|
<div class="line"><span class="lineno"> 497</span> <span class="keyword">const</span> <span class="keywordtype">int</span> num_classes = 8;</div>
|
|
<div class="line"><span class="lineno"> 498</span> <span class="keyword">const</span> std::array<std::array<double, 3>, num_classes> centres = {</div>
|
|
<div class="line"><span class="lineno"> 499</span> <span class="comment">// centres of each class cluster</span></div>
|
|
<div class="line"><span class="lineno"> 500</span> std::array<double, 3>({.5, .5, .5}), <span class="comment">// centre of class 1</span></div>
|
|
<div class="line"><span class="lineno"> 501</span> std::array<double, 3>({.5, .5, -.5}), <span class="comment">// centre of class 2</span></div>
|
|
<div class="line"><span class="lineno"> 502</span> std::array<double, 3>({.5, -.5, .5}), <span class="comment">// centre of class 3</span></div>
|
|
<div class="line"><span class="lineno"> 503</span> std::array<double, 3>({.5, -.5, -.5}), <span class="comment">// centre of class 4</span></div>
|
|
<div class="line"><span class="lineno"> 504</span> std::array<double, 3>({-.5, .5, .5}), <span class="comment">// centre of class 5</span></div>
|
|
<div class="line"><span class="lineno"> 505</span> std::array<double, 3>({-.5, .5, -.5}), <span class="comment">// centre of class 6</span></div>
|
|
<div class="line"><span class="lineno"> 506</span> std::array<double, 3>({-.5, -.5, .5}), <span class="comment">// centre of class 7</span></div>
|
|
<div class="line"><span class="lineno"> 507</span> std::array<double, 3>({-.5, -.5, -.5}) <span class="comment">// centre of class 8</span></div>
|
|
<div class="line"><span class="lineno"> 508</span> };</div>
|
|
<div class="line"><span class="lineno"> 509</span> </div>
|
|
<div class="line"><span class="lineno"> 510</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><span class="lineno"> 511</span><span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><span class="lineno"> 512</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><span class="lineno"> 513</span> <span class="keywordflow">for</span> (i = 0; i < N; i++) {</div>
|
|
<div class="line"><span class="lineno"> 514</span> <span class="comment">// select a random class for the point</span></div>
|
|
<div class="line"><span class="lineno"> 515</span> <span class="keywordtype">int</span> cls = std::rand() % num_classes;</div>
|
|
<div class="line"><span class="lineno"> 516</span> </div>
|
|
<div class="line"><span class="lineno"> 517</span> <span class="comment">// create random coordinates (x,y,z) around the centre of the class</span></div>
|
|
<div class="line"><span class="lineno"> 518</span> <a class="code hl_variable" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>[0][i][0] = <a class="code hl_function" href="../../d9/d66/group__machine__learning.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(centres[cls][0] - R, centres[cls][0] + R);</div>
|
|
<div class="line"><span class="lineno"> 519</span> <a class="code hl_variable" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>[0][i][1] = <a class="code hl_function" href="../../d9/d66/group__machine__learning.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(centres[cls][1] - R, centres[cls][1] + R);</div>
|
|
<div class="line"><span class="lineno"> 520</span> <a class="code hl_variable" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>[0][i][2] = <a class="code hl_function" href="../../d9/d66/group__machine__learning.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(centres[cls][2] - R, centres[cls][2] + R);</div>
|
|
<div class="line"><span class="lineno"> 521</span> </div>
|
|
<div class="line"><span class="lineno"> 522</span> <span class="comment">/* The follosing can also be used</span></div>
|
|
<div class="line"><span class="lineno"> 523</span><span class="comment"> for (int j = 0; j < 3; j++)</span></div>
|
|
<div class="line"><span class="lineno"> 524</span><span class="comment"> data[i][j] = _random(centres[class][j] - R, centres[class][j] + R);</span></div>
|
|
<div class="line"><span class="lineno"> 525</span><span class="comment"> */</span></div>
|
|
<div class="line"><span class="lineno"> 526</span> }</div>
|
|
<div class="line"><span class="lineno"> 527</span>}</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
</div><!-- contents -->
|
|
</div><!-- doc-content -->
|
|
<div id="page-nav" class="page-nav-panel">
|
|
<div id="page-nav-resize-handle"></div>
|
|
<div id="page-nav-tree">
|
|
<div id="page-nav-contents">
|
|
</div><!-- page-nav-contents -->
|
|
</div><!-- page-nav-tree -->
|
|
</div><!-- page-nav -->
|
|
</div><!-- container -->
|
|
<!-- start footer part -->
|
|
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
|
<ul>
|
|
<li class="navelem"><a href="../../dir_3343723ae086de42ee4ca9774da3a13f.html">machine_learning</a></li><li class="navelem"><a href="../../d4/def/kohonen__som__topology_8cpp.html">kohonen_som_topology.cpp</a></li>
|
|
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.14.0 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|