I am using a straightforward application of the Keras API in R. Depending on the set.seed(value)
value, sometimes it will converge and sometimes it won't. I assume because the seed sets the initially randomized weights. If it doesn't converge at first, I can usually get it to converge on a different run by changing the seed value, but I have to monitor/stop it manually. How can I stop Keras if the model hasn't converged after a specified time (e.g., stop it after 600 seconds and restart it with a different seed value).
set.seed(42)
x <- as.matrix(train_data)
y <- as.matrix(train_targets)
model = keras_model_sequential() %>%
layer_dense(units=64, kernel_regularizer=regularizer_l2(0.001), activation="relu", input_shape=dim(train_data)[[2]]) %>%
layer_dense(units=32, kernel_regularizer=regularizer_l2(0.001), activation = "relu") %>%
layer_dense(units=1, activation="linear")
model %>% compile(
loss = "mse",
optimizer = "rmsprop",
metrics = list("mae")
)
model %>% fit(x, y, epochs = 50,verbose = 0)
CodePudding user response:
One option is to define a function that calls itself, perhaps performing an action like setting seed before doing so. Based on