Skip to content

Commit 32501af

Browse files
saitcakmakfacebook-github-bot
authored andcommitted
Update the docstring of ChoiceEncode (#1462)
Summary: Pull Request resolved: #1462 I found the original docstring to be misleading (stating that they're converted to float). The rest of the docstring corrects that but that only works if you read the whole thing. It should be more clear now. Reviewed By: mpolson64 Differential Revision: D43635696 fbshipit-source-id: 20359de31852132928d28ca773b070c25208e816
1 parent e353436 commit 32501af

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

ax/modelbridge/transforms/choice_encode.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,14 @@
2424

2525

2626
class ChoiceEncode(Transform):
27-
"""Convert general ChoiceParameters to Float ChoiceParameters.
27+
"""Convert general ChoiceParameters to integer or float ChoiceParameters.
2828
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,
3430
then the values are normalized to the unit interval while retaining relative
3531
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.
3735
3836
In the inverse transform, parameters will be mapped back onto the original domain.
3937
@@ -188,15 +186,21 @@ def _transform_search_space(self, search_space: SearchSpace) -> SearchSpace:
188186

189187

190188
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+
"""
191195
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.
193197
values = np.array(p.values, dtype=float)
194198
vmin, vmax = values.min(), values.max()
195199
if len(values) > 1:
196200
values = (values - vmin) / (vmax - vmin)
197201
ptype = ParameterType.FLOAT
198202
else:
199-
# If values are unordered or not numeric, use integer encoding
203+
# If values are unordered or not numeric, use integer encoding.
200204
# The reason for using integers rather than floats is somewhat arcane - it has
201205
# to do with slightly different representation of floats in pure python and in
202206
# PyTorch, which require some careful handling when untransform the choices that

0 commit comments

Comments
 (0)