Skip to content

Commit

Permalink
deploy: 32f3a62
Browse files Browse the repository at this point in the history
  • Loading branch information
Az-r-ow committed May 18, 2024
1 parent 6a6e01e commit 63af064
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
49 changes: 31 additions & 18 deletions cpp-docs/BCE_8hpp_source.html
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,39 @@
<div class="fragment"><div class="line"><a id="l00001" name="l00001"></a><span class="lineno"> 1</span><span class="preprocessor">#pragma once</span></div>
<div class="line"><a id="l00002" name="l00002"></a><span class="lineno"> 2</span> </div>
<div class="line"><a id="l00003" name="l00003"></a><span class="lineno"> 3</span><span class="preprocessor">#include &quot;Loss.hpp&quot;</span></div>
<div class="line"><a id="l00004" name="l00004"></a><span class="lineno"> 4</span> </div>
<div class="line"><a id="l00005" name="l00005"></a><span class="lineno"> 5</span><span class="keyword">namespace </span>NeuralNet {</div>
<div class="foldopen" id="foldopen00009" data-start="{" data-end="};">
<div class="line"><a id="l00009" name="l00009"></a><span class="lineno"><a class="line" href="classNeuralNet_1_1BCE.html"> 9</a></span><span class="keyword">class </span><a class="code hl_class" href="classNeuralNet_1_1BCE.html">BCE</a> : <span class="keyword">public</span> <a class="code hl_class" href="classNeuralNet_1_1Loss.html">Loss</a> {</div>
<div class="line"><a id="l00010" name="l00010"></a><span class="lineno"> 10</span> <span class="keyword">public</span>:</div>
<div class="line"><a id="l00011" name="l00011"></a><span class="lineno"> 11</span> <span class="keyword">static</span> <span class="keywordtype">double</span> cmpLoss(<span class="keyword">const</span> Eigen::MatrixXd &amp;o, <span class="keyword">const</span> Eigen::MatrixXd &amp;y) {</div>
<div class="line"><a id="l00012" name="l00012"></a><span class="lineno"> 12</span> Eigen::MatrixXd loss = -(y.array() * o.array().log() +</div>
<div class="line"><a id="l00013" name="l00013"></a><span class="lineno"> 13</span> (1.0 - y.array()) * (1.0 - o.array()).log());</div>
<div class="line"><a id="l00014" name="l00014"></a><span class="lineno"> 14</span> <span class="keywordflow">return</span> loss.sum();</div>
<div class="line"><a id="l00015" name="l00015"></a><span class="lineno"> 15</span> }</div>
<div class="line"><a id="l00004" name="l00004"></a><span class="lineno"> 4</span><span class="preprocessor">#include &quot;utils/Functions.hpp&quot;</span></div>
<div class="line"><a id="l00005" name="l00005"></a><span class="lineno"> 5</span> </div>
<div class="line"><a id="l00006" name="l00006"></a><span class="lineno"> 6</span><span class="keyword">namespace </span>NeuralNet {</div>
<div class="foldopen" id="foldopen00010" data-start="{" data-end="};">
<div class="line"><a id="l00010" name="l00010"></a><span class="lineno"><a class="line" href="classNeuralNet_1_1BCE.html"> 10</a></span><span class="keyword">class </span><a class="code hl_class" href="classNeuralNet_1_1BCE.html">BCE</a> : <span class="keyword">public</span> <a class="code hl_class" href="classNeuralNet_1_1Loss.html">Loss</a> {</div>
<div class="line"><a id="l00011" name="l00011"></a><span class="lineno"> 11</span> <span class="keyword">public</span>:</div>
<div class="line"><a id="l00012" name="l00012"></a><span class="lineno"> 12</span> <span class="keyword">static</span> <span class="keywordtype">double</span> cmpLoss(<span class="keyword">const</span> Eigen::MatrixXd &amp;o, <span class="keyword">const</span> Eigen::MatrixXd &amp;y) {</div>
<div class="line"><a id="l00013" name="l00013"></a><span class="lineno"> 13</span> <span class="keyword">constexpr</span> <span class="keywordtype">double</span> threshold = 1.0e-5;</div>
<div class="line"><a id="l00014" name="l00014"></a><span class="lineno"> 14</span> Eigen::MatrixXd oTrim = trim(o, threshold);</div>
<div class="line"><a id="l00015" name="l00015"></a><span class="lineno"> 15</span> Eigen::MatrixXd yTrim = trim(y, threshold);</div>
<div class="line"><a id="l00016" name="l00016"></a><span class="lineno"> 16</span> </div>
<div class="line"><a id="l00017" name="l00017"></a><span class="lineno"> 17</span> <span class="keyword">static</span> Eigen::MatrixXd cmpLossGrad(<span class="keyword">const</span> Eigen::MatrixXd &amp;yHat,</div>
<div class="line"><a id="l00018" name="l00018"></a><span class="lineno"> 18</span> <span class="keyword">const</span> Eigen::MatrixXd &amp;y) {</div>
<div class="line"><a id="l00019" name="l00019"></a><span class="lineno"> 19</span> <span class="keywordflow">return</span> (yHat.array() - y.array()) / (yHat.array() * (1.0 - y.array()));</div>
<div class="line"><a id="l00020" name="l00020"></a><span class="lineno"> 20</span> }</div>
<div class="line"><a id="l00021" name="l00021"></a><span class="lineno"> 21</span>};</div>
<div class="line"><a id="l00017" name="l00017"></a><span class="lineno"> 17</span> Eigen::MatrixXd loss =</div>
<div class="line"><a id="l00018" name="l00018"></a><span class="lineno"> 18</span> -(yTrim.array() * oTrim.array().log() +</div>
<div class="line"><a id="l00019" name="l00019"></a><span class="lineno"> 19</span> (1.0 - yTrim.array()) * (1.0 - oTrim.array()).log());</div>
<div class="line"><a id="l00020" name="l00020"></a><span class="lineno"> 20</span> </div>
<div class="line"><a id="l00021" name="l00021"></a><span class="lineno"> 21</span> <span class="keywordflow">if</span> (loss.array().isNaN().any())</div>
<div class="line"><a id="l00022" name="l00022"></a><span class="lineno"> 22</span> <span class="keywordflow">throw</span> std::runtime_error(</div>
<div class="line"><a id="l00023" name="l00023"></a><span class="lineno"> 23</span> <span class="stringliteral">&quot;NaN value encountered. Inputs might be too big&quot;</span>);</div>
<div class="line"><a id="l00024" name="l00024"></a><span class="lineno"> 24</span> </div>
<div class="line"><a id="l00025" name="l00025"></a><span class="lineno"> 25</span> <span class="keywordflow">return</span> loss.sum();</div>
<div class="line"><a id="l00026" name="l00026"></a><span class="lineno"> 26</span> }</div>
<div class="line"><a id="l00027" name="l00027"></a><span class="lineno"> 27</span> </div>
<div class="line"><a id="l00028" name="l00028"></a><span class="lineno"> 28</span> <span class="keyword">static</span> Eigen::MatrixXd cmpLossGrad(<span class="keyword">const</span> Eigen::MatrixXd &amp;yHat,</div>
<div class="line"><a id="l00029" name="l00029"></a><span class="lineno"> 29</span> <span class="keyword">const</span> Eigen::MatrixXd &amp;y) {</div>
<div class="line"><a id="l00030" name="l00030"></a><span class="lineno"> 30</span> <span class="keyword">constexpr</span> <span class="keywordtype">double</span> epsilon = 1.0e-9;</div>
<div class="line"><a id="l00031" name="l00031"></a><span class="lineno"> 31</span> <span class="keywordflow">return</span> (yHat.array() - y.array()) /</div>
<div class="line"><a id="l00032" name="l00032"></a><span class="lineno"> 32</span> ((yHat.array() * (1.0 - yHat.array())) + epsilon);</div>
<div class="line"><a id="l00033" name="l00033"></a><span class="lineno"> 33</span> }</div>
<div class="line"><a id="l00034" name="l00034"></a><span class="lineno"> 34</span>};</div>
</div>
<div class="line"><a id="l00022" name="l00022"></a><span class="lineno"> 22</span> </div>
<div class="line"><a id="l00023" name="l00023"></a><span class="lineno"> 23</span>} <span class="comment">// namespace NeuralNet</span></div>
<div class="ttc" id="aclassNeuralNet_1_1BCE_html"><div class="ttname"><a href="classNeuralNet_1_1BCE.html">NeuralNet::BCE</a></div><div class="ttdef"><b>Definition</b> BCE.hpp:9</div></div>
<div class="line"><a id="l00035" name="l00035"></a><span class="lineno"> 35</span> </div>
<div class="line"><a id="l00036" name="l00036"></a><span class="lineno"> 36</span>} <span class="comment">// namespace NeuralNet</span></div>
<div class="ttc" id="aclassNeuralNet_1_1BCE_html"><div class="ttname"><a href="classNeuralNet_1_1BCE.html">NeuralNet::BCE</a></div><div class="ttdef"><b>Definition</b> BCE.hpp:10</div></div>
<div class="ttc" id="aclassNeuralNet_1_1Loss_html"><div class="ttname"><a href="classNeuralNet_1_1Loss.html">NeuralNet::Loss</a></div><div class="ttdef"><b>Definition</b> Loss.hpp:6</div></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
Expand Down
27 changes: 16 additions & 11 deletions cpp-docs/Functions_8hpp_source.html
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
<div class="line"><a id="l00128" name="l00128"></a><span class="lineno"> 128</span> </div>
<div class="line"><a id="l00129" name="l00129"></a><span class="lineno"> 129</span><span class="comment">/* MATHEMATICAL FUNCTIONS */</span></div>
<div class="line"><a id="l00130" name="l00130"></a><span class="lineno"> 130</span> </div>
<div class="line"><a id="l00138" name="l00138"></a><span class="lineno"> 138</span><span class="keyword">inline</span> <span class="keywordtype">double</span> sqr(<span class="keyword">const</span> <span class="keywordtype">double</span> x) { <span class="keywordflow">return</span> x * x; };</div>
<div class="line"><a id="l00138" name="l00138"></a><span class="lineno"> 138</span><span class="keyword">inline</span> <span class="keyword">constexpr</span> <span class="keywordtype">double</span> sqr(<span class="keyword">const</span> <span class="keywordtype">double</span> x) { <span class="keywordflow">return</span> x * x; };</div>
<div class="line"><a id="l00139" name="l00139"></a><span class="lineno"> 139</span> </div>
<div class="line"><a id="l00140" name="l00140"></a><span class="lineno"> 140</span><span class="comment">/* VECTOR OPERATIONS */</span></div>
<div class="line"><a id="l00141" name="l00141"></a><span class="lineno"> 141</span> </div>
Expand Down Expand Up @@ -284,16 +284,21 @@
<div class="line"><a id="l00323" name="l00323"></a><span class="lineno"> 323</span> <span class="keywordflow">return</span> hardmaxMatrix;</div>
<div class="line"><a id="l00324" name="l00324"></a><span class="lineno"> 324</span>}</div>
<div class="line"><a id="l00325" name="l00325"></a><span class="lineno"> 325</span> </div>
<div class="line"><a id="l00326" name="l00326"></a><span class="lineno"> 326</span><span class="comment">/* SIGNAL HANDLING */</span></div>
<div class="line"><a id="l00327" name="l00327"></a><span class="lineno"> 327</span><span class="keyword">static</span> <span class="keywordtype">void</span> signalHandler(<span class="keywordtype">int</span> signum) {</div>
<div class="line"><a id="l00328" name="l00328"></a><span class="lineno"> 328</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Interrupt signal (&quot;</span> &lt;&lt; signum &lt;&lt; <span class="stringliteral">&quot;) received.\n&quot;</span>;</div>
<div class="line"><a id="l00329" name="l00329"></a><span class="lineno"> 329</span> </div>
<div class="line"><a id="l00330" name="l00330"></a><span class="lineno"> 330</span> <span class="comment">// cleanup and close up stuff here</span></div>
<div class="line"><a id="l00331" name="l00331"></a><span class="lineno"> 331</span> <span class="comment">// terminate program</span></div>
<div class="line"><a id="l00332" name="l00332"></a><span class="lineno"> 332</span> exit(signum);</div>
<div class="line"><a id="l00333" name="l00333"></a><span class="lineno"> 333</span>};</div>
<div class="line"><a id="l00334" name="l00334"></a><span class="lineno"> 334</span> </div>
<div class="line"><a id="l00335" name="l00335"></a><span class="lineno"> 335</span>} <span class="comment">// namespace NeuralNet</span></div>
<div class="line"><a id="l00334" name="l00334"></a><span class="lineno"> 334</span><span class="keyword">static</span> Eigen::MatrixXd trim(<span class="keyword">const</span> Eigen::MatrixXd &amp;logits,</div>
<div class="line"><a id="l00335" name="l00335"></a><span class="lineno"> 335</span> <span class="keywordtype">double</span> threshold = 0.01) {</div>
<div class="line"><a id="l00336" name="l00336"></a><span class="lineno"> 336</span> <span class="keywordflow">return</span> (logits.array() &lt; threshold).select(0, logits);</div>
<div class="line"><a id="l00337" name="l00337"></a><span class="lineno"> 337</span>}</div>
<div class="line"><a id="l00338" name="l00338"></a><span class="lineno"> 338</span> </div>
<div class="line"><a id="l00339" name="l00339"></a><span class="lineno"> 339</span><span class="comment">/* SIGNAL HANDLING */</span></div>
<div class="line"><a id="l00340" name="l00340"></a><span class="lineno"> 340</span><span class="keyword">static</span> <span class="keywordtype">void</span> signalHandler(<span class="keywordtype">int</span> signum) {</div>
<div class="line"><a id="l00341" name="l00341"></a><span class="lineno"> 341</span> std::cout &lt;&lt; <span class="stringliteral">&quot;Interrupt signal (&quot;</span> &lt;&lt; signum &lt;&lt; <span class="stringliteral">&quot;) received.\n&quot;</span>;</div>
<div class="line"><a id="l00342" name="l00342"></a><span class="lineno"> 342</span> </div>
<div class="line"><a id="l00343" name="l00343"></a><span class="lineno"> 343</span> <span class="comment">// cleanup and close up stuff here</span></div>
<div class="line"><a id="l00344" name="l00344"></a><span class="lineno"> 344</span> <span class="comment">// terminate program</span></div>
<div class="line"><a id="l00345" name="l00345"></a><span class="lineno"> 345</span> exit(signum);</div>
<div class="line"><a id="l00346" name="l00346"></a><span class="lineno"> 346</span>};</div>
<div class="line"><a id="l00347" name="l00347"></a><span class="lineno"> 347</span> </div>
<div class="line"><a id="l00348" name="l00348"></a><span class="lineno"> 348</span>} <span class="comment">// namespace NeuralNet</span></div>
</div><!-- fragment --></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Expand Down
2 changes: 1 addition & 1 deletion cpp-docs/ModelCheckpoint_8hpp_source.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
<div class="line"><a id="l00067" name="l00067"></a><span class="lineno"> 67</span> <span class="keyword">private</span>:</div>
<div class="line"><a id="l00068" name="l00068"></a><span class="lineno"> 68</span> std::string folderPath, filename;</div>
<div class="line"><a id="l00069" name="l00069"></a><span class="lineno"> 69</span> <span class="keywordtype">bool</span> saveBestOnly, verbose;</div>
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno"> 70</span> <span class="keywordtype">double</span> bestLoss = 10, bestAccuracy = 0;</div>
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno"> 70</span> <span class="keywordtype">double</span> bestLoss = std::numeric_limits&lt;double&gt;::max(), bestAccuracy = 0;</div>
<div class="line"><a id="l00071" name="l00071"></a><span class="lineno"> 71</span> <span class="keywordtype">int</span> numEpochs, bestEpoch;</div>
<div class="line"><a id="l00072" name="l00072"></a><span class="lineno"> 72</span> std::unordered_map&lt;std::string, Logs&gt; logs;</div>
<div class="line"><a id="l00073" name="l00073"></a><span class="lineno"> 73</span> </div>
Expand Down

0 comments on commit 63af064

Please sign in to comment.