Skip to content

Running R Keras model with custom loss crash when run twice on different data sets #1401

Open
@william-denault

Description

@william-denault

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

Error in py_call_impl(callable, call_args$unnamed, call_args$named) : RuntimeError: in user code:

File  ...\DOCUME~1\VIRTUA~1\R-TENS~1\Lib\site-packages\keras\src\engine\training.py", line 1401, in train_function  *
    return step_function(self, iterator)
File  ...\R\cache\R\renv\library\comoR-4eef73a7\R-4.3\x86_64-w64-mingw32\reticulate\python\rpytools\call.py", line 16, in python_function  *
    raise error

RuntimeError: NA/NaN argument

and here a minimal reproducible example.

 rm(list=ls())
    y = rnorm(1000)
    x= rnorm (10rm(list=ls())
    y = rnorm(1000)
    x= rnorm (1000)
num_classes=10
mat <- matrix( rnorm (1000*num_classes), ncol=num_classes)
x_train =x
y_train =mat
length(x_train)
dim(y_train)

model <- keras_model_sequential() %>%
  layer_dense(units = 64, activation = 'relu', input_shape = c(1)) %>%
  layer_dense(units = 64, activation = 'relu' ) %>%
  layer_dense(units = num_classes, activation = 'softmax')


custom_loss <- function(y_true, y_pred) {
  tt <- 0
  for (i in 1:nrow(y_true)) {
    tt <- tt + log(sum(exp(y_true[i,]) * y_pred[i,]))
  }
  mse <- -tt
  return(mse)
}
blank_model <-  model %>% compile(
  loss = custom_loss,
  optimizer = 'adam',
  metrics = c('accuracy')
)


model1 <- blank_model
model2 <- blank_model


history <-model1 %>% fit(
  x_train, y_train,
  epochs = 40,
  batch_size = 100
)

x_train2 =x[-1]
y_train2 =mat[-1,]

history <-model2 %>% fit(
  x_train2, y_train2,
  epochs = 40,
  batch_size = 100
)
00)
num_classes=10
mat <- matrix( rnorm (1000*num_classes), ncol=num_classes)
x_train =x
y_train =mat
length(x_train)
dim(y_train)

model <- keras_model_sequential() %>%
  layer_dense(units = 64, activation = 'relu', input_shape = c(1)) %>%
  layer_dense(units = 64, activation = 'relu' ) %>%
  layer_dense(units = num_classes, activation = 'softmax')


custom_loss <- function(y_true, y_pred) {
  tt <- 0
  for (i in 1:nrow(y_true)) {
    tt <- tt + log(sum(exp(y_true[i,]) * y_pred[i,]))
  }
  mse <- -tt
  return(mse)
}
blank_model <-  model %>% compile(
  loss = custom_loss,
  optimizer = 'adam',
  metrics = c('accuracy')
)


model1 <- blank_model
model2 <- blank_model


history <-model1 %>% fit(
  x_train, y_train,
  epochs = 40,
  batch_size = 100
)

x_train2 =x[-1]
y_train2 =mat[-1,]

history <-model2 %>% fit(
  x_train2, y_train2,
  epochs = 40,
  batch_size = 100
)

The model should be easily fitted to different dataset but crashes instead without any clear reason.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions