You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Google Colab中构建带变量依赖的交互式超参数调优表单?

Linked Interactive Widgets for Hyperparameter Tuning in Google Colab

Absolutely! You can build this dynamic, linked form in Google Colab using ipywidgets—it’s perfect for hyperparameter tuning where you need dependent controls. Here’s a complete, working example that ties your x_max dropdown to the x_init slider:

# Import required libraries
import ipywidgets as widgets
from IPython.display import display

# Create the x_max dropdown with options [1,2,3]
x_max = widgets.Dropdown(
    options=[1, 2, 3],
    value=1,  # Default selected value
    description='x_max:',
    disabled=False,
)

# Define a function to update the x_init slider when x_max changes
def update_x_init_range(change):
    # Update the maximum value of x_init to match the selected x_max
    x_init.max = change.new
    # Reset x_init to 0 if it's now larger than the new max (optional)
    if x_init.value > change.new:
        x_init.value = 0

# Create the initial x_init slider (max starts at x_max's default value)
x_init = widgets.IntSlider(
    value=0,
    min=0,
    max=x_max.value,
    step=1,
    description='x_init:',
    disabled=False,
    continuous_update=False,
    orientation='horizontal',
    readout=True,
    readout_format='d'
)

# Link the x_max dropdown to the update function
x_max.observe(update_x_init_range, names='value')

# Display both widgets
display(x_max, x_init)

How this works:

  • Dropdown Setup: We start by creating the x_max dropdown with your specified options and a default value of 1.
  • Update Function: The update_x_init_range function triggers whenever x_max changes. It adjusts the max property of the x_init slider to match the selected x_max value, and optionally resets x_init to 0 if it’s now out of bounds.
  • Slider Initialization: The x_init slider starts with a max value matching the default x_max (1).
  • Linking Controls: Using observe(), we tell the dropdown to call our update function every time its value changes—this creates the dynamic linkage you need.
  • Display: Finally, we render both widgets so you can interact with them directly in your Colab notebook.

Extra Tip:

To access the current values of these widgets (for use in your hyperparameter tuning code), just use x_max.value and x_init.value—they’ll always reflect the latest user selections.

内容的提问来源于stack exchange,提问作者Joshua Lian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:09:35