index.html 12.6 KB
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
        
        <link rel="canonical" href="http://hyperopt.github.io/hyperopt/interfacing-languages/">
        <link rel="shortcut icon" href="../img/favicon.ico">
        <title>Interfacing with other languages - Hyperopt Documentation</title>
        <link href="../css/bootstrap-custom.min.css" rel="stylesheet">
        <link href="../css/font-awesome.min.css" rel="stylesheet">
        <link href="../css/base.css" rel="stylesheet">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css">
        <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->

        <script src="../js/jquery-1.10.2.min.js" defer></script>
        <script src="../js/bootstrap-3.0.3.min.js" defer></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/yaml.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/python.min.js"></script>
        <script>hljs.initHighlightingOnLoad();</script> 
    </head>

    <body>

        <div class="navbar navbar-default navbar-fixed-top" role="navigation">
            <div class="container">

                <!-- Collapsed navigation -->
                <div class="navbar-header">
                    <!-- Expander button -->
                    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                        <span class="sr-only">Toggle navigation</span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>
                    <a class="navbar-brand" href="..">Hyperopt Documentation</a>
                </div>

                <!-- Expanded navigation -->
                <div class="navbar-collapse collapse">
                        <!-- Main navigation -->
                        <ul class="nav navbar-nav">
                            <li >
                                <a href="..">Home</a>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown">Getting started <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
<li >
    <a href="../getting-started/overview/">Getting started with Hyperopt</a>
</li>
                                    
<li >
    <a href="../getting-started/minimizing_functions/">Minimizing functions</a>
</li>
                                    
<li >
    <a href="../getting-started/search_spaces/">Defining search spaces</a>
</li>
                                </ul>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown">Installation <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
<li >
    <a href="../setup/installation-notes/">Installation notes</a>
</li>
                                    
<li >
    <a href="../setup/running-tests/">Running tests</a>
</li>
                                </ul>
                            </li>
                            <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown">Scale out <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
<li >
    <a href="../scaleout/mongodb/">MongoDB</a>
</li>
                                    
<li >
    <a href="../scaleout/spark/">Spark</a>
</li>
                                </ul>
                            </li>
                            <li class="dropdown active">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown">Misc <b class="caret"></b></a>
                                <ul class="dropdown-menu">
                                    
<li class="active">
    <a href="./">Interfacing with other languages</a>
</li>
                                    
<li >
    <a href="../related-work/">Related work</a>
</li>
                                    
<li >
    <a href="../scipy_submission/">SciPy 2013 submission</a>
</li>
                                </ul>
                            </li>
                        </ul>

                    <ul class="nav navbar-nav navbar-right">
                        <li>
                            <a href="#" data-toggle="modal" data-target="#mkdocs_search_modal">
                                <i class="fa fa-search"></i> Search
                            </a>
                        </li>
                            <li >
                                <a rel="next" href="../scaleout/spark/">
                                    <i class="fa fa-arrow-left"></i> Previous
                                </a>
                            </li>
                            <li >
                                <a rel="prev" href="../related-work/">
                                    Next <i class="fa fa-arrow-right"></i>
                                </a>
                            </li>
                            <li>
                                <a href="http://github.com/hyperopt/hyperopt/edit/master/docs/interfacing-languages.md"><i class="fa fa-github"></i> Edit on GitHub</a>
                            </li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="container">
                <div class="col-md-3"><div class="bs-sidebar hidden-print affix well" role="complementary">
    <ul class="nav bs-sidenav">
        <li class="main active"><a href="#interfacing-hyperopt-with-other-programming-languages">Interfacing Hyperopt with other programming languages</a></li>
            <li><a href="#wrapping-a-call-to-non-python-code">Wrapping a call to non-Python code</a></li>
            <li><a href="#communicating-with-mongodb-directly">Communicating with MongoDB Directly</a></li>
    </ul>
</div></div>
                <div class="col-md-9" role="main">

