{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true, "pycharm": { "name": "#%% md\n" } }, "source": [ "# OPTaaS: Surrogate Visualization with Holoviews and Plotly\n", "\n", "If you have the holoviews and plotly libraries installed it is also possible to get an interactive 3d view of\n", "the surrogate predictions" ] }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "import numpy as np\n", "\n", "from mindfoundry.optaas.client.goal import Goal\n", "from mindfoundry.optaas.client.task import Task\n", "from mindfoundry.optaas.client.parameter import FloatParameter, CategoricalParameter\n", "from mindfoundry.optaas.client.viz import SurrogateViz\n", "\n", "## Connect to OPTaaS using your API Key\n", "from mindfoundry.optaas.client.client import OPTaaSClient\n", "client = OPTaaSClient('https://optaas.mindfoundry.ai', '')\n", "\n", "## Start the holoviews/plotly engine\n", "import holoviews as hv\n", "hv.extension('plotly')" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## Create a simple task optimizing a function with 3 continuous and one categorical parameter" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "task = client.create_task(\n", " title='Test',\n", " parameters=[\n", " FloatParameter('x', minimum=-2., maximum=2.),\n", " FloatParameter('y', minimum=-2., maximum=2.),\n", " FloatParameter('z', minimum=-2., maximum=2.),\n", " CategoricalParameter('c', values=[\"a\", \"b\"]),\n", " ],\n", " goal=Goal.min,\n", ")\n", "\n", "\n", "## Define the scoring function\n", "def scoring_function(x, y, z, c):\n", " rate = {\"a\": 1.5, \"b\": 0.75}[c] \n", " score = np.sin(rate*x*3) + np.cos(rate*y*3) + np.sin(z*3) + {\"a\": -0.5, \"b\": 0}[c]\n", " return score\n", "\n", "\n", "best_result = task.run(scoring_function, max_iterations=30)\n", "print(\"Best Result:\", best_result)" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## Make a visualizer object and visualize the Surrogate Mean and Variance in 'x' and 'z'" ], "metadata": { "collapsed": false } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "visualizer = SurrogateViz(client=client, task=task)\n", "visualizer.plot_surrogate_mean_and_std(\"x\",\"z\", fig_width=800, fig_height=700, z_range=(-3,3.))" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } }, { "cell_type": "markdown", "source": [ "## We can also make a view with the uncertainties overlaid above and below the mean" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%% md\n" } } }, { "cell_type": "code", "execution_count": null, "outputs": [], "source": [ "visualizer.plot_surrogate_with_uncertainties(\"x\",\"z\", fig_width=600, fig_height=700, z_range=(-4.,4.))" ], "metadata": { "collapsed": false, "pycharm": { "name": "#%%\n" } } } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.6" }, "pycharm": { "stem_cell": { "cell_type": "raw", "source": [], "metadata": { "collapsed": false } } } }, "nbformat": 4, "nbformat_minor": 0 }