how to fix "sink stack is full" over training a model

I want to train a regression model by "keras_model_sequential" in R. For finding the best parameters over the grid search I have used "tuning_run". But I got this error:

training run 1/128 (flags = list(0.05, 66, "relu", 8, 10, 0.001, 0.2))
Error in sink(file = output_file, type = "output", split = TRUE) :
sink stack is full
Calls: tuning_run ... with_changed_file_copy -> force -> sink -> .handleSimpleError -> h

I need to mention, that a folder named "runs" was created in the data and script path which has a lot of subfolders whose name is like a date format. maybe this is the reason.

This is my code

library(plyr) 
library(boot)
library(keras)
library(tensorflow)
library(kerasR) 
library(tidyverse)
library(tfruns)
library(MLmetrics)

  df= mainlist[[1]]  # data which is a 33*31 dataframe (33 samples and 31 features which last column is target)
  x = (length(df))-1
  print(x)
  df1 = df[, 2:x]
  
  #normalization
  df2 = df[, 2:length(df)]
  normalize <- function(x) {
    return ((x - min(x)) / (max(x) - min(x)))
  }
  maxmindf <- as.data.frame(lapply(df2, normalize))
  attach(maxmindf)
  df_norm<-as.matrix(maxmindf)
  
  # Determine sample size
  ind <- sample(2, nrow(df_norm), replace=TRUE, prob=c(0.80, 0.20))
  
  # Split the  data(peaks)
  training <- df_norm[ind==1, 1:ncol(df_norm)-1]
  test1 <- df_norm[ind==2, 1:ncol(df_norm)-1]
  training_target <- df_norm[ind==1, ncol(df_norm)]
  test1_target <- df_norm[ind==2, ncol(df_norm)]
  
  #number of nodes in the first hidden layer
  u1_1 = ceiling((1/2) * (ncol(training)+1))
  u2_1 = ceiling(1* (ncol(training)+1))
  u3_1 = ceiling((2/3) * (ncol(training)+1))
  u4_1 = ceiling(2*(ncol(training)))
  
  ####a) Declaring the flags for hyperparameters
  FLAGS = flags(
    flag_numeric("dropout1", 0.05),
    flag_integer("units",u1_1),
    flag_string("activation1", "relu"),
    flag_integer("batchsize1",8), 
    flag_integer("Epoch1",50), 
    flag_numeric("learning_rate", 0.01),
    flag_numeric("val_split",0.2),
    flag_numeric("reg_l1",0.001)
    )
  
  # ####b) Defining the DNN model
build_model<-function() {
  model <- keras_model_sequential()
  model %>%
    layer_dense(units = FLAGS$units, activation = FLAGS$activation1, input_shape = c(dim(training)[2])) %>%
    layer_dropout(rate = FLAGS$dropout1) %>%
    
    layer_dense(units=1, activation ="linear")
  
  #####c) Compiling the DNN model
  model %>% compile(
    loss = "mse",
    optimizer =optimizer_adam(FLAGS$learning_rate),
    metrics = c("mse"))
  model
}
model<-build_model()
model %>% summary()

print_dot_callback <- callback_lambda(
  on_epoch_end = function(epoch, logs) {
    if (epoch %% 80 == 0) cat("\n")
    cat(".")})

early_stop <- callback_early_stopping(monitor = "val_loss", mode='min',patience =20)


###########d) Fitting the DNN model#################
model_Final<-build_model()
model_fit_Final<-model_Final %>% fit(
  training,
  training_target,
  epochs =FLAGS$Epoch1, batch_size = FLAGS$batchsize1,
  shuffled=F,
  validation_split = FLAGS$val_split,
  verbose=0,
  
  callbacks = list(early_stop, print_dot_callback)
  
)


################a) Inner cross-validation##########################