<h1 id="interfacing-hyperopt-with-other-programming-languages">Interfacing Hyperopt with other programming languages</h1>
<p>There are basically two ways to interface hyperopt with other languages:</p>
<ol>
<li>you can write a Python wrapper around your cost function that is not written in Python, or</li>
<li>you can replace the <code>hyperopt-mongo-worker</code> program and communicate with MongoDB directly using JSON.</li>
</ol>
<h2 id="wrapping-a-call-to-non-python-code">Wrapping a call to non-Python code</h2>
<p>The easiest way to use hyperopt to optimize the arguments to a non-python function, such as for example an external executable, is to write a Python function wrapper around that external executable. Supposing you have an executable <code>foo</code> that takes an integer command-line argument <code>--n</code> and prints out a score, you might wrap it like this:</p>
<pre><code class="python">import subprocess
def foo_wrapper(n):
    # Optional: write out a script for the external executable
    # (we just call foo with the argument proposed by hyperopt)
    proc = subprocess.Popen(['foo', '--n', n], stdout=subprocess.PIPE)
    proc_out, proc_err = proc.communicate()
    # &lt;you might have to do some more elaborate parsing of foo's output here&gt;
    score = float(proc_out)
    return score
</code></pre>

<p>Of course, to optimize the <code>n</code> argument to <code>foo</code> you also need to call hyperopt.fmin, and define the search space. I can only imagine that you will want to do this part in Python.</p>
<pre><code class="python">from hyperopt import fmin, hp, random

best_n = fmin(foo_wrapper, hp.quniform('n', 1, 100, 1), algo=random.suggest)

print best_n
</code></pre>

<p>When the search space is larger than the simple one here, you might want or need the wrapper function to translate its argument into some kind of configuration file/script for the external executable.</p>
<p>This approach is perfectly compatible with MongoTrials.</p>
<h2 id="communicating-with-mongodb-directly">Communicating with MongoDB Directly</h2>
<p>It is possible to interface more directly with the search process (when using MongoTrials) by communicating with MongoDB directly, just like <code>hyperopt-mongo-worker</code> does. It's beyond the scope of a tutorial to explain how to do this, but Hannes Schultz (@temporaer) got hyperopt working with his MDBQ project, which is a standalone mongodb-based task queue:</p>
<p><a href="https://github.com/temporaer/MDBQ/blob/master/src/example/hyperopt_client.cpp">Hyperopt C++ Client</a></p>
<p>Have a look at that code, as well as the contents of <a href="https://github.com/jaberg/hyperopt/blob/master/hyperopt/mongoexp.py">hyperopt/mongoexp.py</a> to understand how worker processes are expected to reserve jobs in the work queue, and store results back to MongoDB.</p></div>
        </div>

        <footer class="col-md-12">
            <hr>
            <p>Documentation built with <a href="https://www.mkdocs.org/">MkDocs</a>.</p>
        </footer>
        <script>
            var base_url = "..",
                shortcuts = {"search": 83, "next": 78, "help": 191, "previous": 80};
        </script>
        <script src="../js/base.js" defer></script>
        <script src="../search/main.js" defer></script>

        <div class="modal" id="mkdocs_search_modal" tabindex="-1" role="dialog" aria-labelledby="Search Modal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="exampleModalLabel">Search</h4>
            </div>
            <div class="modal-body">
                <p>
                    From here you can search these documents. Enter
                    your search terms below.
                </p>
                <form role="form">
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="Search..." id="mkdocs-search-query" title="Type search term here">
                    </div>
                </form>
                <div id="mkdocs-search-results"></div>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div><div class="modal" id="mkdocs_keyboard_modal" tabindex="-1" role="dialog" aria-labelledby="Keyboard Shortcuts Modal" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                <h4 class="modal-title" id="exampleModalLabel">Keyboard Shortcuts</h4>
            </div>
            <div class="modal-body">
              <table class="table">
                <thead>
                  <tr>
                    <th style="width: 20%;">Keys</th>
                    <th>Action</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <td class="help shortcut"><kbd>?</kbd></td>
                    <td>Open this help</td>
                  </tr>
                  <tr>
                    <td class="next shortcut"><kbd>n</kbd></td>
                    <td>Next page</td>
                  </tr>
                  <tr>
                    <td class="prev shortcut"><kbd>p</kbd></td>
                    <td>Previous page</td>
                  </tr>
                  <tr>
                    <td class="search shortcut"><kbd>s</kbd></td>
                    <td>Search</td>
                  </tr>
                </tbody>
              </table>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </div>
</div>

    </body>
</html>