# 3.2 为超参数选择合适的范围（Using an appropriate scale to pick hyperparameters）

pick hyperparameters）

随机取值并不是在有效范围内的随机均匀取值，而是选择合适的标尺，用于探究这些超参数

对于超参数#layers和#hidden units，都是正整数，是可以进行均匀随机采样的，即超参数每次变化的尺度都是一致

对于某些超参数，可能需要非均匀随机采样（即非均匀刻度尺）。例如超参数$$\alpha$$，待调范围是\[0.0001, 1]。如果使用均匀随机采样，90%的采样点分布在\[0.1, 1]之间，只有10%分布在\[0.0001, 0.1]之间。而最佳的$$\alpha$$值可能主要分布在\[0.0001, 0.1]之间，因此更应在区间\[0.0001, 0.1]内细分更多刻度

通常的做法是将linear scale转换为log scale，将均匀尺度转化为非均匀尺度，然后再在log scale下进行均匀采样。这样，\[0.0001, 0.001]，\[0.001, 0.01]，\[0.01, 0.1]，\[0.1, 1]各个区间内随机采样的超参数个数基本一致，扩大了之前\[0.0001, 0.1]区间内采样值个数

![](/files/-Le0caOEj4o0lu0KaOTL)

如果线性区间为\[a, b]，令m=log(a)，n=log(b)，则对应的log区间为\[m,n]。对log区间的\[m,n]进行随机均匀采样，得到的采样值r，最后反推到线性区间，即$$10^r$$.$$10^r$$是最终采样的超参数。代码为：

```python
m = np.log10(a)
n = np.log10(b)
r = np.random.rand()
r = m + (n-m)*r
r = np.power(10,r)
```

动量梯度因子$$\beta$$在超参数调试也需要进行非均匀采样。一般$$\beta$$的取值范围在\[0.9, 0.999]之间，1−$$\beta$$的取值范围在\[0.001, 0.1]。那么直接对1−$$\beta$$在\[0.001, 0.1]区间内进行log变换

为什么$$\beta$$也需要向$$\alpha$$那样做非均匀采样：

假设$$\beta$$从0.9000变化为0.9005，那么$$\frac{1}{1-\beta}$$基本没有变化。但假设β从0.9990变化为0.9995，那么$$\frac{1}{1-\beta}$$前后差别1000。$$\beta$$越接近1，指数加权平均的个数越多，变化越大。所以对$$\beta$$接近1的区间，应该采集得更密集一些


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://baozoulin.gitbook.io/neural-networks-and-deep-learning/di-er-men-ke-gai-shan-shen-ceng-shen-jing-wang-luo-chao-can-shu-tiao-shi-zheng-ze-hua-yi-ji-you-hua/improving-deep-neural-networks/hyperparameter-tuning/32-wei-chao-can-shu-xuan-ze-he-shi-de-fan-wei-ff08-using-an-appropriate-scale-to-pick-hyperparameter.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
