Keras键盘中断停止训练了吗?

时间:2018-10-31 15:47:40

标签: keras deep-learning

在tensorflow中似乎有一种方法:Keyboard interrupt tensorflow run and save at that point

在Keras中有这样的东西吗?

2 个答案:

答案 0 :(得分:7)

您可以捕获b:{2}异常并将模型保存在{a:{1,4},b:{2},c:{3},d:{5}}块中:

KeyboardInterrupt

答案 1 :(得分:0)

我发现的最佳方法是使用屏幕上的鼠标位置作为输入。

在下面的示例中,如果将鼠标移到(x <10)的左边缘,角膜将停止:

def queryMousePosition():
    from ctypes import windll, Structure, c_long, byref
    class POINT(Structure): _fields_ = [("x", c_long), ("y", c_long)]
    pt = POINT()
    windll.user32.GetCursorPos(byref(pt))
    return pt.x, pt.y  # %timeit queryMousePosition()


class TerminateOnFlag(keras.callbacks.Callback):
    def on_batch_end(self, batch, logs=None):
        mouse_x, mouse_y = queryMousePosition()
        if mouse_x < 10:
            self.model.stop_training = True

callbacks=[keras.callbacks.ReduceLROnPlateau(), TerminateOnFlag()]

model.fit_generator(..., callbacks=callbacks, ...)

(您可以轻松地添加其他种类的在线交互,并以鼠标位置作为输入...)