

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>server &mdash; LEAF 0.1 documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../_static/customheader.css" type="text/css" />
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" /> 

  
  <script src="../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../index.html" class="icon icon-home"> LEAF
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Getting Started</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../install/get_leaf.html">Installation and Contribution</a></li>
</ul>
<p class="caption"><span class="caption-text">User Guide</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tutorials/index.html">General Examples</a></li>
</ul>
<p class="caption"><span class="caption-text">API Documentation</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../autodoc/modules.html">models</a></li>
</ul>
<p class="caption"><span class="caption-text">Additional Information</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../contact.html">Contact</a></li>
<li class="toctree-l1"><a class="reference internal" href="../citations.html">Citing LEAF</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">LEAF</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html">Docs</a> &raquo;</li>
        
          <li><a href="index.html">Module code</a> &raquo;</li>
        
      <li>server</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for server</h1><div class="highlight"><pre>
<span></span><span class="kn">import</span> <span class="nn">random</span>


<span class="kn">from</span> <span class="nn">baseline_constants</span> <span class="k">import</span> <span class="n">BYTES_WRITTEN_KEY</span><span class="p">,</span> <span class="n">BYTES_READ_KEY</span><span class="p">,</span> <span class="n">LOCAL_COMPUTATIONS_KEY</span>


<div class="viewcode-block" id="Server"><a class="viewcode-back" href="../autodoc/server.html#server.Server">[docs]</a><span class="k">class</span> <span class="nc">Server</span><span class="p">:</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">model</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span> <span class="o">=</span> <span class="n">model</span>  <span class="c1"># global model of the server.</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">selected_clients</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">updates</span> <span class="o">=</span> <span class="p">[]</span>

<div class="viewcode-block" id="Server.select_clients"><a class="viewcode-back" href="../autodoc/server.html#server.Server.select_clients">[docs]</a>    <span class="k">def</span> <span class="nf">select_clients</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">possible_clients</span><span class="p">,</span> <span class="n">num_clients</span><span class="o">=</span><span class="mi">20</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Selects num_clients clients randomly from possible_clients.</span>
<span class="sd">        </span>
<span class="sd">        Note that within function, num_clients is set to</span>
<span class="sd">            min(num_clients, len(possible_clients)).</span>

<span class="sd">        Args:</span>
<span class="sd">            possible_clients: Clients from which the server can select.</span>
<span class="sd">            num_clients: Number of clients to select; default 20</span>
<span class="sd">        Return:</span>
<span class="sd">            list of (num_train_samples, num_test_samples)</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">num_clients</span> <span class="o">=</span> <span class="nb">min</span><span class="p">(</span><span class="n">num_clients</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">possible_clients</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">selected_clients</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">possible_clients</span><span class="p">,</span> <span class="n">num_clients</span><span class="p">)</span>

        <span class="k">return</span> <span class="p">[(</span><span class="nb">len</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">train_data</span><span class="p">[</span><span class="s1">&#39;y&#39;</span><span class="p">]),</span> <span class="nb">len</span><span class="p">(</span><span class="n">c</span><span class="o">.</span><span class="n">eval_data</span><span class="p">[</span><span class="s1">&#39;y&#39;</span><span class="p">]))</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">selected_clients</span><span class="p">]</span></div>

<div class="viewcode-block" id="Server.train_model"><a class="viewcode-back" href="../autodoc/server.html#server.Server.train_model">[docs]</a>    <span class="k">def</span> <span class="nf">train_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">num_epochs</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">batch_size</span><span class="o">=</span><span class="mi">10</span><span class="p">,</span> <span class="n">minibatch</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">clients</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Trains self.model on given clients.</span>
<span class="sd">        </span>
<span class="sd">        Trains model on self.selected_clients if clients=None;</span>
<span class="sd">        each client&#39;s data is trained with the given number of epochs</span>
<span class="sd">        and batches.</span>

<span class="sd">        Args:</span>
<span class="sd">            clients: list of Client objects.</span>
<span class="sd">            num_epochs: Number of epochs to train.</span>
<span class="sd">            batch_size: Size of training batches.</span>
<span class="sd">            minibatch: fraction of client&#39;s data to apply minibatch sgd,</span>
<span class="sd">                None to use FedAvg</span>
<span class="sd">        Return:</span>
<span class="sd">            bytes_written: number of bytes written by each client to server </span>
<span class="sd">                dictionary with client ids as keys and integer values.</span>
<span class="sd">            client computations: number of FLOPs computed by each client</span>
<span class="sd">                dictionary with client ids as keys and integer values.</span>
<span class="sd">            bytes_read: number of bytes read by each client from server</span>
<span class="sd">                dictionary with client ids as keys and integer values.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">clients</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">clients</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">selected_clients</span>
        <span class="n">sys_metrics</span> <span class="o">=</span> <span class="p">{</span>
            <span class="n">c</span><span class="o">.</span><span class="n">id</span><span class="p">:</span> <span class="p">{</span><span class="n">BYTES_WRITTEN_KEY</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
                   <span class="n">BYTES_READ_KEY</span><span class="p">:</span> <span class="mi">0</span><span class="p">,</span>
                   <span class="n">LOCAL_COMPUTATIONS_KEY</span><span class="p">:</span> <span class="mi">0</span><span class="p">}</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">clients</span><span class="p">}</span>
        <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">clients</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">send_to</span><span class="p">([</span><span class="n">c</span><span class="p">])</span>
            <span class="n">sys_metrics</span><span class="p">[</span><span class="n">c</span><span class="o">.</span><span class="n">id</span><span class="p">][</span><span class="n">BYTES_READ_KEY</span><span class="p">]</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">size</span>

            <span class="n">comp</span><span class="p">,</span> <span class="n">num_samples</span><span class="p">,</span> <span class="n">update</span> <span class="o">=</span> <span class="n">c</span><span class="o">.</span><span class="n">train</span><span class="p">(</span><span class="n">num_epochs</span><span class="p">,</span> <span class="n">batch_size</span><span class="p">,</span> <span class="n">minibatch</span><span class="p">)</span>
            <span class="n">sys_metrics</span><span class="p">[</span><span class="n">c</span><span class="o">.</span><span class="n">id</span><span class="p">][</span><span class="n">LOCAL_COMPUTATIONS_KEY</span><span class="p">]</span> <span class="o">=</span> <span class="n">comp</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">updates</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">num_samples</span><span class="p">,</span> <span class="n">update</span><span class="p">))</span>
            <span class="n">sys_metrics</span><span class="p">[</span><span class="n">c</span><span class="o">.</span><span class="n">id</span><span class="p">][</span><span class="n">BYTES_WRITTEN_KEY</span><span class="p">]</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">size</span>
        <span class="k">return</span> <span class="n">sys_metrics</span></div>

