Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gensim word2vec print log loss

how to print to log (file or stout) the loss of each epoch in the training phase, when using gensim word2vec model.

I tried :

 logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s')
 logging.root.setLevel(level=logging.INFO)

But I didn't saw any loss printing.

like image 385
Dkova Avatar asked Dec 13 '22 12:12

Dkova


1 Answers

You can get the latest training loss of a word2vec model with the method get_latest_training_loss(). If you want to print the loss after every epoch you can add a callback that does this. For example:

from gensim.test.utils import common_texts, get_tmpfile
from gensim.models import Word2Vec
from gensim.models.callbacks import CallbackAny2Vec

class callback(CallbackAny2Vec):
    '''Callback to print loss after each epoch.'''

    def __init__(self):
        self.epoch = 0

    def on_epoch_end(self, model):
        loss = model.get_latest_training_loss()
        print('Loss after epoch {}: {}'.format(self.epoch, loss))
        self.epoch += 1

model = Word2Vec(common_texts, size=100, window=5, min_count=1, 
                 compute_loss=True, callbacks=[callback()])

However, the loss is computed in a cumulative way (i.e. the loss that gets printed after each epoch is the total loss of all epochs so far). See gojomo's answer here for more explanation.

like image 66
Anna Krogager Avatar answered Dec 29 '22 05:12

Anna Krogager