196 lines
18 KiB
HTML
196 lines
18 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
|
<title>LCOV - BayesNet Coverage Report - bayesnet/classifiers/KDB.cc</title>
|
|
<link rel="stylesheet" type="text/css" href="../../gcov.css">
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<table width="100%" border=0 cellspacing=0 cellpadding=0>
|
|
<tr><td class="title">LCOV - code coverage report</td></tr>
|
|
<tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
|
|
|
|
<tr>
|
|
<td width="100%">
|
|
<table cellpadding=1 border=0 width="100%">
|
|
<tr>
|
|
<td width="10%" class="headerItem">Current view:</td>
|
|
<td width="10%" class="headerValue"><a href="../../index.html" target="_parent">top level</a> - <a href="index.html" target="_parent">bayesnet/classifiers</a> - KDB.cc<span style="font-size: 80%;"> (source / <a href="KDB.cc.func-c.html">functions</a>)</span></td>
|
|
<td width="5%"></td>
|
|
<td width="5%"></td>
|
|
<td width="5%" class="headerCovTableHead">Coverage</td>
|
|
<td width="5%" class="headerCovTableHead" title="Covered + Uncovered code">Total</td>
|
|
<td width="5%" class="headerCovTableHead" title="Exercised code only">Hit</td>
|
|
</tr>
|
|
<tr>
|
|
<td class="headerItem">Test:</td>
|
|
<td class="headerValue">BayesNet Coverage Report</td>
|
|
<td></td>
|
|
<td class="headerItem">Lines:</td>
|
|
<td class="headerCovTableEntryHi">96.3 %</td>
|
|
<td class="headerCovTableEntry">54</td>
|
|
<td class="headerCovTableEntry">52</td>
|
|
</tr>
|
|
<tr>
|
|
<td class="headerItem">Test Date:</td>
|
|
<td class="headerValue">2024-05-06 17:54:04</td>
|
|
<td></td>
|
|
<td class="headerItem">Functions:</td>
|
|
<td class="headerCovTableEntryHi">100.0 %</td>
|
|
<td class="headerCovTableEntry">5</td>
|
|
<td class="headerCovTableEntry">5</td>
|
|
</tr>
|
|
<tr>
|
|
<td class="headerItem">Legend:</td>
|
|
<td class="headerValueLeg"> Lines:
|
|
<span class="coverLegendCov">hit</span>
|
|
<span class="coverLegendNoCov">not hit</span>
|
|
</td>
|
|
<td></td>
|
|
</tr>
|
|
<tr><td><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
|
|
</table>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
|
|
</table>
|
|
|
|
<table cellpadding=0 cellspacing=0 border=0>
|
|
<tr>
|
|
<td><br></td>
|
|
</tr>
|
|
<tr>
|
|
<td>
|
|
<pre class="sourceHeading"> Line data Source code</pre>
|
|
<pre class="source">
|
|
<span id="L1"><span class="lineNum"> 1</span> : // ***************************************************************</span>
|
|
<span id="L2"><span class="lineNum"> 2</span> : // SPDX-FileCopyrightText: Copyright 2024 Ricardo Montañana Gómez</span>
|
|
<span id="L3"><span class="lineNum"> 3</span> : // SPDX-FileType: SOURCE</span>
|
|
<span id="L4"><span class="lineNum"> 4</span> : // SPDX-License-Identifier: MIT</span>
|
|
<span id="L5"><span class="lineNum"> 5</span> : // ***************************************************************</span>
|
|
<span id="L6"><span class="lineNum"> 6</span> : </span>
|
|
<span id="L7"><span class="lineNum"> 7</span> : #include "KDB.h"</span>
|
|
<span id="L8"><span class="lineNum"> 8</span> : </span>
|
|
<span id="L9"><span class="lineNum"> 9</span> : namespace bayesnet {</span>
|
|
<span id="L10"><span class="lineNum"> 10</span> <span class="tlaGNC tlaBgGNC"> 148 : KDB::KDB(int k, float theta) : Classifier(Network()), k(k), theta(theta)</span></span>
|
|
<span id="L11"><span class="lineNum"> 11</span> : {</span>
|
|
<span id="L12"><span class="lineNum"> 12</span> <span class="tlaGNC"> 444 : validHyperparameters = { "k", "theta" };</span></span>
|
|
<span id="L13"><span class="lineNum"> 13</span> : </span>
|
|
<span id="L14"><span class="lineNum"> 14</span> <span class="tlaGNC"> 444 : }</span></span>
|
|
<span id="L15"><span class="lineNum"> 15</span> <span class="tlaGNC"> 12 : void KDB::setHyperparameters(const nlohmann::json& hyperparameters_)</span></span>
|
|
<span id="L16"><span class="lineNum"> 16</span> : {</span>
|
|
<span id="L17"><span class="lineNum"> 17</span> <span class="tlaGNC"> 12 : auto hyperparameters = hyperparameters_;</span></span>
|
|
<span id="L18"><span class="lineNum"> 18</span> <span class="tlaGNC"> 12 : if (hyperparameters.contains("k")) {</span></span>
|
|
<span id="L19"><span class="lineNum"> 19</span> <span class="tlaGNC"> 4 : k = hyperparameters["k"];</span></span>
|
|
<span id="L20"><span class="lineNum"> 20</span> <span class="tlaGNC"> 4 : hyperparameters.erase("k");</span></span>
|
|
<span id="L21"><span class="lineNum"> 21</span> : }</span>
|
|
<span id="L22"><span class="lineNum"> 22</span> <span class="tlaGNC"> 12 : if (hyperparameters.contains("theta")) {</span></span>
|
|
<span id="L23"><span class="lineNum"> 23</span> <span class="tlaGNC"> 4 : theta = hyperparameters["theta"];</span></span>
|
|
<span id="L24"><span class="lineNum"> 24</span> <span class="tlaGNC"> 4 : hyperparameters.erase("theta");</span></span>
|
|
<span id="L25"><span class="lineNum"> 25</span> : }</span>
|
|
<span id="L26"><span class="lineNum"> 26</span> <span class="tlaGNC"> 12 : Classifier::setHyperparameters(hyperparameters);</span></span>
|
|
<span id="L27"><span class="lineNum"> 27</span> <span class="tlaGNC"> 12 : }</span></span>
|
|
<span id="L28"><span class="lineNum"> 28</span> <span class="tlaGNC"> 52 : void KDB::buildModel(const torch::Tensor& weights)</span></span>
|
|
<span id="L29"><span class="lineNum"> 29</span> : {</span>
|
|
<span id="L30"><span class="lineNum"> 30</span> : /*</span>
|
|
<span id="L31"><span class="lineNum"> 31</span> : 1. For each feature Xi, compute mutual information, I(X;C),</span>
|
|
<span id="L32"><span class="lineNum"> 32</span> : where C is the class.</span>
|
|
<span id="L33"><span class="lineNum"> 33</span> : 2. Compute class conditional mutual information I(Xi;XjIC), f or each</span>
|
|
<span id="L34"><span class="lineNum"> 34</span> : pair of features Xi and Xj, where i#j.</span>
|
|
<span id="L35"><span class="lineNum"> 35</span> : 3. Let the used variable list, S, be empty.</span>
|
|
<span id="L36"><span class="lineNum"> 36</span> : 4. Let the DAG network being constructed, BN, begin with a single</span>
|
|
<span id="L37"><span class="lineNum"> 37</span> : class node, C.</span>
|
|
<span id="L38"><span class="lineNum"> 38</span> : 5. Repeat until S includes all domain features</span>
|
|
<span id="L39"><span class="lineNum"> 39</span> : 5.1. Select feature Xmax which is not in S and has the largest value</span>
|
|
<span id="L40"><span class="lineNum"> 40</span> : I(Xmax;C).</span>
|
|
<span id="L41"><span class="lineNum"> 41</span> : 5.2. Add a node to BN representing Xmax.</span>
|
|
<span id="L42"><span class="lineNum"> 42</span> : 5.3. Add an arc from C to Xmax in BN.</span>
|
|
<span id="L43"><span class="lineNum"> 43</span> : 5.4. Add m = min(lSl,/c) arcs from m distinct features Xj in S with</span>
|
|
<span id="L44"><span class="lineNum"> 44</span> : the highest value for I(Xmax;X,jC).</span>
|
|
<span id="L45"><span class="lineNum"> 45</span> : 5.5. Add Xmax to S.</span>
|
|
<span id="L46"><span class="lineNum"> 46</span> : Compute the conditional probabilility infered by the structure of BN by</span>
|
|
<span id="L47"><span class="lineNum"> 47</span> : using counts from DB, and output BN.</span>
|
|
<span id="L48"><span class="lineNum"> 48</span> : */</span>
|
|
<span id="L49"><span class="lineNum"> 49</span> : // 1. For each feature Xi, compute mutual information, I(X;C),</span>
|
|
<span id="L50"><span class="lineNum"> 50</span> : // where C is the class.</span>
|
|
<span id="L51"><span class="lineNum"> 51</span> <span class="tlaGNC"> 52 : addNodes();</span></span>
|
|
<span id="L52"><span class="lineNum"> 52</span> <span class="tlaGNC"> 156 : const torch::Tensor& y = dataset.index({ -1, "..." });</span></span>
|
|
<span id="L53"><span class="lineNum"> 53</span> <span class="tlaGNC"> 52 : std::vector<double> mi;</span></span>
|
|
<span id="L54"><span class="lineNum"> 54</span> <span class="tlaGNC"> 396 : for (auto i = 0; i < features.size(); i++) {</span></span>
|
|
<span id="L55"><span class="lineNum"> 55</span> <span class="tlaGNC"> 1032 : torch::Tensor firstFeature = dataset.index({ i, "..." });</span></span>
|
|
<span id="L56"><span class="lineNum"> 56</span> <span class="tlaGNC"> 344 : mi.push_back(metrics.mutualInformation(firstFeature, y, weights));</span></span>
|
|
<span id="L57"><span class="lineNum"> 57</span> <span class="tlaGNC"> 344 : }</span></span>
|
|
<span id="L58"><span class="lineNum"> 58</span> : // 2. Compute class conditional mutual information I(Xi;XjIC), f or each</span>
|
|
<span id="L59"><span class="lineNum"> 59</span> <span class="tlaGNC"> 52 : auto conditionalEdgeWeights = metrics.conditionalEdge(weights);</span></span>
|
|
<span id="L60"><span class="lineNum"> 60</span> : // 3. Let the used variable list, S, be empty.</span>
|
|
<span id="L61"><span class="lineNum"> 61</span> <span class="tlaGNC"> 52 : std::vector<int> S;</span></span>
|
|
<span id="L62"><span class="lineNum"> 62</span> : // 4. Let the DAG network being constructed, BN, begin with a single</span>
|
|
<span id="L63"><span class="lineNum"> 63</span> : // class node, C.</span>
|
|
<span id="L64"><span class="lineNum"> 64</span> : // 5. Repeat until S includes all domain features</span>
|
|
<span id="L65"><span class="lineNum"> 65</span> : // 5.1. Select feature Xmax which is not in S and has the largest value</span>
|
|
<span id="L66"><span class="lineNum"> 66</span> : // I(Xmax;C).</span>
|
|
<span id="L67"><span class="lineNum"> 67</span> <span class="tlaGNC"> 52 : auto order = argsort(mi);</span></span>
|
|
<span id="L68"><span class="lineNum"> 68</span> <span class="tlaGNC"> 396 : for (auto idx : order) {</span></span>
|
|
<span id="L69"><span class="lineNum"> 69</span> : // 5.2. Add a node to BN representing Xmax.</span>
|
|
<span id="L70"><span class="lineNum"> 70</span> : // 5.3. Add an arc from C to Xmax in BN.</span>
|
|
<span id="L71"><span class="lineNum"> 71</span> <span class="tlaGNC"> 344 : model.addEdge(className, features[idx]);</span></span>
|
|
<span id="L72"><span class="lineNum"> 72</span> : // 5.4. Add m = min(lSl,/c) arcs from m distinct features Xj in S with</span>
|
|
<span id="L73"><span class="lineNum"> 73</span> : // the highest value for I(Xmax;X,jC).</span>
|
|
<span id="L74"><span class="lineNum"> 74</span> <span class="tlaGNC"> 344 : add_m_edges(idx, S, conditionalEdgeWeights);</span></span>
|
|
<span id="L75"><span class="lineNum"> 75</span> : // 5.5. Add Xmax to S.</span>
|
|
<span id="L76"><span class="lineNum"> 76</span> <span class="tlaGNC"> 344 : S.push_back(idx);</span></span>
|
|
<span id="L77"><span class="lineNum"> 77</span> : }</span>
|
|
<span id="L78"><span class="lineNum"> 78</span> <span class="tlaGNC"> 448 : }</span></span>
|
|
<span id="L79"><span class="lineNum"> 79</span> <span class="tlaGNC"> 344 : void KDB::add_m_edges(int idx, std::vector<int>& S, torch::Tensor& weights)</span></span>
|
|
<span id="L80"><span class="lineNum"> 80</span> : {</span>
|
|
<span id="L81"><span class="lineNum"> 81</span> <span class="tlaGNC"> 344 : auto n_edges = std::min(k, static_cast<int>(S.size()));</span></span>
|
|
<span id="L82"><span class="lineNum"> 82</span> <span class="tlaGNC"> 344 : auto cond_w = clone(weights);</span></span>
|
|
<span id="L83"><span class="lineNum"> 83</span> <span class="tlaGNC"> 344 : bool exit_cond = k == 0;</span></span>
|
|
<span id="L84"><span class="lineNum"> 84</span> <span class="tlaGNC"> 344 : int num = 0;</span></span>
|
|
<span id="L85"><span class="lineNum"> 85</span> <span class="tlaGNC"> 1004 : while (!exit_cond) {</span></span>
|
|
<span id="L86"><span class="lineNum"> 86</span> <span class="tlaGNC"> 2640 : auto max_minfo = argmax(cond_w.index({ idx, "..." })).item<int>();</span></span>
|
|
<span id="L87"><span class="lineNum"> 87</span> <span class="tlaGNC"> 660 : auto belongs = find(S.begin(), S.end(), max_minfo) != S.end();</span></span>
|
|
<span id="L88"><span class="lineNum"> 88</span> <span class="tlaGNC"> 1764 : if (belongs && cond_w.index({ idx, max_minfo }).item<float>() > theta) {</span></span>
|
|
<span id="L89"><span class="lineNum"> 89</span> : try {</span>
|
|
<span id="L90"><span class="lineNum"> 90</span> <span class="tlaGNC"> 320 : model.addEdge(features[max_minfo], features[idx]);</span></span>
|
|
<span id="L91"><span class="lineNum"> 91</span> <span class="tlaGNC"> 320 : num++;</span></span>
|
|
<span id="L92"><span class="lineNum"> 92</span> : }</span>
|
|
<span id="L93"><span class="lineNum"> 93</span> <span class="tlaUNC tlaBgUNC"> 0 : catch (const std::invalid_argument& e) {</span></span>
|
|
<span id="L94"><span class="lineNum"> 94</span> : // Loops are not allowed</span>
|
|
<span id="L95"><span class="lineNum"> 95</span> <span class="tlaUNC"> 0 : }</span></span>
|
|
<span id="L96"><span class="lineNum"> 96</span> : }</span>
|
|
<span id="L97"><span class="lineNum"> 97</span> <span class="tlaGNC tlaBgGNC"> 2640 : cond_w.index_put_({ idx, max_minfo }, -1);</span></span>
|
|
<span id="L98"><span class="lineNum"> 98</span> <span class="tlaGNC"> 1980 : auto candidates_mask = cond_w.index({ idx, "..." }).gt(theta);</span></span>
|
|
<span id="L99"><span class="lineNum"> 99</span> <span class="tlaGNC"> 660 : auto candidates = candidates_mask.nonzero();</span></span>
|
|
<span id="L100"><span class="lineNum"> 100</span> <span class="tlaGNC"> 660 : exit_cond = num == n_edges || candidates.size(0) == 0;</span></span>
|
|
<span id="L101"><span class="lineNum"> 101</span> <span class="tlaGNC"> 660 : }</span></span>
|
|
<span id="L102"><span class="lineNum"> 102</span> <span class="tlaGNC"> 2692 : }</span></span>
|
|
<span id="L103"><span class="lineNum"> 103</span> <span class="tlaGNC"> 8 : std::vector<std::string> KDB::graph(const std::string& title) const</span></span>
|
|
<span id="L104"><span class="lineNum"> 104</span> : {</span>
|
|
<span id="L105"><span class="lineNum"> 105</span> <span class="tlaGNC"> 8 : std::string header{ title };</span></span>
|
|
<span id="L106"><span class="lineNum"> 106</span> <span class="tlaGNC"> 8 : if (title == "KDB") {</span></span>
|
|
<span id="L107"><span class="lineNum"> 107</span> <span class="tlaGNC"> 8 : header += " (k=" + std::to_string(k) + ", theta=" + std::to_string(theta) + ")";</span></span>
|
|
<span id="L108"><span class="lineNum"> 108</span> : }</span>
|
|
<span id="L109"><span class="lineNum"> 109</span> <span class="tlaGNC"> 16 : return model.graph(header);</span></span>
|
|
<span id="L110"><span class="lineNum"> 110</span> <span class="tlaGNC"> 8 : }</span></span>
|
|
<span id="L111"><span class="lineNum"> 111</span> : }</span>
|
|
</pre>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
<br>
|
|
|
|
<table width="100%" border=0 cellspacing=0 cellpadding=0>
|
|
<tr><td class="ruler"><img src="../../glass.png" width=3 height=3 alt=""></td></tr>
|
|
<tr><td class="versionInfo">Generated by: <a href="https://github.com//linux-test-project/lcov" target="_parent">LCOV version 2.0-1</a></td></tr>
|
|
</table>
|
|
<br>
|
|
|
|
</body>
|
|
</html>
|