Spaces:
Sleeping
Sleeping
Commit ·
32f3f07
1
Parent(s): fdd3bfb
Add slider to change the number of data points
Browse files- gp_visualizer.py +55 -18
gp_visualizer.py
CHANGED
|
@@ -84,10 +84,20 @@ def get_function(function, xlim=(-1, 1), nsample=100):
|
|
| 84 |
return x, y
|
| 85 |
|
| 86 |
|
| 87 |
-
def get_data_points(function, xlim=(-1, 1), nsample=10, sigma=0):
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
x = np.sort(x)
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
x = x.reshape(-1, 1)
|
| 92 |
return x, y
|
| 93 |
|
|
@@ -116,10 +126,16 @@ class GPVisualizer:
|
|
| 116 |
self.canvas_width = width
|
| 117 |
self.canvas_height = height
|
| 118 |
|
| 119 |
-
self.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
self.kernel = eval_kernel(self.DEFAULT_KERNEL)
|
| 121 |
|
| 122 |
-
self.x_train, self.y_train = self.generate_data(
|
| 123 |
self.model = self.train_model(self.kernel, self.x_train, self.y_train)
|
| 124 |
|
| 125 |
self.plot_options = {
|
|
@@ -136,8 +152,12 @@ class GPVisualizer:
|
|
| 136 |
display: none;
|
| 137 |
}"""
|
| 138 |
|
| 139 |
-
def generate_data(self
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
def train_model(self, kernel, x_train, y_train):
|
| 143 |
gpr = GaussianProcessRegressor(kernel=kernel, random_state=0)
|
|
@@ -156,7 +176,7 @@ class GPVisualizer:
|
|
| 156 |
ax = fig.add_axes([0., 0., 1., 1.]) #
|
| 157 |
ax.margins(x=0, y=0) # no padding in both directions
|
| 158 |
|
| 159 |
-
x_test, y_test = get_function(self.function, xlim=(-2, 2), nsample=100)
|
| 160 |
y_pred, y_std = self.model.predict(x_test, return_std=True)
|
| 161 |
|
| 162 |
# plot
|
|
@@ -232,18 +252,22 @@ class GPVisualizer:
|
|
| 232 |
)
|
| 233 |
return fig
|
| 234 |
|
| 235 |
-
def
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
x = np.linspace(-1, 1, 10)
|
| 239 |
-
y = numexpr.evaluate(function, local_dict={'x': x, **NUMEXPR_CONSTANTS})
|
| 240 |
-
except Exception as e:
|
| 241 |
-
raise ValueError(f"Invalid function: {e}")
|
| 242 |
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 244 |
|
| 245 |
# reset data and model
|
| 246 |
-
self.x_train, self.y_train = self.generate_data(
|
| 247 |
self.model = self.train_model(self.kernel, self.x_train, self.y_train)
|
| 248 |
|
| 249 |
return self.plot()
|
|
@@ -285,6 +309,14 @@ class GPVisualizer:
|
|
| 285 |
value=self.DEFAULT_FUNCTION,
|
| 286 |
interactive=True,
|
| 287 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
# upload data
|
| 290 |
file_chooser = gr.File(label="Choose a file", visible=False, elem_id="rowheight")
|
|
@@ -325,10 +357,15 @@ class GPVisualizer:
|
|
| 325 |
gr.Markdown(''.join(open('usage.md', 'r').readlines()))
|
| 326 |
|
| 327 |
function_box.submit(
|
| 328 |
-
fn=self.
|
| 329 |
inputs=function_box,
|
| 330 |
outputs=[self.canvas],
|
| 331 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
|
| 333 |
kernel_spec.submit(
|
| 334 |
fn=self.update_kernel_spec,
|
|
|
|
| 84 |
return x, y
|
| 85 |
|
| 86 |
|
| 87 |
+
def get_data_points(function, xlim=(-1, 1), nsample=10, sigma=0, seed=0):
|
| 88 |
+
num_points_to_generate = 100
|
| 89 |
+
if nsample > num_points_to_generate:
|
| 90 |
+
raise ValueError(f"nsample too large, limit to {num_points_to_generate}")
|
| 91 |
+
|
| 92 |
+
rng = np.random.default_rng(seed)
|
| 93 |
+
x = rng.uniform(xlim[0], xlim[1], size=num_points_to_generate)
|
| 94 |
+
x = x[:nsample]
|
| 95 |
x = np.sort(x)
|
| 96 |
+
|
| 97 |
+
rng = np.random.default_rng(seed)
|
| 98 |
+
noise = sigma * rng.standard_normal(nsample)
|
| 99 |
+
y = numexpr.evaluate(function, local_dict={'x': x, **NUMEXPR_CONSTANTS}) + noise
|
| 100 |
+
|
| 101 |
x = x.reshape(-1, 1)
|
| 102 |
return x, y
|
| 103 |
|
|
|
|
| 126 |
self.canvas_width = width
|
| 127 |
self.canvas_height = height
|
| 128 |
|
| 129 |
+
self.data_options = {
|
| 130 |
+
"function": self.DEFAULT_FUNCTION,
|
| 131 |
+
"nsample": 30,
|
| 132 |
+
"sigma": 0,
|
| 133 |
+
}
|
| 134 |
+
self._data_seed = 0
|
| 135 |
+
|
| 136 |
self.kernel = eval_kernel(self.DEFAULT_KERNEL)
|
| 137 |
|
| 138 |
+
self.x_train, self.y_train = self.generate_data()
|
| 139 |
self.model = self.train_model(self.kernel, self.x_train, self.y_train)
|
| 140 |
|
| 141 |
self.plot_options = {
|
|
|
|
| 152 |
display: none;
|
| 153 |
}"""
|
| 154 |
|
| 155 |
+
def generate_data(self):
|
| 156 |
+
function = self.data_options["function"]
|
| 157 |
+
nsample = self.data_options["nsample"]
|
| 158 |
+
sigma = self.data_options["sigma"]
|
| 159 |
+
|
| 160 |
+
return get_data_points(function, xlim=(-1, 1), nsample=nsample, sigma=sigma, seed=self._data_seed)
|
| 161 |
|
| 162 |
def train_model(self, kernel, x_train, y_train):
|
| 163 |
gpr = GaussianProcessRegressor(kernel=kernel, random_state=0)
|
|
|
|
| 176 |
ax = fig.add_axes([0., 0., 1., 1.]) #
|
| 177 |
ax.margins(x=0, y=0) # no padding in both directions
|
| 178 |
|
| 179 |
+
x_test, y_test = get_function(self.data_options["function"], xlim=(-2, 2), nsample=100)
|
| 180 |
y_pred, y_std = self.model.predict(x_test, return_std=True)
|
| 181 |
|
| 182 |
# plot
|
|
|
|
| 252 |
)
|
| 253 |
return fig
|
| 254 |
|
| 255 |
+
def update_data_options(self, **kwargs):
|
| 256 |
+
for key, value in kwargs.items():
|
| 257 |
+
if key in self.data_options:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
|
| 259 |
+
# if function - test if valid
|
| 260 |
+
if key == "function":
|
| 261 |
+
try:
|
| 262 |
+
x = np.linspace(-1, 1, 10)
|
| 263 |
+
y = numexpr.evaluate(value, local_dict={'x': x, **NUMEXPR_CONSTANTS})
|
| 264 |
+
except Exception as e:
|
| 265 |
+
raise ValueError(f"Invalid function: {e}")
|
| 266 |
+
|
| 267 |
+
self.data_options[key] = value
|
| 268 |
|
| 269 |
# reset data and model
|
| 270 |
+
self.x_train, self.y_train = self.generate_data()
|
| 271 |
self.model = self.train_model(self.kernel, self.x_train, self.y_train)
|
| 272 |
|
| 273 |
return self.plot()
|
|
|
|
| 309 |
value=self.DEFAULT_FUNCTION,
|
| 310 |
interactive=True,
|
| 311 |
)
|
| 312 |
+
num_points_slider = gr.Slider(
|
| 313 |
+
label="Number of data points",
|
| 314 |
+
minimum=0,
|
| 315 |
+
maximum=100,
|
| 316 |
+
step=1,
|
| 317 |
+
value=30,
|
| 318 |
+
interactive=True,
|
| 319 |
+
)
|
| 320 |
|
| 321 |
# upload data
|
| 322 |
file_chooser = gr.File(label="Choose a file", visible=False, elem_id="rowheight")
|
|
|
|
| 357 |
gr.Markdown(''.join(open('usage.md', 'r').readlines()))
|
| 358 |
|
| 359 |
function_box.submit(
|
| 360 |
+
fn=lambda function: self.update_data_options(function=function),
|
| 361 |
inputs=function_box,
|
| 362 |
outputs=[self.canvas],
|
| 363 |
)
|
| 364 |
+
num_points_slider.change(
|
| 365 |
+
fn=lambda nsample: self.update_data_options(nsample=nsample),
|
| 366 |
+
inputs=num_points_slider,
|
| 367 |
+
outputs=[self.canvas],
|
| 368 |
+
)
|
| 369 |
|
| 370 |
kernel_spec.submit(
|
| 371 |
fn=self.update_kernel_spec,
|