nCVI=5 
Hyperpar = data.frame() #the results of each combination of hyperparameters resulting from each inner partition will be saved
for (i in 1:nCVI){  #do it to choose best parameters
  print("I is:")
  print(i)
  Sam_per=sample(1:nrow(training),nrow(training))
  
  X_trII=training[Sam_per,]
  y_trII=training_target[Sam_per]
  # print(head(X_trII, 3))
  print("----------------------")
  print(head(y_trII,3))
  
  ############b) Grid search using the tuning_run() function of tfruns package########
  runs.sp<-tuning_run(paste0("train.R")
                      ,runs_dir = '_tuningE1'
                      ,flags=list(dropout1 = c(0,0.05),
                                  units = c(u1_1, u2_1),
                                  activation1 = ("relu"),
                                  batchsize1 = c(8, 16),
                                  Epoch1 = c(10,50),
                                  learning_rate = c(0.001),
                                  val_split = c(0.2)),
                      sample = 0.2,
                      confirm = FALSE,
                      echo =F)
  # clean_runs(ls_runs(completed == FALSE))
  #####c) Saving each combination of hyperparameters in the Hyperpar data.frame
  
  runs.sp = runs.sp[order(runs.sp$flag_units,runs.sp$flag_dropout1, runs.sp$flag_batchsize1, runs.sp$flag_Epoch1),]
  
  runs.sp$grid_length = 1:nrow(runs.sp) #we save the grid lenght and also important parameters
  Parameters = data.frame(grid_length=runs.sp$grid_length,
                          metric_val_mse=runs.sp$metric_val_mse,
                          flag_dropout1=runs.sp$flag_dropout1,
                          flag_units=runs.sp$flag_units, 
                          flag_batchsize1=runs.sp$flag_batchsize1,
                          epochs_completed=runs.sp$epochs_completed,
                          flag_learning_rate=runs.sp$flag_learning_rate, 
                          flag_activation1=runs.sp$flag_activation1)
  
  Hyperpar = rbind(Hyperpar,data.frame(Parameters)) #we saved the important parameters   
}
#####d) Summarizing the five inner fold by hyperparameter combination
#the average prediction performance is obtained for each inner fold
Hyperpar %>%
  group_by(grid_length) %>%
  summarise(val_mse=mean(metric_val_mse),
            dropout1=mean(flag_dropout1), 
            units=mean(flag_units),   
            batchsize1=mean(flag_batchsize1), 
            learning_rate=mean(flag_learning_rate),  
            epochs=mean( epochs_completed)) %>%      
  select(grid_length,val_mse,dropout1,units,batchsize1,
         learning_rate, epochs) %>%
  mutate_if(is.numeric, funs(round(., 3)))
Hyperpar_Opt =  Hyperpar 


######e) ############ select the best combinition of hyperparameters 
Min = min(Hyperpar_Opt$val_mse)
pos_opt = which(Hyperpar_Opt$val_mse==Min)
pos_opt=pos_opt[1]
Optimal_Hyper=Hyperpar_Opt[pos_opt,]
#####Selecting the best hyperparameters
Drop_O = Optimal_Hyper$dropout1
Epoch_O = round(Optimal_Hyper$epochs,0)
Units_O = round(Optimal_Hyper$units,0)
activation_O = unique(Hyperpar$flag_activation1)
batchsize_O = round(Optimal_Hyper$batchsize1,0)
lr_O = Optimal_Hyper$learning_rate

print_dot_callback <- callback_lambda(
 on_epoch_end = function(epoch, logs) {
     if (epoch %% 20 == 0) cat("\n")
    cat(".")})

#refitting the model with optimal values
model_Sec<-keras_model_sequential()
model_Sec %>%
  layer_dense(units =Units_O , activation =activation_O, input_shape =
                c(dim(training)[2])) %>%
  layer_dropout(rate =Drop_O) %>%
  layer_dense(units =1, activation =activation_O)
model_Sec %>% compile(
  loss = "mean_squared_error",
  optimizer = optimizer_adam(lr=lr_O),
  metrics = c("mean_squared_error"))

# fit the model with our data
ModelFited<-model_Sec %>% fit(
  X_trII,   y_trII,
  epochs=Epoch_O, batch_size =batchsize_O, #####validation_split=0.2,
  early_stop,
  verbose=0
 ,callbacks=list(print_dot_callback)
)

#############g) Prediction of testing set ##########################
Yhat=model_Sec%>% predict(test1)
y_p=Yhat
y_p_tst =as.numeric(y_p)
#y_tst=y[tst_set]
plot(test1_target,y_p_tst)
MSE=mean((test1_target - y_p_tst)^2)

How can I fix the error?
Thanks in advance

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.