LightGBM icon indicating copy to clipboard operation
LightGBM copied to clipboard

[python package]: when some features are categorical, validation performance continually diverges from manual computation via predict as num_iterations grows

Open j-mark-hou opened this issue 9 years ago • 2 comments

It seems like these two should generate the same output: method 1. the validation performance printed by the train() function during training method 2. taking the trained model, using predict(_, num_iterations) on the validation data, and manually computing the validation performance after the fact

However, when some of the x-features are categorical, it appears that as the num_iterations variable in the predict() function grows, these two methods of computing the validation performance increasingly diverge from each other. (I haven't encountered this issue when there are no categorical features)

I've included an example below. I've also encountered examples where the divergence is greater: there was one situation in which the validation performance printed during the training (as in method 1) jumped suddenly by ~5% after about 200 trees, while doing the manual thing (as in method 2) didn't exhibit this change at all (though I'm not able to replicate this example with synthetic data).

Given that the validation set is used for early stopping, this seems like it could potentially tank performance.

import lightgbm as lgb
import numpy as np
import pandas as pd

n= 10000
X = np.random.randint(1000, size=(n,2))
y = X[:,0]+X[:,1] + np.random.normal(size=n)

Xtrain, ytrain = X[:int(.8*n),:], y[:int(.8*n)]
Xval, yval = X[int(.8*n):,:], y[int(.8*n):]

ds_train = lgb.Dataset(Xtrain, ytrain, categorical_feature=[1])
ds_val = lgb.Dataset(Xval, yval, categorical_feature=[1])

# do training
params = {'objective':'regression_l2',  'metric':'rmse', 'learning_rate':.1}
gbm = lgb.train(params, ds_train, num_boost_round=200, valid_sets=[ds_val, ds_train], categorical_feature=[1])
# some output from the train() function:
# [1]	training's rmse: 389.564	valid_0's rmse: 392.207
# [2]	training's rmse: 372.434	valid_0's rmse: 374.672
# [3]	training's rmse: 357.943	valid_0's rmse: 359.794
# [4]	training's rmse: 345.742	valid_0's rmse: 347.258
# [5]	training's rmse: 335.507	valid_0's rmse: 336.72
# [6]	training's rmse: 326.964	valid_0's rmse: 327.987
# [7]	training's rmse: 319.86	valid_0's rmse: 320.653
# [8]	training's rmse: 313.98	valid_0's rmse: 314.582
# [9]	training's rmse: 309.098	valid_0's rmse: 309.517
# [10]	training's rmse: 305.084	valid_0's rmse: 305.34
# ...
# [50]	training's rmse: 285.118	valid_0's rmse: 286.825
# ...
# [100]	training's rmse: 284.436	valid_0's rmse: 287.846
# ...
# [150]	training's rmse: 284.124	valid_0's rmse: 288.625
# ...
# [200]	training's rmse: 283.933	valid_0's rmse: 289.151


# now, use the trained model to predict on the validation data.
#  the numbers will increasingly diverge from the numbers above
#  as  num_trees grows
for i in [1,2,3,4,5,6,7,8,9,10, 50, 100, 150, 200]:
    print(i, np.sqrt(np.mean(np.square(gbm.predict(Xval, num_iteration=i)-yval))))
# [1] 392.206870633
# [2] 374.672285315
# [3] 359.793837232
# [4] 347.258134764
# [5] 336.720465828
# [6] 327.986942857
# [7] 320.653435392
# [8] 314.582362664
# [9] 309.517422037
# [10] 305.358680831
# [50] 286.60567136
# [100] 287.453146514
# [150] 287.882472117
# [200] 288.111978824

j-mark-hou avatar Aug 03 '17 18:08 j-mark-hou

This seems like an issue with categorical values appearing in the validation set that don't appear in the training set. For example, the code below runs fine:

n=1000
X = np.array([np.random.randint(1000, size=n),np.tile(np.arange(int(.5*n)),2)]).transpose()
y = X[:,0]+X[:,1] + np.random.normal(size=n)

Xtrain, ytrain = X[:int(.5*n),:], y[:int(.5*n)]
Xval, yval = X[int(.5*n):,:], y[int(.5*n):]

ds_train = lgb.Dataset(Xtrain, ytrain, categorical_feature=[1])
ds_val = lgb.Dataset(Xval, yval, categorical_feature=[1])

params = {'objective':'regression_l2',  'metric':'rmse', 'learning_rate':.1}
gbm = lgb.train(params, ds_train, num_boost_round=200, valid_sets=[ds_val, ds_train], categorical_feature=[1])
'''

j-mark-hou avatar Aug 03 '17 19:08 j-mark-hou

This was based on the old version of lightgbm that apparently didn't include the optimized way of doing categoricals. New version of lightgbm is giving me an error so I will close for now. until I can check if the error still occurs for the new version.

j-mark-hou avatar Aug 03 '17 20:08 j-mark-hou