-
Notifications
You must be signed in to change notification settings - Fork 282
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
Running R Keras model with custom loss crash when run twice on different data sets #1401
Comments
Keras models are modified-in-place, they are not copy-on-modify like most other R objects (they behave like R environments, not R lists). In this snippet above,
This is coming from the custom loss function. When defining a custom loss function or metric for the first time, I suggest inserting a diagnostic You can get the runtime batch size of I was unable to reproduce the crash by running the code. Please make sure you are running the latest version of reticulate, tensorflow, and keras, as some segfault-causing bugs were fixed in recent releases. You can also try a preview of keras version 3 under the |
Dear Tomasz, Thank you very much for your detailed and swift answer. I think my main problem was coming from the miss matching between the dataset dimension and the batch size. Maybe this is a newbie question but how do handle case where the size of your dataset leads to poor divisibility condition (e.g. prime number or the size only allow small or large divisor?) Do you just drop some data ? I am not sure what you mean by 'You can get the runtime batch size of y_true as a tensor with |
If you're using the new keras3, you can use the custom_loss2 <- function(y_true, y_pred) {
case_wise_tt <- op_vectorized_map(c(y_true, y_pred), function(x) {
c(y_true1, y_pred1) %<-% x
## you can write from the debugger here and work with the tracing tensors
## in an interactive context
# browser()
message("y_true1 = ", y_true1)
message("y_pred1 = ", y_pred1)
op_log(op_sum(op_exp(y_true1) * y_pred1))
})
tt <- op_sum(case_wise_tt)
mse <- -tt
mse
}
y_true <- op_arange(20) |> op_reshape(c(4, -1))
y_pred <- y_true + 20
custom_loss2(y_true, y_pred) If you're using library(tensorflow)
custom_loss3 <- function(y_true, y_pred) {
case_wise_tt <- tf$vectorized_map(
elems = c(y_true, y_pred),
fn = function(x) {
c(y_true1, y_pred1) %<-% x
message("y_true1 = ", y_true1)
message("y_pred1 = ", y_pred1)
log(sum(exp(y_true1) * y_pred1))
}
)
tt <- sum(case_wise_tt)
mse <- -tt
mse
}
y_true <- tensorflow::as_tensor(0:19, shape = c(4, -1), dtype = "float64")
y_pred <- y_true + 20
custom_loss3(y_true, y_pred) Note that the tensorflow R package defines many Group generics, so If you have an actual need for a |
I am trying to develop some specific type of neural net with a special type of loss function. The loss is somewhat strange but in my project in actually make sens.
Any how I am trying to first set up the architecture and then fit the model to different data set x_train and y_train and x_train2 and y_train2.
I really don t get why it crashes when fitted to the x_train2 and y_train2.
Here the error
and here a minimal reproducible example.
The model should be easily fitted to different dataset but crashes instead without any clear reason.
The text was updated successfully, but these errors were encountered: