训练期间损失不会减少(Word2Vec,Gensim)
Loss does not decrease during training (Word2Vec, Gensim)
什么会导致 model.get_latest_training_loss()
的损失在每个时期增加?
代码,用于训练:
class EpochSaver(CallbackAny2Vec):
'''Callback to save model after each epoch and show training parameters '''
def __init__(self, savedir):
self.savedir = savedir
self.epoch = 0
os.makedirs(self.savedir, exist_ok=True)
def on_epoch_end(self, model):
savepath = os.path.join(self.savedir, "model_neg{}_epoch.gz".format(self.epoch))
model.save(savepath)
print(
"Epoch saved: {}".format(self.epoch + 1),
"Start next epoch ... ", sep="\n"
)
if os.path.isfile(os.path.join(self.savedir, "model_neg{}_epoch.gz".format(self.epoch - 1))):
print("Previous model deleted ")
os.remove(os.path.join(self.savedir, "model_neg{}_epoch.gz".format(self.epoch - 1)))
self.epoch += 1
print("Model loss:", model.get_latest_training_loss())
def train():
### Initialize model ###
print("Start training Word2Vec model")
workers = multiprocessing.cpu_count()/2
model = Word2Vec(
DocIter(),
size=300, alpha=0.03, min_alpha=0.00025, iter=20,
min_count=10, hs=0, negative=10, workers=workers,
window=10, callbacks=[EpochSaver("./checkpoints")],
compute_loss=True
)
输出:
时期(1 到 20)的损失:
Model loss: 745896.8125
Model loss: 1403872.0
Model loss: 2022238.875
Model loss: 2552509.0
Model loss: 3065454.0
Model loss: 3549122.0
Model loss: 4096209.75
Model loss: 4615430.0
Model loss: 5103492.5
Model loss: 5570137.5
Model loss: 5955891.0
Model loss: 6395258.0
Model loss: 6845765.0
Model loss: 7260698.5
Model loss: 7712688.0
Model loss: 8144109.5
Model loss: 8542560.0
Model loss: 8903244.0
Model loss: 9280568.0
Model loss: 9676936.0
我做错了什么?
语言阿拉伯语。
作为来自 DocIter 的输入 - 带有标记的列表。
从 gensim 3.6.0 开始,报告的损失值可能不是很合理,每次调用 train()
时只重置计数,而不是每个内部纪元。此问题即将进行一些修复:
https://github.com/RaRe-Technologies/gensim/pull/2135
与此同时,先前值与最新值之间的差异可能更有意义。在这种情况下,您的数据表明第一个时期的总损失为 745896,而最后一个时期的总损失为 (9676936-9280568=) 396,368——这可能表明了所希望的进展。
根据gojomo的建议,你可以在回调函数中计算损失的差异:
from gensim.models.callbacks import CallbackAny2Vec
from gensim.models import Word2Vec
# init callback class
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()
if self.epoch == 0:
print('Loss after epoch {}: {}'.format(self.epoch, loss))
else:
print('Loss after epoch {}: {}'.format(self.epoch, loss- self.loss_previous_step))
self.epoch += 1
self.loss_previous_step = loss
为了训练您的模型,在 word2vec 训练方法中添加 computer_loss = True
和 callbacks=[callback()]
:
# init word2vec class
w2v_model = Word2Vec(min_count=20,
window=12
size=100,
workers=2)
# build vovab
w2v_model.build_vocab(sentences)
# train the w2v model
w2v_model.train(senteces,
total_examples=w2v_model.corpus_count,
epochs=10,
report_delay=1,
compute_loss = True, # set compute_loss = True
callbacks=[callback()]) # add the callback class
# save the word2vec model
w2v_model.save('word2vec.model')
这将输出如下内容:
Loss after epoch 0: 4448638.5
Loss after epoch 1: 3283735.5
Loss after epoch 2: 2826198.0
Loss after epoch 3: 2680974.0
Loss after epoch 4: 2601113.0
Loss after epoch 5: 2271333.0
Loss after epoch 6: 2052050.0
Loss after epoch 7: 2011768.0
Loss after epoch 8: 1927454.0
Loss after epoch 9: 1887798.0
什么会导致 model.get_latest_training_loss()
的损失在每个时期增加?
代码,用于训练:
class EpochSaver(CallbackAny2Vec):
'''Callback to save model after each epoch and show training parameters '''
def __init__(self, savedir):
self.savedir = savedir
self.epoch = 0
os.makedirs(self.savedir, exist_ok=True)
def on_epoch_end(self, model):
savepath = os.path.join(self.savedir, "model_neg{}_epoch.gz".format(self.epoch))
model.save(savepath)
print(
"Epoch saved: {}".format(self.epoch + 1),
"Start next epoch ... ", sep="\n"
)
if os.path.isfile(os.path.join(self.savedir, "model_neg{}_epoch.gz".format(self.epoch - 1))):
print("Previous model deleted ")
os.remove(os.path.join(self.savedir, "model_neg{}_epoch.gz".format(self.epoch - 1)))
self.epoch += 1
print("Model loss:", model.get_latest_training_loss())
def train():
### Initialize model ###
print("Start training Word2Vec model")
workers = multiprocessing.cpu_count()/2
model = Word2Vec(
DocIter(),
size=300, alpha=0.03, min_alpha=0.00025, iter=20,
min_count=10, hs=0, negative=10, workers=workers,
window=10, callbacks=[EpochSaver("./checkpoints")],
compute_loss=True
)
输出:
时期(1 到 20)的损失:
Model loss: 745896.8125
Model loss: 1403872.0
Model loss: 2022238.875
Model loss: 2552509.0
Model loss: 3065454.0
Model loss: 3549122.0
Model loss: 4096209.75
Model loss: 4615430.0
Model loss: 5103492.5
Model loss: 5570137.5
Model loss: 5955891.0
Model loss: 6395258.0
Model loss: 6845765.0
Model loss: 7260698.5
Model loss: 7712688.0
Model loss: 8144109.5
Model loss: 8542560.0
Model loss: 8903244.0
Model loss: 9280568.0
Model loss: 9676936.0
我做错了什么?
语言阿拉伯语。 作为来自 DocIter 的输入 - 带有标记的列表。
从 gensim 3.6.0 开始,报告的损失值可能不是很合理,每次调用 train()
时只重置计数,而不是每个内部纪元。此问题即将进行一些修复:
https://github.com/RaRe-Technologies/gensim/pull/2135
与此同时,先前值与最新值之间的差异可能更有意义。在这种情况下,您的数据表明第一个时期的总损失为 745896,而最后一个时期的总损失为 (9676936-9280568=) 396,368——这可能表明了所希望的进展。
根据gojomo的建议,你可以在回调函数中计算损失的差异:
from gensim.models.callbacks import CallbackAny2Vec
from gensim.models import Word2Vec
# init callback class
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()
if self.epoch == 0:
print('Loss after epoch {}: {}'.format(self.epoch, loss))
else:
print('Loss after epoch {}: {}'.format(self.epoch, loss- self.loss_previous_step))
self.epoch += 1
self.loss_previous_step = loss
为了训练您的模型,在 word2vec 训练方法中添加 computer_loss = True
和 callbacks=[callback()]
:
# init word2vec class
w2v_model = Word2Vec(min_count=20,
window=12
size=100,
workers=2)
# build vovab
w2v_model.build_vocab(sentences)
# train the w2v model
w2v_model.train(senteces,
total_examples=w2v_model.corpus_count,
epochs=10,
report_delay=1,
compute_loss = True, # set compute_loss = True
callbacks=[callback()]) # add the callback class
# save the word2vec model
w2v_model.save('word2vec.model')
这将输出如下内容:
Loss after epoch 0: 4448638.5
Loss after epoch 1: 3283735.5
Loss after epoch 2: 2826198.0
Loss after epoch 3: 2680974.0
Loss after epoch 4: 2601113.0
Loss after epoch 5: 2271333.0
Loss after epoch 6: 2052050.0
Loss after epoch 7: 2011768.0
Loss after epoch 8: 1927454.0
Loss after epoch 9: 1887798.0