mirror of
https://github.com/TheAlgorithms/C
synced 2025-01-06 01:52:00 +03:00
1078 lines
95 KiB
HTML
1078 lines
95 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
|
|
<meta name="generator" content="Doxygen 1.8.18"/>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
<title>Algorithms_in_C: machine_learning/kohonen_som_trace.c File Reference</title>
|
|
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../jquery.js"></script>
|
|
<script type="text/javascript" src="../../dynsections.js"></script>
|
|
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../resize.js"></script>
|
|
<script type="text/javascript" src="../../navtreedata.js"></script>
|
|
<script type="text/javascript" src="../../navtree.js"></script>
|
|
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="../../search/searchdata.js"></script>
|
|
<script type="text/javascript" src="../../search/search.js"></script>
|
|
<script type="text/x-mathjax-config">
|
|
MathJax.Hub.Config({
|
|
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
|
|
jax: ["input/TeX","output/HTML-CSS"],
|
|
});
|
|
</script>
|
|
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
|
|
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
|
|
</head>
|
|
<body>
|
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
|
<div id="titlearea">
|
|
<table cellspacing="0" cellpadding="0">
|
|
<tbody>
|
|
<tr style="height: 56px;">
|
|
<td id="projectalign" style="padding-left: 0.5em;">
|
|
<div id="projectname">Algorithms_in_C
|
|
 <span id="projectnumber">1.0.0</span>
