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

[R-package] R session crashes on iris example #3445

Closed
OldMortality opened this issue Oct 8, 2020 · 10 comments
Closed

[R-package] R session crashes on iris example #3445

OldMortality opened this issue Oct 8, 2020 · 10 comments

Comments

@OldMortality
Copy link

R crashes when I get to lgb.train statement.

This is happening in my project, but I can recreate it in the iris example (attached)

I will attach the code, and a screenshot of the error.
This has worked for me before, I don't know what has changed.

lightgbm R fatal error.zip

The FAQ says I may ping a member with the relevant knowledge, so I will attempt to do this here: @guolinke @Laurae2

Many thanks for your attention.

I am using Windows 10, R version 4.0.2.
I installed the lightgbm package like this:

# PKG_URL <- "https://github.com/microsoft/LightGBM/releases/download/v3.0.0/lightgbm-3.0.0-r-cran.tar.gz"
# 
# remotes::install_url(PKG_URL)
@jameslamb
Copy link
Collaborator

Hi @OldMortality , I can help you with this. Thanks for the repo and for using LightGBM!

I've copied your code below, as that makes it a little easier for others to see and improves the relevance of search results in this repo

reproducible example (click me)
rm(list=ls())
library(lightgbm)

# We load the default iris dataset shipped with R
data(iris)


# We must convert factors to numeric
# They must be starting from number 0 to use multiclass
# For instance: 0, 1, 2, 3, 4, 5...
iris$Species <- as.numeric(as.factor(iris$Species)) - 1L
# prevent R-session from crashing: response must be a factor
# iris$Species <- as.factor(iris$Species)

# We cut the data set into 80% train and 20% validation
# The 10 last samples of each class are for validation



train <- as.matrix(iris[c(1L:40L, 51L:90L, 101L:140L), ])
test <- as.matrix(iris[c(41L:50L, 91L:100L, 141L:150L), ])
dtrain <- lgb.Dataset(data = train[, 1L:4L], label = train[, 5L])
dtest <- lgb.Dataset.create.valid(dtrain, data = test[, 1L:4L], label = test[, 5L])
valids <- list(test = dtest)

# Method 1 of training
params <- list(objective = "multiclass", metric = "multi_error", num_class = 3L)
model <- lgb.train(
  params
  , dtrain
  , 100L
  , valids
  , min_data = 1L
  , learning_rate = 1.0
  , early_stopping_rounds = 10L
)

s# We can predict on test data, outputs a 90-length vector
# Order: obs1 class1, obs1 class2, obs1 class3, obs2 class1, obs2 class2, obs2 class3...
my_preds <- predict(model, test[, 1L:4L])

# Method 2 of training, identical
model <- lgb.train(
  list()
  , dtrain
  , 100L
  , valids
  , min_data = 1L
  , learning_rate = 1.0
  , early_stopping_rounds = 10L
  , objective = "multiclass"
  , metric = "multi_error"
  , num_class = 3L
)

# We can predict on test data, identical
my_preds <- predict(model, test[, 1L:4L])

# A (30x3) matrix with the predictions, use parameter reshape
# class1 class2 class3
#   obs1   obs1   obs1
#   obs2   obs2   obs2
#   ....   ....   ....
my_preds <- predict(model, test[, 1L:4L], reshape = TRUE)

and the screenshot:

image


I'll try running this on my Windows laptop later tonight, and let you know if I'm able to reproduce it. In the meantime, you could check if any of the recent bugfixes in this repo happened to fix the problem you're facing, by installing from our latest submission to CRAN:

remotes::install_url("https://github.com/microsoft/LightGBM/files/5303046/lightgbm_3.0.0.2.tar.gz")

Please let me know if that fixes your problem. I'll respond later with the results of my attempt to reproduce this.

@jameslamb jameslamb changed the title R session crashes on iris example [R-package] R session crashes on iris example Oct 8, 2020
@OldMortality
Copy link
Author

Hi,

Thank you very much for your help with this.
I have installed the latest submission as you suggested, but it has the same problem.
It is quite strange, I did not have this problem before. This morning, without any provocation on my part, it gave me this error. After some googling, I found that there had been a similar problem in the python version, and the solution to that was to change the response variables to factors. I tried that, and it seemed to work for a while, and then I got the error again!
I hope you will be able to recreate the error. Many thanks for your help, I very much appreciate it.

@jameslamb
Copy link
Collaborator

I tested on my Windows laptop tonight (Windows 10, RStudio 1.2.5019, R 4.0.2). I have good news and bad news:

bad news

When In installed like this

remove.packages("lightgbm")
remotes::install_url("https://github.com/microsoft/LightGBM/files/5303046/lightgbm_3.0.0.2.tar.gz")

And then ran the code you provided, my RStudio session did crash in the first call to lgb.train(), just like yours did.

good news

When I restarted my R session and ran the sample code from the beginning. It all succeeded without error! I repeated this three times, and it worked each time.

Can you please try restarting your R session in RStudio and re-running the code? I wonder if something about the way remotes::install_url() installs {lightgbm} is resulting in corruption in your environment.

@OldMortality
Copy link
Author

I have done better than restarting the R-session: I restarted the computer! But to no avail.
I will try again now, just for luck.

@OldMortality
Copy link
Author

Well, I restarted R, and did:

remove.packages("lightgbm")
remotes::install_url("https://github.com/microsoft/LightGBM/files/5303046/lightgbm_3.0.0.2.tar.gz")

and then I still had the error.
Then I restarted R, from R-studio.
And now it works!

So that's great. It is getting a little late here in New Zealand, so I'll leave it for today. Many thanks for your help. I very much appreciate it, because I was entirely stuck with this.

Cheers,

Michel

@OldMortality
Copy link
Author

The problem first arose because I had a bug in the code. I seem to have an infinite loop (or a Very Long Loop) and I had interrupted RStudio through the task manager, and that then leaves lightgbm in a bad state. Even though there is a workaround, it would be great to have a fix, because I cannot re-install lightgbm, reload the data, and rerun everything every time I try to fix this bug.

@jameslamb
Copy link
Collaborator

You shouldn't have to re-install lightgbm. For me, it was enough to just install it one time, then restart my R session. Ater that, every new session should work with just library(lightgbm) (no need to install again).

@OldMortality
Copy link
Author

Great, thank you. It is all working for me now. Many thanks for you help.

@jameslamb
Copy link
Collaborator

oh good, I'm glad! Thanks for using {lightgbm}, come back any time if you face other issues.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity since it was closed. To start a new related discussion, open a new issue at https://github.com/microsoft/LightGBM/issues including a reference to this.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants