Compiling a neural network in R 4.0.2

Hello. I am working with the neural network for time series prediction in RStudio. I successfully run the following block of code, connected with installing the necessary packages and visualixing the data:

#Installing the necessary packages
library(keras)
library(lubridate)
install.packages("tidymodels")
library(tidymodels)
library(tidyverse)
library(recipes)


#Installing "kerasgenerator"
install.packages("remotes")
library(remotes)
install_github("bagasbgy/kerasgenerator")
library(kerasgenerator)

#Getting the data
airpassenger_df <- as_tibble(AirPassengers) %>%
  mutate(date = seq.Date(ymd("1949-01-01"), by = "1 month", length.out = nrow(.))) %>% 
  select(date, passenger = x)

# quick check
ggplot(airpassenger_df, aes(x = date, y = passenger)) +
  geom_line() +
  labs(title = "Number of international airline passengers", x = NULL, y = NULL) +
  theme_minimal()

After this I run the code, aimed at fitting a convolutional neural network (CNN) to the univariate time-series row. First, I preprocess the data:

#Practical example of suprervised time-series settings
#Many-to-one model

x<-"passenger"
y<-"passenger"

length_out<-1
stride<-1
lookback<-1
timesteps<-24


# validation and test sample size
val_size <- 12 * length_out
test_size <- 12 * length_out

# test split
data_test <- airpassenger_df %>% 
  filter(row_number() > n() - test_size - lookback - timesteps + 1)

# initial train after test-split
data_train <- airpassenger_df %>% 
  filter(row_number() <= n() - test_size)

# validation split
data_val <- data_train %>% 
  filter(row_number() > n() - val_size - lookback - timesteps + 1)

# final train after validation-split
data_train <- data_train %>% 
  filter(row_number() <= n() - val_size)


# create preprocess recipe
rec <- recipe(passenger ~ ., data = data_train) %>% 
  step_rm(date) %>% 
  step_log(passenger) %>% 
  step_normalize(passenger) %>% 
  prep()

# prepare recipes-revert functions
rec_rev <- function(x, rec) {
  
  means <- rec$steps[[3]]$means[["passenger"]]
  sds <- rec$steps[[3]]$sds[["passenger"]]
  
  x <- exp(x * sds + means)
  x <- round(x)
  x <- ifelse(x < 0, 0, x)
  
  x
  
}

# get all preprocessed data
data_train_prep <- juice(rec)
data_val_prep <- bake(rec, data_val)
data_test_prep <- bake(rec, data_test)


# generator settings
batch_size <- 12

# train-val-test generator
data_train_gen <- flow_series_from_dataframe(
  data = data_train_prep,
  x = x,
  y = y,
  length_out = length_out,
  stride = stride,
  lookback = lookback,
  timesteps = timesteps,
  batch_size = batch_size,
  mode = "training"
)

data_val_gen <- flow_series_from_dataframe(
  data = data_val_prep,
  x = x,
  y = y,
  length_out = length_out,
  stride = stride,
  lookback = lookback,
  timesteps = timesteps,
  batch_size = batch_size,
  mode = "training"
)

data_test_gen <- flow_series_from_dataframe(
  data = data_test_prep,
  x = x,
  y = y,
  length_out = length_out,
  stride = stride,
  lookback = lookback,
  timesteps = timesteps,
  batch_size = batch_size,
  mode = "training"
)

Second, I define the convolutional neural network and state its parameters:

#a convolutional neural network 
# define input layer
input <- layer_input(name = "input", shape = c(timesteps, length(x)))

# define hidden layers
cnn1 <- input %>% 
  layer_conv_1d(name = "cnn_1_1", filters = 16, kernel_size = 12, padding = "same") %>% 
  layer_activation_leaky_relu(name = "cnn_1_1_act") %>% 
  layer_flatten(name = "cnn_1_1_flat")

cnn2 <- input %>% 
  layer_conv_1d(name = "cnn_2_1", filters = 16, kernel_size = 3, padding = "same") %>% 
  layer_activation_leaky_relu(name = "cnn_2_1_act") %>% 
  layer_flatten(name = "cnn_2_1_flat")

cnn <- list(cnn1, cnn2) %>% 
  layer_concatenate(name = "cnn_concat") %>% 
  layer_dropout(name = "cnn_dp", rate = 0.1)

fc <- cnn %>% 
  layer_dense(name = "dense_1", units = 32) %>% 
  layer_activation_leaky_relu(name = "dense_1_act") %>% 
  layer_dropout(name = "dense_1_dp", rate = 0.1)

# define output layers
output <- fc %>%
  layer_dense(name = "output", units = length(y) * length_out) %>% 
  layer_reshape(name = "output_reshape", target_shape = c(length_out, length(y)))

# wrap-up to full model
model <- keras_model(inputs = input, outputs = output)

# compile the model
model %>% compile(
  optimizer = optimizer_adam(lr = 0.0001),
  loss = "mean_absolute_error"
)

# model summary
summary(model)

This model has a bit more than 24000 parameters. Further, I fit the model to the preprocessed data:

#Now let's fit the model
#get generator meta
steps_per_epoch <- generator_meta(data_train_gen, "steps_to_all")
validation_steps <- generator_meta(data_val_gen, "steps_to_all")

# fit the model
history <- model %>% fit_generator(
  generator = data_train_gen,
  steps_per_epoch = steps_per_epoch,
  epochs = 100,
  validation_data = data_val_gen,
  validation_steps = validation_steps
)

# visualize training results
plot(history)

At this stage the model fitting process fails.
The model has been fit to data for more than 1,5 hours, but all I see during this period is this:

image

When I press on the red stop button in R console, the following window appears:

image

In the end, I only have to close current R sesssion, as it no longer responds to my commands
The neural network hasn't been fit to data yet.

Could you,please, tell me, what is the problem with this block of code? How to solve it?
Maybe the problem is with my notebook. Its characterstics:

  • Windows 8.1
  • 1000 Gb HDD;
  • Intel Core i5-4210U 1.7GHz with boost to 2.7 GHz
  • NVIDIA GeForce 840M with 2 Gb VRAM
  • 6 GB DDR3 L Memory
    R version 4.0.2

Thank you for your effort!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.