|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# |
| 4 | +# This source code is licensed under the MIT license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | + |
| 8 | +import torch |
| 9 | +from botorch.models.transforms.factory import get_rounding_input_transform |
| 10 | +from botorch.models.transforms.input import ChainedInputTransform, Normalize, Round |
| 11 | +from botorch.utils.rounding import OneHotArgmaxSTE |
| 12 | +from botorch.utils.testing import BotorchTestCase |
| 13 | +from botorch.utils.transforms import normalize, unnormalize |
| 14 | + |
| 15 | + |
| 16 | +class TestGetRoundingInputTransform(BotorchTestCase): |
| 17 | + def test_get_rounding_input_transform(self): |
| 18 | + for dtype in (torch.float, torch.double): |
| 19 | + one_hot_bounds = torch.tensor( |
| 20 | + [ |
| 21 | + [0, 5], |
| 22 | + [0, 4], |
| 23 | + [0, 1], |
| 24 | + [0, 1], |
| 25 | + [0, 1], |
| 26 | + [0, 1], |
| 27 | + [0, 1], |
| 28 | + ], |
| 29 | + dtype=dtype, |
| 30 | + device=self.device, |
| 31 | + ).t() |
| 32 | + with self.assertRaises(ValueError): |
| 33 | + # test no integer or categorical |
| 34 | + get_rounding_input_transform( |
| 35 | + one_hot_bounds=one_hot_bounds, |
| 36 | + ) |
| 37 | + integer_indices = [1] |
| 38 | + categorical_features = {2: 2, 4: 3} |
| 39 | + tf = get_rounding_input_transform( |
| 40 | + one_hot_bounds=one_hot_bounds, |
| 41 | + integer_indices=integer_indices, |
| 42 | + categorical_features=categorical_features, |
| 43 | + ) |
| 44 | + self.assertIsInstance(tf, ChainedInputTransform) |
| 45 | + tfs = list(tf.items()) |
| 46 | + self.assertEqual(len(tfs), 3) |
| 47 | + # test unnormalize |
| 48 | + tf_name_i, tf_i = tfs[0] |
| 49 | + self.assertEqual(tf_name_i, "unnormalize_tf") |
| 50 | + self.assertIsInstance(tf_i, Normalize) |
| 51 | + self.assertTrue(tf_i.reverse) |
| 52 | + bounds = one_hot_bounds[:, integer_indices] |
| 53 | + offset = bounds[:1, :] |
| 54 | + coefficient = bounds[1:2, :] - offset |
| 55 | + self.assertTrue(torch.equal(tf_i.coefficient, coefficient)) |
| 56 | + self.assertTrue(torch.equal(tf_i.offset, offset)) |
| 57 | + self.assertEqual(tf_i._d, one_hot_bounds.shape[1]) |
| 58 | + self.assertEqual( |
| 59 | + tf_i.indices, torch.tensor(integer_indices, device=self.device) |
| 60 | + ) |
| 61 | + # test round |
| 62 | + tf_name_i, tf_i = tfs[1] |
| 63 | + self.assertEqual(tf_name_i, "round") |
| 64 | + self.assertIsInstance(tf_i, Round) |
| 65 | + self.assertEqual(tf_i.integer_indices.tolist(), integer_indices) |
| 66 | + self.assertEqual(tf_i.categorical_features, categorical_features) |
| 67 | + # test normalize |
| 68 | + tf_name_i, tf_i = tfs[2] |
| 69 | + self.assertEqual(tf_name_i, "normalize_tf") |
| 70 | + self.assertIsInstance(tf_i, Normalize) |
| 71 | + self.assertFalse(tf_i.reverse) |
| 72 | + self.assertTrue(torch.equal(tf_i.coefficient, coefficient)) |
| 73 | + self.assertTrue(torch.equal(tf_i.offset, offset)) |
| 74 | + self.assertEqual(tf_i._d, one_hot_bounds.shape[1]) |
| 75 | + |
| 76 | + # test forward |
| 77 | + X = torch.rand( |
| 78 | + 2, 4, one_hot_bounds.shape[1], dtype=dtype, device=self.device |
| 79 | + ) |
| 80 | + X_tf = tf(X) |
| 81 | + # assert the continuous param is unaffected |
| 82 | + self.assertTrue(torch.equal(X_tf[..., 0], X[..., 0])) |
| 83 | + # check that integer params are rounded |
| 84 | + X_int = X[..., integer_indices] |
| 85 | + unnormalized_X_int = unnormalize(X_int, bounds) |
| 86 | + rounded_X_int = normalize(unnormalized_X_int.round(), bounds) |
| 87 | + self.assertTrue(torch.equal(rounded_X_int, X_tf[..., integer_indices])) |
| 88 | + # check that categoricals are discretized |
| 89 | + for start, card in categorical_features.items(): |
| 90 | + end = start + card |
| 91 | + discretized_feat = OneHotArgmaxSTE.apply(X[..., start:end]) |
| 92 | + self.assertTrue(torch.equal(discretized_feat, X_tf[..., start:end])) |
| 93 | + # test transform on train/eval/fantasize |
| 94 | + for tf_i in tf.values(): |
| 95 | + self.assertFalse(tf_i.transform_on_train) |
| 96 | + self.assertTrue(tf_i.transform_on_eval) |
| 97 | + self.assertTrue(tf_i.transform_on_fantasize) |
| 98 | + |
| 99 | + # test no integer |
| 100 | + tf = get_rounding_input_transform( |
| 101 | + one_hot_bounds=one_hot_bounds, |
| 102 | + categorical_features=categorical_features, |
| 103 | + ) |
| 104 | + tfs = list(tf.items()) |
| 105 | + # round should be the only transform |
| 106 | + self.assertEqual(len(tfs), 1) |
| 107 | + tf_name_i, tf_i = tfs[0] |
| 108 | + self.assertEqual(tf_name_i, "round") |
| 109 | + self.assertIsInstance(tf_i, Round) |
| 110 | + self.assertEqual(tf_i.integer_indices.tolist(), []) |
| 111 | + self.assertEqual(tf_i.categorical_features, categorical_features) |
| 112 | + # test no categoricals |
| 113 | + tf = get_rounding_input_transform( |
| 114 | + one_hot_bounds=one_hot_bounds, |
| 115 | + integer_indices=integer_indices, |
| 116 | + ) |
| 117 | + tfs = list(tf.items()) |
| 118 | + self.assertEqual(len(tfs), 3) |
| 119 | + _, tf_i = tfs[1] |
| 120 | + self.assertEqual(tf_i.integer_indices.tolist(), integer_indices) |
| 121 | + self.assertEqual(tf_i.categorical_features, {}) |
| 122 | + # test initialization |
| 123 | + tf = get_rounding_input_transform( |
| 124 | + one_hot_bounds=one_hot_bounds, |
| 125 | + integer_indices=integer_indices, |
| 126 | + categorical_features=categorical_features, |
| 127 | + initialization=True, |
| 128 | + ) |
| 129 | + tfs = list(tf.items()) |
| 130 | + self.assertEqual(len(tfs), 3) |
| 131 | + # check that bounds are adjusted for integers on unnormalize |
| 132 | + _, tf_i = tfs[0] |
| 133 | + offset_init = bounds[:1, :] - 0.4999 |
| 134 | + coefficient_init = bounds[1:2, :] + 0.4999 - offset_init |
| 135 | + self.assertTrue(torch.equal(tf_i.coefficient, coefficient_init)) |
| 136 | + self.assertTrue(torch.equal(tf_i.offset, offset_init)) |
| 137 | + # check that bounds are adjusted for integers on normalize |
| 138 | + _, tf_i = tfs[2] |
| 139 | + self.assertTrue(torch.equal(tf_i.coefficient, coefficient)) |
| 140 | + self.assertTrue(torch.equal(tf_i.offset, offset)) |
| 141 | + # test return numeric |
| 142 | + tf = get_rounding_input_transform( |
| 143 | + one_hot_bounds=one_hot_bounds, |
| 144 | + integer_indices=integer_indices, |
| 145 | + categorical_features=categorical_features, |
| 146 | + return_numeric=True, |
| 147 | + ) |
| 148 | + tfs = list(tf.items()) |
| 149 | + self.assertEqual(len(tfs), 4) |
| 150 | + tf_name_i, tf_i = tfs[3] |
| 151 | + self.assertEqual(tf_name_i, "one_hot_to_numeric") |
| 152 | + # transform to numeric on train |
| 153 | + # (e.g. for kernels that expect a integer representation) |
| 154 | + self.assertTrue(tf_i.transform_on_train) |
| 155 | + self.assertTrue(tf_i.transform_on_eval) |
| 156 | + self.assertTrue(tf_i.transform_on_fantasize) |
| 157 | + self.assertEqual(tf_i.categorical_features, categorical_features) |
| 158 | + self.assertEqual(tf_i.numeric_dim, 4) |
| 159 | + # test return numeric and no categorical |
| 160 | + tf = get_rounding_input_transform( |
| 161 | + one_hot_bounds=one_hot_bounds, |
| 162 | + integer_indices=integer_indices, |
| 163 | + return_numeric=True, |
| 164 | + ) |
| 165 | + tfs = list(tf.items()) |
| 166 | + # there should be no one hot to numeric transform |
| 167 | + self.assertEqual(len(tfs), 3) |
0 commit comments