Skip to content Skip to sidebar Skip to footer

How To Break The Word2vec Training From A Callback Function?

I am training a skipgram model using gensim word2vec. I would like to exit the training before reaching the number of epochs passed in the parameters based on a specific accuracy t

Solution 1:

If in fact more training makes your Word2Vec model worse on some external evaluation, there is likely something else wrong with your setup. (For example, many many online code examples that call train() multiple times in a loop mismanage the learning-rate alpha such that it actually goes negative, which would mean each training-example results in anti-corrections to the model via backpropagation.)

If instead the main problem is truly overfitting, a better solution than conditional early-stopping would probably be adjusting other parameters, such as the model size, so that it can't overshoot useful generalization no matter how many training passes are made.

But if you really want to try the less-good approach of early stopping, you could potentially raise a catchable exception in your callback, and catch it outside train() to allow your other code to continue with the results of the aborted training. For example...

A custom exception...

classOverfitException(Exception):
    pass

...then in your callback...

    raise OverfitException()

...and around training...

try:
    model.train(...)
except OverfitException:
    print("training cut short")
# ... & your code with partially-trained model continues

But again, this is not the best way to deal with overfitting or other cases where more training is seeming to hurt evaluation-scores.

Post a Comment for "How To Break The Word2vec Training From A Callback Function?"