descent_minimizer.html 21.2 KB
Newer Older
Philipp Frank's avatar
Philipp Frank committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://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/html; charset=utf-8" />
    
    <title>nifty.minimization.descent_minimizer &#8212; NIFTY 3.0.x documentation</title>
    
    <link rel="stylesheet" href="../../../_static/alabaster.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '3.0.x',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
   
  <link rel="stylesheet" href="../../../_static/custom.css" type="text/css" />
  
  
  <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />

  </head>
  <body>
  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <h1>Source code for nifty.minimization.descent_minimizer</h1><div class="highlight"><pre>
<span></span><span class="c1"># NIFTy</span>
<span class="c1"># Copyright (C) 2017  Theo Steininger</span>
<span class="c1">#</span>
<span class="c1"># Author: Theo Steininger</span>
<span class="c1">#</span>
<span class="c1"># This program is free software: you can redistribute it and/or modify</span>
<span class="c1"># it under the terms of the GNU General Public License as published by</span>
<span class="c1"># the Free Software Foundation, either version 3 of the License, or</span>
<span class="c1"># (at your option) any later version.</span>
<span class="c1">#</span>
<span class="c1"># This program is distributed in the hope that it will be useful,</span>
<span class="c1"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c1"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c1"># GNU General Public License for more details.</span>
<span class="c1">#</span>
<span class="c1"># You should have received a copy of the GNU General Public License</span>
<span class="c1"># along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>

<span class="kn">import</span> <span class="nn">abc</span>
<span class="kn">from</span> <span class="nn">nifty.nifty_meta</span> <span class="k">import</span> <span class="n">NiftyMeta</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">keepers</span> <span class="k">import</span> <span class="n">Loggable</span>

<span class="kn">from</span> <span class="nn">.line_searching</span> <span class="k">import</span> <span class="n">LineSearchStrongWolfe</span>


