Fix shape mismatch error in CatCMASampler for categorical problems
#171
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
Problem:
When using the CatCMA sampler in Optuna with categorical parameters, a shape mismatch error occurs during the optimization process. This error prevents the sampler from functioning correctly, hindering the hyperparameter optimization workflow.
Error Details:
Root Cause:
The categorical parameters are being one-hot encoded with differing lengths based on the number of choices per categorical variable. When these ragged arrays are converted to NumPy arrays with dtype=object, they lose their uniform shape, causing mismatches during optimizer computations.
Description of the changes
To resolve the shape mismatch, ensure that all categorical parameters are encoded into fixed-size, flat arrays. This can be achieved by concatenating all one-hot encoded vectors into a single flat array with a consistent length across all trials. Here's how to implement this:
Calculate Total Categories: Determine the total number of categories across all categorical variables to establish a fixed size for the concatenated array.
Impact of the Change:
Consistency: Ensures that all categorical parameter arrays have a consistent shape, eliminating broadcasting issues.
Compatibility: Aligns with the CatCMA optimizer's expectations, allowing seamless integration of categorical parameters.
Robustness: Prevents runtime errors related to array shape mismatches, enhancing the reliability of the optimization process.
To illustrate:
If you have:
cat1 = ['A', 'B', 'C'] # 3 choices
cat2 = ['X', 'Y', 'Z', 'W'] # 4 choices
Original method would try to create:
→ Results in shape mismatch
New method creates:
→ Works correctly with the optimizer
Contributor Agreements