Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[python-package] fix retrain on sequence dataset #6414

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add testing for incremental training on Dataset with lgb.Sequence
  • Loading branch information
eromoe committed Jul 14, 2024
commit a07800c1d97ee279292463a9132dac60052640f7
5 changes: 4 additions & 1 deletion python-package/lightgbm/basic.py
Original file line number Diff line number Diff line change
@@ -1193,7 +1193,10 @@ def predict(
)
elif isinstance(data, list):
if isinstance(data[0], Sequence):
data = np.concatenate([i[:] for i in data])
try:
data = np.concatenate([i[:] for i in data])
except BaseException as err:
raise ValueError('Cannot convert Sequence list to numpy array.') from err
else:
try:
data = np.array(data)
59 changes: 59 additions & 0 deletions tests/python_package_test/test_sequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: mithril
# Created Date: 2024-07-14 14:18:46
# Last Modified: 2024-07-14 14:44:50


import numpy as np
import pytest
import sklearn.datasets
import lightgbm as lgb


class PartitionSequence(lgb.Sequence):
def __init__(self, data:np.ndarray, batch_size=4096):
self.data = data
self.batch_size = batch_size

def __getitem__(self, idx):
return self.data[idx]

def __len__(self):
return len(self.data)


def test_list_of_sequence():
X, y = sklearn.datasets.load_breast_cancer(return_X_y=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you move this test, please use the cached version of this function.

*load_breast_cancer(return_X_y=True), test_size=0.1, random_state=2

*load_breast_cancer(return_X_y=True), test_size=0.1, random_state=2

That'll make the tests a little faster.

X_seq = list()
y_seq = list()
for i in range(2):
X_seq.append(PartitionSequence(X, 200))
y_seq.append(y)

y = np.concatenate(y_seq)

dataset = lgb.Dataset(X_seq, label=y, free_raw_data=False)

params = {
"objective": "binary",
"metric": "auc",
"min_data": 10,
"num_leaves": 10,
"verbose": -1,
"num_threads": 1,
"max_bin": 255,
"gpu_use_dp": True,
}

model1 = lgb.train(
params,
dataset,
keep_training_booster=True,
)

model2 = lgb.train(
params,
dataset,
init_model=model1,
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make this test stricter than simply "training runs without error". Could you add assertions after training checking that:

Loading
Oops, something went wrong.