Skip to content

Commit

Permalink
deploy: df28830
Browse files Browse the repository at this point in the history
  • Loading branch information
mieskolainen committed Oct 20, 2024
1 parent a35e7c9 commit 77be4fb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 29 deletions.
60 changes: 34 additions & 26 deletions _modules/icenet/tools/aux.html
Original file line number Diff line number Diff line change
Expand Up @@ -1694,13 +1694,17 @@ <h1>Source code for icenet.tools.aux</h1><div class="highlight"><pre>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;</span>
<span class="sd"> Create model filename</span>
<span class="sd"> </span>
<span class="sd"> This function automatically takes the minimum validation loss epoch / iteration,</span>
<span class="sd"> if epoch == - 1, by first reading the last epoch / iteration file.</span>
<span class="sd"> This function automatically takes the minimum validation loss epoch / iteration</span>
<span class="sd"> </span>
<span class="sd"> if epoch == - 1, we try to find the best loss model</span>
<span class="sd"> epoch == - 2, we take the latest epoch</span>
<span class="sd"> </span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="k">def</span> <span class="nf">createfilename</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
<span class="k">return</span> <span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="n">path</span><span class="si">}</span><span class="s1">/</span><span class="si">{</span><span class="n">label</span><span class="si">}</span><span class="s1">_</span><span class="si">{</span><span class="n">i</span><span class="si">}{</span><span class="n">filetype</span><span class="si">}</span><span class="s1">&#39;</span>

<span class="k">if</span> <span class="n">epoch</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">epoch</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>
<span class="k">if</span> <span class="n">epoch</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">epoch</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">:</span>

<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;Loading the latest model by timestamp&#39;</span><span class="p">,</span> <span class="s1">&#39;yellow&#39;</span><span class="p">)</span>

<span class="n">list_of_files</span> <span class="o">=</span> <span class="n">glob</span><span class="o">.</span><span class="n">glob</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="n">path</span><span class="si">}</span><span class="s1">/</span><span class="si">{</span><span class="n">label</span><span class="si">}</span><span class="s1">_*</span><span class="si">{</span><span class="n">filetype</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
Expand All @@ -1710,37 +1714,41 @@ <h1>Source code for icenet.tools.aux</h1><div class="highlight"><pre>
<span class="n">txt</span> <span class="o">+=</span> <span class="sa">f</span><span class="s2">&quot; under path </span><span class="si">{</span><span class="n">path</span><span class="si">}</span><span class="s2">&quot;</span>
<span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="n">txt</span><span class="p">)</span>

<span class="c1"># Latest model</span>
<span class="c1"># The latest model</span>
<span class="n">filename</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">list_of_files</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">getctime</span><span class="p">)</span>

<span class="c1"># ----------------------------------------------------</span>
<span class="c1"># Try to find the best model</span>
<span class="n">succeeded</span> <span class="o">=</span> <span class="kc">False</span>
<span class="k">try</span><span class="p">:</span>
<span class="c1"># Try with pickle load</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">file</span><span class="p">:</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
<span class="n">succeeded</span> <span class="o">=</span> <span class="kc">True</span>

<span class="k">if</span> <span class="n">epoch</span> <span class="o">==</span> <span class="o">-</span><span class="mi">1</span><span class="p">:</span>

<span class="k">except</span><span class="p">:</span>
<span class="c1"># Try with torch load</span>
<span class="n">succeeded</span> <span class="o">=</span> <span class="kc">False</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">map_location</span> <span class="o">=</span> <span class="s1">&#39;cpu&#39;</span><span class="p">)</span>
<span class="c1"># Try with pickle load</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="s1">&#39;rb&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">file</span><span class="p">:</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">pickle</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
<span class="n">succeeded</span> <span class="o">=</span> <span class="kc">True</span>

<span class="k">except</span><span class="p">:</span>
<span class="c1"># Try with torch load</span>
<span class="k">try</span><span class="p">:</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">torch</span><span class="o">.</span><span class="n">load</span><span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">map_location</span> <span class="o">=</span> <span class="s1">&#39;cpu&#39;</span><span class="p">)</span>
<span class="n">succeeded</span> <span class="o">=</span> <span class="kc">True</span>

<span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;Problem in finding the model [</span><span class="si">{</span><span class="n">label</span><span class="si">}</span><span class="s1">] with the minimum validation loss&#39;</span><span class="p">,</span> <span class="s1">&#39;red&#39;</span><span class="p">)</span>

