[python package]: datasets with common dataset.reference
When doing lgb.train with a training and validation set, the train function sets the reference of the valid_data to be train_set (line 131 engine.py). this set_reference then checks if the reference of the valid_data is the train_set (line 1009 basics.py), and if not, sets the reference of valid_data to be train_set.
This throws an error if the train_set was constructed with free_raw_data=True. In particular, this is an issue if I want to:
- create a single big lgb.Dataset object with all the data
- free the raw data used
- create subsets of the single big lgb.Dataset for training and validation
It seems like this should work, given that both train and validation have the same bin structure, as they are subsets of a single lgb.Dataset object.
The problem seems like it could be solved by changing the set reference function (line 1009 basics.py), so that instead of checking if self.reference is reference, it instead iterates up the entire chain of potentially chained references and detects if the ultimate parent of both self.reference and reference are the same.
Example code for reproducing error:
import lightgbm as lgb
import numpy as np
import pandas as pd
import sys
import platform
print(sys.version)
# 3.6.0 |Anaconda 4.3.0 (64-bit)| (default, Dec 23 2016, 11:57:41) [MSC v.1900 64 bit (AMD64)]
print(platform.system(), platform.release(), platform.version())
# Windows 2012ServerR2 6.3.9600
# 1. generate some data
n = 1000
tmp_df = pd.DataFrame({'x1':np.random.normal(size=n),'x2':np.random.normal(size=n), 'y':np.random.normal(size=n) })
tmp_df.head()
tmp_dat = lgb.Dataset(tmp_df[['x1', 'x2']], tmp_df['y'])
# generate training/val subsets
tmp_dat_train = tmp_dat.subset(np.arange(int(n*.8)))
tmp_dat_val = tmp_dat.subset(np.arange(int(n*.8), n))
#2. print the various references of the various lgb.Dataset objects
print(tmp_dat, tmp_dat.reference)
print(tmp_dat_train, tmp_dat_train.reference)
print(tmp_dat_val, tmp_dat_val.reference)
# <lightgbm.basic.Dataset object at 0x000000AF75B196A0> None
# <lightgbm.basic.Dataset object at 0x000000AF75B196D8> <lightgbm.basic.Dataset object at 0x000000AF75B196A0>
# <lightgbm.basic.Dataset object at 0x000000AF75B19E10> <lightgbm.basic.Dataset object at 0x000000AF75B196A0>
# # note: tmp_dat_val and tmp_dat_train share the same reference, which is tmp_dat
# 3. do training, where the validation's reference is the training
params = {'objective':'regression_l2',
'metric':'rmse'}
gbm = lgb.train(params, tmp_dat, num_boost_round=20, valid_sets=[tmp_dat, tmp_dat_val])
# everything runs, no errors
#4. do training, where the validation's reference is NOT the training, but
# rather validation and training share the same reference
params = {'objective':'regression_l2',
'metric':'rmse'}
gbm = lgb.train(params, tmp_dat_train, num_boost_round=20, valid_sets=[tmp_dat_train, tmp_dat_val])
# LightGBMError: Cannot set reference after freed raw data, set free_raw_data=False when construct Dataset to avoid this.
It really is a issue. I think it can be fixed. Would you like to create a PR to fix it?
sure i'll do that.
@j-mark-hou any updates ?