Browse code

correctly calculate predicted model and error

Add a curve to the SKOptLearner plotter that shows the
predicted model. Update invocation of SKOptLearner in
example notebook to use better defaults.

Joseph Weston authored on 24/05/2018 14:59:20
Showing 2 changed files
... ...
@@ -62,28 +62,34 @@ class SKOptLearner(Optimizer, BaseLearner):
62 62
     def npoints(self):
63 63
         return len(self.Xi)
64 64
 
65
-    def plot(self):
65
+    def plot(self, nsamples=200):
66 66
         hv = ensure_holoviews()
67 67
         if self.space.n_dims > 1:
68 68
             raise ValueError('Can only plot 1D functions')
69 69
         bounds = self.space.bounds[0]
70 70
         if not self.Xi:
71
-            p = hv.Scatter([]) * hv.Area([])
71
+            p = hv.Scatter([]) * hv.Curve([]) * hv.Area([])
72 72
         else:
73 73
             scatter = hv.Scatter(([p[0] for p in self.Xi], self.yi))
74 74
             if self.models:
75
-                # Plot 95% confidence interval as colored area around points
76 75
                 model = self.models[-1]
77
-                xs = np.linspace(*bounds, 201)
78
-                y_pred, sigma = model.predict(np.atleast_2d(xs).transpose(),
79
-                                              return_std=True)
76
+                xs = np.linspace(*bounds, nsamples)
77
+                xsp = self.space.transform(xs.reshape(-1, 1).tolist())
78
+                y_pred, sigma = model.predict(xsp, return_std=True)
79
+                # Plot model prediction for function
80
+                curve = hv.Curve(
81
+                    (xs, y_pred)
82
+                ).opts(style=dict(line_dash='dashed'))
83
+                # Plot 95% confidence interval as colored area around points
80 84
                 area = hv.Area(
81 85
                     (xs, y_pred - 1.96 * sigma, y_pred + 1.96 * sigma),
82 86
                     vdims=['y', 'y2'],
83 87
                 ).opts(style=dict(alpha=0.5, line_alpha=0))
88
+
84 89
             else:
85 90
                 area = hv.Area([])
86
-            p = scatter * area
91
+                curve = hv.Curve([])
92
+            p = scatter * curve * area
87 93
 
88 94
         # Plot with 5% empty margins such that the boundary points are visible
89 95
         margin = 0.05 * (bounds[1] - bounds[0])
... ...
@@ -764,9 +764,11 @@
764 764
    "outputs": [],
765 765
    "source": [
766 766
     "learner = adaptive.SKOptLearner(g, dimensions=[(-2., 2.)],\n",
767
-    "                                base_estimator=\"ET\",\n",
768
-    "                                acq_optimizer=\"sampling\")\n",
769
-    "runner = adaptive.Runner(learner, goal=lambda l: l.loss() < 1)\n",
767
+    "                                base_estimator=\"GP\",\n",
768
+    "                                acq_func=\"gp_hedge\",\n",
769
+    "                                acq_optimizer=\"lbfgs\",\n",
770
+    "                               )\n",
771
+    "runner = adaptive.Runner(learner, ntasks=1, goal=lambda l: l.npoints > 40)\n",
770 772
     "runner.live_info()"
771 773
    ]
772 774
   },