<div class="viewcode-block" id="DescentMinimizer"><a class="viewcode-back" href="../../../descent_minimizer.html#nifty.DescentMinimizer">[docs]</a><span class="k">class</span> <span class="nc">DescentMinimizer</span><span class="p">(</span><span class="n">Loggable</span><span class="p">,</span> <span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot; A base class used by gradient methods to find a local minimum.</span>

<span class="sd">    Descent minimization methods are used to find a local minimum of a scalar</span>
<span class="sd">    function by following a descent direction. This class implements the</span>
<span class="sd">    minimization procedure once a descent direction is known. The descent</span>
<span class="sd">    direction has to be implemented separately.</span>

<span class="sd">    Parameters</span>
<span class="sd">    ----------</span>
<span class="sd">    line_searcher : callable *optional*</span>
<span class="sd">        Function which infers the step size in the descent direction</span>
<span class="sd">        (default : LineSearchStrongWolfe()).</span>
<span class="sd">    callback : callable *optional*</span>
<span class="sd">        Function f(energy, iteration_number) supplied by the user to perform</span>
<span class="sd">        in-situ analysis at every iteration step. When being called the</span>
<span class="sd">        current energy and iteration_number are passed. (default: None)</span>
<span class="sd">    convergence_tolerance : float *optional*</span>
<span class="sd">        Tolerance specifying the case of convergence. (default: 1E-4)</span>
<span class="sd">    convergence_level : integer *optional*</span>
<span class="sd">        Number of times the tolerance must be undershot before convergence</span>
<span class="sd">        is reached. (default: 3)</span>
<span class="sd">    iteration_limit : integer *optional*</span>
<span class="sd">        Maximum number of iterations performed (default: None).</span>

<span class="sd">    Attributes</span>
<span class="sd">    ----------</span>
<span class="sd">    convergence_tolerance : float</span>
<span class="sd">        Tolerance specifying the case of convergence.</span>
<span class="sd">    convergence_level : integer</span>
<span class="sd">        Number of times the tolerance must be undershot before convergence</span>
<span class="sd">        is reached. (default: 3)</span>
<span class="sd">    iteration_limit : integer</span>
<span class="sd">        Maximum number of iterations performed.</span>
<span class="sd">    line_searcher : LineSearch</span>
<span class="sd">        Function which infers the optimal step size for functional minization</span>
<span class="sd">        given a descent direction.</span>
<span class="sd">    callback : function</span>
<span class="sd">        Function f(energy, iteration_number) supplied by the user to perform</span>
<span class="sd">        in-situ analysis at every iteration step. When being called the</span>
<span class="sd">        current energy and iteration_number are passed.</span>

<span class="sd">    Notes</span>
<span class="sd">    ------</span>
<span class="sd">    The callback function can be used to externally stop the minimization by</span>
<span class="sd">    raising a `StopIteration` exception.</span>
<span class="sd">    Check `get_descent_direction` of a derived class for information on the</span>
<span class="sd">    concrete minization scheme.</span>

<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">__metaclass__</span> <span class="o">=</span> <span class="n">NiftyMeta</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">line_searcher</span><span class="o">=</span><span class="n">LineSearchStrongWolfe</span><span class="p">(),</span> <span class="n">callback</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span>
                 <span class="n">convergence_tolerance</span><span class="o">=</span><span class="mf">1E-4</span><span class="p">,</span> <span class="n">convergence_level</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
                 <span class="n">iteration_limit</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">convergence_tolerance</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">float</span><span class="p">(</span><span class="n">convergence_tolerance</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">convergence_level</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">int</span><span class="p">(</span><span class="n">convergence_level</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">iteration_limit</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">iteration_limit</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">iteration_limit</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">iteration_limit</span> <span class="o">=</span> <span class="n">iteration_limit</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">line_searcher</span> <span class="o">=</span> <span class="n">line_searcher</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">callback</span> <span class="o">=</span> <span class="n">callback</span>

    <span class="k">def</span> <span class="nf">__call__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">energy</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot; Performs the minimization of the provided Energy functional.</span>

<span class="sd">        Parameters</span>
<span class="sd">        ----------</span>
<span class="sd">        energy : Energy object</span>
<span class="sd">           Energy object which provides value, gradient and curvature at a</span>
<span class="sd">           specific position in parameter space.</span>

<span class="sd">        Returns</span>
<span class="sd">        -------</span>
<span class="sd">        energy : Energy object</span>
<span class="sd">            Latest `energy` of the minimization.</span>
<span class="sd">        convergence : integer</span>
<span class="sd">            Latest convergence level indicating whether the minimization</span>
<span class="sd">            has converged or not.</span>

<span class="sd">        Note</span>
<span class="sd">        ----</span>
<span class="sd">        The minimization is stopped if</span>
<span class="sd">            * the callback function raises a `StopIteration` exception,</span>
<span class="sd">            * a perfectly flat point is reached,</span>
<span class="sd">            * according to the line-search the minimum is found,</span>
<span class="sd">            * the target convergence level is reached,</span>
<span class="sd">            * the iteration limit is reached.</span>

<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">convergence</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">f_k_minus_1</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">step_length</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">iteration_number</span> <span class="o">=</span> <span class="mi">1</span>

        <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">callback</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">callback</span><span class="p">(</span><span class="n">energy</span><span class="p">,</span> <span class="n">iteration_number</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">StopIteration</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Minimization was stopped by callback &quot;</span>
                                     <span class="s2">&quot;function.&quot;</span><span class="p">)</span>
                    <span class="k">break</span>

            <span class="c1"># compute the the gradient for the current location</span>
            <span class="n">gradient</span> <span class="o">=</span> <span class="n">energy</span><span class="o">.</span><span class="n">gradient</span>
            <span class="n">gradient_norm</span> <span class="o">=</span> <span class="n">gradient</span><span class="o">.</span><span class="n">dot</span><span class="p">(</span><span class="n">gradient</span><span class="p">)</span>

            <span class="c1"># check if position is at a flat point</span>
            <span class="k">if</span> <span class="n">gradient_norm</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Reached perfectly flat point. Stopping.&quot;</span><span class="p">)</span>
                <span class="n">convergence</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">convergence_level</span><span class="o">+</span><span class="mi">2</span>
                <span class="k">break</span>

            <span class="c1"># current position is encoded in energy object</span>
            <span class="n">descend_direction</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_descend_direction</span><span class="p">(</span><span class="n">energy</span><span class="p">)</span>

            <span class="c1"># compute the step length, which minimizes energy.value along the</span>
            <span class="c1"># search direction</span>
            <span class="n">step_length</span><span class="p">,</span> <span class="n">f_k</span><span class="p">,</span> <span class="n">new_energy</span> <span class="o">=</span> \
                <span class="bp">self</span><span class="o">.</span><span class="n">line_searcher</span><span class="o">.</span><span class="n">perform_line_search</span><span class="p">(</span>
                                               <span class="n">energy</span><span class="o">=</span><span class="n">energy</span><span class="p">,</span>
                                               <span class="n">pk</span><span class="o">=</span><span class="n">descend_direction</span><span class="p">,</span>
                                               <span class="n">f_k_minus_1</span><span class="o">=</span><span class="n">f_k_minus_1</span><span class="p">)</span>
            <span class="n">f_k_minus_1</span> <span class="o">=</span> <span class="n">energy</span><span class="o">.</span><span class="n">value</span>
            <span class="n">energy</span> <span class="o">=</span> <span class="n">new_energy</span>

            <span class="c1"># check convergence</span>
            <span class="n">delta</span> <span class="o">=</span> <span class="nb">abs</span><span class="p">(</span><span class="n">gradient</span><span class="p">)</span><span class="o">.</span><span class="n">max</span><span class="p">()</span> <span class="o">*</span> <span class="p">(</span><span class="n">step_length</span><span class="o">/</span><span class="n">gradient_norm</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;Iteration : </span><span class="si">%08u</span><span class="s2">   step_length = </span><span class="si">%3.1E</span><span class="s2">   &quot;</span>
                              <span class="s2">&quot;delta = </span><span class="si">%3.1E</span><span class="s2">&quot;</span> <span class="o">%</span>
                              <span class="p">(</span><span class="n">iteration_number</span><span class="p">,</span> <span class="n">step_length</span><span class="p">,</span> <span class="n">delta</span><span class="p">))</span>
            <span class="k">if</span> <span class="n">delta</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">convergence</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">convergence_level</span> <span class="o">+</span> <span class="mi">2</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Found minimum according to line-search. &quot;</span>
                                 <span class="s2">&quot;Stopping.&quot;</span><span class="p">)</span>
                <span class="k">break</span>
            <span class="k">elif</span> <span class="n">delta</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">convergence_tolerance</span><span class="p">:</span>
                <span class="n">convergence</span> <span class="o">+=</span> <span class="mi">1</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Updated convergence level to: </span><span class="si">%u</span><span class="s2">&quot;</span> <span class="o">%</span>
                                 <span class="n">convergence</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">convergence</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">convergence_level</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;Reached target convergence level.&quot;</span><span class="p">)</span>
                    <span class="k">break</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">convergence</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">convergence</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">iteration_limit</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">iteration_number</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">iteration_limit</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s2">&quot;Reached iteration limit. Stopping.&quot;</span><span class="p">)</span>
                    <span class="k">break</span>

            <span class="n">iteration_number</span> <span class="o">+=</span> <span class="mi">1</span>

        <span class="k">return</span> <span class="n">energy</span><span class="p">,</span> <span class="n">convergence</span>

    <span class="nd">@abc</span><span class="o">.</span><span class="n">abstractmethod</span>
    <span class="k">def</span> <span class="nf">get_descend_direction</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">energy</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">NotImplementedError</span></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../../index.html">
              <img class="logo" src="../../../_static/nifty_logo_black.png" alt="Logo"/>
            </a></p><div class="relations">
<h3>Related Topics</h3>
<ul>
  <li><a href="../../../index.html">Documentation overview</a><ul>
  <li><a href="../../index.html">Module code</a><ul>
  </ul></li>
  </ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="footer">
      &copy;2017, Theo Steininger.
      
      |
      Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.1</a>
      &amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.10</a>
      
    </div>

    

    
  </body>
</html>