307 lines
33 KiB
HTML
307 lines
33 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/ensembles/Ensemble.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/ensembles</a> - Ensemble.cc<span style="font-size: 80%;"> (source / <a href="Ensemble.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">100.0 %</td>
|
|
<td class="headerCovTableEntry">155</td>
|
|
<td class="headerCovTableEntry">155</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">25</td>
|
|
<td class="headerCovTableEntry">25</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 "Ensemble.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>
|
|
<span id="L11"><span class="lineNum"> 11</span> <span class="tlaGNC tlaBgGNC"> 324 : Ensemble::Ensemble(bool predict_voting) : Classifier(Network()), n_models(0), predict_voting(predict_voting)</span></span>
|
|
<span id="L12"><span class="lineNum"> 12</span> : {</span>
|
|
<span id="L13"><span class="lineNum"> 13</span> : </span>
|
|
<span id="L14"><span class="lineNum"> 14</span> <span class="tlaGNC"> 324 : };</span></span>
|
|
<span id="L15"><span class="lineNum"> 15</span> : const std::string ENSEMBLE_NOT_FITTED = "Ensemble has not been fitted";</span>
|
|
<span id="L16"><span class="lineNum"> 16</span> <span class="tlaGNC"> 40 : void Ensemble::trainModel(const torch::Tensor& weights)</span></span>
|
|
<span id="L17"><span class="lineNum"> 17</span> : {</span>
|
|
<span id="L18"><span class="lineNum"> 18</span> <span class="tlaGNC"> 40 : n_models = models.size();</span></span>
|
|
<span id="L19"><span class="lineNum"> 19</span> <span class="tlaGNC"> 660 : for (auto i = 0; i < n_models; ++i) {</span></span>
|
|
<span id="L20"><span class="lineNum"> 20</span> : // fit with std::vectors</span>
|
|
<span id="L21"><span class="lineNum"> 21</span> <span class="tlaGNC"> 620 : models[i]->fit(dataset, features, className, states);</span></span>
|
|
<span id="L22"><span class="lineNum"> 22</span> : }</span>
|
|
<span id="L23"><span class="lineNum"> 23</span> <span class="tlaGNC"> 40 : }</span></span>
|
|
<span id="L24"><span class="lineNum"> 24</span> <span class="tlaGNC"> 56 : std::vector<int> Ensemble::compute_arg_max(std::vector<std::vector<double>>& X)</span></span>
|
|
<span id="L25"><span class="lineNum"> 25</span> : {</span>
|
|
<span id="L26"><span class="lineNum"> 26</span> <span class="tlaGNC"> 56 : std::vector<int> y_pred;</span></span>
|
|
<span id="L27"><span class="lineNum"> 27</span> <span class="tlaGNC"> 12400 : for (auto i = 0; i < X.size(); ++i) {</span></span>
|
|
<span id="L28"><span class="lineNum"> 28</span> <span class="tlaGNC"> 12344 : auto max = std::max_element(X[i].begin(), X[i].end());</span></span>
|
|
<span id="L29"><span class="lineNum"> 29</span> <span class="tlaGNC"> 24688 : y_pred.push_back(std::distance(X[i].begin(), max));</span></span>
|
|
<span id="L30"><span class="lineNum"> 30</span> : }</span>
|
|
<span id="L31"><span class="lineNum"> 31</span> <span class="tlaGNC"> 112 : return y_pred;</span></span>
|
|
<span id="L32"><span class="lineNum"> 32</span> <span class="tlaGNC"> 56 : }</span></span>
|
|
<span id="L33"><span class="lineNum"> 33</span> <span class="tlaGNC"> 424 : torch::Tensor Ensemble::compute_arg_max(torch::Tensor& X)</span></span>
|
|
<span id="L34"><span class="lineNum"> 34</span> : {</span>
|
|
<span id="L35"><span class="lineNum"> 35</span> <span class="tlaGNC"> 424 : auto y_pred = torch::argmax(X, 1);</span></span>
|
|
<span id="L36"><span class="lineNum"> 36</span> <span class="tlaGNC"> 848 : return y_pred;</span></span>
|
|
<span id="L37"><span class="lineNum"> 37</span> <span class="tlaGNC"> 424 : }</span></span>
|
|
<span id="L38"><span class="lineNum"> 38</span> <span class="tlaGNC"> 164 : torch::Tensor Ensemble::voting(torch::Tensor& votes)</span></span>
|
|
<span id="L39"><span class="lineNum"> 39</span> : {</span>
|
|
<span id="L40"><span class="lineNum"> 40</span> : // Convert m x n_models tensor to a m x n_class_states with voting probabilities</span>
|
|
<span id="L41"><span class="lineNum"> 41</span> <span class="tlaGNC"> 164 : auto y_pred_ = votes.accessor<int, 2>();</span></span>
|
|
<span id="L42"><span class="lineNum"> 42</span> <span class="tlaGNC"> 164 : std::vector<int> y_pred_final;</span></span>
|
|
<span id="L43"><span class="lineNum"> 43</span> <span class="tlaGNC"> 164 : int numClasses = states.at(className).size();</span></span>
|
|
<span id="L44"><span class="lineNum"> 44</span> : // votes is m x n_models with the prediction of every model for each sample</span>
|
|
<span id="L45"><span class="lineNum"> 45</span> <span class="tlaGNC"> 164 : auto result = torch::zeros({ votes.size(0), numClasses }, torch::kFloat32);</span></span>
|
|
<span id="L46"><span class="lineNum"> 46</span> <span class="tlaGNC"> 164 : auto sum = std::reduce(significanceModels.begin(), significanceModels.end());</span></span>
|
|
<span id="L47"><span class="lineNum"> 47</span> <span class="tlaGNC"> 42084 : for (int i = 0; i < votes.size(0); ++i) {</span></span>
|
|
<span id="L48"><span class="lineNum"> 48</span> : // n_votes store in each index (value of class) the significance added by each model</span>
|
|
<span id="L49"><span class="lineNum"> 49</span> : // i.e. n_votes[0] contains how much value has the value 0 of class. That value is generated by the models predictions</span>
|
|
<span id="L50"><span class="lineNum"> 50</span> <span class="tlaGNC"> 41920 : std::vector<double> n_votes(numClasses, 0.0);</span></span>
|
|
<span id="L51"><span class="lineNum"> 51</span> <span class="tlaGNC"> 375272 : for (int j = 0; j < n_models; ++j) {</span></span>
|
|
<span id="L52"><span class="lineNum"> 52</span> <span class="tlaGNC"> 333352 : n_votes[y_pred_[i][j]] += significanceModels.at(j);</span></span>
|
|
<span id="L53"><span class="lineNum"> 53</span> : }</span>
|
|
<span id="L54"><span class="lineNum"> 54</span> <span class="tlaGNC"> 41920 : result[i] = torch::tensor(n_votes);</span></span>
|
|
<span id="L55"><span class="lineNum"> 55</span> <span class="tlaGNC"> 41920 : }</span></span>
|
|
<span id="L56"><span class="lineNum"> 56</span> : // To only do one division and gain precision</span>
|
|
<span id="L57"><span class="lineNum"> 57</span> <span class="tlaGNC"> 164 : result /= sum;</span></span>
|
|
<span id="L58"><span class="lineNum"> 58</span> <span class="tlaGNC"> 328 : return result;</span></span>
|
|
<span id="L59"><span class="lineNum"> 59</span> <span class="tlaGNC"> 164 : }</span></span>
|
|
<span id="L60"><span class="lineNum"> 60</span> <span class="tlaGNC"> 100 : std::vector<std::vector<double>> Ensemble::predict_proba(std::vector<std::vector<int>>& X)</span></span>
|
|
<span id="L61"><span class="lineNum"> 61</span> : {</span>
|
|
<span id="L62"><span class="lineNum"> 62</span> <span class="tlaGNC"> 100 : if (!fitted) {</span></span>
|
|
<span id="L63"><span class="lineNum"> 63</span> <span class="tlaGNC"> 24 : throw std::logic_error(ENSEMBLE_NOT_FITTED);</span></span>
|
|
<span id="L64"><span class="lineNum"> 64</span> : }</span>
|
|
<span id="L65"><span class="lineNum"> 65</span> <span class="tlaGNC"> 76 : return predict_voting ? predict_average_voting(X) : predict_average_proba(X);</span></span>
|
|
<span id="L66"><span class="lineNum"> 66</span> : }</span>
|
|
<span id="L67"><span class="lineNum"> 67</span> <span class="tlaGNC"> 452 : torch::Tensor Ensemble::predict_proba(torch::Tensor& X)</span></span>
|
|
<span id="L68"><span class="lineNum"> 68</span> : {</span>
|
|
<span id="L69"><span class="lineNum"> 69</span> <span class="tlaGNC"> 452 : if (!fitted) {</span></span>
|
|
<span id="L70"><span class="lineNum"> 70</span> <span class="tlaGNC"> 24 : throw std::logic_error(ENSEMBLE_NOT_FITTED);</span></span>
|
|
<span id="L71"><span class="lineNum"> 71</span> : }</span>
|
|
<span id="L72"><span class="lineNum"> 72</span> <span class="tlaGNC"> 428 : return predict_voting ? predict_average_voting(X) : predict_average_proba(X);</span></span>
|
|
<span id="L73"><span class="lineNum"> 73</span> : }</span>
|
|
<span id="L74"><span class="lineNum"> 74</span> <span class="tlaGNC"> 68 : std::vector<int> Ensemble::predict(std::vector<std::vector<int>>& X)</span></span>
|
|
<span id="L75"><span class="lineNum"> 75</span> : {</span>
|
|
<span id="L76"><span class="lineNum"> 76</span> <span class="tlaGNC"> 68 : auto res = predict_proba(X);</span></span>
|
|
<span id="L77"><span class="lineNum"> 77</span> <span class="tlaGNC"> 104 : return compute_arg_max(res);</span></span>
|
|
<span id="L78"><span class="lineNum"> 78</span> <span class="tlaGNC"> 52 : }</span></span>
|
|
<span id="L79"><span class="lineNum"> 79</span> <span class="tlaGNC"> 436 : torch::Tensor Ensemble::predict(torch::Tensor& X)</span></span>
|
|
<span id="L80"><span class="lineNum"> 80</span> : {</span>
|
|
<span id="L81"><span class="lineNum"> 81</span> <span class="tlaGNC"> 436 : auto res = predict_proba(X);</span></span>
|
|
<span id="L82"><span class="lineNum"> 82</span> <span class="tlaGNC"> 840 : return compute_arg_max(res);</span></span>
|
|
<span id="L83"><span class="lineNum"> 83</span> <span class="tlaGNC"> 420 : }</span></span>
|
|
<span id="L84"><span class="lineNum"> 84</span> <span class="tlaGNC"> 296 : torch::Tensor Ensemble::predict_average_proba(torch::Tensor& X)</span></span>
|
|
<span id="L85"><span class="lineNum"> 85</span> : {</span>
|
|
<span id="L86"><span class="lineNum"> 86</span> <span class="tlaGNC"> 296 : auto n_states = models[0]->getClassNumStates();</span></span>
|
|
<span id="L87"><span class="lineNum"> 87</span> <span class="tlaGNC"> 296 : torch::Tensor y_pred = torch::zeros({ X.size(1), n_states }, torch::kFloat32);</span></span>
|
|
<span id="L88"><span class="lineNum"> 88</span> <span class="tlaGNC"> 296 : auto threads{ std::vector<std::thread>() };</span></span>
|
|
<span id="L89"><span class="lineNum"> 89</span> <span class="tlaGNC"> 296 : std::mutex mtx;</span></span>
|
|
<span id="L90"><span class="lineNum"> 90</span> <span class="tlaGNC"> 1764 : for (auto i = 0; i < n_models; ++i) {</span></span>
|
|
<span id="L91"><span class="lineNum"> 91</span> <span class="tlaGNC"> 1468 : threads.push_back(std::thread([&, i]() {</span></span>
|
|
<span id="L92"><span class="lineNum"> 92</span> <span class="tlaGNC"> 1468 : auto ypredict = models[i]->predict_proba(X);</span></span>
|
|
<span id="L93"><span class="lineNum"> 93</span> <span class="tlaGNC"> 1468 : std::lock_guard<std::mutex> lock(mtx);</span></span>
|
|
<span id="L94"><span class="lineNum"> 94</span> <span class="tlaGNC"> 1468 : y_pred += ypredict * significanceModels[i];</span></span>
|
|
<span id="L95"><span class="lineNum"> 95</span> <span class="tlaGNC"> 1468 : }));</span></span>
|
|
<span id="L96"><span class="lineNum"> 96</span> : }</span>
|
|
<span id="L97"><span class="lineNum"> 97</span> <span class="tlaGNC"> 1764 : for (auto& thread : threads) {</span></span>
|
|
<span id="L98"><span class="lineNum"> 98</span> <span class="tlaGNC"> 1468 : thread.join();</span></span>
|
|
<span id="L99"><span class="lineNum"> 99</span> : }</span>
|
|
<span id="L100"><span class="lineNum"> 100</span> <span class="tlaGNC"> 296 : auto sum = std::reduce(significanceModels.begin(), significanceModels.end());</span></span>
|
|
<span id="L101"><span class="lineNum"> 101</span> <span class="tlaGNC"> 296 : y_pred /= sum;</span></span>
|
|
<span id="L102"><span class="lineNum"> 102</span> <span class="tlaGNC"> 592 : return y_pred;</span></span>
|
|
<span id="L103"><span class="lineNum"> 103</span> <span class="tlaGNC"> 296 : }</span></span>
|
|
<span id="L104"><span class="lineNum"> 104</span> <span class="tlaGNC"> 44 : std::vector<std::vector<double>> Ensemble::predict_average_proba(std::vector<std::vector<int>>& X)</span></span>
|
|
<span id="L105"><span class="lineNum"> 105</span> : {</span>
|
|
<span id="L106"><span class="lineNum"> 106</span> <span class="tlaGNC"> 44 : auto n_states = models[0]->getClassNumStates();</span></span>
|
|
<span id="L107"><span class="lineNum"> 107</span> <span class="tlaGNC"> 44 : std::vector<std::vector<double>> y_pred(X[0].size(), std::vector<double>(n_states, 0.0));</span></span>
|
|
<span id="L108"><span class="lineNum"> 108</span> <span class="tlaGNC"> 44 : auto threads{ std::vector<std::thread>() };</span></span>
|
|
<span id="L109"><span class="lineNum"> 109</span> <span class="tlaGNC"> 44 : std::mutex mtx;</span></span>
|
|
<span id="L110"><span class="lineNum"> 110</span> <span class="tlaGNC"> 576 : for (auto i = 0; i < n_models; ++i) {</span></span>
|
|
<span id="L111"><span class="lineNum"> 111</span> <span class="tlaGNC"> 532 : threads.push_back(std::thread([&, i]() {</span></span>
|
|
<span id="L112"><span class="lineNum"> 112</span> <span class="tlaGNC"> 532 : auto ypredict = models[i]->predict_proba(X);</span></span>
|
|
<span id="L113"><span class="lineNum"> 113</span> <span class="tlaGNC"> 532 : assert(ypredict.size() == y_pred.size());</span></span>
|
|
<span id="L114"><span class="lineNum"> 114</span> <span class="tlaGNC"> 532 : assert(ypredict[0].size() == y_pred[0].size());</span></span>
|
|
<span id="L115"><span class="lineNum"> 115</span> <span class="tlaGNC"> 532 : std::lock_guard<std::mutex> lock(mtx);</span></span>
|
|
<span id="L116"><span class="lineNum"> 116</span> : // Multiply each prediction by the significance of the model and then add it to the final prediction</span>
|
|
<span id="L117"><span class="lineNum"> 117</span> <span class="tlaGNC"> 110284 : for (auto j = 0; j < ypredict.size(); ++j) {</span></span>
|
|
<span id="L118"><span class="lineNum"> 118</span> <span class="tlaGNC"> 109752 : std::transform(y_pred[j].begin(), y_pred[j].end(), ypredict[j].begin(), y_pred[j].begin(),</span></span>
|
|
<span id="L119"><span class="lineNum"> 119</span> <span class="tlaGNC"> 739464 : [significanceModels = significanceModels[i]](double x, double y) { return x + y * significanceModels; });</span></span>
|
|
<span id="L120"><span class="lineNum"> 120</span> : }</span>
|
|
<span id="L121"><span class="lineNum"> 121</span> <span class="tlaGNC"> 532 : }));</span></span>
|
|
<span id="L122"><span class="lineNum"> 122</span> : }</span>
|
|
<span id="L123"><span class="lineNum"> 123</span> <span class="tlaGNC"> 576 : for (auto& thread : threads) {</span></span>
|
|
<span id="L124"><span class="lineNum"> 124</span> <span class="tlaGNC"> 532 : thread.join();</span></span>
|
|
<span id="L125"><span class="lineNum"> 125</span> : }</span>
|
|
<span id="L126"><span class="lineNum"> 126</span> <span class="tlaGNC"> 44 : auto sum = std::reduce(significanceModels.begin(), significanceModels.end());</span></span>
|
|
<span id="L127"><span class="lineNum"> 127</span> : //Divide each element of the prediction by the sum of the significances</span>
|
|
<span id="L128"><span class="lineNum"> 128</span> <span class="tlaGNC"> 8436 : for (auto j = 0; j < y_pred.size(); ++j) {</span></span>
|
|
<span id="L129"><span class="lineNum"> 129</span> <span class="tlaGNC"> 51544 : std::transform(y_pred[j].begin(), y_pred[j].end(), y_pred[j].begin(), [sum](double x) { return x / sum; });</span></span>
|
|
<span id="L130"><span class="lineNum"> 130</span> : }</span>
|
|
<span id="L131"><span class="lineNum"> 131</span> <span class="tlaGNC"> 88 : return y_pred;</span></span>
|
|
<span id="L132"><span class="lineNum"> 132</span> <span class="tlaGNC"> 44 : }</span></span>
|
|
<span id="L133"><span class="lineNum"> 133</span> <span class="tlaGNC"> 32 : std::vector<std::vector<double>> Ensemble::predict_average_voting(std::vector<std::vector<int>>& X)</span></span>
|
|
<span id="L134"><span class="lineNum"> 134</span> : {</span>
|
|
<span id="L135"><span class="lineNum"> 135</span> <span class="tlaGNC"> 32 : torch::Tensor Xt = bayesnet::vectorToTensor(X, false);</span></span>
|
|
<span id="L136"><span class="lineNum"> 136</span> <span class="tlaGNC"> 32 : auto y_pred = predict_average_voting(Xt);</span></span>
|
|
<span id="L137"><span class="lineNum"> 137</span> <span class="tlaGNC"> 32 : std::vector<std::vector<double>> result = tensorToVectorDouble(y_pred);</span></span>
|
|
<span id="L138"><span class="lineNum"> 138</span> <span class="tlaGNC"> 64 : return result;</span></span>
|
|
<span id="L139"><span class="lineNum"> 139</span> <span class="tlaGNC"> 32 : }</span></span>
|
|
<span id="L140"><span class="lineNum"> 140</span> <span class="tlaGNC"> 164 : torch::Tensor Ensemble::predict_average_voting(torch::Tensor& X)</span></span>
|
|
<span id="L141"><span class="lineNum"> 141</span> : {</span>
|
|
<span id="L142"><span class="lineNum"> 142</span> : // Build a m x n_models tensor with the predictions of each model</span>
|
|
<span id="L143"><span class="lineNum"> 143</span> <span class="tlaGNC"> 164 : torch::Tensor y_pred = torch::zeros({ X.size(1), n_models }, torch::kInt32);</span></span>
|
|
<span id="L144"><span class="lineNum"> 144</span> <span class="tlaGNC"> 164 : auto threads{ std::vector<std::thread>() };</span></span>
|
|
<span id="L145"><span class="lineNum"> 145</span> <span class="tlaGNC"> 164 : std::mutex mtx;</span></span>
|
|
<span id="L146"><span class="lineNum"> 146</span> <span class="tlaGNC"> 1380 : for (auto i = 0; i < n_models; ++i) {</span></span>
|
|
<span id="L147"><span class="lineNum"> 147</span> <span class="tlaGNC"> 1216 : threads.push_back(std::thread([&, i]() {</span></span>
|
|
<span id="L148"><span class="lineNum"> 148</span> <span class="tlaGNC"> 1216 : auto ypredict = models[i]->predict(X);</span></span>
|
|
<span id="L149"><span class="lineNum"> 149</span> <span class="tlaGNC"> 1216 : std::lock_guard<std::mutex> lock(mtx);</span></span>
|
|
<span id="L150"><span class="lineNum"> 150</span> <span class="tlaGNC"> 3648 : y_pred.index_put_({ "...", i }, ypredict);</span></span>
|
|
<span id="L151"><span class="lineNum"> 151</span> <span class="tlaGNC"> 2432 : }));</span></span>
|
|
<span id="L152"><span class="lineNum"> 152</span> : }</span>
|
|
<span id="L153"><span class="lineNum"> 153</span> <span class="tlaGNC"> 1380 : for (auto& thread : threads) {</span></span>
|
|
<span id="L154"><span class="lineNum"> 154</span> <span class="tlaGNC"> 1216 : thread.join();</span></span>
|
|
<span id="L155"><span class="lineNum"> 155</span> : }</span>
|
|
<span id="L156"><span class="lineNum"> 156</span> <span class="tlaGNC"> 328 : return voting(y_pred);</span></span>
|
|
<span id="L157"><span class="lineNum"> 157</span> <span class="tlaGNC"> 164 : }</span></span>
|
|
<span id="L158"><span class="lineNum"> 158</span> <span class="tlaGNC"> 80 : float Ensemble::score(torch::Tensor& X, torch::Tensor& y)</span></span>
|
|
<span id="L159"><span class="lineNum"> 159</span> : {</span>
|
|
<span id="L160"><span class="lineNum"> 160</span> <span class="tlaGNC"> 80 : auto y_pred = predict(X);</span></span>
|
|
<span id="L161"><span class="lineNum"> 161</span> <span class="tlaGNC"> 72 : int correct = 0;</span></span>
|
|
<span id="L162"><span class="lineNum"> 162</span> <span class="tlaGNC"> 22584 : for (int i = 0; i < y_pred.size(0); ++i) {</span></span>
|
|
<span id="L163"><span class="lineNum"> 163</span> <span class="tlaGNC"> 22512 : if (y_pred[i].item<int>() == y[i].item<int>()) {</span></span>
|
|
<span id="L164"><span class="lineNum"> 164</span> <span class="tlaGNC"> 19668 : correct++;</span></span>
|
|
<span id="L165"><span class="lineNum"> 165</span> : }</span>
|
|
<span id="L166"><span class="lineNum"> 166</span> : }</span>
|
|
<span id="L167"><span class="lineNum"> 167</span> <span class="tlaGNC"> 144 : return (double)correct / y_pred.size(0);</span></span>
|
|
<span id="L168"><span class="lineNum"> 168</span> <span class="tlaGNC"> 72 : }</span></span>
|
|
<span id="L169"><span class="lineNum"> 169</span> <span class="tlaGNC"> 52 : float Ensemble::score(std::vector<std::vector<int>>& X, std::vector<int>& y)</span></span>
|
|
<span id="L170"><span class="lineNum"> 170</span> : {</span>
|
|
<span id="L171"><span class="lineNum"> 171</span> <span class="tlaGNC"> 52 : auto y_pred = predict(X);</span></span>
|
|
<span id="L172"><span class="lineNum"> 172</span> <span class="tlaGNC"> 44 : int correct = 0;</span></span>
|
|
<span id="L173"><span class="lineNum"> 173</span> <span class="tlaGNC"> 11164 : for (int i = 0; i < y_pred.size(); ++i) {</span></span>
|
|
<span id="L174"><span class="lineNum"> 174</span> <span class="tlaGNC"> 11120 : if (y_pred[i] == y[i]) {</span></span>
|
|
<span id="L175"><span class="lineNum"> 175</span> <span class="tlaGNC"> 9276 : correct++;</span></span>
|
|
<span id="L176"><span class="lineNum"> 176</span> : }</span>
|
|
<span id="L177"><span class="lineNum"> 177</span> : }</span>
|
|
<span id="L178"><span class="lineNum"> 178</span> <span class="tlaGNC"> 88 : return (double)correct / y_pred.size();</span></span>
|
|
<span id="L179"><span class="lineNum"> 179</span> <span class="tlaGNC"> 44 : }</span></span>
|
|
<span id="L180"><span class="lineNum"> 180</span> <span class="tlaGNC"> 4 : std::vector<std::string> Ensemble::show() const</span></span>
|
|
<span id="L181"><span class="lineNum"> 181</span> : {</span>
|
|
<span id="L182"><span class="lineNum"> 182</span> <span class="tlaGNC"> 4 : auto result = std::vector<std::string>();</span></span>
|
|
<span id="L183"><span class="lineNum"> 183</span> <span class="tlaGNC"> 20 : for (auto i = 0; i < n_models; ++i) {</span></span>
|
|
<span id="L184"><span class="lineNum"> 184</span> <span class="tlaGNC"> 16 : auto res = models[i]->show();</span></span>
|
|
<span id="L185"><span class="lineNum"> 185</span> <span class="tlaGNC"> 16 : result.insert(result.end(), res.begin(), res.end());</span></span>
|
|
<span id="L186"><span class="lineNum"> 186</span> <span class="tlaGNC"> 16 : }</span></span>
|
|
<span id="L187"><span class="lineNum"> 187</span> <span class="tlaGNC"> 8 : return result;</span></span>
|
|
<span id="L188"><span class="lineNum"> 188</span> <span class="tlaGNC"> 4 : }</span></span>
|
|
<span id="L189"><span class="lineNum"> 189</span> <span class="tlaGNC"> 16 : std::vector<std::string> Ensemble::graph(const std::string& title) const</span></span>
|
|
<span id="L190"><span class="lineNum"> 190</span> : {</span>
|
|
<span id="L191"><span class="lineNum"> 191</span> <span class="tlaGNC"> 16 : auto result = std::vector<std::string>();</span></span>
|
|
<span id="L192"><span class="lineNum"> 192</span> <span class="tlaGNC"> 108 : for (auto i = 0; i < n_models; ++i) {</span></span>
|
|
<span id="L193"><span class="lineNum"> 193</span> <span class="tlaGNC"> 92 : auto res = models[i]->graph(title + "_" + std::to_string(i));</span></span>
|
|
<span id="L194"><span class="lineNum"> 194</span> <span class="tlaGNC"> 92 : result.insert(result.end(), res.begin(), res.end());</span></span>
|
|
<span id="L195"><span class="lineNum"> 195</span> <span class="tlaGNC"> 92 : }</span></span>
|
|
<span id="L196"><span class="lineNum"> 196</span> <span class="tlaGNC"> 32 : return result;</span></span>
|
|
<span id="L197"><span class="lineNum"> 197</span> <span class="tlaGNC"> 16 : }</span></span>
|
|
<span id="L198"><span class="lineNum"> 198</span> <span class="tlaGNC"> 28 : int Ensemble::getNumberOfNodes() const</span></span>
|
|
<span id="L199"><span class="lineNum"> 199</span> : {</span>
|
|
<span id="L200"><span class="lineNum"> 200</span> <span class="tlaGNC"> 28 : int nodes = 0;</span></span>
|
|
<span id="L201"><span class="lineNum"> 201</span> <span class="tlaGNC"> 348 : for (auto i = 0; i < n_models; ++i) {</span></span>
|
|
<span id="L202"><span class="lineNum"> 202</span> <span class="tlaGNC"> 320 : nodes += models[i]->getNumberOfNodes();</span></span>
|
|
<span id="L203"><span class="lineNum"> 203</span> : }</span>
|
|
<span id="L204"><span class="lineNum"> 204</span> <span class="tlaGNC"> 28 : return nodes;</span></span>
|
|
<span id="L205"><span class="lineNum"> 205</span> : }</span>
|
|
<span id="L206"><span class="lineNum"> 206</span> <span class="tlaGNC"> 28 : int Ensemble::getNumberOfEdges() const</span></span>
|
|
<span id="L207"><span class="lineNum"> 207</span> : {</span>
|
|
<span id="L208"><span class="lineNum"> 208</span> <span class="tlaGNC"> 28 : int edges = 0;</span></span>
|
|
<span id="L209"><span class="lineNum"> 209</span> <span class="tlaGNC"> 348 : for (auto i = 0; i < n_models; ++i) {</span></span>
|
|
<span id="L210"><span class="lineNum"> 210</span> <span class="tlaGNC"> 320 : edges += models[i]->getNumberOfEdges();</span></span>
|
|
<span id="L211"><span class="lineNum"> 211</span> : }</span>
|
|
<span id="L212"><span class="lineNum"> 212</span> <span class="tlaGNC"> 28 : return edges;</span></span>
|
|
<span id="L213"><span class="lineNum"> 213</span> : }</span>
|
|
<span id="L214"><span class="lineNum"> 214</span> <span class="tlaGNC"> 4 : int Ensemble::getNumberOfStates() const</span></span>
|
|
<span id="L215"><span class="lineNum"> 215</span> : {</span>
|
|
<span id="L216"><span class="lineNum"> 216</span> <span class="tlaGNC"> 4 : int nstates = 0;</span></span>
|
|
<span id="L217"><span class="lineNum"> 217</span> <span class="tlaGNC"> 20 : for (auto i = 0; i < n_models; ++i) {</span></span>
|
|
<span id="L218"><span class="lineNum"> 218</span> <span class="tlaGNC"> 16 : nstates += models[i]->getNumberOfStates();</span></span>
|
|
<span id="L219"><span class="lineNum"> 219</span> : }</span>
|
|
<span id="L220"><span class="lineNum"> 220</span> <span class="tlaGNC"> 4 : return nstates;</span></span>
|
|
<span id="L221"><span class="lineNum"> 221</span> : }</span>
|
|
<span id="L222"><span class="lineNum"> 222</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>
|