<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Richard Liaw on Medium]]></title>
        <description><![CDATA[Stories by Richard Liaw on Medium]]></description>
        <link>https://medium.com/@richliaw?source=rss-842dc7bf293f------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*hjpbjkJ0aRKrCMoOOCM1iA.jpeg</url>
            <title>Stories by Richard Liaw on Medium</title>
            <link>https://medium.com/@richliaw?source=rss-842dc7bf293f------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 15 Nov 2026 18:44:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@richliaw/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Configuring and Scaling ML with Hydra + Ray]]></title>
            <link>https://medium.com/distributed-computing-with-ray/configuring-and-scaling-ml-with-hydra-ray-80135141e215?source=rss-842dc7bf293f------2</link>
            <guid isPermaLink="false">https://medium.com/p/80135141e215</guid>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Richard Liaw]]></dc:creator>
            <pubDate>Tue, 26 Jan 2021 17:01:55 GMT</pubDate>
            <atom:updated>2021-04-01T17:04:10.101Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>By </strong><a href="https://twitter.com/richliaw"><strong>Richard Liaw</strong></a><strong>, </strong><a href="https://twitter.com/bllchmbrs"><strong>Bill Chambers</strong></a><strong>, and </strong><a href="https://github.com/jieru-hu"><strong>Jieru Hu</strong></a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7JGZucOPkH4P8dKORIvdGQ.png" /></figure><p><a href="https://hydra.cc/">Hydra</a>, from Facebook AI, is a framework for elegantly configuring complex applications. Since its initial release, Hydra has become a popular framework adopted by researchers and practitioners. We are happy to announce that users can now scale and launch jobs to the cloud through the new <a href="https://hydra.cc/docs/plugins/ray_launcher">Hydra Ray Launcher</a>!</p><p><a href="https://github.com/ray-project/ray">Ray</a> is a library that fits Hydra’s needs perfectly. <a href="https://ray.io/">Ray is a simple yet powerful Python library for parallel and distributed programming</a> with a great ecosystem of ML libraries (for distributed training, reinforcement learning, and model serving), as well as community libraries and integrations (e.g., <a href="https://docs.ray.io/en/master/dask-on-ray.html">Dask on Ray</a>, <a href="https://horovod.readthedocs.io/en/stable/ray_include.html">Horovod on Ray</a>).</p><p>Hydra Ray Launcher enables you to easily configure and launch your application on Ray in 3 different ways. You can launch your application by:</p><ol><li>Starting or connecting to a Ray cluster on AWS EC2 for short lived clusters</li><li>Connecting to an existing Ray cluster if you have a long running Ray cluster.</li><li>Starting a new Ray cluster locally.</li></ol><h3>Walkthrough</h3><p>Below, we walk you through installation and running the example applications provided by the launcher. Please check Hydra Ray Launcher’s <a href="https://hydra.cc/docs/plugins/ray_launcher">documentation</a> for more details.</p><h4><strong>Installation</strong></h4><pre>pip install hydra-ray-launcher --pre</pre><h4><strong>Launch to an AWS cluster</strong></h4><p>Launching a Ray application on AWS can be done by setting hydra/launcher=aws. This will allow your application to run on a new or existing AWS EC2 Ray Cluster. Launching on AWS is built on top of Ray’s cluster launcher CLI (<a href="https://medium.com/distributed-computing-with-ray/a-step-by-step-guide-to-scaling-your-first-python-application-in-the-cloud-8761fe331ef1">learn more here</a>). The cluster launcher CLI expects <a href="https://docs.ray.io/en/master/cluster/cloud.html#aws-gcp-azure">an autoscaler yaml</a> for cluster configurations.</p><p>You can configure your cluster just like how you configure any Hydra application: yaml, strict config and command line override.</p><pre>@hydra.main(config_name=&quot;config&quot;)<br>def my_app(cfg: DictConfig) -&gt; None:<br>    log.info(f&quot;Executing task {cfg.task}&quot;)<br>    time.sleep(1)<br><br><br>if __name__ == &quot;__main__&quot;:<br>    my_app()</pre><p>From the command line:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AkL7Cd7ZShJYXcJIkxkr7g.png" /></figure><h4><strong>Launch on an existing Ray cluster</strong></h4><p>You can also launch the application on an existing Ray cluster by configuring the launcher to connect to the cluster. For instance, you might want to run the same Ray script on your local machine by accessing the Ray cluster you started previously.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wjTmHDDtT7RNKx_qxhT0_Q.png" /></figure><h4><strong>Launch by spinning up a new Ray cluster locally (For testing)</strong></h4><p>For a quick test locally, you can spin up a Ray cluster at initialization time. Note how the only difference is to specify the hydra launcher as ray, as opposed to ray_aws shown in the previous example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nDWjJq0chtLnRna2F3p-sA.png" /></figure><h4>Next steps</h4><p>Please check out Hydra Ray Launcher’s <a href="https://hydra.cc/docs/plugins/ray_launcher">documentation</a> for more details on the launcher. If you’d like to learn more about Hydra, check out the Hydra <a href="https://hydra.cc/">Website</a> and join the <a href="https://github.com/facebookresearch/hydra#community">community</a>. If you’d like to learn more about <a href="https://ray.io/&#39;">Ray</a>, check out <a href="https://docs.ray.io/en/master/">the Ray Documentation</a>, <a href="https://github.com/anyscale/academy">Ray Tutorials on Anyscale Academy</a>, <a href="http://anyscale.com/ray-summit">Ray Summit</a>, and join <a href="https://discuss.ray.io/">the Ray forums</a>.</p><p>We’d love to hear your feedback and experience with the new Hydra Ray launcher!</p><p><em>Originally published at </em><a href="https://www.anyscale.com/blog/configuring-and-scaling-ml-with-hydra-ray"><em>https://www.anyscale.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80135141e215" width="1" height="1" alt=""><hr><p><a href="https://medium.com/distributed-computing-with-ray/configuring-and-scaling-ml-with-hydra-ray-80135141e215">Configuring and Scaling ML with Hydra + Ray</a> was originally published in <a href="https://medium.com/distributed-computing-with-ray">Distributed Computing with Ray</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ray for Windows]]></title>
            <link>https://medium.com/distributed-computing-with-ray/ray-for-windows-61c9f11adb65?source=rss-842dc7bf293f------2</link>
            <guid isPermaLink="false">https://medium.com/p/61c9f11adb65</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Richard Liaw]]></dc:creator>
            <pubDate>Sat, 26 Sep 2020 01:11:55 GMT</pubDate>
            <atom:updated>2020-09-26T03:39:38.519Z</atom:updated>
            <content:encoded><![CDATA[<p><strong><em>In case you missed it: </em></strong><em>Ray supports Windows natively as of June 2020. You can try </em><a href="https://docs.ray.io/en/master/installation.html#windows-support"><em>Ray on Windows</em></a><em> via </em>pip install -U ray<em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Y6LD6DD990zi2EqZ" /><figcaption>By <a href="https://unsplash.com/@sarahdorweiler?utm_source=medium&amp;utm_medium=referral">Sarah Dorweiler</a></figcaption></figure><p><a href="http://docs.ray.io/">Ray provides a simple, universal API</a> for scaling Python applications across compute clusters with minimal effort. Many users love using Ray since it reduces the complexity of building concurrent and distributed programs.</p><p>However, in the first two years of developing Ray, <a href="https://github.com/ray-project/ray/issues/631">we neglected to provide native Windows support.</a> Though most distributed execution environments ran Linux, developers needed to prototype on their local laptop or desktop, which often was a Windows machine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/770/1*i3_kB0J5c97gAtqd3-JRMw.png" /></figure><p>In the past year, one of the Ray contributors, Mehrdad Niknami, led a huge effort to close this gap. There were countless technical challenges, including running Redis on Windows and building our own equivalent Windows shims for POSIX APIs.</p><p>After almost 9 months of work, we were finally able to build Windows Python wheels, which we released with version 0.8.6 in June. And, <strong>moving forward, we will be supporting and maintaining the Windows build to make sure more developers can leverage Ray.</strong></p><p><strong>As we approach the release of Ray 1.0, we will share a detailed account of our development efforts toward Windows support.</strong> In that post, we will take a deep dive into the challenges we faced and the solutions we came up with, which we hope will be valuable for other projects embarking on a similar journey<strong>.</strong></p><p>While the port of Ray to Windows is not yet finished, it has been quite successful so far, and we are aiming to <a href="https://github.com/ray-project/ray/issues/9114">improve it over the next couple releases</a>. Please try it out today and let us know if you run into any issues!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=61c9f11adb65" width="1" height="1" alt=""><hr><p><a href="https://medium.com/distributed-computing-with-ray/ray-for-windows-61c9f11adb65">Ray for Windows</a> was originally published in <a href="https://medium.com/distributed-computing-with-ray">Distributed Computing with Ray</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to tune Pytorch Lightning hyperparameters]]></title>
            <link>https://medium.com/data-science/how-to-tune-pytorch-lightning-hyperparameters-80089a281646?source=rss-842dc7bf293f------2</link>
            <guid isPermaLink="false">https://medium.com/p/80089a281646</guid>
            <category><![CDATA[hyperparameter-tuning]]></category>
            <category><![CDATA[pytorch-lightning]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[pytorch]]></category>
            <dc:creator><![CDATA[Richard Liaw]]></dc:creator>
            <pubDate>Tue, 18 Aug 2020 21:28:39 GMT</pubDate>
            <atom:updated>2020-10-24T13:38:45.326Z</atom:updated>
            <content:encoded><![CDATA[<h4><em>Wondering how to optimize Pytorch Lightning hyperparameters in 30 lines of code?</em></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/1*DnX6uxo7g5-LFzXGpESH4g.png" /></figure><p><a href="https://github.com/PyTorchLightning/pytorch-lightning">Pytorch Lightning</a> is one of the hottest AI libraries of 2020, and it makes AI research scalable and fast to iterate on. But if you use Pytorch Lightning, you’ll need to do <strong>hyperparameter tuning</strong>.</p><p>Proper hyperparameter tuning can make the difference between a good training run and a failing one. Everyone knows that you can dramatically boost the accuracy of your model with good tuning methods!</p><p>In this blog post, we’ll demonstrate how to use <a href="https://docs.ray.io/en/latest/tune.html">Ray Tune, an industry standard for hyperparameter tuning</a>, with PyTorch Lightning. <a href="https://docs.ray.io/en/latest/tune/index.html">Ray Tune</a> is part of <a href="https://ray.io/">Ray</a>, a library for scaling Python.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*aBs5S6cV6xTDBFExnbXybw.png" /></figure><p>It is available as a PyPI package and can be installed like this:</p><pre>pip install &quot;ray[tune]&quot;</pre><p>To use <a href="https://docs.ray.io/en/latest/tune/index.html">Ray Tune</a> with PyTorch Lightning, we <strong>only need to add a few lines of code!!</strong></p><h3>Getting started with Ray Tune + PTL!</h3><p>To run the code in this blog post, be sure to first run:</p><pre>pip install &quot;ray[tune]&quot; <br>pip install &quot;pytorch-lightning&gt;=1.0&quot; <br>pip install &quot;pytorch-lightning-bolts&gt;=0.2.5&quot;</pre><p><em>The below example is tested on ray==1.0.1 , pytorch-lightning==1.0.2, and pytorch-lightning-bolts==0.2.5. </em><a href="https://github.com/ray-project/ray/blob/releases/1.0.1/python/ray/tune/examples/mnist_ptl_mini.py"><strong>See the full example here.</strong></a></p><p>Let’s first start with some imports:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d76d7a7283b80551738e7dce8134cadd/href">https://medium.com/media/d76d7a7283b80551738e7dce8134cadd/href</a></iframe><p>After imports, there are three easy steps.</p><ol><li>Create your LightningModule</li><li>Create a function that calls Trainer.fit with the Tune callback</li><li>Use tune.run to execute your hyperparameter search.</li></ol><h4>Step 1: create your LightningModule</h4><p>First step, create your LightningModule. Your LightningModule should take a <strong>configuration</strong> dict as a parameter on initialization. This dict should then set the model parameters you want to tune. Your module could look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/19eeb5d8fc588153fa68d555ee4741e1/href">https://medium.com/media/19eeb5d8fc588153fa68d555ee4741e1/href</a></iframe><h4>Step 2: Create a function that calls Trainer.fit with the Tune callback</h4><p>We will use a callback to communicate with <a href="https://docs.ray.io/en/latest/tune/index.html">Ray Tune</a>. The callback is very simple:</p><pre>from ray.tune.integration.pytorch_lightning import TuneReportCallback</pre><pre>...<br>metrics = {&quot;loss&quot;: &quot;ptl/val_loss&quot;, &quot;acc&quot;: &quot;ptl/val_accuracy&quot;}<br>callbacks = [TuneReportCallback(metrics, on=&quot;validation_end&quot;)]<br>trainer = pl.Trainer(... callbacks=callbacks)</pre><p>This callback ensures that after each validation epoch, we report the loss metrics back to <a href="https://docs.ray.io/en/latest/tune/index.html">Ray Tune</a>. The val_loss and val_accuracy keys correspond to the return value of the validation_epoch_end method.</p><p>Further, <a href="https://docs.ray.io/en/latest/tune/index.html">Ray Tune</a> will start a number of different training runs. To create multiple training runs (for the hyperparameter search), we thus need to wrap the trainer call in a function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e12a1066fe78f2a4217eb9e9c109624/href">https://medium.com/media/0e12a1066fe78f2a4217eb9e9c109624/href</a></iframe><p>The train_mnist() function expects a config dict, which it then passes to the LightningModule. This config dict will contain the hyperparameter values of one evaluation.</p><h4>Step 3: Use tune.run to execute your hyperparameter search.</h4><p>Finally, we need to call ray.tune to optimize our parameters. Here, our first step is to tell <a href="https://docs.ray.io/en/latest/tune/index.html">Ray Tune</a> which values are valid choices for the parameters. This is called the <a href="https://docs.ray.io/en/latest/tune/api_docs/search_space.html"><strong>search space</strong></a>, and we can define it like so:</p><pre># Defining a search space!</pre><pre>config = {<br> <strong>&quot;layer_1_size&quot;</strong>: tune.choice([32, 64, 128]),<br> <strong>&quot;layer_2_size&quot;</strong>: tune.choice([64, 128, 256]),<br> <strong>&quot;lr&quot;</strong>: tune.loguniform(1e-4, 1e-1),<br> <strong>&quot;batch_size&quot;</strong>: tune.choice([32, 64, 128])<br>}</pre><p>Let’s take a quick look at the search space. For the first and second layer sizes, we let <a href="https://docs.ray.io/en/latest/tune/index.html">Ray Tune</a> choose between three different fixed values. The learning rate is sampled between 0.0001 and 0.1. For the batch size, also a choice of three fixed values is given. Of course, there are <a href="https://docs.ray.io/en/master/tune/api_docs/grid_random.html#random-distributions-api">many other (even custom) methods available</a> for defining the search space.</p><p><a href="https://docs.ray.io/en/latest/tune/index.html">Ray Tune</a> will now proceed to sample ten different parameter combinations randomly, train them, and compare their performance afterwards.</p><p>We wrap the train_mnist function in tune.with_parameters to pass constants like the maximum number of epochs to train each model and the number of GPUs available for each trial. <a href="https://docs.ray.io/en/master/using-ray-with-gpus.html#fractional-gpus">Ray Tune supports fractional GPUs</a>, so something like gpus=0.25 is totally valid as long as the model still fits on the GPU memory.</p><pre># Execute the hyperparameter search<br>analysis = tune.run(<br> tune.with_parameters(train_mnist_tune, epochs=10, gpus=0),<br> config=config,<br> num_samples=10)</pre><p>The final invocation of tune.run can look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/db7bbe262fadefc6fb7dfea4125624ea/href">https://medium.com/media/db7bbe262fadefc6fb7dfea4125624ea/href</a></iframe><p>And finally, the tuning result could look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a1ef78a06b7e19f8b97d19e4bea8c4e/href">https://medium.com/media/9a1ef78a06b7e19f8b97d19e4bea8c4e/href</a></iframe><p>In this simple example, a number of configurations reached a good accuracy. The best result we observed was a validation accuracy of <strong>0.978105</strong> with a batch size of 32, layer sizes of 128 and 64, and a small learning rate around 0.001. We can also see that the learning rate seems to be the main factor influencing performance — if it is too large, the runs fail to reach a good accuracy.</p><p>You can retrieve the best score by using the return value of tune.run :</p><pre>analysis = tune.run(...)</pre><pre>best_trial = analysis.best_trial  # Get best trial<br>best_config = analysis.best_config  # Get best trial&#39;s hyperparameters<br>best_logdir = analysis.best_logdir  # Get best trial&#39;s logdir<br>best_checkpoint = analysis.best_checkpoint  # Get best trial&#39;s best checkpoint<br>best_result = analysis.best_result  # Get best trial&#39;s last results</pre><p>You can also easily leverage some of <a href="https://docs.ray.io/en/latest/tune/index.html">Ray Tune</a>’s more powerful optimization features. For example, <a href="https://docs.ray.io/en/latest/tune/index.html">Ray Tune</a>’s search algorithm allows you to easily optimize the landscape of hyperparameter combinations. Ray Tune’s schedulers can also stop bad performing trials early to save resources.</p><p><a href="https://github.com/ray-project/ray/blob/releases/1.0.1/python/ray/tune/examples/mnist_ptl_mini.py"><strong>See the full example here.</strong></a></p><h4>Inspecting results in Tensorboard</h4><p>Ray Tune automatically exports metrics <a href="https://docs.ray.io/en/latest/tune/tutorials/tune-usage.html#logging-tensorboard">into TensorBoard,</a> and also easily supports W&amp;B.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dRDhRuVKoXgDgi0zSuYI4A.png" /></figure><p>That’s it!</p><h4><strong>To enable easy hyperparameter tuning with </strong><a href="https://docs.ray.io/en/latest/tune/index.html"><strong>Ray Tune</strong></a><strong>, we only needed to add a callback, wrap the train function, and then start Tune.</strong></h4><p>Of course, this is a very simple example that doesn’t leverage many of Ray Tune’s search features, like <a href="https://docs.ray.io/en/master/tune/api_docs/schedulers.html#hyperband-tune-schedulers-hyperbandscheduler">early stopping of bad performing trials</a> or <a href="https://docs.ray.io/en/master/tune/api_docs/schedulers.html#population-based-training-tune-schedulers-populationbasedtraining">population based training</a>. If you would like to see a full example for these, please have a look at our <a href="https://docs.ray.io/en/master/tune/tutorials/tune-pytorch-lightning.html">full PyTorch Lightning tutorial</a>.</p><p>If you’ve been successful in using PyTorch Lightning with <a href="https://docs.ray.io/en/latest/tune/index.html">Ray Tune</a>, or if you need help with anything, please reach out by <a href="https://forms.gle/9TSdDYUgxYs8SA9e8">joining our Slack</a> or dropping by our <a href="https://github.com/ray-project/ray">Github</a> — we would love to hear from you!</p><h3>If you liked this blog post, be sure to check out:</h3><ul><li>Our <a href="https://wandb.ai/amogkam/transformers/reports/Hyperparameter-Optimization-for-Huggingface-Transformers--VmlldzoyMTc2ODI">Weights and Biases report</a> on Hyperparameter Optimization for Transformers</li><li>The <a href="https://medium.com/distributed-computing-with-ray/the-simplest-way-to-serve-your-nlp-model-in-production-with-pure-python-d42b6a97ad55">simplest way to serve your NLP model</a> from scratch</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=80089a281646" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/how-to-tune-pytorch-lightning-hyperparameters-80089a281646">How to tune Pytorch Lightning hyperparameters</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Faster and Cheaper Pytorch with RaySGD]]></title>
            <link>https://medium.com/distributed-computing-with-ray/faster-and-cheaper-pytorch-with-raysgd-a5a44d4fd220?source=rss-842dc7bf293f------2</link>
            <guid isPermaLink="false">https://medium.com/p/a5a44d4fd220</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <dc:creator><![CDATA[Richard Liaw]]></dc:creator>
            <pubDate>Tue, 07 Apr 2020 15:45:23 GMT</pubDate>
            <atom:updated>2021-03-31T22:26:49.606Z</atom:updated>
            <content:encoded><![CDATA[<h3>Faster and Cheaper PyTorch with RaySGD</h3><h4>Distributed training is annoying to set up and expensive to run. Here’s <a href="https://ray.readthedocs.io/en/latest/raysgd/raysgd_pytorch.html">a library to make distributed Pytorch model training simple and cheap</a>.</h4><p>Distributing your deep learning model training has become a question of <strong>when</strong> you do it, not<strong> if</strong>. State-of-the-art ML models like <a href="https://arxiv.org/pdf/1810.04805.pdf">BERT</a> have 100s of millions of parameters, and training these large networks will take you days if not weeks on one machine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dBHiWXHUbdJWzo8Evg6DDw.png" /><figcaption>Language models are just getting bigger and bigger.</figcaption></figure><p>Fundamentally, you have <strong>two</strong> choices when it comes to training deep learning models:</p><p><strong>Option 1:</strong> Tolerate the 20 hour training times, or focus on models that are small enough to train on a single node (or single GPU) to keep things simple and be able to use standard tools like Jupyter Notebooks.</p><p><strong>Option 2:</strong> Go through a mountain of pain and try to distribute your training.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fEfFwYPf4fR6YTaV" /><figcaption>Photo by <a href="https://unsplash.com/@majatravels?utm_source=medium&amp;utm_medium=referral">Maja Kochanowska</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>So, what does it take to distribute training today?</h3><p>To take your training beyond a single node, you’re going to have to deal with:</p><ol><li>Messy distributed systems deployments (including setting up networking, containerization, credentials).</li><li>A huge AWS bill for expensive nodes (current solutions don’t allow you to use cheap, preemptible instances).</li><li>Losing access to your favorite tools such as Jupyter notebooks.</li></ol><p>You can use one of the integrated tools for doing distributed training like <a href="https://pytorch.org/tutorials/intermediate/ddp_tutorial.html">Torch Distributed Data Parallel</a> or <a href="https://www.tensorflow.org/guide/distributed_training">tf.Distributed</a>. While these are “integrated”, they are certainly not a walk in the park to use.</p><p><a href="https://pytorch.org/tutorials/beginner/aws_distributed_training_tutorial.html">Torch’s AWS tutorial</a> demonstrates the many setup steps you’re going to have to follow to simply get the cluster running, and Tensorflow 2.0 has <a href="https://github.com/tensorflow/tensorflow/issues?q=is%3Aissue+is%3Aopen+label%3Acomp%3Adist-strat">a bunch of issues</a>.</p><p>Maybe you might look at something like <a href="https://github.com/horovod/horovod">Horovod</a>, but Horovod is going to require you to fight against antiquated frameworks like MPI and wait a long time for compilation when you launch.</p><p>Along with this complex setup, you’re going to need to give up the typical tools that you use, like Jupyter notebooks. On top of that you’re going to have to use expensive on-demand instances because none of these frameworks are fault-tolerant.</p><p>In our own work, we identified these issues as roadblocks to simplifying distributed deep learning training. We set out to create our own solution that solves these key problems.</p><blockquote>So, what’s the better way?</blockquote><h3>RaySGD — the simple distributed training solution</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*UHI6GR-pOcb7Alxl" /></figure><p>To solve the problems above, we built <a href="https://ray.readthedocs.io/en/latest/raysgd/raysgd_pytorch.html">RaySGD</a>.</p><p>It’s a <a href="https://ray.readthedocs.io/en/latest/raysgd/raysgd_pytorch.html">lightweight Python library</a> built on top of distributed PyTorch that not only makes deployment <strong>easy,</strong> but it provides you with the <strong>flexibility</strong> you need to do cutting edge research and development.</p><p>Now, we’re sure you’re asking yourself,</p><blockquote>How is this better than what already exists?</blockquote><p>RaySGD focuses on several key benefits:</p><ul><li><strong>Seamless Parallelization</strong>: Go from 1 GPU to 100 GPUs with a single argument.</li><li><strong>Accelerate Training</strong>: Built-in support for mixed precision training with NVIDIA Apex.</li><li><strong>Simple, Native Interfaces</strong>: We’ve kept the interface simple to make it easy to migrate existing training code and keep the mental overhead low — just learn a couple of new lines of code.</li><li><strong>Fault Tolerance</strong>: Supports automatic recovery when machines on the cloud are preempted. Now, you can use spot instances to reduce costs by up to 90%.</li><li><strong>Seamless Hyperparameter Tuning</strong>: <a href="https://ray.readthedocs.io/en/latest/raysgd/raysgd_pytorch.html">RaySGD</a> integrates with <a href="https://ray.readthedocs.io/en/latest/tune.html">RayTune</a>, a cutting edge, distributed, hyperparameter tuning framework.</li></ul><p>On top of what we achieved above, <a href="https://ray.readthedocs.io/en/latest/raysgd/raysgd_pytorch.html">RaySGD</a> matches the performance of specialized, SOTA deep learning frameworks, like Horovod.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*SNk46S2--FW__4HO" /><figcaption>Comparing Horovod vs Ray (which uses Pytorch Distributed DataParallel underneath the hood) on p3dn.24xlarge instances. Horovod and Ray perform similarly across different scales.</figcaption></figure><p><a href="https://ray.readthedocs.io/en/latest/raysgd/raysgd_pytorch.html">RaySGD</a> is also able to outperform the default torch.nn.DataParallel by up to 20% on 8 GPUs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*2KZGI7h3kZV8VR9e" /><figcaption>Comparing PyTorch DataParallel vs Ray (which uses Pytorch Distributed DataParallel underneath the hood) on p3dn.24xlarge instances. Ray is able to scale better with and without mixed precision, with up to 20% faster performance on 8 GPUs.</figcaption></figure><p><a href="https://ray.readthedocs.io/en/latest/raysgd/raysgd_pytorch.html">RaySGD</a> is built on top of <a href="https://ray.io/">Ray</a>, a framework for fast and simple distributed computing. Training on spot or preemptible instances for huge cost savings is as simple as changing a single line of code: trainer.train(max_retries=100)</p><h3>How do I get started?</h3><p><a href="https://ray.readthedocs.io/en/latest/raysgd/raysgd_pytorch.html">RaySGD</a> provides a minimal API that gives a user the typical customizability they’re already familiar with from TensorFlow or PyTorch. Here’s the minimum you need to run a multi-GPU training job.</p><ol><li>pip install -U ray torch</li><li>Run the below script.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/36648b02724c30890abc9bea7fdd3622/href">https://medium.com/media/36648b02724c30890abc9bea7fdd3622/href</a></iframe><p>This simple script will download CIFAR10 and use a ResNet18 model to do image classification. You can run on multiple GPUs with a single parameter change ( num_workers=N ).</p><h3>OK then, how do I scale out PyTorch training across a cluster?</h3><p>Don’t worry, this is just 4 extra steps. I’ll demonstrate how to <a href="https://ray.readthedocs.io/en/latest/raysgd/raysgd_pytorch.html">run RaySGD</a> on AWS, but it’s just as easy to run this on SLURM, Azure, GCP, or your local cluster.</p><ol><li>Download the following YAML file and the previous python script (save it as pytorch.py).</li><li>Change ray.init() in the script to ray.init(address=&quot;auto&quot;)</li><li>Change num_workers=16 in the TorchTrainer constructor.</li><li>Run ray submit ray-cluster.yaml pytorch.py --start --stop . This automatically starts the preemptible cluster (16 total V100 GPUs), and shuts down the cluster as soon as training is finished. For 30 minutes, this will cost… $7.44.</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/086d16809247ac5ab205f82627feead9/href">https://medium.com/media/086d16809247ac5ab205f82627feead9/href</a></iframe><p>To run on GCP or Azure, simply change a couple lines in the above YAML — <a href="https://ray.readthedocs.io/en/latest/autoscaling.html">more instructions here</a>.</p><p>As you can see, getting up and running with RaySGD is simple — you’ve learned almost everything you need in this blog post. So now get started and let us know what you think!</p><ul><li><a href="https://ray.readthedocs.io/en/latest/raysgd/raysgd_pytorch.html">Documentation</a></li><li><a href="https://github.com/ray-project/ray/tree/master/python/ray/util/sgd/torch">Code</a></li><li>Examples for <a href="https://ray.readthedocs.io/en/latest/raysgd/raysgd_pytorch.html#torchtrainer-examples">customizing training</a></li><li>Examples for distributed <a href="https://ray.readthedocs.io/en/latest/raysgd/raysgd_pytorch.html#advanced-hyperparameter-tuning">hyperparameter tuning</a></li><li>Follow us on <a href="https://twitter.com/raydistributed">Twitter</a>!</li><li>Join our <a href="https://discuss.ray.io/">Discourse</a>!</li><li>Join our <a href="https://docs.google.com/forms/u/2/d/1447zOvIDD1x72ULkltN3KBHTAHUkVE4HVC5yO-WTcYs/edit">Slack</a>!</li><li>Join us at <a href="http://anyscale.com/ray-summit">Ray Summit</a>!</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a5a44d4fd220" width="1" height="1" alt=""><hr><p><a href="https://medium.com/distributed-computing-with-ray/faster-and-cheaper-pytorch-with-raysgd-a5a44d4fd220">Faster and Cheaper Pytorch with RaySGD</a> was originally published in <a href="https://medium.com/distributed-computing-with-ray">Distributed Computing with Ray</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Scaling Experiments at Berkeley AI Research]]></title>
            <link>https://medium.com/riselab/scaling-experiments-at-berkeley-ai-research-2dfdf41cc7f2?source=rss-842dc7bf293f------2</link>
            <guid isPermaLink="false">https://medium.com/p/2dfdf41cc7f2</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[artificial-intelligence]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <dc:creator><![CDATA[Richard Liaw]]></dc:creator>
            <pubDate>Tue, 21 Jan 2020 21:45:07 GMT</pubDate>
            <atom:updated>2020-01-21T21:45:07.486Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Originally </em><a href="https://bair.berkeley.edu/blog/2020/01/16/tune/"><em>posted on the BAIR Blog</em></a><em>.</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/704/0*-ivxiOHwFGeT8oQJ.png" /></figure><p>As AI research becomes more compute intensive, many AI researchers have become squeezed for time and resources. Many researchers now rely on cloud providers like Amazon Web Services or Google Compute Platform to access the huge amounts of computational resources necessary for training large models.</p><p>In this blog post, we share our experiences in developing two critical software libraries that many <a href="https://bair.berkeley.edu/">BAIR</a> researchers use to execute large-scale AI experiments: <a href="https://ray.readthedocs.io/en/latest/tune.html">Ray Tune</a> and the <a href="https://ray.readthedocs.io/en/latest/autoscaling.html">Ray Cluster Launcher</a>, both of which now back many popular open-source AI libraries.</p><h3>Understanding research infrastructure</h3><p>To put things into perspective, let’s first take a look at a standard machine learning workflow in industry (Figure 1).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/0*_bVmi9jbiU3vKjS6.png" /></figure><p><em>Figure 1 represents the typical ML model development workflow in </em><strong><em>industry</em></strong><em>.</em></p><p>The typical <em>research</em> workflow is actually a tight loop between steps 2 and 3, generalizing to something like Figure 2.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/673/0*owMcuCGvlwxcctNs.png" /></figure><p><em>Figure 2 represents the typical ML model development workflow for </em><strong><em>research</em></strong><em>. The research workflow is typically a subsection of the industry workflow.</em></p><p>The research workflow is very much an iterative process and is often bottlenecked by the <em>experiment execution</em> step (Figure 2, B). Typically, an “experiment” consists of multiple training jobs, or “trials”, where each trial is a job that trains a single model. Each trial might train a model using a different set of configuration parameters (hyperparameters) or a different seed.</p><p>At Berkeley, we saw that AI researchers moving to the cloud spent a lot of time writing their own experiment execution tools that wrangled cloud-provider APIs for starting instances, setting up dependencies, and launching experiments.</p><p>Unfortunately, despite the vast amounts of time poured into developing these tools, these ad-hoc solutions are typically limited in functionality:</p><ul><li><strong>Simplistic Architecture</strong>: Each trial is typically launched on a separate node without any centralized control logic. This makes it difficult for researchers to implement optimization techniques such as <a href="https://deepmind.com/blog/article/population-based-training-neural-networks">Population-based Training</a> or <a href="https://github.com/fmfn/BayesianOptimization">Bayesian Optimization</a> that require coordination between different runs.</li><li><strong>Lack of Fault Handling</strong>: The results of training jobs are lost forever if an instance fails. Researchers often manually manage the failover by tracking live experiments on a spreadsheet, but this is both time consuming and error-prone.</li><li><strong>No Spot Instance Discount</strong>: The lack of fault tolerance capabilities also means forgoing the spot instance discount (<a href="https://aws.amazon.com/ec2/spot/">up to 90</a>%) that cloud providers offer.</li></ul><p>To sum up, instrumenting and managing distributed experiments on cloud resources is both burdensome and difficult to get right. As such, easy-to-use frameworks that bridge the gap between execution and research can greatly accelerate the research process. Members from a couple of labs across BAIR collaborated to build two complementary tools for AI experimentation in the cloud:</p><p><a href="https://ray.readthedocs.io/en/latest/tune.html">Ray Tune</a>: a fault-tolerant framework for training and hyperparameter tuning. Specifically, Ray Tune (or “Tune” for short):</p><ol><li>Coordinates among parallel jobs to enable <strong>parallel hyperparameter optimization</strong>.</li><li><strong>Automatically checkpoints and resumes</strong> training jobs in case of machine failures.</li><li>Offers many state-of-the-art hyperparameter search algorithms such as <a href="https://ray.readthedocs.io/en/latest/tune-schedulers.html">Population-based Training and HyperBand</a>.</li></ol><p><a href="https://ray.readthedocs.io/en/latest/autoscaling.html">Ray Cluster Launcher</a>: a utility for managing resource provisioning and cluster configurations across AWS, GCP, and Kubernetes.</p><h3>Ray Tune</h3><p>Tune was built to address the shortcomings of these ad-hoc experiment execution tools. This was done by leveraging the <strong>Ray Actor API</strong> and adding <strong>failure handling</strong>.</p><h4>Actor-based Training</h4><p>Many techniques for hyperparameter optimization require a framework that monitors the metrics of all concurrent training jobs and controls the training execution. To address this, Tune uses a master-worker architecture to centralize decision-making and communicates with its distributed workers using the Ray Actor API.</p><p><em>What is the Ray Actor API?</em> Ray <a href="https://ray.readthedocs.io/en/latest/actors.html">provides an API</a> to create an “actor” from a Python class. This enables classes and objects to be used in parallel and distributed settings.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*GO4Zwa-Wy-nQbpsq.png" /></figure><p>Tune uses a <a href="https://ray.readthedocs.io/en/latest/tune-usage.html#tune-training-api"><em>Trainable</em></a> class interface to define an actor class specifically for training models. This interface exposes methods such as _train, _stop, _save, and _restore, which allows Tune to monitor intermediate training metrics and kill low-performing trials.</p><pre>class NewTrainable(tune.Trainable):<br>    def _setup(self, config):<br>        ...</pre><pre>    def _train(self):<br>        &quot;&quot;&quot;Run 1 step of training (e.g., one epoch).</pre><pre>        Returns:<br>            A dict of training metrics.<br>        &quot;&quot;&quot;<br>        ...</pre><pre>    def _save(self, checkpoint_dir):<br>        ...</pre><pre>    def _restore(self, checkpoint_path):<br>        ...</pre><p>More importantly, by leveraging the Actor API, we can implement parallel hyperparameter optimization methods in Tune such as <a href="https://ray.readthedocs.io/en/latest/tune-schedulers.html#asynchronous-hyperband">HyperBand</a> and Parallel Bayesian Optimization which was not possible with prior experiment execution tools that researchers used.</p><h4>Fault Tolerance</h4><p>Cloud providers often offer “preemptible instances” (i.e., spot instances) at a significant discount. The steep discounts allow researchers to lower their cloud computing costs significantly. However, the downside is that the cloud provider can terminate or stop your machine at any time, causing you to lose training progress.</p><p>To enable spot instance usage, we built Tune to automatically checkpoint and resume training jobs across different machines in the cluster so that experiments would be resilient to preemption and cluster resizing.</p><pre><em># Tune will resume training jobs from the last checkpoint</em><br><em># even if machines are removed.</em></pre><pre>analysis = tune.run(<br>    NewTrainable,<br>    checkpoint_freq=5,  <em># Checkpoint every 5 epochs</em><br>    config={&quot;lr&quot;: tune.grid_search([0.001, 0.01, 0.1])},<br>)</pre><h3>How does it work?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/972/0*hK-TEWj2kRYzq9vr.jpg" /></figure><p>If a node is lost while a training job is still executing on that node and a checkpoint of the training job (trial) exists, Tune will wait until available resources are available to begin executing the trial again.</p><p>If the trial is placed on a different node, Tune will automatically push the previous checkpoint file to that node and restore the state, allowing the trial to resume from the latest checkpoint <em>even after failure</em>.</p><h3>Ray Cluster Launcher</h3><p>Above, we described the pains of wrangling cloud-provider APIs to automate the cluster setup process. However, even with a tool for spinning up clusters, researchers still have to go through a tedious workflow to run experiments:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/843/0*xKTPAuAjaNyll37w.png" /></figure><p>To simplify this, we built the Ray Cluster Launcher, a tool <a href="https://ray.readthedocs.io/en/latest/autoscaling.html">for provisioning and autoscaling resources</a> and starting a Ray cluster on AWS EC2, GCP, and Kubernetes. We then abstracted all of the above steps for running an experiment into <em>just a short configuration file and a single command</em>:</p><pre><em># CLUSTER.yaml</em><br>cluster_name: tune-default<br>provider: {type: aws, region: us-west-2}<br>auth: {ssh_user: ubuntu}<br>min_workers: 0<br>max_workers: 2<br><em># Deep Learning AMI (Ubuntu) Version 21.0</em><br>head_node: {<br>    InstanceType: c4.2xlarge,<br>    ImageId: ami-0b294f219d14e6a82}<br>worker_nodes: {<br>    InstanceType: c4.2xlarge,<br>    ImageId: ami-0b294f219d14e6a82}<br>setup_commands: <em># Set up each node.</em><br>    - pip install ray numpy pandas<br>file_mounts: {<br>  &#39;/home/ubuntu/files&#39;:&#39;my_files/&#39;,<br>}</pre><p>The below command starts a cluster, uploads and runs a script for distributed hyperparameter tuning, then shuts down the cluster.</p><pre>$ ray submit CLUSTER.yaml --start --stop tune_experiment.py \<br>     --args=&quot;--address=auto&quot;</pre><p>Researchers are now using both Ray Tune and the Ray Cluster Launcher to launch hundreds of parallel jobs at once across dozens of GPU machines at once. <a href="https://ray.readthedocs.io/en/latest/tune-distributed.html">The Ray Tune documentation page for distributed experiments</a> shows you how you can do this too.</p><h3>Putting things together</h3><p>Over the last year, we’ve been working with different groups across BAIR to better allow researchers to leverage the cloud. We had to make Ray Tune and the Ray Cluster Launcher general enough to support a large body of research codebases while making the barrier to entry so low that anyone could try it out in a couple of minutes.</p><pre><em># An example Ray Tune script for PyTorch.</em><br>import torch.optim as optim<br>from ray import tune<br>from ray.tune.examples.mnist_pytorch import (<br>    get_data_loaders, ConvNet, train, test)</pre><pre>class TrainMNIST(tune.Trainable):<br>    def _setup(self, config):<br>        self.train_loader, self.test_loader = get_data_loaders()<br>        self.model = ConvNet()<br>        self.optimizer = optim.SGD(<br>            self.model.parameters(), lr=config.get(&quot;lr&quot;, 0.01))</pre><pre>    def _train(self):<br>        train(self.model, self.optimizer, self.train_loader)<br>        acc = test(self.model, self.test_loader)<br>        return {&quot;mean_accuracy&quot;: acc}</pre><pre>    def _save(self, checkpoint_dir):<br>        checkpoint_path = os.path.join(checkpoint_dir, &quot;model.pth&quot;)<br>        torch.save(self.model.state_dict(), checkpoint_path)<br>        return checkpoint_path</pre><pre>    def _restore(self, checkpoint_path):<br>        self.model.load_state_dict(torch.load(checkpoint_path))</pre><pre>analysis = tune.run(<br>    TrainMNIST,<br>    stop={&quot;training_iteration&quot;: 50},<br>    config={&quot;lr&quot;: tune.grid_search([0.001, 0.01, 0.1])})</pre><pre>print(&quot;Best hyperparameters: &quot;, analysis.get_best_config(<br>    metric=&quot;mean_accuracy&quot;))<br><em># Get a dataframe for analyzing trial results.</em><br>df = analysis.dataframe()</pre><p>Tune has grown to become a popular open-source project for hyperparameter tuning. It is used in many other popular research projects, ranging from <a href="https://github.com/arcelien/pba">Population-based Data Augmentation</a>, to <a href="https://github.com/allenai/allentune">Hyperparameter Tuning for AllenNLP</a>, to <a href="https://medium.com/riselab/scalable-automl-for-time-series-prediction-using-ray-and-analytics-zoo-b79a6fd08139">AutoML in AnalyticsZoo</a>.</p><p>Numerous open-source research projects from BAIR now rely on the combination of Ray Tune and the Ray Cluster Launcher to orchestrate and execute distributed experiments, including <a href="https://github.com/rail-berkeley/softlearning">rail-berkeley/softlearning</a>, <a href="https://github.com/HumanCompatibleAI/adversarial-policies">HumanCompatibleAI/adversarial-policies</a>, and <a href="https://github.com/flow-project/flow">flow-project/flow</a>.</p><p>So go ahead and <a href="https://ray.readthedocs.io/en/latest/tune-distributed.html">try Ray Tune and Cluster Launcher yourself</a>!</p><h3>Links</h3><ul><li>Previous BAIR blog posts about the Ray project:</li><li><a href="https://bair.berkeley.edu/blog/2018/01/09/ray/">Ray: A Distributed System for AI</a></li><li><a href="https://bair.berkeley.edu/blog/2018/12/12/rllib/">Scaling Multi-Agent Reinforcement Learning</a></li><li><a href="https://bair.berkeley.edu/blog/2019/10/14/functional-rl/">Functional RL with Keras and Tensorflow Eager</a></li><li><a href="https://ray.io/">Ray website</a></li><li><a href="https://github.com/ray-project/ray">Ray GitHub page</a></li><li><a href="https://ray.readthedocs.io/en/latest/tune.html">Tune</a></li><li><a href="https://ray.readthedocs.io/en/latest/autoscaling.html">Ray Cluster Launcher</a></li><li><a href="https://ray.readthedocs.io/en/latest/">Ray-project landing page</a></li><li><a href="https://ray.readthedocs.io/en/latest/installation.html">Installation</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2dfdf41cc7f2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/riselab/scaling-experiments-at-berkeley-ai-research-2dfdf41cc7f2">Scaling Experiments at Berkeley AI Research</a> was originally published in <a href="https://medium.com/riselab">riselab</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cutting edge hyperparameter tuning with Ray Tune]]></title>
            <link>https://medium.com/riselab/cutting-edge-hyperparameter-tuning-with-ray-tune-be6c0447afdf?source=rss-842dc7bf293f------2</link>
            <guid isPermaLink="false">https://medium.com/p/be6c0447afdf</guid>
            <category><![CDATA[tools]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[data]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Richard Liaw]]></dc:creator>
            <pubDate>Tue, 20 Aug 2019 01:26:52 GMT</pubDate>
            <atom:updated>2019-08-28T21:22:09.139Z</atom:updated>
            <content:encoded><![CDATA[<h4><a href="https://ray.readthedocs.io/en/latest/tune.html">Ray Tune is a Python library that accelerates hyperparameter tuning</a> by allowing you to leverage cutting edge optimization algorithms at scale.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GsJLYcS5W2tCfHg4NDOscA.png" /></figure><p>Behind most of the major flashy results in machine learning is a graduate student (me) or engineer spending hours training a model and tuning algorithm parameters. This is the tedious boring work that makes the headlines possible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*L3E67jSnLTCxy2ks" /></figure><p>Here in the <a href="https://rise.cs.berkeley.edu/">RISELab</a>, we’re finding it more and more necessary to leverage cutting-edge hyperparameter tuning tools to keep up with the state of the art. Advancements in deep learning performance are becoming more and more dependent on newer and better hyperparameter tuning algorithms such as <strong>Population Based Training</strong> (<strong>PBT</strong>), HyperBand, and ASHA.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Cp7gPWoQQzIgc184" /><figcaption>Population-based Training improves DeepMind’s algorithms on many domains by significant margins. Source: <a href="https://deepmind.com/blog/population-based-training-neural-networks/">https://deepmind.com/blog/population-based-training-neural-networks/</a></figcaption></figure><p>These algorithms provide two critical benefits:</p><ul><li><strong>They maximize model performance</strong>: e.g., DeepMind uses PBT to <a href="https://www.reddit.com/r/MachineLearning/comments/ajgzoc/we_are_oriol_vinyals_and_david_silver_from/">achieve superhuman performance on StarCraft</a>; Waymo uses <a href="https://www.technologyreview.com/s/614004/deepmind-is-helping-waymo-evolve-better-self-driving-ai-algorithms/">PBT to enable self-driving cars</a>.</li><li><strong>They minimize training costs</strong>: <a href="https://determined.ai/blog/addressing-challenges-parallel-hyperparameter-optimization/">HyperBand and ASHA converge to high-quality configurations</a> in half the time taken by previous approaches; <a href="https://arxiv.org/abs/1905.05393">population-based data augmentation algorithms</a> cut costs by orders of magnitude.</li></ul><p>Yet, we see that a vast majority of researchers and teams do not leverage such algorithms.</p><p>Why? Well, most existing hyperparameter search frameworks do not have these newer optimization algorithms. And once you reach a certain scale, most existing solutions for parallel hyperparameter search can be a hassle to use — you’ll need to configure each machine for each run and often manage a separate database.</p><p>Practically speaking, <strong>implementing and maintaining these algorithms requires a significant amount of time and engineering.</strong></p><p><em>But it doesn’t need to be this way. </em>We believe there’s no reason why hyperparameter tuning at scale needs to be this hard. All AI researchers and engineers should be able to seamlessly run a parallel asynchronous grid search across 8 GPUs and even scale out to leverage Population Based Training or any Bayesian optimization algorithm on the cloud.</p><p>In this blog post, we’ll introduce <a href="https://ray.readthedocs.io/en/latest/tune.html">Tune, a powerful hyperparameter tuning library</a> built on <a href="https://github.com/ray-project/ray">Ray</a> designed to remove the friction from scaling and setting up experiment execution and hyperparameter tuning.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*EZKV8RTgDt0NfL49" /><figcaption>Tune scales your training from a single machine to a large distributed cluster without changing your code.</figcaption></figure><h4>Tune is a powerful Python library that accelerates hyperparameter tuning. Here are some core features:</h4><ul><li>Tune provides distributed asynchronous optimization out of the box by <a href="https://github.com/ray-project/ray">leveraging Ray</a>.</li><li>You can scale a hyperparameter search from a single machine to a large distributed cluster without changing your code.</li><li>Tune offers state of the art algorithms including (but not limited to) <a href="https://ray.readthedocs.io/en/latest/tune-schedulers.html#asynchronous-hyperband">ASHA</a>,<a href="https://ray.readthedocs.io/en/latest/tune-searchalg.html#bohb"> BOHB</a>, and <a href="https://ray.readthedocs.io/en/latest/tune-schedulers.html#population-based-training-pbt">Population-Based Training</a>.</li><li>Automatically visualize results with <a href="https://www.tensorflow.org/get_started/summaries_and_tensorboard">TensorBoard</a> or <a href="https://github.com/mlflow/mlflow">MLFlow</a>.</li><li>Tune integrates with many optimization libraries such as<a href="http://ax.dev/"> Ax/Botorch</a>,<a href="https://github.com/hyperopt/hyperopt"> HyperOpt</a>, and <a href="https://github.com/fmfn/BayesianOptimization">Bayesian Optimization</a> and enables you to scale them transparently.</li><li>Tune supports any machine learning framework, including PyTorch, TensorFlow, XGBoost, LightGBM, and Keras.</li></ul><p>Beyond Tune’s core features, there are two primary reasons why researchers and developers prefer Tune over other existing hyperparameter tuning frameworks: <strong>scale</strong> and <strong>flexibility</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mxyRF_di4Bt7BX9-" /><figcaption>Note for Search Algorithms: as of 8/12/2019, HpBandSter supports HyperBand, Random Search, and BOHB. KerasTuner supports Random Search, HyperBand, and Bayesian Optimization. Optuna supports Median (Percentile) Stopping, ASHA, Random Search, and Bayesian Optimization (TPE). HyperOpt supports Bayesian Optimization and Random Search. Tune supports PBT, BOHB, ASHA, HyperBand, Median Stopping, Random Search, Bayesian Optimization (TPE, etc), and numerous others due to library integrations.</figcaption></figure><h4>Tune simplifies scaling.</h4><p>Leverage all of the cores and GPUs on your machine to perform parallel asynchronous hyperparameter tuning by adding fewer than 10 lines of Python.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f0bb84a19d3022af93049ba131aa798/href">https://medium.com/media/0f0bb84a19d3022af93049ba131aa798/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*egs-KDkpylsuM0LNnDX36A.png" /><figcaption><a href="https://twitter.com/MarcCoru/status/1080596327006945281">https://twitter.com/MarcCoru/status/1080596327006945281</a></figcaption></figure><p>With another configuration file and 4 lines of code, launch a massive distributed hyperparameter tuning cluster on the cloud and automatically shut down the machines (we’ll show you how to do this below).</p><p>With Tune’s built-in fault tolerance, trial migration, and cluster autoscaling, you can safely leverage spot (preemptible) instances and reduce cloud costs by <a href="https://aws.amazon.com/ec2/spot/">up to 90%.</a></p><h4>Tune is flexible.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TIpM56EvejedgqkoLTaGEw.png" /></figure><p>Tune integrates seamlessly with experiment management tools such as <a href="https://ray.readthedocs.io/en/latest/tune-usage.html#mlflow">MLFlow</a> and <a href="https://ray.readthedocs.io/en/latest/tune-usage.html#visualizing-results">TensorBoard</a>.</p><p>Tune provides a <a href="https://ray.readthedocs.io/en/latest/tune-searchalg.html#contributing-a-new-algorithm">flexible interface for optimization</a> algorithms, allowing you to easily implement and scale new optimization algorithms.</p><p>You can use Tune to leverage and scale many cutting edge optimization algorithms and libraries such as <a href="https://github.com/hyperopt/hyperopt">HyperOpt</a> (below) and <a href="http://ax.dev/">Ax</a> without modifying any model training code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a623874508766cd0e0f6bb6a6ad2470/href">https://medium.com/media/6a623874508766cd0e0f6bb6a6ad2470/href</a></iframe><h3>Using Tune is Easy!</h3><p>Let’s now dive into a concrete example that shows how you to leverage a popular early stopping algorithm (ASHA). We will start by running an example hyperparameter tuning script with Tune across all of the cores on your workstation. We’ll then scale out the same hyperparameter tuning experiment on the cloud with about 10 lines of code using <a href="https://github.com/ray-project/ray">Ray</a>.</p><p>You can download <a href="https://gist.github.com/richardliaw/c66357d057e24ca8c285a811d4a485d7">a full version of the hyperparameter tuning code in this blog here</a> (<a href="https://gist.github.com/richardliaw/e07b420663bde594107a4c17c2680aa0">distributed experiment configuration here</a>).</p><p>We’ll be using PyTorch in this example, but we also have examples for <a href="https://ray.readthedocs.io/en/latest/tune-examples.html">Tensorflow and Keras available.</a></p><p>Tune is packaged as part of <a href="https://ray.readthedocs.io/en/latest/installation.html">Ray</a>. To run this example, you will need to install the following: pip install ray torch torchvision</p><p>We first run some imports :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0f350998355a77584ed89b65146577d/href">https://medium.com/media/a0f350998355a77584ed89b65146577d/href</a></iframe><p>Let’s write a neural network with PyTorch:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/224a7c1cd1e079c575444c2422a01b6c/href">https://medium.com/media/224a7c1cd1e079c575444c2422a01b6c/href</a></iframe><p>To start using Tune, add a simple logging statement to the PyTorch training below function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5c996bec079b5eead32bdab57df1be2/href">https://medium.com/media/c5c996bec079b5eead32bdab57df1be2/href</a></iframe><p>Notice that there’s a couple helper functions in the above training script; you can see <a href="https://github.com/ray-project/ray/blob/master/python/ray/tune/examples/mnist_pytorch.py">their definitions here.</a></p><h4>Running Tune</h4><p>Let’s run 1 trial, randomly sampling from a uniform distribution for learning rate and momentum.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63e2d8e19eca854751ad291ae6737db4/href">https://medium.com/media/63e2d8e19eca854751ad291ae6737db4/href</a></iframe><p>Now, you’ve run your first Tune run! You can easily enable GPU usage by specifying GPU resources — <a href="https://ray.readthedocs.io/en/latest/tune-usage.html#using-gpus-resource-allocation">see the documentation</a> for more details. We can then plot the performance of this trial (requires matplotlib).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c0f652b823d481eccc905474677be1d/href">https://medium.com/media/2c0f652b823d481eccc905474677be1d/href</a></iframe><h4>Parallel execution and early stopping</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*ex13kSs6cKmDAIkp" /><figcaption>Early stopping with ASHA.</figcaption></figure><p>Let’s integrate ASHA, a scalable algorithm for early stopping (<a href="https://blog.ml.cmu.edu/2018/12/12/massively-parallel-hyperparameter-optimization/">blog post</a> and <a href="https://arxiv.org/abs/1810.05934">paper</a>). ASHA terminates trials that are less promising and allocates more time and resources to more promising trials.</p><p>Parallelize your search across all available cores on your machine with num_samples (extra trials will be queued).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61f4f5349047149b4eca88306f5033b5/href">https://medium.com/media/61f4f5349047149b4eca88306f5033b5/href</a></iframe><p>You can use the same DataFrame plotting as the previous example. After running, if Tensorboard is installed, you can also use Tensorboard for visualizing results: tensorboard --logdir ~/ray_results</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JMYx6TJEYFjETK0ELwhnMA.png" /></figure><h3>Going distributed</h3><p>Setting up a distributed hyperparameter search is often too much work. Tune and Ray make this seamless.</p><h4>Launching the cloud with a simple configuration file</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SxNdcQlY8M7eR-jT" /><figcaption>Launch a cluster and distribute hyperparameter search without changing your code</figcaption></figure><p>First, we’ll create a YAML file which configures a Ray cluster. As part of <a href="http://ray.readthedocs.io/en/latest/">Ray</a>, Tune interoperates very cleanly with the <a href="https://ray.readthedocs.io/en/latest/autoscaling.html">Ray cluster launcher</a>. The same commands shown below will work on GCP, AWS, and <a href="https://ray.readthedocs.io/en/latest/autoscaling.html#quick-start-private-cluster">local private clusters</a>. We’ll use 3 worker nodes in addition to a head node, so we should have a total of 32 vCPUs on the cluster — allowing us to evaluate 32 hyperparameter configurations in parallel.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8239a0a708e0479a803051474d2c2d23/href">https://medium.com/media/8239a0a708e0479a803051474d2c2d23/href</a></iframe><h4>Putting things together</h4><p>To distribute your hyperparameter search across the Ray cluster, you’ll need to append this to the top of your script:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a2eb5fddc86e869b05518f1e09980c6/href">https://medium.com/media/8a2eb5fddc86e869b05518f1e09980c6/href</a></iframe><p>Given the large increase in compute, we should be able to increase our search space and number of samples in our search space:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/845200c18350e3604f977044b17b2c56/href">https://medium.com/media/845200c18350e3604f977044b17b2c56/href</a></iframe><p>You can download <a href="https://gist.github.com/richardliaw/c66357d057e24ca8c285a811d4a485d7">a full version of the script in this blog here</a> (as tune_script.py).</p><h4>Launching your experiment</h4><p>To launch your experiment, you can run (assuming your code so far is in a file tune_script.py):</p><pre>$ ray submit tune-default.yaml tune_script.py --start \<br>     --args=”localhost:6379”</pre><p>This will launch your cluster on AWS, upload tune_script.py onto the head node, and run python tune_script localhost:6379, which is a port opened by Ray to enable distributed execution.</p><p>All of the output of your script will show up on your console. Note that the cluster will setup the head node first before any of the worker nodes, so at first you may see only 4 CPUs available. After some time, you can see 24 trials being executed in parallel, and the other trials will be queued up to be executed as soon as a trial is free.</p><p>To shut down your cluster, you can run:</p><pre>$ ray down tune-default.yaml</pre><p>And you’re done 🎉!</p><h3>Learn more:</h3><p>Tune has numerous other features that enable researchers and practitioners to accelerate their development. Other Tune features not covered in this blogpost include:</p><ul><li>A simple API for <a href="https://ray.readthedocs.io/en/latest/tune-distributed.html#pre-emptible-instances-cloud">running distributed fault-tolerant experiments</a></li><li>Distributed Hyperparameter search over <a href="https://ray.readthedocs.io/en/latest/distributed_training.html">Distributed Data Parallel training</a> for PyTorch</li><li><a href="https://ray.readthedocs.io/en/latest/tune-schedulers.html#population-based-training-pbt">Population-based Training</a></li></ul><p>For users that have access to the cloud, Tune and Ray provide a number of utilities that enable a seamless transition between development on your laptop and execution on the cloud. The <a href="https://ray.readthedocs.io/en/latest/tune-distributed.html#common-commands">documentation</a> includes:</p><ul><li>running the experiment in a background session</li><li>submitting trials to an existing experiment</li><li>visualizing all results of a distributed experiment in TensorBoard.</li></ul><blockquote>Tune is designed to scale experiment execution and hyperparameter search with ease. If you have any comments or suggestions or are interested in contributing to Tune, you can <a href="https://github.com/richardliaw">reach out to me</a> or the <a href="https://groups.google.com/forum/#!forum/ray-dev">ray-dev mailing list</a>.</blockquote><blockquote>Code: <a href="https://github.com/ray-project/ray/tree/master/python/ray/tune">https://github.com/ray-project/ray/tree/master/python/ray/tune</a><br>Docs: <a href="http://ray.readthedocs.io/en/latest/tune.html">http://ray.readthedocs.io/en/latest/tune.html</a></blockquote><p><em>Thanks to Allan Peng, Eric Liang, Joey Gonzalez, Ion Stoica, Eugene Vinitsky, Lisa Dunlap, Philipp Moritz, Andrew Tan, Alvin Wan, Daniel Rothchild, Brijen Thananjeyan, Alok Singh, Robert Nishihara (and maybe others?) for reading through various versions of this blog post!</em></p><p><em>Originally published at </em><a href="https://towardsdatascience.com/fast-hyperparameter-tuning-at-scale-d428223b081c"><em>https://towardsdatascience.com</em></a><em> on August 20, 2019.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=be6c0447afdf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/riselab/cutting-edge-hyperparameter-tuning-with-ray-tune-be6c0447afdf">Cutting edge hyperparameter tuning with Ray Tune</a> was originally published in <a href="https://medium.com/riselab">riselab</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ray Tune: a Python library for fast hyperparameter tuning at any scale]]></title>
            <link>https://medium.com/data-science/fast-hyperparameter-tuning-at-scale-d428223b081c?source=rss-842dc7bf293f------2</link>
            <guid isPermaLink="false">https://medium.com/p/d428223b081c</guid>
            <category><![CDATA[pytorch]]></category>
            <category><![CDATA[big-data]]></category>
            <category><![CDATA[towards-data-science]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[machine-learning]]></category>
            <dc:creator><![CDATA[Richard Liaw]]></dc:creator>
            <pubDate>Sun, 18 Aug 2019 01:48:38 GMT</pubDate>
            <atom:updated>2020-07-06T08:35:10.348Z</atom:updated>
            <content:encoded><![CDATA[<h4><em>Scale your search from laptop to hundreds of machines without changing your code. </em><a href="https://ray.readthedocs.io/en/latest/tune.html"><em>Check out Ray Tune</em></a><em>.</em></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bdnd8Y6fv8xeFqNuXvSeNA.png" /></figure><p>If you’ve ever tried to tune hyperparameters for a machine learning model, you know that it can be a very painful process. Simple approaches quickly become time-consuming.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*L3E67jSnLTCxy2ks" /></figure><p>And now more than ever, you <strong>absolutely need</strong> cutting-edge hyperparameter tuning tools to keep up with the state-of-the-art.</p><p>Model advancements are becoming more and more dependent on newer and better hyperparameter tuning algorithms such as <strong>Population Based Training</strong> (<strong>PBT</strong>), HyperBand, and ASHA.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Cp7gPWoQQzIgc184" /><figcaption>Population-based Training improves DeepMind’s state-of-the-art algorithms on many domains by significant margins. Source: <a href="https://deepmind.com/blog/population-based-training-neural-networks/">https://deepmind.com/blog/population-based-training-neural-networks/</a></figcaption></figure><p>These algorithms provide two critical benefits:</p><ul><li><strong>They maximize model performance</strong>: e.g., DeepMind uses PBT to <a href="https://www.reddit.com/r/MachineLearning/comments/ajgzoc/we_are_oriol_vinyals_and_david_silver_from/">achieve superhuman performance on StarCraft</a>; Waymo uses <a href="https://www.technologyreview.com/s/614004/deepmind-is-helping-waymo-evolve-better-self-driving-ai-algorithms/">PBT to enable self-driving cars</a>.</li><li><strong>They minimize training costs</strong>: <a href="https://determined.ai/blog/addressing-challenges-parallel-hyperparameter-optimization/">HyperBand and ASHA converge to high-quality configurations</a> in half the time taken by previous approaches; <a href="https://arxiv.org/abs/1905.05393">population-based data augmentation algorithms</a> cuts costs by orders of magnitude.</li></ul><p>The fact of the matter is that the vast majority of researchers and teams do not leverage such algorithms. Most existing hyperparameter search frameworks do not have these newer optimization algorithms. And once you reach a certain scale, most existing solutions for parallel hyperparameter search can be a hassle to use — you’ll need to configure each machine for each run and often manage a separate database.</p><p>Practically speaking, <strong>implementing and maintaining these algorithms requires a significant amount of time and engineering.</strong></p><p><em>But it doesn’t need to be this way. </em>There’s no reason why you can’t easily incorporate hyperparameter tuning into your machine learning project, seamlessly run a parallel asynchronous grid search on 8 GPUs in your cluster, and leverage Population Based Training or any Bayesian optimization algorithm at scale on the cloud.</p><p>In this blog post, we’ll introduce <a href="https://ray.readthedocs.io/en/latest/tune.html">RayTune, a powerful hyperparameter optimization library</a> designed to remove the friction from scaling experiment execution and hyperparameter search.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*EZKV8RTgDt0NfL49" /><figcaption>Tune scales your training from a single machine to a large distributed cluster without changing your code.</figcaption></figure><h4>RayTune is a powerful library that accelerates hyperparameter optimization. Here are some core features:</h4><ul><li>RayTune provides distributed asynchronous optimization out of the box.</li><li>RayTune offers state of the art algorithms including (but not limited to) <a href="https://ray.readthedocs.io/en/latest/tune-schedulers.html#asynchronous-hyperband">ASHA</a>,<a href="https://ray.readthedocs.io/en/latest/tune-searchalg.html#bohb"> BOHB</a>, and <a href="https://ray.readthedocs.io/en/latest/tune-schedulers.html#population-based-training-pbt">Population-Based Training</a>.</li><li>You can scale a RayTune hyperparameter search from a single machine to a large distributed cluster without changing your code.</li><li>RayTune integrates with many optimization libraries such as<a href="http://ax.dev/"> Ax/Botorch</a>,<a href="https://github.com/hyperopt/hyperopt"> HyperOpt</a>, and <a href="https://github.com/fmfn/BayesianOptimization">Bayesian Optimization</a> and enables you to scale them transparently.</li><li>RayTune supports any machine learning framework, including PyTorch, TensorFlow, XGBoost, LightGBM, scikit-learn, and Keras.</li></ul><p>Beyond RayTune’s core features, there are two primary reasons why researchers and developers prefer RayTune over other existing hyperparameter tuning frameworks: <strong>scale</strong> and <strong>flexibility</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mxyRF_di4Bt7BX9-" /><figcaption>Note for Search Algorithms: as of 8/12/2019, HpBandSter supports HyperBand, Random Search, and BOHB. KerasTuner supports Random Search, HyperBand, and Bayesian Optimization. Optuna supports Median (Percentile) Stopping, ASHA, Random Search, and Bayesian Optimization (TPE). HyperOpt supports Bayesian Optimization and Random Search. Tune supports PBT, BOHB, ASHA, HyperBand, Median Stopping, Random Search, Bayesian Optimization (TPE, etc), and numerous others due to library integrations.</figcaption></figure><h4>Tune simplifies scaling.</h4><p>Leverage all of the cores and GPUs on your machine to perform parallel asynchronous hyperparameter tuning by adding fewer than 10 lines of Python.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f0bb84a19d3022af93049ba131aa798/href">https://medium.com/media/0f0bb84a19d3022af93049ba131aa798/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*egs-KDkpylsuM0LNnDX36A.png" /><figcaption><a href="https://twitter.com/MarcCoru/status/1080596327006945281">https://twitter.com/MarcCoru/status/1080596327006945281</a></figcaption></figure><p>With another configuration file and 4 lines of code, launch a massive distributed hyperparameter search on the cloud and automatically shut down the machines (we’ll show you how to do this below).</p><p>With Tune’s built-in fault tolerance, trial migration, and cluster autoscaling, you can safely leverage spot (preemptible) instances and reduce cloud costs by <a href="https://aws.amazon.com/ec2/spot/">up to 90%.</a></p><h4>Tune is flexible.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TIpM56EvejedgqkoLTaGEw.png" /></figure><p>Tune integrates seamlessly with experiment management tools such as <a href="https://ray.readthedocs.io/en/latest/tune-usage.html#mlflow">MLFlow</a> and <a href="https://ray.readthedocs.io/en/latest/tune-usage.html#visualizing-results">TensorBoard</a>.</p><p>Tune provides a <a href="https://ray.readthedocs.io/en/latest/tune-searchalg.html#contributing-a-new-algorithm">flexible interface for optimization</a> algorithms, allowing you to easily implement and scale new optimization algorithms.</p><p>You can use Tune to leverage and scale many state-of-the-art search algorithms and libraries such as <a href="https://github.com/hyperopt/hyperopt">HyperOpt</a> (below) and <a href="http://ax.dev/">Ax</a> without modifying any model training code.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a623874508766cd0e0f6bb6a6ad2470/href">https://medium.com/media/6a623874508766cd0e0f6bb6a6ad2470/href</a></iframe><h3>Using Tune is Easy!</h3><p>Let’s now dive into a concrete example that shows how you to leverage a state-of-the-art early stopping algorithm (ASHA). We will start by running Tune across all of the cores on your workstation. We’ll then scale out the same experiment on the cloud with about 10 lines of code.</p><p>We’ll be using PyTorch in this example, but we also have examples for <a href="https://ray.readthedocs.io/en/latest/tune-examples.html">Tensorflow and Keras available.</a></p><p>Tune is installed as part of <a href="https://ray.readthedocs.io/en/latest/installation.html">Ray</a>. To run this example, you will need to install the following: pip install ray torch torchvision.</p><p>You can download <a href="https://gist.github.com/richardliaw/c66357d057e24ca8c285a811d4a485d7">a full version of the blog in this blog here</a>.</p><p>We first run some imports :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0f350998355a77584ed89b65146577d/href">https://medium.com/media/a0f350998355a77584ed89b65146577d/href</a></iframe><p>Let’s write a neural network with PyTorch:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/224a7c1cd1e079c575444c2422a01b6c/href">https://medium.com/media/224a7c1cd1e079c575444c2422a01b6c/href</a></iframe><p>To start using Tune, add a simple logging statement to the PyTorch training below function.</p><pre>def train_mnist(config):<br>    model = ConvNet()<br>    train_loader, test_loader = get_data_loaders()<br>    optimizer = optim.SGD(<br>        model.parameters(), <br>        lr=config[&quot;lr&quot;], <br>        momentum=config[&quot;momentum&quot;])<br>    for i in range(10):<br>        train(model, optimizer, train_loader, torch.device(&quot;cpu&quot;))<br>        acc = test(model, test_loader, torch.device(&quot;cpu&quot;))<br><strong>        tune.track.log(mean_accuracy=acc)</strong><br>        if i % 5 == 0:<br>            # This saves the model to the trial directory<br>            torch.save(model, &quot;./model.pth&quot;)</pre><p>Notice that there’s a couple helper functions in the above training script; you can see <a href="https://github.com/ray-project/ray/blob/master/python/ray/tune/examples/mnist_pytorch.py">their definitions here.</a></p><h4>Running Tune</h4><p>Let’s run 1 trial, randomly sampling from a uniform distribution for learning rate and momentum.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/63e2d8e19eca854751ad291ae6737db4/href">https://medium.com/media/63e2d8e19eca854751ad291ae6737db4/href</a></iframe><p>Now, you’ve run your first Tune run! You can easily enable GPU usage by specifying GPU resources — <a href="https://ray.readthedocs.io/en/latest/tune-usage.html#using-gpus-resource-allocation">see the documentation</a> for more details. We can then plot the performance of this trial.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c0f652b823d481eccc905474677be1d/href">https://medium.com/media/2c0f652b823d481eccc905474677be1d/href</a></iframe><h4>Parallel execution and early stopping</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*ex13kSs6cKmDAIkp" /><figcaption>Early stopping with ASHA.</figcaption></figure><p>Let’s integrate ASHA, a scalable algorithm for early stopping (<a href="https://blog.ml.cmu.edu/2018/12/12/massively-parallel-hyperparameter-optimization/">blog post</a> and <a href="https://arxiv.org/abs/1810.05934">paper</a>). ASHA terminates trials that are less promising and allocates more time and resources to more promising trials.</p><p>Parallelize your search across all available cores on your machine with num_samples (extra trials will be queued).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61f4f5349047149b4eca88306f5033b5/href">https://medium.com/media/61f4f5349047149b4eca88306f5033b5/href</a></iframe><p>You can use the same DataFrame plotting as the previous example. After running, if Tensorboard is installed, you can also use Tensorboard for visualizing results: tensorboard --logdir ~/ray_results</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JMYx6TJEYFjETK0ELwhnMA.png" /></figure><h3>Going distributed</h3><p>Setting up a distributed hyperparameter search is often too much work. Tune and Ray make this seamless.</p><h4>Launching the cloud with a simple configuration file</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SxNdcQlY8M7eR-jT" /><figcaption>Launch a cluster and distribute hyperparameter search without changing your code</figcaption></figure><p>First, we’ll create a YAML file which configures a Ray cluster. As part of <a href="http://ray.readthedocs.io/en/latest/">Ray</a>, Tune interoperates very cleanly with the <a href="https://ray.readthedocs.io/en/latest/autoscaling.html">Ray cluster launcher</a>. The same commands shown below will work on GCP, AWS, and <a href="https://ray.readthedocs.io/en/latest/autoscaling.html#quick-start-private-cluster">local private clusters</a>. We’ll use 3 worker nodes in addition to a head node, so we should have a total of 32 vCPUs on the cluster — allowing us to evaluate 32 hyperparameter configurations in parallel.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8239a0a708e0479a803051474d2c2d23/href">https://medium.com/media/8239a0a708e0479a803051474d2c2d23/href</a></iframe><h4>Putting things together</h4><p>To distribute your hyperparameter search across the Ray cluster, you’ll need to append this to the top of your script:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a2eb5fddc86e869b05518f1e09980c6/href">https://medium.com/media/8a2eb5fddc86e869b05518f1e09980c6/href</a></iframe><p>Given the large increase in compute, we should be able to increase our search space and number of samples in our search space:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/845200c18350e3604f977044b17b2c56/href">https://medium.com/media/845200c18350e3604f977044b17b2c56/href</a></iframe><p>You can download <a href="https://gist.github.com/richardliaw/c66357d057e24ca8c285a811d4a485d7">a full version of the script in this blog here</a> (as tune_script.py).</p><h4>Launching your experiment</h4><p>To launch your experiment, you can run (assuming your code so far is in a file tune_script.py):</p><pre>$ ray submit tune-default.yaml tune_script.py --start \<br>     --args=”localhost:6379”</pre><p>This will launch your cluster on AWS, upload tune_script.py onto the head node, and run python tune_script localhost:6379, which is a port opened by Ray to enable distributed execution.</p><p>All of the output of your script will show up on your console. Note that the cluster will setup the head node first before any of the worker nodes, so at first you may see only 4 CPUs available. After some time, you can see 24 trials being executed in parallel, and the other trials will be queued up to be executed as soon as a trial is free.</p><p>To shut down your cluster, you can run:</p><pre>$ ray down tune-default.yaml</pre><p>And you’re done 🎉!</p><h3>Learn more:</h3><p>Tune has numerous other features that enable researchers and practitioners to accelerate their development. Other Tune features not covered in this blogpost include:</p><ul><li>A simple API for <a href="https://ray.readthedocs.io/en/latest/tune-distributed.html#pre-emptible-instances-cloud">running distributed fault-tolerant experiments</a></li><li>Distributed Hyperparameter search over <a href="https://ray.readthedocs.io/en/latest/distributed_training.html">Distributed Data Parallel training</a> for PyTorch</li><li><a href="https://ray.readthedocs.io/en/latest/tune-schedulers.html#population-based-training-pbt">Population-based Training</a></li></ul><p>For users that have access to the cloud, Tune and Ray provide a number of utilities that enable a seamless transition between development on your laptop and execution on the cloud. The <a href="https://ray.readthedocs.io/en/latest/tune-distributed.html#common-commands">documentation</a> includes:</p><ul><li>running the experiment in a background session</li><li>submitting trials to an existing experiment</li><li>visualizing all results of a distributed experiment in TensorBoard.</li></ul><blockquote>Tune is designed to scale experiment execution and hyperparameter search with ease. If you have any comments or suggestions or are interested in contributing to Tune, you can <a href="https://github.com/richardliaw">reach out to me</a> or the <a href="https://groups.google.com/forum/#!forum/ray-dev">ray-dev mailing list</a>.</blockquote><blockquote>Code: <a href="https://github.com/ray-project/ray/tree/master/python/ray/tune">https://github.com/ray-project/ray/tree/master/python/ray/tune</a><br>Docs: <a href="http://ray.readthedocs.io/en/latest/tune.html">http://ray.readthedocs.io/en/latest/tune.html</a></blockquote><p>For other readings on hyperparameter tuning, check out Neptune.ai’s <a href="https://neptune.ai/blog/optuna-vs-hyperopt">blog post on Optuna vs HyperOpt</a>!</p><p><em>Thanks to Allan Peng, Eric Liang, Joey Gonzalez, Ion Stoica, Eugene Vinitsky, Lisa Dunlap, Philipp Moritz, Andrew Tan, Alvin Wan, Daniel Rothchild, Brijen Thananjeyan, Alok Singh (and maybe others?) for reading through various versions of this blog post!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d428223b081c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/fast-hyperparameter-tuning-at-scale-d428223b081c">Ray Tune: a Python library for fast hyperparameter tuning at any scale</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>