mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-03-23 05:12:40 +08:00
697 lines
83 KiB
HTML
697 lines
83 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
|
|
<meta name="generator" content="Doxygen 1.9.2"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>Algorithms_in_C++: machine_learning/kohonen_som_topology.cpp File Reference</title>
|
|
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../jquery.js"></script>
|
|
<script type="text/javascript" src="../../dynsections.js"></script>
|
|
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../resize.js"></script>
|
|
<script type="text/javascript" src="../../navtreedata.js"></script>
|
|
<script type="text/javascript" src="../../navtree.js"></script>
|
|
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../search/searchdata.js"></script>
|
|
<script type="text/javascript" src="../../search/search.js"></script>
|
|
<script type="text/x-mathjax-config">
|
|
MathJax.Hub.Config({
|
|
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
|
|
jax: ["input/TeX","output/HTML-CSS"],
|
|
});
|
|
</script>
|
|
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
|
|
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
|
|
</head>
|
|
<body>
|
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
|
<div id="titlearea">
|
|
<table cellspacing="0" cellpadding="0">
|
|
<tbody>
|
|
<tr style="height: 56px;">
|
|
<td id="projectalign" style="padding-left: 0.5em;">
|
|
<div id="projectname">Algorithms_in_C++<span id="projectnumber"> 1.0.0</span>
|
|
</div>
|
|
<div id="projectbrief">Set of algorithms implemented in C++.</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.9.2 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
var searchBox = new SearchBox("searchBox", "../../search",'Search','.html');
|
|
/* @license-end */
|
|
</script>
|
|
<script type="text/javascript" src="../../menudata.js"></script>
|
|
<script type="text/javascript" src="../../menu.js"></script>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(function() {
|
|
initMenu('../../',true,false,'search.php','Search');
|
|
$(document).ready(function() { init_search(); });
|
|
});
|
|
/* @license-end */
|
|
</script>
|
|
<div id="main-nav"></div>
|
|
</div><!-- top -->
|
|
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
|
<div id="nav-tree">
|
|
<div id="nav-tree-contents">
|
|
<div id="nav-sync" class="sync"></div>
|
|
</div>
|
|
</div>
|
|
<div id="splitbar" style="-moz-user-select:none;"
|
|
class="ui-resizable-handle">
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt MIT */
|
|
$(document).ready(function(){initNavTree('d4/def/kohonen__som__topology_8cpp.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="#namespaces">Namespaces</a> |
|
|
<a href="#define-members">Macros</a> |
|
|
<a href="#func-members">Functions</a> |
|
|
<a href="#var-members">Variables</a> </div>
|
|
<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" frameborder="0" src="../../d5/dcd/kohonen__som__topology_8cpp__incl.svg" width="100%" height="395"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
|
|
</div>
|
|
</div>
|
|
</div><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="namespaces" name="namespaces"></a>
|
|
Namespaces</h2></td></tr>
|
|
<tr class="memitem:d8/d77/namespacemachine__learning"><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>
|
|
<tr class="separator:"><td class="memSeparator" colspan="2"> </td></tr>
|
|
</table><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
|
|
Functions</h2></td></tr>
|
|
<tr class="memitem:gaf5ce14f026d6d231bef29161bac2b485"><td class="memItemLeft" align="right" valign="top">double </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d9/d66/group__machine__learning.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a> (double a, double b)</td></tr>
|
|
<tr class="separator:gaf5ce14f026d6d231bef29161bac2b485"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem: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 <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray</a>< double > > &X)</td></tr>
|
|
<tr class="separator:gabc90175770bf0d5853c466e14993a08c"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem: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 <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray</a>< double > > &X, double *val, int *x_idx, int *y_idx)</td></tr>
|
|
<tr class="separator:ga60f9186ccb682724a8792a2bf81e9b9e"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem: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 <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray</a>< double > > > &W)</td></tr>
|
|
<tr class="separator:aa72a53c88203fde278f1fe6c3afe5b07"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem: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 <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray</a>< double > &X, <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray</a>< double > > > *W, <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray</a>< double > > *D, double alpha, int R)</td></tr>
|
|
<tr class="separator:ae868ad43698a1d69ba46ea3827d7d2c3"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem: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 <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray</a>< double > > &X, <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray</a>< double > > > *W, double alpha_min)</td></tr>
|
|
<tr class="separator:ac43d294e21a0c4fa33c53757df054576"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a48efb079040c7aaa3a4917a0e486cba9"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d4/def/kohonen__som__topology_8cpp.html#a48efb079040c7aaa3a4917a0e486cba9">test_2d_classes</a> (<a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray</a>< double > > *<a class="el" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>)</td></tr>
|
|
<tr class="separator:a48efb079040c7aaa3a4917a0e486cba9"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a1440a7779ac56f47a3f355ce4a8c7da0"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d4/def/kohonen__som__topology_8cpp.html#a1440a7779ac56f47a3f355ce4a8c7da0">test1</a> ()</td></tr>
|
|
<tr class="separator:a1440a7779ac56f47a3f355ce4a8c7da0"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a1302662a56ebf67a21249270b017297e"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d4/def/kohonen__som__topology_8cpp.html#a1302662a56ebf67a21249270b017297e">test_3d_classes1</a> (<a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray</a>< double > > *<a class="el" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>)</td></tr>
|
|
<tr class="separator:a1302662a56ebf67a21249270b017297e"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a0283886819c7c140a023582b7269e2d0"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d4/def/kohonen__som__topology_8cpp.html#a0283886819c7c140a023582b7269e2d0">test2</a> ()</td></tr>
|
|
<tr class="separator:a0283886819c7c140a023582b7269e2d0"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a4b7ab643f6a5002f991837de46f70653"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d4/def/kohonen__som__topology_8cpp.html#a4b7ab643f6a5002f991837de46f70653">test_3d_classes2</a> (<a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray</a>< double > > *<a class="el" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>)</td></tr>
|
|
<tr class="separator:a4b7ab643f6a5002f991837de46f70653"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a6d0455dd5c30adda100e95f0423c786e"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d4/def/kohonen__som__topology_8cpp.html#a6d0455dd5c30adda100e95f0423c786e">test3</a> ()</td></tr>
|
|
<tr class="separator:a6d0455dd5c30adda100e95f0423c786e"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a2256c10b16edba377b64a44b6c656908"><td class="memItemLeft" align="right" valign="top">double </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d4/def/kohonen__som__topology_8cpp.html#a2256c10b16edba377b64a44b6c656908">get_clock_diff</a> (clock_t start_t, clock_t end_t)</td></tr>
|
|
<tr class="separator:a2256c10b16edba377b64a44b6c656908"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d4/def/kohonen__som__topology_8cpp.html#a3c04138a5bfe5d72780bb7e82a18e627">main</a> (int argc, char **argv)</td></tr>
|
|
<tr class="separator:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memSeparator" colspan="2"> </td></tr>
|
|
</table><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="var-members" name="var-members"></a>
|
|
Variables</h2></td></tr>
|
|
<tr class="memitem: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>
|
|
<tr class="separator:a7220dbb7fa896d83bfb7a50e4fce1786"><td class="memSeparator" colspan="2"> </td></tr>
|
|
</table>
|
|
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
|
|
<div class="textblock"><p ><a href="https://en.wikipedia.org/wiki/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>
|
|
</div><h2 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"><em>start_t</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">clock_t </td>
|
|
<td class="paramname"><em>end_t</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></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>
|
|
<div class="fragment"><div class="line"><a id="l00577" name="l00577"></a><span class="lineno"> 577</span> {</div>
|
|
<div class="line"><a id="l00578" name="l00578"></a><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"><a id="l00579" name="l00579"></a><span class="lineno"> 579</span>}</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="a3c04138a5bfe5d72780bb7e82a18e627" name="a3c04138a5bfe5d72780bb7e82a18e627"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a3c04138a5bfe5d72780bb7e82a18e627">◆ </a></span>main()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">int main </td>
|
|
<td>(</td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>argc</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">char ** </td>
|
|
<td class="paramname"><em>argv</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
<p >Main function </p>
|
|
<div class="fragment"><div class="line"><a id="l00582" name="l00582"></a><span class="lineno"> 582</span> {</div>
|
|
<div class="line"><a id="l00583" name="l00583"></a><span class="lineno"> 583</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><a id="l00584" name="l00584"></a><span class="lineno"> 584</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Using OpenMP based parallelization\n"</span>;</div>
|
|
<div class="line"><a id="l00585" name="l00585"></a><span class="lineno"> 585</span><span class="preprocessor">#else</span></div>
|
|
<div class="line"><a id="l00586" name="l00586"></a><span class="lineno"> 586</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"NOT using OpenMP based parallelization\n"</span>;</div>
|
|
<div class="line"><a id="l00587" name="l00587"></a><span class="lineno"> 587</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a id="l00588" name="l00588"></a><span class="lineno"> 588</span> </div>
|
|
<div class="line"><a id="l00589" name="l00589"></a><span class="lineno"> 589</span> <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/random/srand.html">std::srand</a>(<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/chrono/c/time.html">std::time</a>(<span class="keyword">nullptr</span>));</div>
|
|
<div class="line"><a id="l00590" name="l00590"></a><span class="lineno"> 590</span> </div>
|
|
<div class="line"><a id="l00591" name="l00591"></a><span class="lineno"> 591</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/chrono/c/clock_t.html">std::clock_t</a> start_clk = <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/chrono/c/clock.html">std::clock</a>();</div>
|
|
<div class="line"><a id="l00592" name="l00592"></a><span class="lineno"> 592</span> <a class="code hl_function" href="../../d4/def/kohonen__som__topology_8cpp.html#a1440a7779ac56f47a3f355ce4a8c7da0">test1</a>();</div>
|
|
<div class="line"><a id="l00593" name="l00593"></a><span class="lineno"> 593</span> <span class="keyword">auto</span> end_clk = <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/chrono/c/clock.html">std::clock</a>();</div>
|
|
<div class="line"><a id="l00594" name="l00594"></a><span class="lineno"> 594</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Test 1 completed in "</span> << <a class="code hl_function" href="../../d4/def/kohonen__som__topology_8cpp.html#a2256c10b16edba377b64a44b6c656908">get_clock_diff</a>(start_clk, end_clk)</div>
|
|
<div class="line"><a id="l00595" name="l00595"></a><span class="lineno"> 595</span> << <span class="stringliteral">" sec\n"</span>;</div>
|
|
<div class="line"><a id="l00596" name="l00596"></a><span class="lineno"> 596</span> </div>
|
|
<div class="line"><a id="l00597" name="l00597"></a><span class="lineno"> 597</span> start_clk = <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/chrono/c/clock.html">std::clock</a>();</div>
|
|
<div class="line"><a id="l00598" name="l00598"></a><span class="lineno"> 598</span> <a class="code hl_function" href="../../d4/def/kohonen__som__topology_8cpp.html#a0283886819c7c140a023582b7269e2d0">test2</a>();</div>
|
|
<div class="line"><a id="l00599" name="l00599"></a><span class="lineno"> 599</span> end_clk = <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/chrono/c/clock.html">std::clock</a>();</div>
|
|
<div class="line"><a id="l00600" name="l00600"></a><span class="lineno"> 600</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Test 2 completed in "</span> << <a class="code hl_function" href="../../d4/def/kohonen__som__topology_8cpp.html#a2256c10b16edba377b64a44b6c656908">get_clock_diff</a>(start_clk, end_clk)</div>
|
|
<div class="line"><a id="l00601" name="l00601"></a><span class="lineno"> 601</span> << <span class="stringliteral">" sec\n"</span>;</div>
|
|
<div class="line"><a id="l00602" name="l00602"></a><span class="lineno"> 602</span> </div>
|
|
<div class="line"><a id="l00603" name="l00603"></a><span class="lineno"> 603</span> start_clk = <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/chrono/c/clock.html">std::clock</a>();</div>
|
|
<div class="line"><a id="l00604" name="l00604"></a><span class="lineno"> 604</span> <a class="code hl_function" href="../../d4/def/kohonen__som__topology_8cpp.html#a6d0455dd5c30adda100e95f0423c786e">test3</a>();</div>
|
|
<div class="line"><a id="l00605" name="l00605"></a><span class="lineno"> 605</span> end_clk = <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/chrono/c/clock.html">std::clock</a>();</div>
|
|
<div class="line"><a id="l00606" name="l00606"></a><span class="lineno"> 606</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> << <span class="stringliteral">"Test 3 completed in "</span> << <a class="code hl_function" href="../../d4/def/kohonen__som__topology_8cpp.html#a2256c10b16edba377b64a44b6c656908">get_clock_diff</a>(start_clk, end_clk)</div>
|
|
<div class="line"><a id="l00607" name="l00607"></a><span class="lineno"> 607</span> << <span class="stringliteral">" sec\n"</span>;</div>
|
|
<div class="line"><a id="l00608" name="l00608"></a><span class="lineno"> 608</span> </div>
|
|
<div class="line"><a id="l00609" name="l00609"></a><span class="lineno"> 609</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a></div>
|
|
<div class="line"><a id="l00610" name="l00610"></a><span class="lineno"> 610</span> << <span class="stringliteral">"(Note: Calculated times include: creating test sets, training "</span></div>
|
|
<div class="line"><a id="l00611" name="l00611"></a><span class="lineno"> 611</span> <span class="stringliteral">"model and writing files to disk.)\n\n"</span>;</div>
|
|
<div class="line"><a id="l00612" name="l00612"></a><span class="lineno"> 612</span> <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><a id="l00613" name="l00613"></a><span class="lineno"> 613</span>}</div>
|
|
<div class="ttc" id="abasic_ostream_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a></div></div>
|
|
<div class="ttc" id="aclock_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/chrono/c/clock.html">std::clock</a></div><div class="ttdeci">T clock(T... args)</div></div>
|
|
<div class="ttc" id="aclock_t_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/chrono/c/clock_t.html">std::clock_t</a></div></div>
|
|
<div class="ttc" id="akohonen__som__topology_8cpp_html_a0283886819c7c140a023582b7269e2d0"><div class="ttname"><a href="../../d4/def/kohonen__som__topology_8cpp.html#a0283886819c7c140a023582b7269e2d0">test2</a></div><div class="ttdeci">void test2()</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.cpp:451</div></div>
|
|
<div class="ttc" id="akohonen__som__topology_8cpp_html_a1440a7779ac56f47a3f355ce4a8c7da0"><div class="ttname"><a href="../../d4/def/kohonen__som__topology_8cpp.html#a1440a7779ac56f47a3f355ce4a8c7da0">test1</a></div><div class="ttdeci">void test1()</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.cpp:369</div></div>
|
|
<div class="ttc" id="akohonen__som__topology_8cpp_html_a2256c10b16edba377b64a44b6c656908"><div class="ttname"><a href="../../d4/def/kohonen__som__topology_8cpp.html#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> kohonen_som_topology.cpp:577</div></div>
|
|
<div class="ttc" id="akohonen__som__topology_8cpp_html_a6d0455dd5c30adda100e95f0423c786e"><div class="ttname"><a href="../../d4/def/kohonen__som__topology_8cpp.html#a6d0455dd5c30adda100e95f0423c786e">test3</a></div><div class="ttdeci">void test3()</div><div class="ttdef"><b>Definition:</b> kohonen_som_topology.cpp:537</div></div>
|
|
<div class="ttc" id="asrand_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/numeric/random/srand.html">std::srand</a></div><div class="ttdeci">T srand(T... args)</div></div>
|
|
<div class="ttc" id="atime_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/chrono/c/time.html">std::time</a></div><div class="ttdeci">T time(T... args)</div></div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><div class="zoom"><iframe scrolling="no" frameborder="0" src="../../d4/def/kohonen__som__topology_8cpp_a3c04138a5bfe5d72780bb7e82a18e627_cgraph.svg" width="100%" height="600"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<a id="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"></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><code>test1.csv</code>: random test samples points with a circular pattern</li>
|
|
<li><code>w11.csv</code>: initial random map</li>
|
|
<li><code>w12.csv</code>: trained SOM map </li>
|
|
</ul>
|
|
<div class="fragment"><div class="line"><a id="l00369" name="l00369"></a><span class="lineno"> 369</span> {</div>
|
|
<div class="line"><a id="l00370" name="l00370"></a><span class="lineno"> 370</span> <span class="keywordtype">int</span> j = 0, <a class="code hl_variable" href="../../d6/d42/data__structures_2sparse__table_8cpp.html#a10f3ffb3f6f7e1b83d556b9c8de89a5d">N</a> = 300;</div>
|
|
<div class="line"><a id="l00371" name="l00371"></a><span class="lineno"> 371</span> <span class="keywordtype">int</span> features = 2;</div>
|
|
<div class="line"><a id="l00372" name="l00372"></a><span class="lineno"> 372</span> <span class="keywordtype">int</span> num_out = 30;</div>
|
|
<div class="line"><a id="l00373" name="l00373"></a><span class="lineno"> 373</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector<std::valarray<double></a>> X(N);</div>
|
|
<div class="line"><a id="l00374" name="l00374"></a><span class="lineno"> 374</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector<std::vector<std::valarray<double></a>>> W(num_out);</div>
|
|
<div class="line"><a id="l00375" name="l00375"></a><span class="lineno"> 375</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/algorithm/max.html">std::max</a>(num_out, N); i++) {</div>
|
|
<div class="line"><a id="l00376" name="l00376"></a><span class="lineno"> 376</span> <span class="comment">// loop till max(N, num_out)</span></div>
|
|
<div class="line"><a id="l00377" name="l00377"></a><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"><a id="l00378" name="l00378"></a><span class="lineno"> 378</span> X[i] = <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray<double></a>(features);</div>
|
|
<div class="line"><a id="l00379" name="l00379"></a><span class="lineno"> 379</span> }</div>
|
|
<div class="line"><a id="l00380" name="l00380"></a><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"><a id="l00381" name="l00381"></a><span class="lineno"> 381</span> W[i] = <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector<std::valarray<double></a>>(num_out);</div>
|
|
<div class="line"><a id="l00382" name="l00382"></a><span class="lineno"> 382</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> k = 0; k < num_out; k++) {</div>
|
|
<div class="line"><a id="l00383" name="l00383"></a><span class="lineno"> 383</span> W[i][k] = <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray<double></a>(features);</div>
|
|
<div class="line"><a id="l00384" name="l00384"></a><span class="lineno"> 384</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><a id="l00385" name="l00385"></a><span class="lineno"> 385</span><span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><a id="l00386" name="l00386"></a><span class="lineno"> 386</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a id="l00387" name="l00387"></a><span class="lineno"> 387</span> <span class="keywordflow">for</span> (j = 0; j < features; j++) {</div>
|
|
<div class="line"><a id="l00388" name="l00388"></a><span class="lineno"> 388</span> <span class="comment">// preallocate with random initial weights</span></div>
|
|
<div class="line"><a id="l00389" name="l00389"></a><span class="lineno"> 389</span> W[i][k][j] = <a class="code hl_function" href="../../d9/d66/group__machine__learning.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(-10, 10);</div>
|
|
<div class="line"><a id="l00390" name="l00390"></a><span class="lineno"> 390</span> }</div>
|
|
<div class="line"><a id="l00391" name="l00391"></a><span class="lineno"> 391</span> }</div>
|
|
<div class="line"><a id="l00392" name="l00392"></a><span class="lineno"> 392</span> }</div>
|
|
<div class="line"><a id="l00393" name="l00393"></a><span class="lineno"> 393</span> }</div>
|
|
<div class="line"><a id="l00394" name="l00394"></a><span class="lineno"> 394</span> </div>
|
|
<div class="line"><a id="l00395" name="l00395"></a><span class="lineno"> 395</span> <a class="code hl_function" href="../../d4/def/kohonen__som__topology_8cpp.html#a48efb079040c7aaa3a4917a0e486cba9">test_2d_classes</a>(&X); <span class="comment">// create test data around circumference of a circle</span></div>
|
|
<div class="line"><a id="l00396" name="l00396"></a><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"><a id="l00397" name="l00397"></a><span class="lineno"> 397</span> <a class="code hl_function" href="../../d8/d77/namespacemachine__learning.html#aa72a53c88203fde278f1fe6c3afe5b07">save_u_matrix</a>(<span class="stringliteral">"w11.csv"</span>, W); <span class="comment">// save initial random weights</span></div>
|
|
<div class="line"><a id="l00398" name="l00398"></a><span class="lineno"> 398</span> <a class="code hl_function" href="../../d8/d77/namespacemachine__learning.html#ac43d294e21a0c4fa33c53757df054576">kohonen_som</a>(X, &W, 1e-4); <span class="comment">// train the SOM</span></div>
|
|
<div class="line"><a id="l00399" name="l00399"></a><span class="lineno"> 399</span> <a class="code hl_function" href="../../d8/d77/namespacemachine__learning.html#aa72a53c88203fde278f1fe6c3afe5b07">save_u_matrix</a>(<span class="stringliteral">"w12.csv"</span>, W); <span class="comment">// save the resultant weights</span></div>
|
|
<div class="line"><a id="l00400" name="l00400"></a><span class="lineno"> 400</span>}</div>
|
|
<div class="ttc" id="adata__structures_2sparse__table_8cpp_html_a10f3ffb3f6f7e1b83d556b9c8de89a5d"><div class="ttname"><a href="../../d6/d42/data__structures_2sparse__table_8cpp.html#a10f3ffb3f6f7e1b83d556b9c8de89a5d">data_structures::sparse_table::N</a></div><div class="ttdeci">constexpr uint32_t N</div><div class="ttdoc">A struct to represent sparse table for min() as their invariant function, for the given array A....</div><div class="ttdef"><b>Definition:</b> sparse_table.cpp:47</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> kohonen_som_topology.cpp:65</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> kohonen_som_topology.cpp:53</div></div>
|
|
<div class="ttc" id="akohonen__som__topology_8cpp_html_a48efb079040c7aaa3a4917a0e486cba9"><div class="ttname"><a href="../../d4/def/kohonen__som__topology_8cpp.html#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> kohonen_som_topology.cpp:330</div></div>
|
|
<div class="ttc" id="amax_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/algorithm/max.html">std::max</a></div><div class="ttdeci">T max(T... args)</div></div>
|
|
<div class="ttc" id="anamespacemachine__learning_html_aa72a53c88203fde278f1fe6c3afe5b07"><div class="ttname"><a href="../../d8/d77/namespacemachine__learning.html#aa72a53c88203fde278f1fe6c3afe5b07">machine_learning::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> kohonen_som_topology.cpp:142</div></div>
|
|
<div class="ttc" id="anamespacemachine__learning_html_ac43d294e21a0c4fa33c53757df054576"><div class="ttname"><a href="../../d8/d77/namespacemachine__learning.html#ac43d294e21a0c4fa33c53757df054576">machine_learning::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> kohonen_som_topology.cpp:269</div></div>
|
|
<div class="ttc" id="avalarray_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray</a></div></div>
|
|
<div class="ttc" id="avector_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a></div></div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><div class="zoom"><iframe scrolling="no" frameborder="0" src="../../d4/def/kohonen__som__topology_8cpp_a1440a7779ac56f47a3f355ce4a8c7da0_cgraph.svg" width="100%" height="600"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<a id="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"></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><code>test2.csv</code>: random test samples points with a lamniscate pattern</li>
|
|
<li><code>w21.csv</code>: initial random map</li>
|
|
<li><code>w22.csv</code>: trained SOM map </li>
|
|
</ul>
|
|
<div class="fragment"><div class="line"><a id="l00451" name="l00451"></a><span class="lineno"> 451</span> {</div>
|
|
<div class="line"><a id="l00452" name="l00452"></a><span class="lineno"> 452</span> <span class="keywordtype">int</span> j = 0, <a class="code hl_variable" href="../../d6/d42/data__structures_2sparse__table_8cpp.html#a10f3ffb3f6f7e1b83d556b9c8de89a5d">N</a> = 300;</div>
|
|
<div class="line"><a id="l00453" name="l00453"></a><span class="lineno"> 453</span> <span class="keywordtype">int</span> features = 3;</div>
|
|
<div class="line"><a id="l00454" name="l00454"></a><span class="lineno"> 454</span> <span class="keywordtype">int</span> num_out = 30;</div>
|
|
<div class="line"><a id="l00455" name="l00455"></a><span class="lineno"> 455</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector<std::valarray<double></a>> X(N);</div>
|
|
<div class="line"><a id="l00456" name="l00456"></a><span class="lineno"> 456</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector<std::vector<std::valarray<double></a>>> W(num_out);</div>
|
|
<div class="line"><a id="l00457" name="l00457"></a><span class="lineno"> 457</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/algorithm/max.html">std::max</a>(num_out, N); i++) {</div>
|
|
<div class="line"><a id="l00458" name="l00458"></a><span class="lineno"> 458</span> <span class="comment">// loop till max(N, num_out)</span></div>
|
|
<div class="line"><a id="l00459" name="l00459"></a><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"><a id="l00460" name="l00460"></a><span class="lineno"> 460</span> X[i] = <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray<double></a>(features);</div>
|
|
<div class="line"><a id="l00461" name="l00461"></a><span class="lineno"> 461</span> }</div>
|
|
<div class="line"><a id="l00462" name="l00462"></a><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"><a id="l00463" name="l00463"></a><span class="lineno"> 463</span> W[i] = <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector<std::valarray<double></a>>(num_out);</div>
|
|
<div class="line"><a id="l00464" name="l00464"></a><span class="lineno"> 464</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> k = 0; k < num_out; k++) {</div>
|
|
<div class="line"><a id="l00465" name="l00465"></a><span class="lineno"> 465</span> W[i][k] = <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray<double></a>(features);</div>
|
|
<div class="line"><a id="l00466" name="l00466"></a><span class="lineno"> 466</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><a id="l00467" name="l00467"></a><span class="lineno"> 467</span><span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><a id="l00468" name="l00468"></a><span class="lineno"> 468</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a id="l00469" name="l00469"></a><span class="lineno"> 469</span> <span class="keywordflow">for</span> (j = 0; j < features; j++) {</div>
|
|
<div class="line"><a id="l00470" name="l00470"></a><span class="lineno"> 470</span> <span class="comment">// preallocate with random initial weights</span></div>
|
|
<div class="line"><a id="l00471" name="l00471"></a><span class="lineno"> 471</span> W[i][k][j] = <a class="code hl_function" href="../../d9/d66/group__machine__learning.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(-10, 10);</div>
|
|
<div class="line"><a id="l00472" name="l00472"></a><span class="lineno"> 472</span> }</div>
|
|
<div class="line"><a id="l00473" name="l00473"></a><span class="lineno"> 473</span> }</div>
|
|
<div class="line"><a id="l00474" name="l00474"></a><span class="lineno"> 474</span> }</div>
|
|
<div class="line"><a id="l00475" name="l00475"></a><span class="lineno"> 475</span> }</div>
|
|
<div class="line"><a id="l00476" name="l00476"></a><span class="lineno"> 476</span> </div>
|
|
<div class="line"><a id="l00477" name="l00477"></a><span class="lineno"> 477</span> <a class="code hl_function" href="../../d4/def/kohonen__som__topology_8cpp.html#a1302662a56ebf67a21249270b017297e">test_3d_classes1</a>(&X); <span class="comment">// create test data around circumference of a circle</span></div>
|
|
<div class="line"><a id="l00478" name="l00478"></a><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"><a id="l00479" name="l00479"></a><span class="lineno"> 479</span> <a class="code hl_function" href="../../d8/d77/namespacemachine__learning.html#aa72a53c88203fde278f1fe6c3afe5b07">save_u_matrix</a>(<span class="stringliteral">"w21.csv"</span>, W); <span class="comment">// save initial random weights</span></div>
|
|
<div class="line"><a id="l00480" name="l00480"></a><span class="lineno"> 480</span> <a class="code hl_function" href="../../d8/d77/namespacemachine__learning.html#ac43d294e21a0c4fa33c53757df054576">kohonen_som</a>(X, &W, 1e-4); <span class="comment">// train the SOM</span></div>
|
|
<div class="line"><a id="l00481" name="l00481"></a><span class="lineno"> 481</span> <a class="code hl_function" href="../../d8/d77/namespacemachine__learning.html#aa72a53c88203fde278f1fe6c3afe5b07">save_u_matrix</a>(<span class="stringliteral">"w22.csv"</span>, W); <span class="comment">// save the resultant weights</span></div>
|
|
<div class="line"><a id="l00482" name="l00482"></a><span class="lineno"> 482</span>}</div>
|
|
<div class="ttc" id="akohonen__som__topology_8cpp_html_a1302662a56ebf67a21249270b017297e"><div class="ttname"><a href="../../d4/def/kohonen__som__topology_8cpp.html#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> kohonen_som_topology.cpp:411</div></div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><div class="zoom"><iframe scrolling="no" frameborder="0" src="../../d4/def/kohonen__som__topology_8cpp_a0283886819c7c140a023582b7269e2d0_cgraph.svg" width="100%" height="600"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<a id="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"></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><code>test3.csv</code>: random test samples points with a circular pattern</li>
|
|
<li><code>w31.csv</code>: initial random map</li>
|
|
<li><code>w32.csv</code>: trained SOM map </li>
|
|
</ul>
|
|
<div class="fragment"><div class="line"><a id="l00537" name="l00537"></a><span class="lineno"> 537</span> {</div>
|
|
<div class="line"><a id="l00538" name="l00538"></a><span class="lineno"> 538</span> <span class="keywordtype">int</span> j = 0, <a class="code hl_variable" href="../../d6/d42/data__structures_2sparse__table_8cpp.html#a10f3ffb3f6f7e1b83d556b9c8de89a5d">N</a> = 500;</div>
|
|
<div class="line"><a id="l00539" name="l00539"></a><span class="lineno"> 539</span> <span class="keywordtype">int</span> features = 3;</div>
|
|
<div class="line"><a id="l00540" name="l00540"></a><span class="lineno"> 540</span> <span class="keywordtype">int</span> num_out = 30;</div>
|
|
<div class="line"><a id="l00541" name="l00541"></a><span class="lineno"> 541</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector<std::valarray<double></a>> X(N);</div>
|
|
<div class="line"><a id="l00542" name="l00542"></a><span class="lineno"> 542</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector<std::vector<std::valarray<double></a>>> W(num_out);</div>
|
|
<div class="line"><a id="l00543" name="l00543"></a><span class="lineno"> 543</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/algorithm/max.html">std::max</a>(num_out, N); i++) {</div>
|
|
<div class="line"><a id="l00544" name="l00544"></a><span class="lineno"> 544</span> <span class="comment">// loop till max(N, num_out)</span></div>
|
|
<div class="line"><a id="l00545" name="l00545"></a><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"><a id="l00546" name="l00546"></a><span class="lineno"> 546</span> X[i] = <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray<double></a>(features);</div>
|
|
<div class="line"><a id="l00547" name="l00547"></a><span class="lineno"> 547</span> }</div>
|
|
<div class="line"><a id="l00548" name="l00548"></a><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"><a id="l00549" name="l00549"></a><span class="lineno"> 549</span> W[i] = <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector<std::valarray<double></a>>(num_out);</div>
|
|
<div class="line"><a id="l00550" name="l00550"></a><span class="lineno"> 550</span> <span class="keywordflow">for</span> (<span class="keywordtype">int</span> k = 0; k < num_out; k++) {</div>
|
|
<div class="line"><a id="l00551" name="l00551"></a><span class="lineno"> 551</span> W[i][k] = <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray<double></a>(features);</div>
|
|
<div class="line"><a id="l00552" name="l00552"></a><span class="lineno"> 552</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><a id="l00553" name="l00553"></a><span class="lineno"> 553</span><span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><a id="l00554" name="l00554"></a><span class="lineno"> 554</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a id="l00555" name="l00555"></a><span class="lineno"> 555</span> <span class="keywordflow">for</span> (j = 0; j < features; j++) {</div>
|
|
<div class="line"><a id="l00556" name="l00556"></a><span class="lineno"> 556</span> <span class="comment">// preallocate with random initial weights</span></div>
|
|
<div class="line"><a id="l00557" name="l00557"></a><span class="lineno"> 557</span> W[i][k][j] = <a class="code hl_function" href="../../d9/d66/group__machine__learning.html#gaf5ce14f026d6d231bef29161bac2b485">_random</a>(-10, 10);</div>
|
|
<div class="line"><a id="l00558" name="l00558"></a><span class="lineno"> 558</span> }</div>
|
|
<div class="line"><a id="l00559" name="l00559"></a><span class="lineno"> 559</span> }</div>
|
|
<div class="line"><a id="l00560" name="l00560"></a><span class="lineno"> 560</span> }</div>
|
|
<div class="line"><a id="l00561" name="l00561"></a><span class="lineno"> 561</span> }</div>
|
|
<div class="line"><a id="l00562" name="l00562"></a><span class="lineno"> 562</span> </div>
|
|
<div class="line"><a id="l00563" name="l00563"></a><span class="lineno"> 563</span> <a class="code hl_function" href="../../d4/def/kohonen__som__topology_8cpp.html#a4b7ab643f6a5002f991837de46f70653">test_3d_classes2</a>(&X); <span class="comment">// create test data around circumference of a circle</span></div>
|
|
<div class="line"><a id="l00564" name="l00564"></a><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"><a id="l00565" name="l00565"></a><span class="lineno"> 565</span> <a class="code hl_function" href="../../d8/d77/namespacemachine__learning.html#aa72a53c88203fde278f1fe6c3afe5b07">save_u_matrix</a>(<span class="stringliteral">"w31.csv"</span>, W); <span class="comment">// save initial random weights</span></div>
|
|
<div class="line"><a id="l00566" name="l00566"></a><span class="lineno"> 566</span> <a class="code hl_function" href="../../d8/d77/namespacemachine__learning.html#ac43d294e21a0c4fa33c53757df054576">kohonen_som</a>(X, &W, 1e-4); <span class="comment">// train the SOM</span></div>
|
|
<div class="line"><a id="l00567" name="l00567"></a><span class="lineno"> 567</span> <a class="code hl_function" href="../../d8/d77/namespacemachine__learning.html#aa72a53c88203fde278f1fe6c3afe5b07">save_u_matrix</a>(<span class="stringliteral">"w32.csv"</span>, W); <span class="comment">// save the resultant weights</span></div>
|
|
<div class="line"><a id="l00568" name="l00568"></a><span class="lineno"> 568</span>}</div>
|
|
<div class="ttc" id="akohonen__som__topology_8cpp_html_a4b7ab643f6a5002f991837de46f70653"><div class="ttname"><a href="../../d4/def/kohonen__som__topology_8cpp.html#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> kohonen_som_topology.cpp:493</div></div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><div class="zoom"><iframe scrolling="no" frameborder="0" src="../../d4/def/kohonen__som__topology_8cpp_a6d0455dd5c30adda100e95f0423c786e_cgraph.svg" width="100%" height="600"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<a id="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"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray</a>< double > > * </td>
|
|
<td class="paramname"><em>data</em></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>
|
|
<div class="fragment"><div class="line"><a id="l00330" name="l00330"></a><span class="lineno"> 330</span> {</div>
|
|
<div class="line"><a id="l00331" name="l00331"></a><span class="lineno"> 331</span> <span class="keyword">const</span> <span class="keywordtype">int</span> <a class="code hl_variable" href="../../d6/d42/data__structures_2sparse__table_8cpp.html#a10f3ffb3f6f7e1b83d556b9c8de89a5d">N</a> = <a class="code hl_variable" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>->size();</div>
|
|
<div class="line"><a id="l00332" name="l00332"></a><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"><a id="l00333" name="l00333"></a><span class="lineno"> 333</span> <span class="keywordtype">int</span> i = 0;</div>
|
|
<div class="line"><a id="l00334" name="l00334"></a><span class="lineno"> 334</span> <span class="keyword">const</span> <span class="keywordtype">int</span> num_classes = 4;</div>
|
|
<div class="line"><a id="l00335" name="l00335"></a><span class="lineno"> 335</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<std::array<double, 2></a>, num_classes> centres = {</div>
|
|
<div class="line"><a id="l00336" name="l00336"></a><span class="lineno"> 336</span> <span class="comment">// centres of each class cluster</span></div>
|
|
<div class="line"><a id="l00337" name="l00337"></a><span class="lineno"> 337</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<double, 2></a>({.5, .5}), <span class="comment">// centre of class 1</span></div>
|
|
<div class="line"><a id="l00338" name="l00338"></a><span class="lineno"> 338</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<double, 2></a>({.5, -.5}), <span class="comment">// centre of class 2</span></div>
|
|
<div class="line"><a id="l00339" name="l00339"></a><span class="lineno"> 339</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<double, 2></a>({-.5, .5}), <span class="comment">// centre of class 3</span></div>
|
|
<div class="line"><a id="l00340" name="l00340"></a><span class="lineno"> 340</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<double, 2></a>({-.5, -.5}) <span class="comment">// centre of class 4</span></div>
|
|
<div class="line"><a id="l00341" name="l00341"></a><span class="lineno"> 341</span> };</div>
|
|
<div class="line"><a id="l00342" name="l00342"></a><span class="lineno"> 342</span> </div>
|
|
<div class="line"><a id="l00343" name="l00343"></a><span class="lineno"> 343</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><a id="l00344" name="l00344"></a><span class="lineno"> 344</span><span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><a id="l00345" name="l00345"></a><span class="lineno"> 345</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a id="l00346" name="l00346"></a><span class="lineno"> 346</span> <span class="keywordflow">for</span> (i = 0; i < <a class="code hl_variable" href="../../d6/d42/data__structures_2sparse__table_8cpp.html#a10f3ffb3f6f7e1b83d556b9c8de89a5d">N</a>; i++) {</div>
|
|
<div class="line"><a id="l00347" name="l00347"></a><span class="lineno"> 347</span> <span class="comment">// select a random class for the point</span></div>
|
|
<div class="line"><a id="l00348" name="l00348"></a><span class="lineno"> 348</span> <span class="keywordtype">int</span> cls = <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/random/rand.html">std::rand</a>() % num_classes;</div>
|
|
<div class="line"><a id="l00349" name="l00349"></a><span class="lineno"> 349</span> </div>
|
|
<div class="line"><a id="l00350" name="l00350"></a><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"><a id="l00351" name="l00351"></a><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"><a id="l00352" name="l00352"></a><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"><a id="l00353" name="l00353"></a><span class="lineno"> 353</span> </div>
|
|
<div class="line"><a id="l00354" name="l00354"></a><span class="lineno"> 354</span> <span class="comment">/* The follosing can also be used</span></div>
|
|
<div class="line"><a id="l00355" name="l00355"></a><span class="lineno"> 355</span><span class="comment"> for (int j = 0; j < 2; j++)</span></div>
|
|
<div class="line"><a id="l00356" name="l00356"></a><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"><a id="l00357" name="l00357"></a><span class="lineno"> 357</span><span class="comment"> */</span></div>
|
|
<div class="line"><a id="l00358" name="l00358"></a><span class="lineno"> 358</span> }</div>
|
|
<div class="line"><a id="l00359" name="l00359"></a><span class="lineno"> 359</span>}</div>
|
|
<div class="ttc" id="aarray_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/array.html">std::array</a></div></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> hash_search.cpp:24</div></div>
|
|
<div class="ttc" id="arand_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/numeric/random/rand.html">std::rand</a></div><div class="ttdeci">T rand(T... args)</div></div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d4/def/kohonen__som__topology_8cpp_a48efb079040c7aaa3a4917a0e486cba9_cgraph.svg" width="372" height="63"><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="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"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray</a>< double > > * </td>
|
|
<td class="paramname"><em>data</em></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>
|
|
<div class="fragment"><div class="line"><a id="l00411" name="l00411"></a><span class="lineno"> 411</span> {</div>
|
|
<div class="line"><a id="l00412" name="l00412"></a><span class="lineno"> 412</span> <span class="keyword">const</span> <span class="keywordtype">size_t</span> <a class="code hl_variable" href="../../d6/d42/data__structures_2sparse__table_8cpp.html#a10f3ffb3f6f7e1b83d556b9c8de89a5d">N</a> = <a class="code hl_variable" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>->size();</div>
|
|
<div class="line"><a id="l00413" name="l00413"></a><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"><a id="l00414" name="l00414"></a><span class="lineno"> 414</span> <span class="keywordtype">int</span> i = 0;</div>
|
|
<div class="line"><a id="l00415" name="l00415"></a><span class="lineno"> 415</span> <span class="keyword">const</span> <span class="keywordtype">int</span> num_classes = 4;</div>
|
|
<div class="line"><a id="l00416" name="l00416"></a><span class="lineno"> 416</span> <span class="keyword">const</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<std::array<double, 3></a>, num_classes> centres = {</div>
|
|
<div class="line"><a id="l00417" name="l00417"></a><span class="lineno"> 417</span> <span class="comment">// centres of each class cluster</span></div>
|
|
<div class="line"><a id="l00418" name="l00418"></a><span class="lineno"> 418</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<double, 3></a>({.5, .5, .5}), <span class="comment">// centre of class 1</span></div>
|
|
<div class="line"><a id="l00419" name="l00419"></a><span class="lineno"> 419</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<double, 3></a>({.5, -.5, -.5}), <span class="comment">// centre of class 2</span></div>
|
|
<div class="line"><a id="l00420" name="l00420"></a><span class="lineno"> 420</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<double, 3></a>({-.5, .5, .5}), <span class="comment">// centre of class 3</span></div>
|
|
<div class="line"><a id="l00421" name="l00421"></a><span class="lineno"> 421</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<double, 3></a>({-.5, -.5 - .5}) <span class="comment">// centre of class 4</span></div>
|
|
<div class="line"><a id="l00422" name="l00422"></a><span class="lineno"> 422</span> };</div>
|
|
<div class="line"><a id="l00423" name="l00423"></a><span class="lineno"> 423</span> </div>
|
|
<div class="line"><a id="l00424" name="l00424"></a><span class="lineno"> 424</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><a id="l00425" name="l00425"></a><span class="lineno"> 425</span><span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><a id="l00426" name="l00426"></a><span class="lineno"> 426</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a id="l00427" name="l00427"></a><span class="lineno"> 427</span> <span class="keywordflow">for</span> (i = 0; i < <a class="code hl_variable" href="../../d6/d42/data__structures_2sparse__table_8cpp.html#a10f3ffb3f6f7e1b83d556b9c8de89a5d">N</a>; i++) {</div>
|
|
<div class="line"><a id="l00428" name="l00428"></a><span class="lineno"> 428</span> <span class="comment">// select a random class for the point</span></div>
|
|
<div class="line"><a id="l00429" name="l00429"></a><span class="lineno"> 429</span> <span class="keywordtype">int</span> cls = <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/random/rand.html">std::rand</a>() % num_classes;</div>
|
|
<div class="line"><a id="l00430" name="l00430"></a><span class="lineno"> 430</span> </div>
|
|
<div class="line"><a id="l00431" name="l00431"></a><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"><a id="l00432" name="l00432"></a><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"><a id="l00433" name="l00433"></a><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"><a id="l00434" name="l00434"></a><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"><a id="l00435" name="l00435"></a><span class="lineno"> 435</span> </div>
|
|
<div class="line"><a id="l00436" name="l00436"></a><span class="lineno"> 436</span> <span class="comment">/* The follosing can also be used</span></div>
|
|
<div class="line"><a id="l00437" name="l00437"></a><span class="lineno"> 437</span><span class="comment"> for (int j = 0; j < 3; j++)</span></div>
|
|
<div class="line"><a id="l00438" name="l00438"></a><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"><a id="l00439" name="l00439"></a><span class="lineno"> 439</span><span class="comment"> */</span></div>
|
|
<div class="line"><a id="l00440" name="l00440"></a><span class="lineno"> 440</span> }</div>
|
|
<div class="line"><a id="l00441" name="l00441"></a><span class="lineno"> 441</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="../../d4/def/kohonen__som__topology_8cpp_a1302662a56ebf67a21249270b017297e_cgraph.svg" width="380" height="63"><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="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"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a>< <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/valarray.html">std::valarray</a>< double > > * </td>
|
|
<td class="paramname"><em>data</em></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>
|
|
<div class="fragment"><div class="line"><a id="l00493" name="l00493"></a><span class="lineno"> 493</span> {</div>
|
|
<div class="line"><a id="l00494" name="l00494"></a><span class="lineno"> 494</span> <span class="keyword">const</span> <span class="keywordtype">size_t</span> <a class="code hl_variable" href="../../d6/d42/data__structures_2sparse__table_8cpp.html#a10f3ffb3f6f7e1b83d556b9c8de89a5d">N</a> = <a class="code hl_variable" href="../../d1/df3/hash__search_8cpp.html#a6e1a77282bc65ad359d753d25df23243">data</a>->size();</div>
|
|
<div class="line"><a id="l00495" name="l00495"></a><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"><a id="l00496" name="l00496"></a><span class="lineno"> 496</span> <span class="keywordtype">int</span> i = 0;</div>
|
|
<div class="line"><a id="l00497" name="l00497"></a><span class="lineno"> 497</span> <span class="keyword">const</span> <span class="keywordtype">int</span> num_classes = 8;</div>
|
|
<div class="line"><a id="l00498" name="l00498"></a><span class="lineno"> 498</span> <span class="keyword">const</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<std::array<double, 3></a>, num_classes> centres = {</div>
|
|
<div class="line"><a id="l00499" name="l00499"></a><span class="lineno"> 499</span> <span class="comment">// centres of each class cluster</span></div>
|
|
<div class="line"><a id="l00500" name="l00500"></a><span class="lineno"> 500</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<double, 3></a>({.5, .5, .5}), <span class="comment">// centre of class 1</span></div>
|
|
<div class="line"><a id="l00501" name="l00501"></a><span class="lineno"> 501</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<double, 3></a>({.5, .5, -.5}), <span class="comment">// centre of class 2</span></div>
|
|
<div class="line"><a id="l00502" name="l00502"></a><span class="lineno"> 502</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<double, 3></a>({.5, -.5, .5}), <span class="comment">// centre of class 3</span></div>
|
|
<div class="line"><a id="l00503" name="l00503"></a><span class="lineno"> 503</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<double, 3></a>({.5, -.5, -.5}), <span class="comment">// centre of class 4</span></div>
|
|
<div class="line"><a id="l00504" name="l00504"></a><span class="lineno"> 504</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<double, 3></a>({-.5, .5, .5}), <span class="comment">// centre of class 5</span></div>
|
|
<div class="line"><a id="l00505" name="l00505"></a><span class="lineno"> 505</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<double, 3></a>({-.5, .5, -.5}), <span class="comment">// centre of class 6</span></div>
|
|
<div class="line"><a id="l00506" name="l00506"></a><span class="lineno"> 506</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<double, 3></a>({-.5, -.5, .5}), <span class="comment">// centre of class 7</span></div>
|
|
<div class="line"><a id="l00507" name="l00507"></a><span class="lineno"> 507</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/array.html">std::array<double, 3></a>({-.5, -.5, -.5}) <span class="comment">// centre of class 8</span></div>
|
|
<div class="line"><a id="l00508" name="l00508"></a><span class="lineno"> 508</span> };</div>
|
|
<div class="line"><a id="l00509" name="l00509"></a><span class="lineno"> 509</span> </div>
|
|
<div class="line"><a id="l00510" name="l00510"></a><span class="lineno"> 510</span><span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><a id="l00511" name="l00511"></a><span class="lineno"> 511</span><span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><a id="l00512" name="l00512"></a><span class="lineno"> 512</span><span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a id="l00513" name="l00513"></a><span class="lineno"> 513</span> <span class="keywordflow">for</span> (i = 0; i < <a class="code hl_variable" href="../../d6/d42/data__structures_2sparse__table_8cpp.html#a10f3ffb3f6f7e1b83d556b9c8de89a5d">N</a>; i++) {</div>
|
|
<div class="line"><a id="l00514" name="l00514"></a><span class="lineno"> 514</span> <span class="comment">// select a random class for the point</span></div>
|
|
<div class="line"><a id="l00515" name="l00515"></a><span class="lineno"> 515</span> <span class="keywordtype">int</span> cls = <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/numeric/random/rand.html">std::rand</a>() % num_classes;</div>
|
|
<div class="line"><a id="l00516" name="l00516"></a><span class="lineno"> 516</span> </div>
|
|
<div class="line"><a id="l00517" name="l00517"></a><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"><a id="l00518" name="l00518"></a><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"><a id="l00519" name="l00519"></a><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"><a id="l00520" name="l00520"></a><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"><a id="l00521" name="l00521"></a><span class="lineno"> 521</span> </div>
|
|
<div class="line"><a id="l00522" name="l00522"></a><span class="lineno"> 522</span> <span class="comment">/* The follosing can also be used</span></div>
|
|
<div class="line"><a id="l00523" name="l00523"></a><span class="lineno"> 523</span><span class="comment"> for (int j = 0; j < 3; j++)</span></div>
|
|
<div class="line"><a id="l00524" name="l00524"></a><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"><a id="l00525" name="l00525"></a><span class="lineno"> 525</span><span class="comment"> */</span></div>
|
|
<div class="line"><a id="l00526" name="l00526"></a><span class="lineno"> 526</span> }</div>
|
|
<div class="line"><a id="l00527" name="l00527"></a><span class="lineno"> 527</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="../../d4/def/kohonen__som__topology_8cpp_a4b7ab643f6a5002f991837de46f70653_cgraph.svg" width="380" height="63"><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 -->
|
|
<!-- start footer part -->
|
|
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
|
|
<ul>
|
|
<li class="navelem"><a class="el" href="../../dir_3343723ae086de42ee4ca9774da3a13f.html">machine_learning</a></li><li class="navelem"><a class="el" href="../../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.9.2 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|