title: How to fix "sink stack is full" over training a model in R
body: <p>I want to train a regression model by &quot;keras_model_sequential&quot; in R. For finding the best parameters over the grid search I have used &quot;tuning_run&quot;. But I got this error:</p>
  <p>training run 1/128 (flags = list(0.05, 66, &quot;relu&quot;, 8, 10, 0.001, 0.2))
  Error in sink(file = output_file, type = &quot;output&quot;, split = TRUE) :</p>
  <p>sink stack is full
  Calls: tuning_run ... with_changed_file_copy -&gt; force -&gt; sink -&gt; .handleSimpleError -&gt; h</p>
  <p>I need to mention, that a folder named &quot;runs&quot; 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.</p>
  <pre><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 &lt;- function(x) {
      return ((x - min(x)) / (max(x) - min(x)))
    }
    maxmindf &lt;- as.data.frame(lapply(df2, normalize))
    attach(maxmindf)
    df_norm&lt;-as.matrix(maxmindf)
    
    # Determine sample size
    ind &lt;- sample(2, nrow(df_norm), replace=TRUE, prob=c(0.80, 0.20))
    
    # Split the  data(peaks)
    training &lt;- df_norm[ind==1, 1:ncol(df_norm)-1]
    test1 &lt;- df_norm[ind==2, 1:ncol(df_norm)-1]
    training_target &lt;- df_norm[ind==1, ncol(df_norm)]
    test1_target &lt;- 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(&quot;dropout1&quot;, 0.05),
      flag_integer(&quot;units&quot;,u1_1),
      flag_string(&quot;activation1&quot;, &quot;relu&quot;),
      flag_integer(&quot;batchsize1&quot;,8), 
      flag_integer(&quot;Epoch1&quot;,50), 
      flag_numeric(&quot;learning_rate&quot;, 0.01),
      flag_numeric(&quot;val_split&quot;,0.2),
      flag_numeric(&quot;reg_l1&quot;,0.001)
      )
    
    # ####b) Defining the DNN model
  build_model&lt;-function() {
    model &lt;- keras_model_sequential()
    model %&gt;%
      layer_dense(units = FLAGS$units, activation = FLAGS$activation1, input_shape = c(dim(training)[2])) %&gt;%
      layer_dropout(rate = FLAGS$dropout1) %&gt;%
      
      layer_dense(units=1, activation =&quot;linear&quot;)
    
    #####c) Compiling the DNN model
    model %&gt;% compile(
      loss = &quot;mse&quot;,
      optimizer =optimizer_adam(FLAGS$learning_rate),
      metrics = c(&quot;mse&quot;))
    model
  }
  model&lt;-build_model()
  model %&gt;% summary()
  
  print_dot_callback &lt;- callback_lambda(
    on_epoch_end = function(epoch, logs) {
      if (epoch %% 80 == 0) cat(&quot;\n&quot;)
      cat(&quot;.&quot;)})
  
  early_stop &lt;- callback_early_stopping(monitor = &quot;val_loss&quot;, mode='min',patience =20)
  
  
  ###########d) Fitting the DNN model#################
  model_Final&lt;-build_model()
  model_fit_Final&lt;-model_Final %&gt;% 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(&quot;I is:&quot;)
    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(&quot;----------------------&quot;)
    print(head(y_trII,3))
    
    ############b) Grid search using the tuning_run() function of tfruns package########
    runs.sp&lt;-tuning_run(paste0(&quot;train.R&quot;)
                        ,runs_dir = '_tuningE1'
                        ,flags=list(dropout1 = c(0,0.05),
                                    units = c(u1_1, u2_1),
                                    activation1 = (&quot;relu&quot;),
                                    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 %&gt;%
    group_by(grid_length) %&gt;%
    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)) %&gt;%      
    select(grid_length,val_mse,dropout1,units,batchsize1,
           learning_rate, epochs) %&gt;%
    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 &lt;- callback_lambda(
   on_epoch_end = function(epoch, logs) {
       if (epoch %% 20 == 0) cat(&quot;\n&quot;)
      cat(&quot;.&quot;)})
  
  #refitting the model with optimal values
  model_Sec&lt;-keras_model_sequential()
  model_Sec %&gt;%
    layer_dense(units =Units_O , activation =activation_O, input_shape =
                  c(dim(training)[2])) %&gt;%
    layer_dropout(rate =Drop_O) %&gt;%
    layer_dense(units =1, activation =activation_O)
  model_Sec %&gt;% compile(
    loss = &quot;mean_squared_error&quot;,
    optimizer = optimizer_adam(lr=lr_O),
    metrics = c(&quot;mean_squared_error&quot;))
  
  # fit the model with our data
  ModelFited&lt;-model_Sec %&gt;% 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%&gt;% 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)
  
  </code></pre>
  <p>Do you have any ideas?
  Thanks in advance.</p>