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
Show file tree
Hide file tree
Changes from 2 commits
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
16 changes: 11 additions & 5 deletions python-package/lightgbm/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1192,15 +1192,21 @@ def predict(
predict_type=predict_type,
)
elif isinstance(data, list):
try:
data = np.array(data)
except BaseException as err:
raise ValueError("Cannot convert data list to numpy array.") from err
if isinstance(data[0], Sequence):
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)
except BaseException as err:
raise ValueError('Cannot convert data list to numpy array.') from err
preds, nrow = self.__pred_for_np2d(
mat=data,
start_iteration=start_iteration,
num_iteration=num_iteration,
predict_type=predict_type,
predict_type=predict_type
eromoe marked this conversation as resolved.
Show resolved Hide resolved
)
elif isinstance(data, dt_DataTable):
preds, nrow = self.__pred_for_np2d(
Expand Down
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
StrikerRUS marked this conversation as resolved.
Show resolved Hide resolved
# -*- 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,
}
eromoe marked this conversation as resolved.
Show resolved Hide resolved

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