我花了很多时间试图通过尽早停止来获得最佳的神经网络模型。我没有使用TensorFlow或Keras之类的框架,而是我在课堂上开发的代码。 我遵循的步骤是:
self.best_model
更新为self.model
(必须使用Deepcopy)。self.best_model
深度复制到self.model
,然后退出功能。 训练函数不会返回模型,但是在退出模型后,它会通过将self.best_model
深度复制到模型中之前所做的任何操作进行更新,因此self.model = deepcopy(self.best_model)
似乎没有任何作用。>
类似这样的东西:
class Optimiser(object):
def __init__(self, model, early_stop_patience, learning_settings,...):
self.model = model
self.best_model = model
self.patience = self.early_stop_patience
self.min_error = 10000
self.stop = False
...
def check_early_stop(self, valid_error):
if valid_error < self.min_error:
self.best_model = deepcopy(self.model)
self.counter = 0
else:
self.counter += 1
if self.counter == self.patience:
self.model = deepcopy(self.best_model) # This seems to work inside this function.
self.stop = True
def train(self, num_epochs):
for epoch in range(1, num_epochs):
self.do_train_epoch() # This one takes self.model and forward propagates and then back propagates.
valid_error = self.get_epoch_stats() # the datasets are also received by the __init__ method, and uses self.model to forward propagate them and calculate error.
self.check_early_stop(valid_error)
if self.stop:
break
model = create_model(layers, weights, bias)
optimiser = Optimiser(model, early_stop_patience, learning_settings,...)
optimiser.train(num_epochs=50)
model.forward_prop(test_set, targets)
## This model should be the one saved when self.check_early_stop
## found a new self.min_error, but is the last one actually,
## when it ran out of patience (as if
## self.model hadn't been updated by self.best_model
## that was found self.patience epochs befores.
## Unless I do return self.model (after updating it with self.best_model),
## in which case I would have to do something like
## model, stats, ..., stuff = optimiser.train(num_epochs=50)
## I won't see it in my model variable being updated.
代码中还有更多内容,实际上我在中断之后返回了一些统计信息。
我使用dill
将self.model
和self.best_model
都写到一个文件中并加载它们,这正是我所期望的,只是没有使用该函数。
也许代码或我的解释不清楚,如果您有兴趣的话,我可以私下向您展示更多代码。
任何帮助将不胜感激!
谢谢!
编辑:对底部代码块的注释进行了一些更正