0

I use the CatBoostModel by vaex.

transactions_sample_merged is a 10000x10 DataFrame.

<class 'pandas.core.frame.DataFrame'>
Int64Index: 10000 entries, 0 to 9999
Data columns (total 10 columns):
 #   Column                  Non-Null Count  Dtype   
---  ------                  --------------  -----   
 0   customer_id             10000 non-null  category
 1   article_id              10000 non-null  category
 2   price                   10000 non-null  float64 
 3   sales_channel_id        10000 non-null  category
 4   FN                      10000 non-null  category
 5   Active                  10000 non-null  category
 6   age                     10000 non-null  float64 
 7   club_member_status      10000 non-null  category
 8   fashion_news_frequency  10000 non-null  category
 9   postal_code             10000 non-null  category
dtypes: category(8), float64(2)

Here are my training codes:

from vaex.ml.catboost import CatBoostModel

df = vaex.from_pandas(transactions_sample_merged)
df_train, df_test = df.ml.train_test_split(test_size=0.2, verbose=False)
features = transactions_sample_merged.columns.values.tolist()
target = "article_id"
features.remove("article_id")

params = {
    'leaf_estimation_method': 'Gradient',
    'learning_rate': 0.1,
    'max_depth': 3,
    'bootstrap_type': 'Bernoulli',
    'subsample': 0.8,
    'sampling_frequency': 'PerTree',
    'colsample_bylevel': 0.8,
    'reg_lambda': 1,
    'objective': 'MultiClass',
    'eval_metric': 'MultiClass',
    'random_state': 42,
    'verbose': 0,
}

booster = CatBoostModel(features=features, target=target, num_boost_round=23,
                        params=params, prediction_type='Class', batch_size=100)
booster.fit(df_train)

Errors:

---------------------------------------------------------------------------
CatBoostError                             Traceback (most recent call last)
/var/folders/ld/9vr50h5s3_q7plthtspg81zw0000gn/T/ipykernel_7409/705783114.py in <module>
     22 booster = CatBoostModel(features=features, target=target, num_boost_round=23,
     23                         params=params, prediction_type='Class', batch_size=100)
---> 24 booster.fit(df_train)

/opt/miniforge3/lib/python3.7/site-packages/vaex/ml/catboost.py in fit(self, df, evals, early_stopping_rounds, verbose_eval, plot, progress, **kwargs)
    161 
    162             # Sum the models
--> 163             self.booster = catboost.sum_models(models, weights=batch_weights, ctr_merge_policy=self.ctr_merge_policy)
    164 
    165 

/opt/miniforge3/lib/python3.7/site-packages/catboost/core.py in sum_models(models, weights, ctr_merge_policy)
   6278 def sum_models(models, weights=None, ctr_merge_policy='IntersectingCountersAverage'):
   6279     result = CatBoost()
-> 6280     result._sum_models(models, weights, ctr_merge_policy)
   6281     return result
   6282 

/opt/miniforge3/lib/python3.7/site-packages/catboost/core.py in _sum_models(self, models_base, weights, ctr_merge_policy)
   1634             weights = [1.0 for _ in models_base]
   1635         models_inner = [model._object for model in models_base]
-> 1636         self._object._sum_models(models_inner, weights, ctr_merge_policy)
   1637         setattr(self, '_random_seed', 0)
   1638         setattr(self, '_learning_rate', 0)

_catboost.pyx in _catboost._CatBoost._sum_models()

_catboost.pyx in _catboost._CatBoost._sum_models()

CatBoostError: catboost/libs/model/model.cpp:1716: Approx dimensions don't match: 92 != 89

This is a multiclass task. The column "article_id" is the target.

What should I do to fix it?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Irvin
  • 27
  • 4
  • perhaps you can try to train the same catboost model outside of vaex, and see if the issue is related to the vaex wrapper or perhaps with catboost itself (or how you use catboost, parameters etc..). The final line of the stacktrace is related to catboost code – Joco Mar 04 '22 at 16:38
  • I found that if I did not use the mini-batch training the error would not occur(did not specify the parameter "batch_size"). why? – Irvin Mar 06 '22 at 11:18
  • Did you have any insight whether the error due to something on the vaex or catboost side? Or something related to your specific usecase? – Joco Mar 06 '22 at 15:06

1 Answers1

0

TLDR: Increase the batch size.

This errors comes from the catboost sum_models function (see documentation).

vaex's wrapper CatBoostModel under the hood trains several models on your data and then aggregates them with catboost's sum_models(see the code).

The error Approx dimensions don't match: X != Y means you have different number of target classes in models. So, that means the sub models you trained before merging were trained on subsets of data containing different number of target classes, in your case 92 and 89. This can easily happen in imbalance dataset where some classes are rare. Every subset had batch_size of elements (in your case 100 is too few), so to solve the issue, increase the batch_size.

alperovich
  • 854
  • 1
  • 9
  • 14