def load_from(cls, other_model):
"""
Import data and parameter values from other model
:param other_model: A ``LabeledWord2Vec`` object, or a ``Word2Vec`` or ``KeyedVectors`` object of Gensim
"""
softmax = getattr(other_model, 'softmax', False)
if softmax:
loss = 'softmax'
elif not other_model.hs and other_model.negative:
loss = 'ns'
else:
loss = 'hs'
new_model = LabeledWord2Vec(
loss=loss,
negative=other_model.negative if loss == 'ns' else 0,
size=other_model.vector_size,
seed=other_model.seed
)
new_model.reset_from(other_model)
for attr in vars(other_model):
if hasattr(new_model, attr):
if not isinstance(other_model, LabeledWord2Vec) and (attr == 'syn1' or attr == 'syn1neg'):
continue
value = getattr(other_model, attr, getattr(new_model, attr))
if isinstance(value, KeyedVectors):
new_model.wv.syn0 = value.syn0
new_model.wv.syn0norm = value.syn0norm
else:
setattr(new_model, attr, value)
return new_model
评论列表
文章目录