我在R中使用了Keras API的一个简单应用程序.根据set.seed(value)
的值,它有时会收敛,有时不会收敛.我假设是因为种子设定了最初随机的权重.如果一开始没有收敛,我通常可以通过更改种子值让它收敛到不同的运行,但我必须手动监视/停止它.如果模型在指定时间后没有收敛(例如,在600秒后停止,并使用不同的种子值重新启动),如何停止Keras.
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)