title: Fixing initial weights when training Keras model in R
body: <p>I want to fix the weights of the model I'm training to get reproducible results for the report. The problem is that on different runs I get similar but slightly different results and training sometimes takes 600 epochs, sometimes takes 3500 using <code>callback_early_stopping</code> monitoring validation MSE and with min_delta of 0.00003.</p>
  <p>Overall, I'm happy enough with results of all runs, but just need to find if there's a way to get reproducible results by fixing weights.</p>
  <p>I tried setting seed at various parts of the process - before creating model, before compiling it and before training but nothing seems to work. Any way to do it?</p>
  <pre><code>BATCH &lt;- nrow(x_train)
  SHAPE &lt;- ncol(x_train)
  
  # Create a neural network model
  set.seed(42)
  model &lt;- keras_model_sequential()
  model %&gt;% 
      layer_dense(units = 12, activation = &quot;relu&quot;, input_shape = c(SHAPE)) %&gt;%
      layer_dense(units = 24, activation = &quot;relu&quot;) %&gt;%
      layer_dense(units = 1, activation = &quot;linear&quot;)
  
  #print model summary
  print(summary(model))
  
  # initialise early stopping callback and optimiser
  early_stoping &lt;- callback_early_stopping(
      monitor = &quot;val_loss&quot;,
      min_delta = 0.00003,
      patience = 50,
      restore_best_weights = TRUE
  )
  optim &lt;- optimizer_adam(learning_rate = 0.00005)
  set.seed(42)
  model %&gt;% compile(
      optimizer = optim,
      loss = &quot;mse&quot;,
      metrics = c(&quot;mse&quot;, &quot;mae&quot;)
  )
  
  # fit model
  set.seed(42)
  val_data &lt;- list(x_val = x_val, y_val = y_val)
  hist &lt;- model %&gt;% fit(
      x = x_train,
      y = y_train,
      batch_size = BATCH,
      epochs = 6000,
      validation_data = val_data,
      shuffle = FALSE,
      callbacks = early_stoping
  )
  
  </code></pre>