|
24 | 24 |
|
25 | 25 |
|
26 | 26 | class ChoiceEncode(Transform):
|
27 |
| - """Convert general ChoiceParameters to Float ChoiceParameters. |
| 27 | + """Convert general ChoiceParameters to integer or float ChoiceParameters. |
28 | 28 |
|
29 |
| - ChoiceParameters will be transformed to ChoiceParameters of float or int type. |
30 |
| - The resulting choice parameter will be considered ordered iff the original |
31 |
| - parameter is. |
32 |
| -
|
33 |
| - If the parameter type is numeric (int, float) and the parameter is orderd, |
| 29 | + If the parameter type is numeric (int, float) and the parameter is ordered, |
34 | 30 | then the values are normalized to the unit interval while retaining relative
|
35 | 31 | spacing. If the parameter type is unordered (categorical) or ordered but
|
36 |
| - non-numeric, this transform uses an integer encoding. |
| 32 | + non-numeric, this transform uses an integer encoding to `0, 1, ..., n_choices - 1`. |
| 33 | + The resulting choice parameter will be considered ordered iff the original |
| 34 | + parameter is. |
37 | 35 |
|
38 | 36 | In the inverse transform, parameters will be mapped back onto the original domain.
|
39 | 37 |
|
@@ -188,15 +186,21 @@ def _transform_search_space(self, search_space: SearchSpace) -> SearchSpace:
|
188 | 186 |
|
189 | 187 |
|
190 | 188 | def transform_choice_values(p: ChoiceParameter) -> Tuple[np.ndarray, ParameterType]:
|
| 189 | + """Transforms the choice values and returns the new parameter type. |
| 190 | +
|
| 191 | + If the choices were numeric (int or float) and ordered, then they're cast |
| 192 | + to float and rescaled to [0, 1]. Otherwise, they're cast to integers |
| 193 | + `0, 1, ..., n_choices - 1`. |
| 194 | + """ |
191 | 195 | if p.is_numeric and p.is_ordered:
|
192 |
| - # If values are ordered numeric, retain relative distances |
| 196 | + # If values are ordered numeric, retain relative distances. |
193 | 197 | values = np.array(p.values, dtype=float)
|
194 | 198 | vmin, vmax = values.min(), values.max()
|
195 | 199 | if len(values) > 1:
|
196 | 200 | values = (values - vmin) / (vmax - vmin)
|
197 | 201 | ptype = ParameterType.FLOAT
|
198 | 202 | else:
|
199 |
| - # If values are unordered or not numeric, use integer encoding |
| 203 | + # If values are unordered or not numeric, use integer encoding. |
200 | 204 | # The reason for using integers rather than floats is somewhat arcane - it has
|
201 | 205 | # to do with slightly different representation of floats in pure python and in
|
202 | 206 | # PyTorch, which require some careful handling when untransform the choices that
|
|
0 commit comments