<span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="nb">print</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;Problem in finding the model [</span><span class="si">{</span><span class="n">label</span><span class="si">}</span><span class="s1">] with the minimum validation loss&#39;</span><span class="p">,</span> <span class="s1">&#39;red&#39;</span><span class="p">)</span>

<span class="k">if</span> <span class="n">succeeded</span><span class="p">:</span>
<span class="c1"># Take the minimum validation loss epoch index</span>
<span class="n">losses</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;losses&#39;</span><span class="p">][</span><span class="s1">&#39;val_losses&#39;</span><span class="p">])</span>
<span class="n">idx</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmin</span><span class="p">(</span><span class="n">losses</span><span class="p">)</span>

<span class="nb">str</span> <span class="o">=</span> <span class="sa">f</span><span class="s1">&#39;Found the best model at epoch [</span><span class="si">{</span><span class="n">idx</span><span class="si">}</span><span class="s1">] with validation loss = </span><span class="si">{</span><span class="n">losses</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span><span class="si">:</span><span class="s1">0.4f</span><span class="si">}</span><span class="s1">&#39;</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="nb">str</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;magenta&#39;</span><span class="p">)</span>

<span class="n">filename</span> <span class="o">=</span> <span class="n">createfilename</span><span class="p">(</span><span class="n">idx</span><span class="p">)</span>
<span class="k">if</span> <span class="n">succeeded</span><span class="p">:</span>
<span class="c1"># Take the minimum validation loss epoch index</span>
<span class="n">losses</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">data</span><span class="p">[</span><span class="s1">&#39;losses&#39;</span><span class="p">][</span><span class="s1">&#39;val_losses&#39;</span><span class="p">])</span>
<span class="n">idx</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">argmin</span><span class="p">(</span><span class="n">losses</span><span class="p">)</span>

<span class="nb">str</span> <span class="o">=</span> <span class="sa">f</span><span class="s1">&#39;Found the best model at epoch [</span><span class="si">{</span><span class="n">idx</span><span class="si">}</span><span class="s1">] with validation loss = </span><span class="si">{</span><span class="n">losses</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span><span class="si">:</span><span class="s1">0.4f</span><span class="si">}</span><span class="s1">&#39;</span>
<span class="nb">print</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="nb">str</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;magenta&#39;</span><span class="p">)</span>

<span class="n">filename</span> <span class="o">=</span> <span class="n">createfilename</span><span class="p">(</span><span class="n">idx</span><span class="p">)</span>
<span class="c1"># ----------------------------------------------------</span>

<span class="k">else</span><span class="p">:</span>
Expand Down
7 changes: 5 additions & 2 deletions modules/icenet.html
Original file line number Diff line number Diff line change
Expand Up @@ -3070,8 +3070,11 @@ <h2>icenet.tools<a class="headerlink" href="#icenet-tools" title="Link to this h
<dt class="sig sig-object py" id="icenet.tools.aux.create_model_filename">
<span class="sig-name descname"><span class="pre">create_model_filename</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.13)"><span class="pre">str</span></a></span></em>, <em class="sig-param"><span class="n"><span class="pre">label</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(in Python v3.13)"><span class="pre">str</span></a></span></em>, <em class="sig-param"><span class="n"><span class="pre">filetype</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'.dat'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">epoch</span></span><span class="p"><span class="pre">:</span></span><span class="w"> </span><span class="n"><a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(in Python v3.13)"><span class="pre">int</span></a><span class="w"> </span><span class="p"><span class="pre">|</span></span><span class="w"> </span><a class="reference external" href="https://docs.python.org/3/library/constants.html#None" title="(in Python v3.13)"><span class="pre">None</span></a></span><span class="w"> </span><span class="o"><span class="pre">=</span></span><span class="w"> </span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference internal" href="../_modules/icenet/tools/aux.html#create_model_filename"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#icenet.tools.aux.create_model_filename" title="Link to this definition">¶</a></dt>
<dd><p>Create model filename</p>
<p>This function automatically takes the minimum validation loss epoch / iteration,
if epoch == - 1, by first reading the last epoch / iteration file.</p>
<p>This function automatically takes the minimum validation loss epoch / iteration</p>
<dl class="simple">
<dt>if epoch == - 1, we try to find the best loss model</dt><dd><p>epoch == - 2, we take the latest epoch</p>
</dd>
</dl>
</dd></dl>

<dl class="py function">
Expand Down
2 changes: 1 addition & 1 deletion searchindex.js

Large diffs are not rendered by default.

0 comments on commit 77be4fb

Please sign in to comment.