<div class="viewcode-block" id="Server.update_model"><a class="viewcode-back" href="../autodoc/server.html#server.Server.update_model">[docs]</a>    <span class="k">def</span> <span class="nf">update_model</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">updates</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">updates</span> <span class="o">=</span> <span class="p">[]</span></div>

<div class="viewcode-block" id="Server.test_model"><a class="viewcode-back" href="../autodoc/server.html#server.Server.test_model">[docs]</a>    <span class="k">def</span> <span class="nf">test_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">clients_to_test</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Tests self.model on given clients.</span>

<span class="sd">        Tests model on self.selected_clients if clients_to_test=None.</span>

<span class="sd">        Args:</span>
<span class="sd">            clients_to_test: list of Client objects.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">clients_to_test</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">clients_to_test</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">selected_clients</span>
        <span class="n">metrics</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">send_to</span><span class="p">(</span><span class="n">clients_to_test</span><span class="p">)</span>
        
        <span class="k">for</span> <span class="n">client</span> <span class="ow">in</span> <span class="n">clients_to_test</span><span class="p">:</span>
            <span class="n">c_metrics</span> <span class="o">=</span> <span class="n">client</span><span class="o">.</span><span class="n">test</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">cur_model</span><span class="p">)</span>
            <span class="n">metrics</span><span class="p">[</span><span class="n">client</span><span class="o">.</span><span class="n">id</span><span class="p">]</span> <span class="o">=</span> <span class="n">c_metrics</span>

        <span class="k">return</span> <span class="n">metrics</span></div>

<div class="viewcode-block" id="Server.get_clients_test_info"><a class="viewcode-back" href="../autodoc/server.html#server.Server.get_clients_test_info">[docs]</a>    <span class="k">def</span> <span class="nf">get_clients_test_info</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">clients</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Returns the ids, hierarchies and num_test_samples for the given clients.</span>

<span class="sd">        Returns info about self.selected_clients if clients=None;</span>

<span class="sd">        Args:</span>
<span class="sd">            clients: list of Client objects.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">clients</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">clients</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">selected_clients</span>
        <span class="n">ids</span> <span class="o">=</span> <span class="p">[</span><span class="n">c</span><span class="o">.</span><span class="n">id</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">clients</span><span class="p">]</span>
        <span class="n">groups</span> <span class="o">=</span> <span class="p">{</span><span class="n">c</span><span class="o">.</span><span class="n">id</span><span class="p">:</span> <span class="n">c</span><span class="o">.</span><span class="n">group</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">clients</span><span class="p">}</span>
        <span class="n">num_samples</span> <span class="o">=</span> <span class="p">{</span><span class="n">c</span><span class="o">.</span><span class="n">id</span><span class="p">:</span> <span class="n">c</span><span class="o">.</span><span class="n">num_test_samples</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">clients</span><span class="p">}</span>
        <span class="k">return</span> <span class="n">ids</span><span class="p">,</span> <span class="n">groups</span><span class="p">,</span> <span class="n">num_samples</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, Talwalkar Lab

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></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="../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>