當前位置: 首頁>>代碼示例>>Python>>正文


Python embeddings.StackedEmbeddings方法代碼示例

本文整理匯總了Python中flair.embeddings.StackedEmbeddings方法的典型用法代碼示例。如果您正苦於以下問題:Python embeddings.StackedEmbeddings方法的具體用法?Python embeddings.StackedEmbeddings怎麽用?Python embeddings.StackedEmbeddings使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在flair.embeddings的用法示例。


在下文中一共展示了embeddings.StackedEmbeddings方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: load_flair

# 需要導入模塊: from flair import embeddings [as 別名]
# 或者: from flair.embeddings import StackedEmbeddings [as 別名]
def load_flair(mode = 'flair'):
    if mode == 'flair':
        stacked_embeddings = StackedEmbeddings([
            WordEmbeddings('glove'),
            PooledFlairEmbeddings('news-forward', pooling='min'),
            PooledFlairEmbeddings('news-backward', pooling='min')
        ])
    else:##bert
        stacked_embeddings = BertEmbeddings('bert-base-uncased')  ##concat last 4 layers give the best
    return stacked_embeddings 
開發者ID:allanj,項目名稱:ner_with_dependency,代碼行數:12,代碼來源:preflair.py

示例2: __init__

# 需要導入模塊: from flair import embeddings [as 別名]
# 或者: from flair.embeddings import StackedEmbeddings [as 別名]
def __init__(self, device_number='cuda:2', use_cuda = True):
        
        self.device_number = device_number
        
        if use_cuda:
            flair.device = torch.device(self.device_number) 
        
        self.stacked_embeddings = StackedEmbeddings([WordEmbeddings('glove'), 
                                        FlairEmbeddings('news-forward'), 
                                        FlairEmbeddings('news-backward'),
                                        ]) 
開發者ID:uhh-lt,項目名稱:bert-sense,代碼行數:13,代碼來源:Flair_Model.py

示例3: main

# 需要導入模塊: from flair import embeddings [as 別名]
# 或者: from flair.embeddings import StackedEmbeddings [as 別名]
def main(data_folder: str, model_folder: str, dev_size: float, nb_epochs: int,
         nb_segment: Optional[int], segment: Optional[int]) -> None:
    nlp = spacy.blank('fr')
    nlp.tokenizer = get_tokenizer(nlp)

    corpus: Corpus = prepare_flair_train_test_corpus(spacy_model=nlp, data_folder=data_folder, dev_size=dev_size,
                                                     nb_segment=nb_segment, segment=segment)
    tag_dictionary = corpus.make_tag_dictionary(tag_type='ner')
    print(tag_dictionary.idx2item)

    embedding_types: List[TokenEmbeddings] = [
        WordEmbeddings('fr'),
        FlairEmbeddings('fr-forward'),
        FlairEmbeddings('fr-backward'),
    ]

    embeddings: StackedEmbeddings = StackedEmbeddings(embeddings=embedding_types)

    tagger: SequenceTagger = SequenceTagger(hidden_size=256,
                                            embeddings=embeddings,
                                            use_crf=True,
                                            tag_dictionary=tag_dictionary,
                                            tag_type='ner')

    trainer: ModelTrainer = ModelTrainer(model=tagger, corpus=corpus, use_tensorboard=True)

    trainer.train(model_folder,
                  max_epochs=nb_epochs,
                  mini_batch_size=32,
                  embeddings_storage_mode="cpu",
                  checkpoint=False,
                  ) 
開發者ID:ELS-RD,項目名稱:anonymisation,代碼行數:34,代碼來源:flair_train.py

示例4: load_context_embeddings_with_flair

# 需要導入模塊: from flair import embeddings [as 別名]
# 或者: from flair.embeddings import StackedEmbeddings [as 別名]
def load_context_embeddings_with_flair(direction='bi', word_embeddings=True,
                                       cache_dir=DEFAULT_CACHE_DIR,
                                       verbose=False):
    """
    :param bidirectional:
    :param cache_dir:
    :param verbose:
    """
    from flair.embeddings import FlairEmbeddings
    from flair.embeddings import WordEmbeddings
    from flair.embeddings import StackedEmbeddings

    embeddings = []

    if word_embeddings:
        fasttext_embedding = WordEmbeddings('da')
        embeddings.append(fasttext_embedding)

    if direction == 'bi' or direction == 'fwd':
        fwd_weight_path = download_model('flair.fwd', cache_dir,
                                         verbose=verbose,
                                         process_func=_unzip_process_func)
        embeddings.append(FlairEmbeddings(fwd_weight_path))

    if direction == 'bi' or direction == 'bwd':
        bwd_weight_path = download_model('flair.bwd', cache_dir,
                                         verbose=verbose,
                                         process_func=_unzip_process_func)
        embeddings.append(FlairEmbeddings(bwd_weight_path))

    if len(embeddings) == 1:
        return embeddings[0]

    return StackedEmbeddings(embeddings=embeddings) 
開發者ID:alexandrainst,項目名稱:danlp,代碼行數:36,代碼來源:embeddings.py


注:本文中的flair.embeddings.StackedEmbeddings方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。