rstudio / keras3

R Interface to Keras
https://keras3.posit.co/
Other
837 stars 283 forks source link

I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2) #1213

Closed username2292 closed 2 years ago

username2292 commented 3 years ago

I am trying to use a LSTM to predict a univariate time series. The idea is to use 24 months to predict the next 12 months. I am getting an error that I cannot fix. I saw some errors like that on Google and I tried to fix it with devtools::install_github("rstudio/keras") without success

Here is my code:

#Define a scale function
scale_data = function(train, test, feature_range = c(-1, 1)) {
  x = train
  fr_min = feature_range[1]
  fr_max = feature_range[2]
  std_train = ((x - min(x) ) / (max(x) - min(x)  ))
  std_test  = ((test - min(x) ) / (max(x) - min(x)  ))

  scaled_train = std_train *(fr_max -fr_min) + fr_min
  scaled_test = std_test *(fr_max -fr_min) + fr_min

  return(list(scaled_train = as.vector(scaled_train), scaled_test = as.vector(scaled_test), scaler= c(min = min(x), max = max(x))))
}

Scaled = scale_data(kt_train_male, kt_test_male, c(0, 1))

kt_train_male_scaled = Scaled$scaled_train
kt_test_male_scaled = Scaled$scaled_test

#Define a invert scaling function
invert_scaling = function(scaled, scaler, feature_range = c(-1, 1)){
  min = scaler[1]
  max = scaler[2]
  t = length(scaled)
  mins = feature_range[1]
  maxs = feature_range[2]
  inverted_dfs = numeric(t)

   for(i in 1:t){
    X = (scaled[i]- mins)/(maxs - mins)
    rawValues = X *(max - min) + min
    inverted_dfs[i] <- rawValues
  }
  return(inverted_dfs)
}

#Split training data in sliding window format
create_dataset = function(data, n_input, n_out){
        X <- matrix(nrow = 0, ncol = n_input)
        Y <- matrix(nrow = 0, ncol = n_out)
        in_start <- 0
        for (i in seq_along(data)) {
                #define the end of the input sequence
                in_end <- in_start + n_input
                out_end <- in_end + n_out
                        if(out_end <= length(data)){
                                X <- rbind(X, data[(in_start+1):in_end])
                                Y <- rbind(Y, data[(in_end+1):out_end])
                        }
                #move along one time step
                in_start = in_start + 1
        }

   list(X, Y)
}

#24 months to predict the next 12 months
n_input = 24 
n_out = 12

train_sliding = create_dataset(data = kt_train_male_scaled, n_input = n_input, n_out = n_out)
X_train = train_sliding[[1]]
dim(X_train) #107 24
y_train = train_sliding[[2]]
dim(y_train) #107 12
#Array transformation to Keras LSTM
dim(X_train) = c(dim(X_train), 1)
dim(X_train) #107 24 1

#Model in Keras
batch_size = 1 

model <- keras_model_sequential() 
model%>%
  layer_lstm(units = 16, activation = "relu", batch_size = batch_size, input_shape = c(n_input, n_out), stateful= TRUE)%>%
  layer_dense(units = 1) %>%
summary(model)

model %>% compile(
  loss = 'mse',
  optimizer = optimizer_adam(lr= 0.02, decay = 1e-6 ),  
  metrics = c('mae')
)

When I try to run the chunck of code bellow I get the following error

Epochs = 100   
for(i in 1:Epochs ){
  model %>% fit(X_train, y_train, epochs=1, batch_size=batch_size, verbose=1, shuffle=FALSE)
  model %>% reset_states()
}

The code is not executed and this error is thrown

2021-04-18 18:57:30.767632: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
Error in py_call_impl(callable, dots$args, dots$keywords) : 
  ValueError: in user code:

    /Users/my_user/Library/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:805 train_function  *
        return step_function(self, iterator)
    /Users/my_user/Library/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:795 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /Users/my_user/Library/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:1259 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /Users/my_user/Library/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /Users/my_user/Library/r-mini

I uploaded my data to make it reproducible. Although the error happens before the test data is used I uploaded it for the sake of completeness. kt_train_male.csv kt_test_male.csv

SimoneStarace commented 3 years ago

2021-04-18 18:57:30.767632: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)

This isn't an error but more like a warning. For this one I recommend reading this post from Stack Overflow.

The true error is this one:

Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: in user code: /Users/my_user/Library/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:805 train_function * return step_function(self, iterator) /Users/my_user/Library/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py:795 step_function ** outputs = model.distribute_strategy.run(run_step, args=(data,)) /Users/my_user/Library/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:1259 run return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs) /Users/my_user/Library/r-miniconda/envs/r-reticulate/lib/python3.6/site-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica return self._call_for_each_replica(fn, args, kwargs) /Users/my_user/Library/r-mini

I don't really know if the problem is how the model is built.

patc3 commented 3 years ago

I have the same error (Error in py_call_impl(callable, dots$args, dots$keywords) : ValueError: in user code:), I don't know what it is either...