def _get_embbeder(self, weigths_name, special_tokens_fix): embedders = {'bert': PretrainedBertEmbedder( pretrained_model=weigths_name, requires_grad=False, top_layer_only=True, special_tokens_fix=special_tokens_fix) } text_field_embedder = BasicTextFieldEmbedder( token_embedders=embedders, embedder_to_indexer_map={"bert": ["bert", "bert-offsets"]}, allow_unmatched_keys=True) return text_field_embedder
def get_token_embedders(model_name, tune_bert=False, special_tokens_fix=0): take_grads = True if tune_bert > 0 else False # bert参数是否也进行训练. bert_token_emb = PretrainedBertEmbedder( pretrained_model=model_name, top_layer_only=True, requires_grad=take_grads, # 只训练最后一层. special_tokens_fix=special_tokens_fix)# 这里面进行了token扩充.然后也补上了新的初始化权重,也复用了就的权重. token_embedders = {'bert': bert_token_emb} embedder_to_indexer_map = {"bert": ["bert", "bert-offsets"]} text_filed_emd = BasicTextFieldEmbedder(token_embedders=token_embedders, embedder_to_indexer_map=embedder_to_indexer_map, allow_unmatched_keys=True) return text_filed_emd
def get_token_embedders(model_name, tune_bert=False, special_tokens_fix=0): take_grads = True if tune_bert > 0 else False bert_token_emb = PretrainedBertEmbedder( pretrained_model=model_name, top_layer_only=True, requires_grad=take_grads, special_tokens_fix=special_tokens_fix) token_embedders = {'bert': bert_token_emb} embedder_to_indexer_map = {"bert": ["bert", "bert-offsets"]} text_filed_emd = BasicTextFieldEmbedder(token_embedders=token_embedders, embedder_to_indexer_map=embedder_to_indexer_map, allow_unmatched_keys=True) return text_filed_emd