|
|
</div>
|
|
<div id="projectbrief">Set of algorithms implemented in C.</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.8.18 -->
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
|
|
/* @license-end */
|
|
</script>
|
|
<script type="text/javascript" src="../../menudata.js"></script>
|
|
<script type="text/javascript" src="../../menu.js"></script>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
$(function() {
|
|
initMenu('../../',true,false,'search.php','Search');
|
|
$(document).ready(function() { init_search(); });
|
|
});
|
|
/* @license-end */</script>
|
|
<div id="main-nav"></div>
|
|
</div><!-- top -->
|
|
<div id="side-nav" class="ui-resizable side-nav-resizable">
|
|
<div id="nav-tree">
|
|
<div id="nav-tree-contents">
|
|
<div id="nav-sync" class="sync"></div>
|
|
</div>
|
|
</div>
|
|
<div id="splitbar" style="-moz-user-select:none;"
|
|
class="ui-resizable-handle">
|
|
</div>
|
|
</div>
|
|
<script type="text/javascript">
|
|
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&dn=gpl-2.0.txt GPL-v2 */
|
|
$(document).ready(function(){initNavTree('d0/d46/kohonen__som__trace_8c.html','../../'); initResizable(); });
|
|
/* @license-end */
|
|
</script>
|
|
<div id="doc-content">
|
|
<!-- window showing the filter options -->
|
|
<div id="MSearchSelectWindow"
|
|
onmouseover="return searchBox.OnSearchSelectShow()"
|
|
onmouseout="return searchBox.OnSearchSelectHide()"
|
|
onkeydown="return searchBox.OnSearchSelectKey(event)">
|
|
</div>
|
|
|
|
<!-- iframe showing the search results (closed by default) -->
|
|
<div id="MSearchResultsWindow">
|
|
<iframe src="javascript:void(0)" frameborder="0"
|
|
name="MSearchResults" id="MSearchResults">
|
|
</iframe>
|
|
</div>
|
|
|
|
<div class="header">
|
|
<div class="summary">
|
|
<a href="#define-members">Macros</a> |
|
|
<a href="#func-members">Functions</a> </div>
|
|
<div class="headertitle">
|
|
<div class="title">kohonen_som_trace.c File Reference</div> </div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
|
|
<p><a href="https://en.wikipedia.org/wiki/Self-organizing_map">Kohonen self organizing map</a> (data tracing)
|
|
<a href="#details">More...</a></p>
|
|
<div class="textblock"><code>#include <math.h></code><br />
|
|
<code>#include <stdio.h></code><br />
|
|
<code>#include <stdlib.h></code><br />
|
|
<code>#include <time.h></code><br />
|
|
</div><div class="textblock"><div class="dynheader">
|
|
Include dependency graph for kohonen_som_trace.c:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../da/dfd/kohonen__som__trace_8c__incl.svg" width="331" height="127"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</div>
|
|
</div><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="define-members"></a>
|
|
Macros</h2></td></tr>
|
|
<tr class="memitem:a525335710b53cb064ca56b936120431e"><td class="memItemLeft" align="right" valign="top"><a id="a525335710b53cb064ca56b936120431e"></a>
|
|
#define </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#a525335710b53cb064ca56b936120431e">_USE_MATH_DEFINES</a></td></tr>
|
|
<tr class="memdesc:a525335710b53cb064ca56b936120431e"><td class="mdescLeft"> </td><td class="mdescRight">required for MS Visual C <br /></td></tr>
|
|
<tr class="separator:a525335710b53cb064ca56b936120431e"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:affe776513b24d84b39af8ab0930fef7f"><td class="memItemLeft" align="right" valign="top"><a id="affe776513b24d84b39af8ab0930fef7f"></a>
|
|
#define </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#affe776513b24d84b39af8ab0930fef7f">max</a>(a, b)   (((a) > (b)) ? (a) : (b))</td></tr>
|
|
<tr class="memdesc:affe776513b24d84b39af8ab0930fef7f"><td class="mdescLeft"> </td><td class="mdescRight">shorthand for maximum value <br /></td></tr>
|
|
<tr class="separator:affe776513b24d84b39af8ab0930fef7f"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:ac6afabdc09a49a433ee19d8a9486056d"><td class="memItemLeft" align="right" valign="top"><a id="ac6afabdc09a49a433ee19d8a9486056d"></a>
|
|
#define </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#ac6afabdc09a49a433ee19d8a9486056d">min</a>(a, b)   (((a) < (b)) ? (a) : (b))</td></tr>
|
|
<tr class="memdesc:ac6afabdc09a49a433ee19d8a9486056d"><td class="mdescLeft"> </td><td class="mdescRight">shorthand for minimum value <br /></td></tr>
|
|
<tr class="separator:ac6afabdc09a49a433ee19d8a9486056d"><td class="memSeparator" colspan="2"> </td></tr>
|
|
</table><table class="memberdecls">
|
|
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="func-members"></a>
|
|
Functions</h2></td></tr>
|
|
<tr class="memitem:af5ce14f026d6d231bef29161bac2b485"><td class="memItemLeft" align="right" valign="top">double </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a> (double a, double b)</td></tr>
|
|
<tr class="memdesc:af5ce14f026d6d231bef29161bac2b485"><td class="mdescLeft"> </td><td class="mdescRight">Helper function to generate a random number in a given interval. <a href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">More...</a><br /></td></tr>
|
|
<tr class="separator:af5ce14f026d6d231bef29161bac2b485"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a7b84b14e60f47812b581d1f93057c85a"><td class="memItemLeft" align="right" valign="top">int </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a> (const char *fname, double **X, int num_points, int num_features)</td></tr>
|
|
<tr class="memdesc:a7b84b14e60f47812b581d1f93057c85a"><td class="mdescLeft"> </td><td class="mdescRight">Save a given n-dimensional data martix to file. <a href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">More...</a><br /></td></tr>
|
|
<tr class="separator:a7b84b14e60f47812b581d1f93057c85a"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a0a292ebd954c568934b1fd06666d27e6"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#a0a292ebd954c568934b1fd06666d27e6">get_min_1d</a> (double const *X, int <a class="el" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>, double *val, int *idx)</td></tr>
|
|
<tr class="memdesc:a0a292ebd954c568934b1fd06666d27e6"><td class="mdescLeft"> </td><td class="mdescRight">Get minimum value and index of the value in a vector. <a href="../../d0/d46/kohonen__som__trace_8c.html#a0a292ebd954c568934b1fd06666d27e6">More...</a><br /></td></tr>
|
|
<tr class="separator:a0a292ebd954c568934b1fd06666d27e6"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a9c98203f55242ff7a8f45a6370840e66"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#a9c98203f55242ff7a8f45a6370840e66">update_weights</a> (double const *x, double *const *W, double *D, int num_out, int num_features, double alpha, int R)</td></tr>
|
|
<tr class="memdesc:a9c98203f55242ff7a8f45a6370840e66"><td class="mdescLeft"> </td><td class="mdescRight">Update weights of the SOM using Kohonen algorithm. <a href="../../d0/d46/kohonen__som__trace_8c.html#a9c98203f55242ff7a8f45a6370840e66">More...</a><br /></td></tr>
|
|
<tr class="separator:a9c98203f55242ff7a8f45a6370840e66"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:aeaeffbff2be4d5d15b0d4f10f846abde"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#aeaeffbff2be4d5d15b0d4f10f846abde">kohonen_som_tracer</a> (double **X, double *const *W, int num_samples, int num_features, int num_out, double alpha_min)</td></tr>
|
|
<tr class="memdesc:aeaeffbff2be4d5d15b0d4f10f846abde"><td class="mdescLeft"> </td><td class="mdescRight">Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given datset. <a href="../../d0/d46/kohonen__som__trace_8c.html#aeaeffbff2be4d5d15b0d4f10f846abde">More...</a><br /></td></tr>
|
|
<tr class="separator:aeaeffbff2be4d5d15b0d4f10f846abde"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a107f00650b8041f77767927073ddddb8"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#a107f00650b8041f77767927073ddddb8">test_circle</a> (double *const *<a class="el" href="../../df/dea/structdata.html">data</a>, int <a class="el" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>)</td></tr>
|
|
<tr class="memdesc:a107f00650b8041f77767927073ddddb8"><td class="mdescLeft"> </td><td class="mdescRight">Creates a random set of points distributed <em>near</em> the circumference of a circle and trains an SOM that finds that circular pattern. <a href="../../d0/d46/kohonen__som__trace_8c.html#a107f00650b8041f77767927073ddddb8">More...</a><br /></td></tr>
|
|
<tr class="separator:a107f00650b8041f77767927073ddddb8"><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="../../d0/d46/kohonen__som__trace_8c.html#a1440a7779ac56f47a3f355ce4a8c7da0">test1</a> ()</td></tr>
|
|
<tr class="memdesc:a1440a7779ac56f47a3f355ce4a8c7da0"><td class="mdescLeft"> </td><td class="mdescRight">Test that creates a random set of points distributed <em>near</em> the circumference of a circle and trains an SOM that finds that circular pattern. <a href="../../d0/d46/kohonen__som__trace_8c.html#a1440a7779ac56f47a3f355ce4a8c7da0">More...</a><br /></td></tr>
|
|
<tr class="separator:a1440a7779ac56f47a3f355ce4a8c7da0"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:aa2246f940155472084ee461f3685d614"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#aa2246f940155472084ee461f3685d614">test_lamniscate</a> (double *const *<a class="el" href="../../df/dea/structdata.html">data</a>, int <a class="el" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>)</td></tr>
|
|
<tr class="memdesc:aa2246f940155472084ee461f3685d614"><td class="mdescLeft"> </td><td class="mdescRight">Creates a random set of points distributed <em>near</em> the locus of the <a href="https://en.wikipedia.org/wiki/Lemniscate_of_Gerono">Lamniscate of Gerono</a>. <a href="../../d0/d46/kohonen__som__trace_8c.html#aa2246f940155472084ee461f3685d614">More...</a><br /></td></tr>
|
|
<tr class="separator:aa2246f940155472084ee461f3685d614"><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="../../d0/d46/kohonen__som__trace_8c.html#a0283886819c7c140a023582b7269e2d0">test2</a> ()</td></tr>
|
|
<tr class="memdesc:a0283886819c7c140a023582b7269e2d0"><td class="mdescLeft"> </td><td class="mdescRight">Test that creates a random set of points distributed <em>near</em> the locus of the <a href="https://en.wikipedia.org/wiki/Lemniscate_of_Gerono">Lamniscate of Gerono</a> and trains an SOM that finds that circular pattern. <a href="../../d0/d46/kohonen__som__trace_8c.html#a0283886819c7c140a023582b7269e2d0">More...</a><br /></td></tr>
|
|
<tr class="separator:a0283886819c7c140a023582b7269e2d0"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a41ae16442e3e5b891a58d2e5932a2cd0"><td class="memItemLeft" align="right" valign="top">void </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#a41ae16442e3e5b891a58d2e5932a2cd0">test_3d_classes</a> (double *const *<a class="el" href="../../df/dea/structdata.html">data</a>, int <a class="el" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>)</td></tr>
|
|
<tr class="memdesc:a41ae16442e3e5b891a58d2e5932a2cd0"><td class="mdescLeft"> </td><td class="mdescRight">Creates a random set of points distributed in four clusters in 3D space with centroids at the points. <a href="../../d0/d46/kohonen__som__trace_8c.html#a41ae16442e3e5b891a58d2e5932a2cd0">More...</a><br /></td></tr>
|
|
<tr class="separator:a41ae16442e3e5b891a58d2e5932a2cd0"><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="../../d0/d46/kohonen__som__trace_8c.html#a6d0455dd5c30adda100e95f0423c786e">test3</a> ()</td></tr>
|
|
<tr class="memdesc:a6d0455dd5c30adda100e95f0423c786e"><td class="mdescLeft"> </td><td class="mdescRight">Test that creates a random set of points distributed in six clusters in 3D space. <a href="../../d0/d46/kohonen__som__trace_8c.html#a6d0455dd5c30adda100e95f0423c786e">More...</a><br /></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="../../d0/d46/kohonen__som__trace_8c.html#a2256c10b16edba377b64a44b6c656908">get_clock_diff</a> (clock_t start_t, clock_t end_t)</td></tr>
|
|
<tr class="memdesc:a2256c10b16edba377b64a44b6c656908"><td class="mdescLeft"> </td><td class="mdescRight">Convert clock cycle difference to time in seconds. <a href="../../d0/d46/kohonen__som__trace_8c.html#a2256c10b16edba377b64a44b6c656908">More...</a><br /></td></tr>
|
|
<tr class="separator:a2256c10b16edba377b64a44b6c656908"><td class="memSeparator" colspan="2"> </td></tr>
|
|
<tr class="memitem:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memItemLeft" align="right" valign="top"><a id="a3c04138a5bfe5d72780bb7e82a18e627"></a>
|
|
int </td><td class="memItemRight" valign="bottom"><a class="el" href="../../d0/d46/kohonen__som__trace_8c.html#a3c04138a5bfe5d72780bb7e82a18e627">main</a> (int argc, char **argv)</td></tr>
|
|
<tr class="memdesc:a3c04138a5bfe5d72780bb7e82a18e627"><td class="mdescLeft"> </td><td class="mdescRight">Main function. <br /></td></tr>
|
|
<tr class="separator:a3c04138a5bfe5d72780bb7e82a18e627"><td class="memSeparator" colspan="2"> </td></tr>
|
|
</table>
|
|
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
|
|
<div class="textblock"><p><a href="https://en.wikipedia.org/wiki/Self-organizing_map">Kohonen self organizing map</a> (data tracing) </p>
|
|
<dl class="section author"><dt>Author</dt><dd><a href="https://github.com/kvedala">Krishna Vedala</a></dd></dl>
|
|
<p>This example implements a powerful self organizing map algorithm. The algorithm creates a connected network of weights that closely follows the given data points. This this creates a chain of nodes that resembles the given input shape. </p><dl class="section see"><dt>See also</dt><dd><a class="el" href="../../d2/df6/kohonen__som__topology_8c.html" title="Kohonen self organizing map (topological map)">kohonen_som_topology.c</a> </dd></dl>
|
|
</div><h2 class="groupheader">Function Documentation</h2>
|
|
<a id="af5ce14f026d6d231bef29161bac2b485"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#af5ce14f026d6d231bef29161bac2b485">◆ </a></span>_random()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">double _random </td>
|
|
<td>(</td>
|
|
<td class="paramtype">double </td>
|
|
<td class="paramname"><em>a</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double </td>
|
|
<td class="paramname"><em>b</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Helper function to generate a random number in a given interval. </p>
|
|
<p><br />
|
|
Steps:</p><ol type="1">
|
|
<li><code>r1 = rand() % 100</code> gets a random number between 0 and 99</li>
|
|
<li><code>r2 = r1 / 100</code> converts random number to be between 0 and 0.99</li>
|
|
<li>scale and offset the random number to given range of \([a,b)\) <p class="formulaDsp">
|
|
\[ y = (b - a) \times \frac{\text{(random number between 0 and RAND_MAX)} \; \text{mod}\; 100}{100} + a \]
|
|
</p>
|
|
</li>
|
|
</ol>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramname">a</td><td>lower limit </td></tr>
|
|
<tr><td class="paramname">b</td><td>upper limit </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<dl class="section return"><dt>Returns</dt><dd>random number in the range \([a,b)\) </dd></dl>
|
|
<div class="fragment"><div class="line"><a name="l00049"></a><span class="lineno"> 49</span> {</div>
|
|
<div class="line"><a name="l00050"></a><span class="lineno"> 50</span>  <span class="keywordtype">int</span> r = rand() % 100;</div>
|
|
<div class="line"><a name="l00051"></a><span class="lineno"> 51</span>  <span class="keywordflow">return</span> ((b - a) * r / 100.f) + a;</div>
|
|
<div class="line"><a name="l00052"></a><span class="lineno"> 52</span> }</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="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 name="l00501"></a><span class="lineno"> 501</span> {</div>
|
|
<div class="line"><a name="l00502"></a><span class="lineno"> 502</span>  <span class="keywordflow">return</span> (<span class="keywordtype">double</span>)(end_t - start_t) / (<span class="keywordtype">double</span>)CLOCKS_PER_SEC;</div>
|
|
<div class="line"><a name="l00503"></a><span class="lineno"> 503</span> }</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="a0a292ebd954c568934b1fd06666d27e6"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a0a292ebd954c568934b1fd06666d27e6">◆ </a></span>get_min_1d()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void get_min_1d </td>
|
|
<td>(</td>
|
|
<td class="paramtype">double const * </td>
|
|
<td class="paramname"><em>X</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>N</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double * </td>
|
|
<td class="paramname"><em>val</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int * </td>
|
|
<td class="paramname"><em>idx</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Get minimum value and index of the value in a vector. </p>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">X</td><td>vector to search </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">N</td><td>number of points in the vector </td></tr>
|
|
<tr><td class="paramdir">[out]</td><td class="paramname">val</td><td>minimum value found </td></tr>
|
|
<tr><td class="paramdir">[out]</td><td class="paramname">idx</td><td>index where minimum value was found </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<div class="fragment"><div class="line"><a name="l00099"></a><span class="lineno"> 99</span> {</div>
|
|
<div class="line"><a name="l00100"></a><span class="lineno"> 100</span>  val[0] = INFINITY; <span class="comment">// initial min value</span></div>
|
|
<div class="line"><a name="l00101"></a><span class="lineno"> 101</span>  </div>
|
|
<div class="line"><a name="l00102"></a><span class="lineno"> 102</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++) <span class="comment">// check each value</span></div>
|
|
<div class="line"><a name="l00103"></a><span class="lineno"> 103</span>  {</div>
|
|
<div class="line"><a name="l00104"></a><span class="lineno"> 104</span>  <span class="keywordflow">if</span> (X[i] < val[0]) <span class="comment">// if a lower value is found</span></div>
|
|
<div class="line"><a name="l00105"></a><span class="lineno"> 105</span>  { <span class="comment">// save the value and its index</span></div>
|
|
<div class="line"><a name="l00106"></a><span class="lineno"> 106</span>  idx[0] = i;</div>
|
|
<div class="line"><a name="l00107"></a><span class="lineno"> 107</span>  val[0] = X[i];</div>
|
|
<div class="line"><a name="l00108"></a><span class="lineno"> 108</span>  }</div>
|
|
<div class="line"><a name="l00109"></a><span class="lineno"> 109</span>  }</div>
|
|
<div class="line"><a name="l00110"></a><span class="lineno"> 110</span> }</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="aeaeffbff2be4d5d15b0d4f10f846abde"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#aeaeffbff2be4d5d15b0d4f10f846abde">◆ </a></span>kohonen_som_tracer()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void kohonen_som_tracer </td>
|
|
<td>(</td>
|
|
<td class="paramtype">double ** </td>
|
|
<td class="paramname"><em>X</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double *const * </td>
|
|
<td class="paramname"><em>W</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>num_samples</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>num_features</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>num_out</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double </td>
|
|
<td class="paramname"><em>alpha_min</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given datset. </p>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">X</td><td>data set </td></tr>
|
|
<tr><td class="paramdir">[in,out]</td><td class="paramname">W</td><td>weights matrix </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">num_samples</td><td>number of output points </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">num_features</td><td>number of features per input sample </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">num_out</td><td>number of output points </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">alpha_min</td><td>terminal value of alpha </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<div class="fragment"><div class="line"><a name="l00175"></a><span class="lineno"> 175</span> {</div>
|
|
<div class="line"><a name="l00176"></a><span class="lineno"> 176</span>  <span class="keywordtype">int</span> R = num_out >> 2, iter = 0;</div>
|
|
<div class="line"><a name="l00177"></a><span class="lineno"> 177</span>  <span class="keywordtype">double</span> alpha = 1.f;</div>
|
|
<div class="line"><a name="l00178"></a><span class="lineno"> 178</span>  <span class="keywordtype">double</span> *D = (<span class="keywordtype">double</span> *)malloc(num_out * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
|
|
<div class="line"><a name="l00179"></a><span class="lineno"> 179</span>  </div>
|
|
<div class="line"><a name="l00180"></a><span class="lineno"> 180</span>  <span class="comment">// Loop alpha from 1 to slpha_min</span></div>
|
|
<div class="line"><a name="l00181"></a><span class="lineno"> 181</span>  <span class="keywordflow">for</span> (; alpha > alpha_min; alpha -= 0.01, iter++)</div>
|
|
<div class="line"><a name="l00182"></a><span class="lineno"> 182</span>  {</div>
|
|
<div class="line"><a name="l00183"></a><span class="lineno"> 183</span>  <span class="comment">// Loop for each sample pattern in the data set</span></div>
|
|
<div class="line"><a name="l00184"></a><span class="lineno"> 184</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> sample = 0; sample < num_samples; sample++)</div>
|
|
<div class="line"><a name="l00185"></a><span class="lineno"> 185</span>  {</div>
|
|
<div class="line"><a name="l00186"></a><span class="lineno"> 186</span>  <span class="keyword">const</span> <span class="keywordtype">double</span> *x = X[sample];</div>
|
|
<div class="line"><a name="l00187"></a><span class="lineno"> 187</span>  <span class="comment">// update weights for the current input pattern sample</span></div>
|
|
<div class="line"><a name="l00188"></a><span class="lineno"> 188</span>  <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a9c98203f55242ff7a8f45a6370840e66">update_weights</a>(x, W, D, num_out, num_features, alpha, R);</div>
|
|
<div class="line"><a name="l00189"></a><span class="lineno"> 189</span>  }</div>
|
|
<div class="line"><a name="l00190"></a><span class="lineno"> 190</span>  </div>
|
|
<div class="line"><a name="l00191"></a><span class="lineno"> 191</span>  <span class="comment">// every 10th iteration, reduce the neighborhood range</span></div>
|
|
<div class="line"><a name="l00192"></a><span class="lineno"> 192</span>  <span class="keywordflow">if</span> (iter % 10 == 0 && R > 1)</div>
|
|
<div class="line"><a name="l00193"></a><span class="lineno"> 193</span>  R--;</div>
|
|
<div class="line"><a name="l00194"></a><span class="lineno"> 194</span>  }</div>
|
|
<div class="line"><a name="l00195"></a><span class="lineno"> 195</span>  </div>
|
|
<div class="line"><a name="l00196"></a><span class="lineno"> 196</span>  free(D);</div>
|
|
<div class="line"><a name="l00197"></a><span class="lineno"> 197</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="../../d0/d46/kohonen__som__trace_8c_aeaeffbff2be4d5d15b0d4f10f846abde_cgraph.svg" width="452" height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<a id="a7b84b14e60f47812b581d1f93057c85a"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a7b84b14e60f47812b581d1f93057c85a">◆ </a></span>save_nd_data()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">int save_nd_data </td>
|
|
<td>(</td>
|
|
<td class="paramtype">const char * </td>
|
|
<td class="paramname"><em>fname</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double ** </td>
|
|
<td class="paramname"><em>X</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>num_points</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>num_features</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Save a given n-dimensional data martix to file. </p>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">fname</td><td>filename to save in (gets overwriten without confirmation) </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">X</td><td>matrix to save </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">num_points</td><td>rows in the matrix = number of points </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">num_features</td><td>columns in the matrix = dimensions of points </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<dl class="section return"><dt>Returns</dt><dd>0 if all ok </dd>
|
|
<dd>
|
|
-1 if file creation failed </dd></dl>
|
|
<div class="fragment"><div class="line"><a name="l00066"></a><span class="lineno"> 66</span> {</div>
|
|
<div class="line"><a name="l00067"></a><span class="lineno"> 67</span>  FILE *fp = fopen(fname, <span class="stringliteral">"wt"</span>);</div>
|
|
<div class="line"><a name="l00068"></a><span class="lineno"> 68</span>  <span class="keywordflow">if</span> (!fp) <span class="comment">// error with fopen</span></div>
|
|
<div class="line"><a name="l00069"></a><span class="lineno"> 69</span>  {</div>
|
|
<div class="line"><a name="l00070"></a><span class="lineno"> 70</span>  <span class="keywordtype">char</span> msg[120];</div>
|
|
<div class="line"><a name="l00071"></a><span class="lineno"> 71</span>  sprintf(msg, <span class="stringliteral">"File error (%s): "</span>, fname);</div>
|
|
<div class="line"><a name="l00072"></a><span class="lineno"> 72</span>  perror(msg);</div>
|
|
<div class="line"><a name="l00073"></a><span class="lineno"> 73</span>  <span class="keywordflow">return</span> -1;</div>
|
|
<div class="line"><a name="l00074"></a><span class="lineno"> 74</span>  }</div>
|
|
<div class="line"><a name="l00075"></a><span class="lineno"> 75</span>  </div>
|
|
<div class="line"><a name="l00076"></a><span class="lineno"> 76</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < num_points; i++) <span class="comment">// for each point in the array</span></div>
|
|
<div class="line"><a name="l00077"></a><span class="lineno"> 77</span>  {</div>
|
|
<div class="line"><a name="l00078"></a><span class="lineno"> 78</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j < num_features; j++) <span class="comment">// for each feature in the array</span></div>
|
|
<div class="line"><a name="l00079"></a><span class="lineno"> 79</span>  {</div>
|
|
<div class="line"><a name="l00080"></a><span class="lineno"> 80</span>  fprintf(fp, <span class="stringliteral">"%.4g"</span>, X[i][j]); <span class="comment">// print the feature value</span></div>
|
|
<div class="line"><a name="l00081"></a><span class="lineno"> 81</span>  <span class="keywordflow">if</span> (j < num_features - 1) <span class="comment">// if not the last feature</span></div>
|
|
<div class="line"><a name="l00082"></a><span class="lineno"> 82</span>  fprintf(fp, <span class="stringliteral">","</span>); <span class="comment">// suffix comma</span></div>
|
|
<div class="line"><a name="l00083"></a><span class="lineno"> 83</span>  }</div>
|
|
<div class="line"><a name="l00084"></a><span class="lineno"> 84</span>  <span class="keywordflow">if</span> (i < num_points - 1) <span class="comment">// if not the last row</span></div>
|
|
<div class="line"><a name="l00085"></a><span class="lineno"> 85</span>  fprintf(fp, <span class="stringliteral">"\n"</span>); <span class="comment">// start a new line</span></div>
|
|
<div class="line"><a name="l00086"></a><span class="lineno"> 86</span>  }</div>
|
|
<div class="line"><a name="l00087"></a><span class="lineno"> 87</span>  fclose(fp);</div>
|
|
<div class="line"><a name="l00088"></a><span class="lineno"> 88</span>  <span class="keywordflow">return</span> 0;</div>
|
|
<div class="line"><a name="l00089"></a><span class="lineno"> 89</span> }</div>
|
|
</div><!-- fragment -->
|
|
</div>
|
|
</div>
|
|
<a id="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 <em>near</em> the circumference of a circle and trains an SOM that finds that circular pattern. </p>
|
|
<p>The following <a href="https://en.wikipedia.org/wiki/Comma-separated_values">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>
|
|
<p>The outputs can be readily plotted in <a href="https:://gnuplot.info">gnuplot</a> using the following snippet </p><div class="fragment"><div class="line">set datafile separator ','</div>
|
|
<div class="line">plot "test1.csv" title "original", \</div>
|
|
<div class="line"> "w11.csv" title "w1", \</div>
|
|
<div class="line"> "w12.csv" title "w2"</div>
|
|
</div><!-- fragment --><p><img src="https://raw.githubusercontent.com/TheAlgorithms/C/docs/images/machine_learning/kohonen/test1.svg" alt="Sample execution
|
|
output" style="pointer-events: none;" class="inline"/> </p>
|
|
<div class="fragment"><div class="line"><a name="l00251"></a><span class="lineno"> 251</span> {</div>
|
|
<div class="line"><a name="l00252"></a><span class="lineno"> 252</span>  <span class="keywordtype">int</span> j, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a> = 500;</div>
|
|
<div class="line"><a name="l00253"></a><span class="lineno"> 253</span>  <span class="keywordtype">int</span> features = 2;</div>
|
|
<div class="line"><a name="l00254"></a><span class="lineno"> 254</span>  <span class="keywordtype">int</span> num_out = 50;</div>
|
|
<div class="line"><a name="l00255"></a><span class="lineno"> 255</span>  </div>
|
|
<div class="line"><a name="l00256"></a><span class="lineno"> 256</span>  <span class="comment">// 2D space, hence size = number of rows * 2</span></div>
|
|
<div class="line"><a name="l00257"></a><span class="lineno"> 257</span>  <span class="keywordtype">double</span> **X = (<span class="keywordtype">double</span> **)malloc(<a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a> * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
|
|
<div class="line"><a name="l00258"></a><span class="lineno"> 258</span>  </div>
|
|
<div class="line"><a name="l00259"></a><span class="lineno"> 259</span>  <span class="comment">// number of clusters nodes * 2</span></div>
|
|
<div class="line"><a name="l00260"></a><span class="lineno"> 260</span>  <span class="keywordtype">double</span> **W = (<span class="keywordtype">double</span> **)malloc(num_out * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
|
|
<div class="line"><a name="l00261"></a><span class="lineno"> 261</span>  </div>
|
|
<div class="line"><a name="l00262"></a><span class="lineno"> 262</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#affe776513b24d84b39af8ab0930fef7f">max</a>(num_out, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); i++) <span class="comment">// loop till max(N, num_out)</span></div>
|
|
<div class="line"><a name="l00263"></a><span class="lineno"> 263</span>  {</div>
|
|
<div class="line"><a name="l00264"></a><span class="lineno"> 264</span>  <span class="keywordflow">if</span> (i < <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>) <span class="comment">// only add new arrays if i < N</span></div>
|
|
<div class="line"><a name="l00265"></a><span class="lineno"> 265</span>  X[i] = (<span class="keywordtype">double</span> *)malloc(features * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
|
|
<div class="line"><a name="l00266"></a><span class="lineno"> 266</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 name="l00267"></a><span class="lineno"> 267</span>  {</div>
|
|
<div class="line"><a name="l00268"></a><span class="lineno"> 268</span>  W[i] = (<span class="keywordtype">double</span> *)malloc(features * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
|
|
<div class="line"><a name="l00269"></a><span class="lineno"> 269</span> <span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><a name="l00270"></a><span class="lineno"> 270</span> <span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><a name="l00271"></a><span class="lineno"> 271</span> <span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a name="l00272"></a><span class="lineno"> 272</span>  <span class="comment">// preallocate with random initial weights</span></div>
|
|
<div class="line"><a name="l00273"></a><span class="lineno"> 273</span>  <span class="keywordflow">for</span> (j = 0; j < features; j++) W[i][j] = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(-1, 1);</div>
|
|
<div class="line"><a name="l00274"></a><span class="lineno"> 274</span>  }</div>
|
|
<div class="line"><a name="l00275"></a><span class="lineno"> 275</span>  }</div>
|
|
<div class="line"><a name="l00276"></a><span class="lineno"> 276</span>  </div>
|
|
<div class="line"><a name="l00277"></a><span class="lineno"> 277</span>  <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a107f00650b8041f77767927073ddddb8">test_circle</a>(X, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); <span class="comment">// create test data around circumference of a circle</span></div>
|
|
<div class="line"><a name="l00278"></a><span class="lineno"> 278</span>  <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">"test1.csv"</span>, X, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>, features); <span class="comment">// save test data points</span></div>
|
|
<div class="line"><a name="l00279"></a><span class="lineno"> 279</span>  <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">"w11.csv"</span>, W, num_out,</div>
|
|
<div class="line"><a name="l00280"></a><span class="lineno"> 280</span>  features); <span class="comment">// save initial random weights</span></div>
|
|
<div class="line"><a name="l00281"></a><span class="lineno"> 281</span>  <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#aeaeffbff2be4d5d15b0d4f10f846abde">kohonen_som_tracer</a>(X, W, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>, features, num_out, 0.1); <span class="comment">// train the SOM</span></div>
|
|
<div class="line"><a name="l00282"></a><span class="lineno"> 282</span>  <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">"w12.csv"</span>, W, num_out,</div>
|
|
<div class="line"><a name="l00283"></a><span class="lineno"> 283</span>  features); <span class="comment">// save the resultant weights</span></div>
|
|
<div class="line"><a name="l00284"></a><span class="lineno"> 284</span>  </div>
|
|
<div class="line"><a name="l00285"></a><span class="lineno"> 285</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#affe776513b24d84b39af8ab0930fef7f">max</a>(num_out, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); i++)</div>
|
|
<div class="line"><a name="l00286"></a><span class="lineno"> 286</span>  {</div>
|
|
<div class="line"><a name="l00287"></a><span class="lineno"> 287</span>  <span class="keywordflow">if</span> (i < <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>)</div>
|
|
<div class="line"><a name="l00288"></a><span class="lineno"> 288</span>  free(X[i]);</div>
|
|
<div class="line"><a name="l00289"></a><span class="lineno"> 289</span>  <span class="keywordflow">if</span> (i < num_out)</div>
|
|
<div class="line"><a name="l00290"></a><span class="lineno"> 290</span>  free(W[i]);</div>
|
|
<div class="line"><a name="l00291"></a><span class="lineno"> 291</span>  }</div>
|
|
<div class="line"><a name="l00292"></a><span class="lineno"> 292</span> }</div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d0/d46/kohonen__som__trace_8c_a1440a7779ac56f47a3f355ce4a8c7da0_cgraph.svg" width="551" height="190"><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="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 <em>near</em> the locus of the <a href="https://en.wikipedia.org/wiki/Lemniscate_of_Gerono">Lamniscate of Gerono</a> and trains an SOM that finds that circular pattern. </p>
|
|
<p>The following <a href="https://en.wikipedia.org/wiki/Comma-separated_values">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>
|
|
<p>The outputs can be readily plotted in <a href="https:://gnuplot.info">gnuplot</a> using the following snippet </p><div class="fragment"><div class="line">set datafile separator ','</div>
|
|
<div class="line">plot "test2.csv" title "original", \</div>
|
|
<div class="line"> "w21.csv" title "w1", \</div>
|
|
<div class="line"> "w22.csv" title "w2"</div>
|
|
</div><!-- fragment --><p><img src="https://raw.githubusercontent.com/TheAlgorithms/C/docs/images/machine_learning/kohonen/test2.svg" alt="Sample execution
|
|
output" style="pointer-events: none;" class="inline"/> </p>
|
|
<div class="fragment"><div class="line"><a name="l00348"></a><span class="lineno"> 348</span> {</div>
|
|
<div class="line"><a name="l00349"></a><span class="lineno"> 349</span>  <span class="keywordtype">int</span> j, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a> = 500;</div>
|
|
<div class="line"><a name="l00350"></a><span class="lineno"> 350</span>  <span class="keywordtype">int</span> features = 2;</div>
|
|
<div class="line"><a name="l00351"></a><span class="lineno"> 351</span>  <span class="keywordtype">int</span> num_out = 20;</div>
|
|
<div class="line"><a name="l00352"></a><span class="lineno"> 352</span>  <span class="keywordtype">double</span> **X = (<span class="keywordtype">double</span> **)malloc(<a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a> * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
|
|
<div class="line"><a name="l00353"></a><span class="lineno"> 353</span>  <span class="keywordtype">double</span> **W = (<span class="keywordtype">double</span> **)malloc(num_out * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
|
|
<div class="line"><a name="l00354"></a><span class="lineno"> 354</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#affe776513b24d84b39af8ab0930fef7f">max</a>(num_out, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); i++)</div>
|
|
<div class="line"><a name="l00355"></a><span class="lineno"> 355</span>  {</div>
|
|
<div class="line"><a name="l00356"></a><span class="lineno"> 356</span>  <span class="keywordflow">if</span> (i < <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>) <span class="comment">// only add new arrays if i < N</span></div>
|
|
<div class="line"><a name="l00357"></a><span class="lineno"> 357</span>  X[i] = (<span class="keywordtype">double</span> *)malloc(features * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
|
|
<div class="line"><a name="l00358"></a><span class="lineno"> 358</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 name="l00359"></a><span class="lineno"> 359</span>  {</div>
|
|
<div class="line"><a name="l00360"></a><span class="lineno"> 360</span>  W[i] = (<span class="keywordtype">double</span> *)malloc(features * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
|
|
<div class="line"><a name="l00361"></a><span class="lineno"> 361</span>  </div>
|
|
<div class="line"><a name="l00362"></a><span class="lineno"> 362</span> <span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><a name="l00363"></a><span class="lineno"> 363</span> <span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><a name="l00364"></a><span class="lineno"> 364</span> <span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a name="l00365"></a><span class="lineno"> 365</span>  <span class="comment">// preallocate with random initial weights</span></div>
|
|
<div class="line"><a name="l00366"></a><span class="lineno"> 366</span>  <span class="keywordflow">for</span> (j = 0; j < features; j++) W[i][j] = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(-1, 1);</div>
|
|
<div class="line"><a name="l00367"></a><span class="lineno"> 367</span>  }</div>
|
|
<div class="line"><a name="l00368"></a><span class="lineno"> 368</span>  }</div>
|
|
<div class="line"><a name="l00369"></a><span class="lineno"> 369</span>  </div>
|
|
<div class="line"><a name="l00370"></a><span class="lineno"> 370</span>  <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#aa2246f940155472084ee461f3685d614">test_lamniscate</a>(X, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); <span class="comment">// create test data around the lamniscate</span></div>
|
|
<div class="line"><a name="l00371"></a><span class="lineno"> 371</span>  <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">"test2.csv"</span>, X, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>, features); <span class="comment">// save test data points</span></div>
|
|
<div class="line"><a name="l00372"></a><span class="lineno"> 372</span>  <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">"w21.csv"</span>, W, num_out,</div>
|
|
<div class="line"><a name="l00373"></a><span class="lineno"> 373</span>  features); <span class="comment">// save initial random weights</span></div>
|
|
<div class="line"><a name="l00374"></a><span class="lineno"> 374</span>  <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#aeaeffbff2be4d5d15b0d4f10f846abde">kohonen_som_tracer</a>(X, W, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>, features, num_out, 0.01); <span class="comment">// train the SOM</span></div>
|
|
<div class="line"><a name="l00375"></a><span class="lineno"> 375</span>  <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">"w22.csv"</span>, W, num_out,</div>
|
|
<div class="line"><a name="l00376"></a><span class="lineno"> 376</span>  features); <span class="comment">// save the resultant weights</span></div>
|
|
<div class="line"><a name="l00377"></a><span class="lineno"> 377</span>  </div>
|
|
<div class="line"><a name="l00378"></a><span class="lineno"> 378</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#affe776513b24d84b39af8ab0930fef7f">max</a>(num_out, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); i++)</div>
|
|
<div class="line"><a name="l00379"></a><span class="lineno"> 379</span>  {</div>
|
|
<div class="line"><a name="l00380"></a><span class="lineno"> 380</span>  <span class="keywordflow">if</span> (i < <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>)</div>
|
|
<div class="line"><a name="l00381"></a><span class="lineno"> 381</span>  free(X[i]);</div>
|
|
<div class="line"><a name="l00382"></a><span class="lineno"> 382</span>  <span class="keywordflow">if</span> (i < num_out)</div>
|
|
<div class="line"><a name="l00383"></a><span class="lineno"> 383</span>  free(W[i]);</div>
|
|
<div class="line"><a name="l00384"></a><span class="lineno"> 384</span>  }</div>
|
|
<div class="line"><a name="l00385"></a><span class="lineno"> 385</span>  free(X);</div>
|
|
<div class="line"><a name="l00386"></a><span class="lineno"> 386</span>  free(W);</div>
|
|
<div class="line"><a name="l00387"></a><span class="lineno"> 387</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="../../d0/d46/kohonen__som__trace_8c_a0283886819c7c140a023582b7269e2d0_cgraph.svg" width="551" height="190"><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="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 six clusters in 3D space. </p>
|
|
<p>The following <a href="https://en.wikipedia.org/wiki/Comma-separated_values">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>
|
|
<p>The outputs can be readily plotted in <a href="https:://gnuplot.info">gnuplot</a> using the following snippet </p><div class="fragment"><div class="line">set datafile separator ','</div>
|
|
<div class="line">plot "test3.csv" title "original", \</div>
|
|
<div class="line"> "w31.csv" title "w1", \</div>
|
|
<div class="line"> "w32.csv" title "w2"</div>
|
|
</div><!-- fragment --><p><img src="https://raw.githubusercontent.com/TheAlgorithms/C/docs/images/machine_learning/kohonen/test3.svg" alt="Sample execution
|
|
output" style="pointer-events: none;" class="inline"/> </p>
|
|
<div class="fragment"><div class="line"><a name="l00452"></a><span class="lineno"> 452</span> {</div>
|
|
<div class="line"><a name="l00453"></a><span class="lineno"> 453</span>  <span class="keywordtype">int</span> j, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a> = 200;</div>
|
|
<div class="line"><a name="l00454"></a><span class="lineno"> 454</span>  <span class="keywordtype">int</span> features = 3;</div>
|
|
<div class="line"><a name="l00455"></a><span class="lineno"> 455</span>  <span class="keywordtype">int</span> num_out = 20;</div>
|
|
<div class="line"><a name="l00456"></a><span class="lineno"> 456</span>  <span class="keywordtype">double</span> **X = (<span class="keywordtype">double</span> **)malloc(<a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a> * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
|
|
<div class="line"><a name="l00457"></a><span class="lineno"> 457</span>  <span class="keywordtype">double</span> **W = (<span class="keywordtype">double</span> **)malloc(num_out * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span> *));</div>
|
|
<div class="line"><a name="l00458"></a><span class="lineno"> 458</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#affe776513b24d84b39af8ab0930fef7f">max</a>(num_out, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); i++)</div>
|
|
<div class="line"><a name="l00459"></a><span class="lineno"> 459</span>  {</div>
|
|
<div class="line"><a name="l00460"></a><span class="lineno"> 460</span>  <span class="keywordflow">if</span> (i < <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>) <span class="comment">// only add new arrays if i < N</span></div>
|
|
<div class="line"><a name="l00461"></a><span class="lineno"> 461</span>  X[i] = (<span class="keywordtype">double</span> *)malloc(features * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
|
|
<div class="line"><a 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 name="l00463"></a><span class="lineno"> 463</span>  {</div>
|
|
<div class="line"><a name="l00464"></a><span class="lineno"> 464</span>  W[i] = (<span class="keywordtype">double</span> *)malloc(features * <span class="keyword">sizeof</span>(<span class="keywordtype">double</span>));</div>
|
|
<div class="line"><a name="l00465"></a><span class="lineno"> 465</span>  </div>
|
|
<div class="line"><a name="l00466"></a><span class="lineno"> 466</span> <span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><a name="l00467"></a><span class="lineno"> 467</span> <span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><a name="l00468"></a><span class="lineno"> 468</span> <span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a name="l00469"></a><span class="lineno"> 469</span>  <span class="comment">// preallocate with random initial weights</span></div>
|
|
<div class="line"><a name="l00470"></a><span class="lineno"> 470</span>  <span class="keywordflow">for</span> (j = 0; j < features; j++) W[i][j] = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(-1, 1);</div>
|
|
<div class="line"><a name="l00471"></a><span class="lineno"> 471</span>  }</div>
|
|
<div class="line"><a name="l00472"></a><span class="lineno"> 472</span>  }</div>
|
|
<div class="line"><a name="l00473"></a><span class="lineno"> 473</span>  </div>
|
|
<div class="line"><a name="l00474"></a><span class="lineno"> 474</span>  <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a41ae16442e3e5b891a58d2e5932a2cd0">test_3d_classes</a>(X, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); <span class="comment">// create test data around the lamniscate</span></div>
|
|
<div class="line"><a name="l00475"></a><span class="lineno"> 475</span>  <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">"test3.csv"</span>, X, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>, features); <span class="comment">// save test data points</span></div>
|
|
<div class="line"><a name="l00476"></a><span class="lineno"> 476</span>  <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">"w31.csv"</span>, W, num_out,</div>
|
|
<div class="line"><a name="l00477"></a><span class="lineno"> 477</span>  features); <span class="comment">// save initial random weights</span></div>
|
|
<div class="line"><a name="l00478"></a><span class="lineno"> 478</span>  <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#aeaeffbff2be4d5d15b0d4f10f846abde">kohonen_som_tracer</a>(X, W, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>, features, num_out, 0.01); <span class="comment">// train the SOM</span></div>
|
|
<div class="line"><a name="l00479"></a><span class="lineno"> 479</span>  <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a>(<span class="stringliteral">"w32.csv"</span>, W, num_out,</div>
|
|
<div class="line"><a name="l00480"></a><span class="lineno"> 480</span>  features); <span class="comment">// save the resultant weights</span></div>
|
|
<div class="line"><a name="l00481"></a><span class="lineno"> 481</span>  </div>
|
|
<div class="line"><a name="l00482"></a><span class="lineno"> 482</span>  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#affe776513b24d84b39af8ab0930fef7f">max</a>(num_out, <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>); i++)</div>
|
|
<div class="line"><a name="l00483"></a><span class="lineno"> 483</span>  {</div>
|
|
<div class="line"><a name="l00484"></a><span class="lineno"> 484</span>  <span class="keywordflow">if</span> (i < <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>)</div>
|
|
<div class="line"><a name="l00485"></a><span class="lineno"> 485</span>  free(X[i]);</div>
|
|
<div class="line"><a name="l00486"></a><span class="lineno"> 486</span>  <span class="keywordflow">if</span> (i < num_out)</div>
|
|
<div class="line"><a name="l00487"></a><span class="lineno"> 487</span>  free(W[i]);</div>
|
|
<div class="line"><a name="l00488"></a><span class="lineno"> 488</span>  }</div>
|
|
<div class="line"><a name="l00489"></a><span class="lineno"> 489</span>  free(X);</div>
|
|
<div class="line"><a name="l00490"></a><span class="lineno"> 490</span>  free(W);</div>
|
|
<div class="line"><a name="l00491"></a><span class="lineno"> 491</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="../../d0/d46/kohonen__som__trace_8c_a6d0455dd5c30adda100e95f0423c786e_cgraph.svg" width="551" height="190"><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="a41ae16442e3e5b891a58d2e5932a2cd0"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a41ae16442e3e5b891a58d2e5932a2cd0">◆ </a></span>test_3d_classes()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void test_3d_classes </td>
|
|
<td>(</td>
|
|
<td class="paramtype">double *const * </td>
|
|
<td class="paramname"><em>data</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>N</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</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>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">N</td><td>number of points required </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<div class="fragment"><div class="line"><a name="l00400"></a><span class="lineno"> 400</span> {</div>
|
|
<div class="line"><a name="l00401"></a><span class="lineno"> 401</span>  <span class="keyword">const</span> <span class="keywordtype">double</span> R = 0.1; <span class="comment">// radius of cluster</span></div>
|
|
<div class="line"><a name="l00402"></a><span class="lineno"> 402</span>  <span class="keywordtype">int</span> i;</div>
|
|
<div class="line"><a name="l00403"></a><span class="lineno"> 403</span>  <span class="keyword">const</span> <span class="keywordtype">int</span> num_classes = 4;</div>
|
|
<div class="line"><a name="l00404"></a><span class="lineno"> 404</span>  <span class="keyword">const</span> <span class="keywordtype">double</span> centres[][3] = {</div>
|
|
<div class="line"><a name="l00405"></a><span class="lineno"> 405</span>  <span class="comment">// centres of each class cluster</span></div>
|
|
<div class="line"><a name="l00406"></a><span class="lineno"> 406</span>  {.5, .5, .5}, <span class="comment">// centre of class 1</span></div>
|
|
<div class="line"><a name="l00407"></a><span class="lineno"> 407</span>  {.5, -.5, -.5}, <span class="comment">// centre of class 2</span></div>
|
|
<div class="line"><a name="l00408"></a><span class="lineno"> 408</span>  {-.5, .5, .5}, <span class="comment">// centre of class 3</span></div>
|
|
<div class="line"><a name="l00409"></a><span class="lineno"> 409</span>  {-.5, -.5 - .5} <span class="comment">// centre of class 4</span></div>
|
|
<div class="line"><a name="l00410"></a><span class="lineno"> 410</span>  };</div>
|
|
<div class="line"><a name="l00411"></a><span class="lineno"> 411</span>  </div>
|
|
<div class="line"><a name="l00412"></a><span class="lineno"> 412</span> <span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><a name="l00413"></a><span class="lineno"> 413</span> <span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><a name="l00414"></a><span class="lineno"> 414</span> <span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a name="l00415"></a><span class="lineno"> 415</span>  <span class="keywordflow">for</span> (i = 0; i < <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++)</div>
|
|
<div class="line"><a name="l00416"></a><span class="lineno"> 416</span>  {</div>
|
|
<div class="line"><a name="l00417"></a><span class="lineno"> 417</span>  <span class="keywordtype">int</span> <span class="keyword">class </span>=</div>
|
|
<div class="line"><a name="l00418"></a><span class="lineno"> 418</span>  rand() % num_classes; <span class="comment">// select a random class for the point</span></div>
|
|
<div class="line"><a name="l00419"></a><span class="lineno"> 419</span>  </div>
|
|
<div class="line"><a name="l00420"></a><span class="lineno"> 420</span>  <span class="comment">// create random coordinates (x,y,z) around the centre of the class</span></div>
|
|
<div class="line"><a name="l00421"></a><span class="lineno"> 421</span>  <a class="code" href="../../df/dea/structdata.html">data</a>[i][0] = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(centres[<span class="keyword">class</span>][0] - R, centres[<span class="keyword">class</span>][0] + R);</div>
|
|
<div class="line"><a name="l00422"></a><span class="lineno"> 422</span>  <a class="code" href="../../df/dea/structdata.html">data</a>[i][1] = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(centres[<span class="keyword">class</span>][1] - R, centres[<span class="keyword">class</span>][1] + R);</div>
|
|
<div class="line"><a name="l00423"></a><span class="lineno"> 423</span>  <a class="code" href="../../df/dea/structdata.html">data</a>[i][2] = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(centres[<span class="keyword">class</span>][2] - R, centres[<span class="keyword">class</span>][2] + R);</div>
|
|
<div class="line"><a name="l00424"></a><span class="lineno"> 424</span>  </div>
|
|
<div class="line"><a name="l00425"></a><span class="lineno"> 425</span>  <span class="comment">/* The follosing can also be used</span></div>
|
|
<div class="line"><a name="l00426"></a><span class="lineno"> 426</span> <span class="comment"> for (int j = 0; j < 3; j++)</span></div>
|
|
<div class="line"><a name="l00427"></a><span class="lineno"> 427</span> <span class="comment"> data[i][j] = _random(centres[class][j] - R, centres[class][j] + R);</span></div>
|
|
<div class="line"><a name="l00428"></a><span class="lineno"> 428</span> <span class="comment"> */</span></div>
|
|
<div class="line"><a name="l00429"></a><span class="lineno"> 429</span>  }</div>
|
|
<div class="line"><a name="l00430"></a><span class="lineno"> 430</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="../../d0/d46/kohonen__som__trace_8c_a41ae16442e3e5b891a58d2e5932a2cd0_cgraph.svg" width="248" height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<a id="a107f00650b8041f77767927073ddddb8"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a107f00650b8041f77767927073ddddb8">◆ </a></span>test_circle()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void test_circle </td>
|
|
<td>(</td>
|
|
<td class="paramtype">double *const * </td>
|
|
<td class="paramname"><em>data</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>N</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Creates a random set of points distributed <em>near</em> the circumference of a circle and trains an SOM that finds that circular pattern. </p>
|
|
<p>The generating function is </p><p class="formulaDsp">
|
|
\begin{eqnarray*} r &\in& [1-\delta r, 1+\delta r)\\ \theta &\in& [0, 2\pi)\\ x &=& r\cos\theta\\ y &=& r\sin\theta \end{eqnarray*}
|
|
</p>
|
|
<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>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">N</td><td>number of points required </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<div class="fragment"><div class="line"><a name="l00213"></a><span class="lineno"> 213</span> {</div>
|
|
<div class="line"><a name="l00214"></a><span class="lineno"> 214</span>  <span class="keyword">const</span> <span class="keywordtype">double</span> R = 0.75, dr = 0.3;</div>
|
|
<div class="line"><a name="l00215"></a><span class="lineno"> 215</span>  <span class="keywordtype">double</span> a_t = 0., b_t = 2.f * M_PI; <span class="comment">// theta random between 0 and 2*pi</span></div>
|
|
<div class="line"><a name="l00216"></a><span class="lineno"> 216</span>  <span class="keywordtype">double</span> a_r = R - dr, b_r = R + dr; <span class="comment">// radius random between R-dr and R+dr</span></div>
|
|
<div class="line"><a name="l00217"></a><span class="lineno"> 217</span>  <span class="keywordtype">int</span> i;</div>
|
|
<div class="line"><a name="l00218"></a><span class="lineno"> 218</span>  </div>
|
|
<div class="line"><a name="l00219"></a><span class="lineno"> 219</span> <span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><a name="l00220"></a><span class="lineno"> 220</span> <span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><a name="l00221"></a><span class="lineno"> 221</span> <span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a name="l00222"></a><span class="lineno"> 222</span>  <span class="keywordflow">for</span> (i = 0; i < <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++)</div>
|
|
<div class="line"><a name="l00223"></a><span class="lineno"> 223</span>  {</div>
|
|
<div class="line"><a name="l00224"></a><span class="lineno"> 224</span>  <span class="keywordtype">double</span> r = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(a_r, b_r); <span class="comment">// random radius</span></div>
|
|
<div class="line"><a name="l00225"></a><span class="lineno"> 225</span>  <span class="keywordtype">double</span> theta = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(a_t, b_t); <span class="comment">// random theta</span></div>
|
|
<div class="line"><a name="l00226"></a><span class="lineno"> 226</span>  <a class="code" href="../../df/dea/structdata.html">data</a>[i][0] = r * cos(theta); <span class="comment">// convert from polar to cartesian</span></div>
|
|
<div class="line"><a name="l00227"></a><span class="lineno"> 227</span>  <a class="code" href="../../df/dea/structdata.html">data</a>[i][1] = r * sin(theta);</div>
|
|
<div class="line"><a name="l00228"></a><span class="lineno"> 228</span>  }</div>
|
|
<div class="line"><a name="l00229"></a><span class="lineno"> 229</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="../../d0/d46/kohonen__som__trace_8c_a107f00650b8041f77767927073ddddb8_cgraph.svg" width="214" height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<a id="aa2246f940155472084ee461f3685d614"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#aa2246f940155472084ee461f3685d614">◆ </a></span>test_lamniscate()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void test_lamniscate </td>
|
|
<td>(</td>
|
|
<td class="paramtype">double *const * </td>
|
|
<td class="paramname"><em>data</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>N</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Creates a random set of points distributed <em>near</em> the locus of the <a href="https://en.wikipedia.org/wiki/Lemniscate_of_Gerono">Lamniscate of Gerono</a>. </p>
|
|
<p class="formulaDsp">
|
|
\begin{eqnarray*} \delta r &=& 0.2\\ \delta x &\in& [-\delta r, \delta r)\\ \delta y &\in& [-\delta r, \delta r)\\ \theta &\in& [0, \pi)\\ x &=& \delta x + \cos\theta\\ y &=& \delta y + \frac{\sin(2\theta)}{2} \end{eqnarray*}
|
|
</p>
|
|
<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>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">N</td><td>number of points required </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<div class="fragment"><div class="line"><a name="l00309"></a><span class="lineno"> 309</span> {</div>
|
|
<div class="line"><a name="l00310"></a><span class="lineno"> 310</span>  <span class="keyword">const</span> <span class="keywordtype">double</span> dr = 0.2;</div>
|
|
<div class="line"><a name="l00311"></a><span class="lineno"> 311</span>  <span class="keywordtype">int</span> i;</div>
|
|
<div class="line"><a name="l00312"></a><span class="lineno"> 312</span>  </div>
|
|
<div class="line"><a name="l00313"></a><span class="lineno"> 313</span> <span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><a name="l00314"></a><span class="lineno"> 314</span> <span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><a name="l00315"></a><span class="lineno"> 315</span> <span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a name="l00316"></a><span class="lineno"> 316</span>  <span class="keywordflow">for</span> (i = 0; i < <a class="code" href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a>; i++)</div>
|
|
<div class="line"><a name="l00317"></a><span class="lineno"> 317</span>  {</div>
|
|
<div class="line"><a name="l00318"></a><span class="lineno"> 318</span>  <span class="keywordtype">double</span> dx = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(-dr, dr); <span class="comment">// random change in x</span></div>
|
|
<div class="line"><a name="l00319"></a><span class="lineno"> 319</span>  <span class="keywordtype">double</span> dy = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(-dr, dr); <span class="comment">// random change in y</span></div>
|
|
<div class="line"><a name="l00320"></a><span class="lineno"> 320</span>  <span class="keywordtype">double</span> theta = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a>(0, M_PI); <span class="comment">// random theta</span></div>
|
|
<div class="line"><a name="l00321"></a><span class="lineno"> 321</span>  <a class="code" href="../../df/dea/structdata.html">data</a>[i][0] = dx + cos(theta); <span class="comment">// convert from polar to cartesian</span></div>
|
|
<div class="line"><a name="l00322"></a><span class="lineno"> 322</span>  <a class="code" href="../../df/dea/structdata.html">data</a>[i][1] = dy + sin(2. * theta) / 2.f;</div>
|
|
<div class="line"><a name="l00323"></a><span class="lineno"> 323</span>  }</div>
|
|
<div class="line"><a name="l00324"></a><span class="lineno"> 324</span> }</div>
|
|
</div><!-- fragment --><div class="dynheader">
|
|
Here is the call graph for this function:</div>
|
|
<div class="dyncontent">
|
|
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d0/d46/kohonen__som__trace_8c_aa2246f940155472084ee461f3685d614_cgraph.svg" width="246" height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
<a id="a9c98203f55242ff7a8f45a6370840e66"></a>
|
|
<h2 class="memtitle"><span class="permalink"><a href="#a9c98203f55242ff7a8f45a6370840e66">◆ </a></span>update_weights()</h2>
|
|
|
|
<div class="memitem">
|
|
<div class="memproto">
|
|
<table class="memname">
|
|
<tr>
|
|
<td class="memname">void update_weights </td>
|
|
<td>(</td>
|
|
<td class="paramtype">double const * </td>
|
|
<td class="paramname"><em>x</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double *const * </td>
|
|
<td class="paramname"><em>W</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double * </td>
|
|
<td class="paramname"><em>D</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>num_out</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>num_features</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">double </td>
|
|
<td class="paramname"><em>alpha</em>, </td>
|
|
</tr>
|
|
<tr>
|
|
<td class="paramkey"></td>
|
|
<td></td>
|
|
<td class="paramtype">int </td>
|
|
<td class="paramname"><em>R</em> </td>
|
|
</tr>
|
|
<tr>
|
|
<td></td>
|
|
<td>)</td>
|
|
<td></td><td></td>
|
|
</tr>
|
|
</table>
|
|
</div><div class="memdoc">
|
|
|
|
<p>Update weights of the SOM using Kohonen algorithm. </p>
|
|
<dl class="params"><dt>Parameters</dt><dd>
|
|
<table class="params">
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">x</td><td>data point </td></tr>
|
|
<tr><td class="paramdir">[in,out]</td><td class="paramname">W</td><td>weights matrix </td></tr>
|
|
<tr><td class="paramdir">[in,out]</td><td class="paramname">D</td><td>temporary vector to store distances </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">num_out</td><td>number of output points </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">num_features</td><td>number of features per input sample </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">alpha</td><td>learning rate \(0<\alpha\le1\) </td></tr>
|
|
<tr><td class="paramdir">[in]</td><td class="paramname">R</td><td>neighborhood range </td></tr>
|
|
</table>
|
|
</dd>
|
|
</dl>
|
|
<div class="fragment"><div class="line"><a name="l00125"></a><span class="lineno"> 125</span> {</div>
|
|
<div class="line"><a name="l00126"></a><span class="lineno"> 126</span>  <span class="keywordtype">int</span> j, k;</div>
|
|
<div class="line"><a name="l00127"></a><span class="lineno"> 127</span>  </div>
|
|
<div class="line"><a name="l00128"></a><span class="lineno"> 128</span> <span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><a name="l00129"></a><span class="lineno"> 129</span> <span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><a name="l00130"></a><span class="lineno"> 130</span> <span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a name="l00131"></a><span class="lineno"> 131</span>  <span class="comment">// step 1: for each output point</span></div>
|
|
<div class="line"><a name="l00132"></a><span class="lineno"> 132</span>  <span class="keywordflow">for</span> (j = 0; j < num_out; j++)</div>
|
|
<div class="line"><a name="l00133"></a><span class="lineno"> 133</span>  {</div>
|
|
<div class="line"><a name="l00134"></a><span class="lineno"> 134</span>  D[j] = 0.f;</div>
|
|
<div class="line"><a name="l00135"></a><span class="lineno"> 135</span>  <span class="comment">// compute Euclidian distance of each output</span></div>
|
|
<div class="line"><a name="l00136"></a><span class="lineno"> 136</span>  <span class="comment">// point from the current sample</span></div>
|
|
<div class="line"><a name="l00137"></a><span class="lineno"> 137</span>  <span class="keywordflow">for</span> (k = 0; k < num_features; k++)</div>
|
|
<div class="line"><a name="l00138"></a><span class="lineno"> 138</span>  D[j] += (W[j][k] - x[k]) * (W[j][k] - x[k]);</div>
|
|
<div class="line"><a name="l00139"></a><span class="lineno"> 139</span>  }</div>
|
|
<div class="line"><a name="l00140"></a><span class="lineno"> 140</span>  </div>
|
|
<div class="line"><a name="l00141"></a><span class="lineno"> 141</span>  <span class="comment">// step 2: get closest node i.e., node with snallest Euclidian distance to</span></div>
|
|
<div class="line"><a name="l00142"></a><span class="lineno"> 142</span>  <span class="comment">// the current pattern</span></div>
|
|
<div class="line"><a name="l00143"></a><span class="lineno"> 143</span>  <span class="keywordtype">int</span> d_min_idx;</div>
|
|
<div class="line"><a name="l00144"></a><span class="lineno"> 144</span>  <span class="keywordtype">double</span> d_min;</div>
|
|
<div class="line"><a name="l00145"></a><span class="lineno"> 145</span>  <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#a0a292ebd954c568934b1fd06666d27e6">get_min_1d</a>(D, num_out, &d_min, &d_min_idx);</div>
|
|
<div class="line"><a name="l00146"></a><span class="lineno"> 146</span>  </div>
|
|
<div class="line"><a name="l00147"></a><span class="lineno"> 147</span>  <span class="comment">// step 3a: get the neighborhood range</span></div>
|
|
<div class="line"><a name="l00148"></a><span class="lineno"> 148</span>  <span class="keywordtype">int</span> from_node = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#affe776513b24d84b39af8ab0930fef7f">max</a>(0, d_min_idx - R);</div>
|
|
<div class="line"><a name="l00149"></a><span class="lineno"> 149</span>  <span class="keywordtype">int</span> to_node = <a class="code" href="../../d0/d46/kohonen__som__trace_8c.html#ac6afabdc09a49a433ee19d8a9486056d">min</a>(num_out, d_min_idx + R + 1);</div>
|
|
<div class="line"><a name="l00150"></a><span class="lineno"> 150</span>  </div>
|
|
<div class="line"><a name="l00151"></a><span class="lineno"> 151</span>  <span class="comment">// step 3b: update the weights of nodes in the</span></div>
|
|
<div class="line"><a name="l00152"></a><span class="lineno"> 152</span>  <span class="comment">// neighborhood</span></div>
|
|
<div class="line"><a name="l00153"></a><span class="lineno"> 153</span> <span class="preprocessor">#ifdef _OPENMP</span></div>
|
|
<div class="line"><a name="l00154"></a><span class="lineno"> 154</span> <span class="preprocessor">#pragma omp for</span></div>
|
|
<div class="line"><a name="l00155"></a><span class="lineno"> 155</span> <span class="preprocessor">#endif</span></div>
|
|
<div class="line"><a name="l00156"></a><span class="lineno"> 156</span>  <span class="keywordflow">for</span> (j = from_node; j < to_node; j++)</div>
|
|
<div class="line"><a name="l00157"></a><span class="lineno"> 157</span>  <span class="keywordflow">for</span> (k = 0; k < num_features; k++)</div>
|
|
<div class="line"><a name="l00158"></a><span class="lineno"> 158</span>  <span class="comment">// update weights of nodes in the neighborhood</span></div>
|
|
<div class="line"><a name="l00159"></a><span class="lineno"> 159</span>  W[j][k] += alpha * (x[k] - W[j][k]);</div>
|
|
<div class="line"><a name="l00160"></a><span class="lineno"> 160</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="../../d0/d46/kohonen__som__trace_8c_a9c98203f55242ff7a8f45a6370840e66_cgraph.svg" width="262" height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
</div>
|
|
</div><!-- contents -->
|
|
</div><!-- doc-content -->
|
|
<div class="ttc" id="akohonen__som__trace_8c_html_a7b84b14e60f47812b581d1f93057c85a"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#a7b84b14e60f47812b581d1f93057c85a">save_nd_data</a></div><div class="ttdeci">int save_nd_data(const char *fname, double **X, int num_points, int num_features)</div><div class="ttdoc">Save a given n-dimensional data martix to file.</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:64</div></div>
|
|
<div class="ttc" id="akohonen__som__trace_8c_html_affe776513b24d84b39af8ab0930fef7f"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#affe776513b24d84b39af8ab0930fef7f">max</a></div><div class="ttdeci">#define max(a, b)</div><div class="ttdoc">shorthand for maximum value</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:26</div></div>
|
|
<div class="ttc" id="akohonen__som__trace_8c_html_a107f00650b8041f77767927073ddddb8"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#a107f00650b8041f77767927073ddddb8">test_circle</a></div><div class="ttdeci">void test_circle(double *const *data, int N)</div><div class="ttdoc">Creates a random set of points distributed near the circumference of a circle and trains an SOM that ...</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:212</div></div>
|
|
<div class="ttc" id="astructdata_html"><div class="ttname"><a href="../../df/dea/structdata.html">data</a></div><div class="ttdef"><b>Definition:</b> prime_factoriziation.c:25</div></div>
|
|
<div class="ttc" id="akohonen__som__trace_8c_html_aeaeffbff2be4d5d15b0d4f10f846abde"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#aeaeffbff2be4d5d15b0d4f10f846abde">kohonen_som_tracer</a></div><div class="ttdeci">void kohonen_som_tracer(double **X, double *const *W, int num_samples, int num_features, int num_out, double alpha_min)</div><div class="ttdoc">Apply incremental algorithm with updating neighborhood and learning rates on all samples in the given...</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:173</div></div>
|
|
<div class="ttc" id="akohonen__som__trace_8c_html_a9c98203f55242ff7a8f45a6370840e66"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#a9c98203f55242ff7a8f45a6370840e66">update_weights</a></div><div class="ttdeci">void update_weights(double const *x, double *const *W, double *D, int num_out, int num_features, double alpha, int R)</div><div class="ttdoc">Update weights of the SOM using Kohonen algorithm.</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:123</div></div>
|
|
<div class="ttc" id="aproblem__13_2sol1_8c_html_a0240ac851181b84ac374872dc5434ee4"><div class="ttname"><a href="../../db/d01/problem__13_2sol1_8c.html#a0240ac851181b84ac374872dc5434ee4">N</a></div><div class="ttdeci">#define N</div><div class="ttdoc">number of digits of the large number</div><div class="ttdef"><b>Definition:</b> sol1.c:109</div></div>
|
|
<div class="ttc" id="akohonen__som__trace_8c_html_a0a292ebd954c568934b1fd06666d27e6"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#a0a292ebd954c568934b1fd06666d27e6">get_min_1d</a></div><div class="ttdeci">void get_min_1d(double const *X, int N, double *val, int *idx)</div><div class="ttdoc">Get minimum value and index of the value in a vector.</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:98</div></div>
|
|
<div class="ttc" id="akohonen__som__trace_8c_html_af5ce14f026d6d231bef29161bac2b485"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#af5ce14f026d6d231bef29161bac2b485">_random</a></div><div class="ttdeci">double _random(double a, double b)</div><div class="ttdoc">Helper function to generate a random number in a given interval.</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:48</div></div>
|
|
<div class="ttc" id="akohonen__som__trace_8c_html_aa2246f940155472084ee461f3685d614"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#aa2246f940155472084ee461f3685d614">test_lamniscate</a></div><div class="ttdeci">void test_lamniscate(double *const *data, int N)</div><div class="ttdoc">Creates a random set of points distributed near the locus of the Lamniscate of Gerono.</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:308</div></div>
|
|
<div class="ttc" id="akohonen__som__trace_8c_html_ac6afabdc09a49a433ee19d8a9486056d"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#ac6afabdc09a49a433ee19d8a9486056d">min</a></div><div class="ttdeci">#define min(a, b)</div><div class="ttdoc">shorthand for minimum value</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:30</div></div>
|
|
<div class="ttc" id="akohonen__som__trace_8c_html_a41ae16442e3e5b891a58d2e5932a2cd0"><div class="ttname"><a href="../../d0/d46/kohonen__som__trace_8c.html#a41ae16442e3e5b891a58d2e5932a2cd0">test_3d_classes</a></div><div class="ttdeci">void test_3d_classes(double *const *data, int N)</div><div class="ttdoc">Creates a random set of points distributed in four clusters in 3D space with centroids at the points.</div><div class="ttdef"><b>Definition:</b> kohonen_som_trace.c:399</div></div>
|
|
<!-- 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="../../d0/d46/kohonen__som__trace_8c.html">kohonen_som_trace.c</a></li>
|
|
<li class="footer">Generated by
|
|
<a href="http://www.doxygen.org/index.html">
|
|
<img class="footer" src="../../doxygen.png" alt="doxygen"/></a> 1.8.18 </li>
|
|
</ul>
|
|
</div>
|
|
</body>
|
|
</html>
|