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


Python elmo._ElmoCharacterEncoder方法代碼示例

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


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

示例1: __init__

# 需要導入模塊: from allennlp.modules import elmo [as 別名]
# 或者: from allennlp.modules.elmo import _ElmoCharacterEncoder [as 別名]
def __init__(self):
        from allennlp.modules.elmo import _ElmoCharacterEncoder
        if not path.isdir(self.path('elmo')):
            makedirs(self.path('elmo'))
        self.fweights = self.ensure_file(path.join('elmo', 'weights.hdf5'), url=self.settings['weights'])
        self.foptions = self.ensure_file(path.join('elmo', 'options.json'), url=self.settings['options'])
        self.embeddings = _ElmoCharacterEncoder(self.foptions, self.fweights) 
開發者ID:vzhong,項目名稱:embeddings,代碼行數:9,代碼來源:elmo.py

示例2: test_elmo_token_representation

# 需要導入模塊: from allennlp.modules import elmo [as 別名]
# 或者: from allennlp.modules.elmo import _ElmoCharacterEncoder [as 別名]
def test_elmo_token_representation(self):
        # Load the test words and convert to char ids
        with open(os.path.join(self.elmo_fixtures_path, "vocab_test.txt"), "r") as fin:
            words = fin.read().strip().split("\n")

        vocab = Vocabulary()
        indexer = ELMoTokenCharactersIndexer()
        tokens = [Token(word) for word in words]

        indices = indexer.tokens_to_indices(tokens, vocab)
        # There are 457 tokens. Reshape into 10 batches of 50 tokens.
        sentences = []
        for k in range(10):
            char_indices = indices["elmo_tokens"][(k * 50) : ((k + 1) * 50)]
            sentences.append(
                indexer.as_padded_tensor_dict(
                    {"elmo_tokens": char_indices}, padding_lengths={"elmo_tokens": 50}
                )["elmo_tokens"]
            )
        batch = torch.stack(sentences)

        elmo_token_embedder = _ElmoCharacterEncoder(self.options_file, self.weight_file)
        elmo_token_embedder_output = elmo_token_embedder(batch)

        # Reshape back to a list of words and compare with ground truth.  Need to also
        # remove <S>, </S>
        actual_embeddings = remove_sentence_boundaries(
            elmo_token_embedder_output["token_embedding"], elmo_token_embedder_output["mask"]
        )[0].data.numpy()
        actual_embeddings = actual_embeddings.reshape(-1, actual_embeddings.shape[-1])

        embedding_file = os.path.join(self.elmo_fixtures_path, "elmo_token_embeddings.hdf5")
        with h5py.File(embedding_file, "r") as fin:
            expected_embeddings = fin["embedding"][...]

        assert numpy.allclose(actual_embeddings[: len(tokens)], expected_embeddings, atol=1e-6) 
開發者ID:allenai,項目名稱:allennlp,代碼行數:38,代碼來源:elmo_test.py

示例3: test_elmo_token_representation_bos_eos

# 需要導入模塊: from allennlp.modules import elmo [as 別名]
# 或者: from allennlp.modules.elmo import _ElmoCharacterEncoder [as 別名]
def test_elmo_token_representation_bos_eos(self):
        # The additional <S> and </S> embeddings added by the embedder should be as expected.
        indexer = ELMoTokenCharactersIndexer()

        elmo_token_embedder = _ElmoCharacterEncoder(self.options_file, self.weight_file)

        for correct_index, token in [[0, "<S>"], [2, "</S>"]]:
            indices = indexer.tokens_to_indices([Token(token)], Vocabulary())
            indices = torch.from_numpy(numpy.array(indices["elmo_tokens"])).view(1, 1, -1)
            embeddings = elmo_token_embedder(indices)["token_embedding"]
            assert numpy.allclose(
                embeddings[0, correct_index, :].data.numpy(), embeddings[0, 1, :].data.numpy()
            ) 
開發者ID:allenai,項目名稱:allennlp,代碼行數:15,代碼來源:elmo_test.py

示例4: test_elmo_token_representation

# 需要導入模塊: from allennlp.modules import elmo [as 別名]
# 或者: from allennlp.modules.elmo import _ElmoCharacterEncoder [as 別名]
def test_elmo_token_representation(self):
        # Load the test words and convert to char ids
        with open(os.path.join(self.elmo_fixtures_path, u'vocab_test.txt'), u'r') as fin:
            words = fin.read().strip().split(u'\n')

        vocab = Vocabulary()
        indexer = ELMoTokenCharactersIndexer()
        tokens = [Token(word) for word in words]

        indices = indexer.tokens_to_indices(tokens, vocab, u"elmo")
        # There are 457 tokens. Reshape into 10 batches of 50 tokens.
        sentences = []
        for k in range(10):
            char_indices = indices[u"elmo"][(k * 50):((k + 1) * 50)]
            sentences.append(
                    indexer.pad_token_sequence(
                            {u'key': char_indices}, desired_num_tokens={u'key': 50}, padding_lengths={}
                    )[u'key']
            )
        batch = torch.from_numpy(numpy.array(sentences))

        elmo_token_embedder = _ElmoCharacterEncoder(self.options_file, self.weight_file)
        elmo_token_embedder_output = elmo_token_embedder(batch)

        # Reshape back to a list of words and compare with ground truth.  Need to also
        # remove <S>, </S>
        actual_embeddings = remove_sentence_boundaries(
                elmo_token_embedder_output[u'token_embedding'],
                elmo_token_embedder_output[u'mask']
        )[0].data.numpy()
        actual_embeddings = actual_embeddings.reshape(-1, actual_embeddings.shape[-1])

        embedding_file = os.path.join(self.elmo_fixtures_path, u'elmo_token_embeddings.hdf5')
        with h5py.File(embedding_file, u'r') as fin:
            expected_embeddings = fin[u'embedding'][...]

        assert numpy.allclose(actual_embeddings[:len(tokens)], expected_embeddings, atol=1e-6) 
開發者ID:plasticityai,項目名稱:magnitude,代碼行數:39,代碼來源:elmo_test.py

示例5: test_elmo_token_representation_bos_eos

# 需要導入模塊: from allennlp.modules import elmo [as 別名]
# 或者: from allennlp.modules.elmo import _ElmoCharacterEncoder [as 別名]
def test_elmo_token_representation_bos_eos(self):
        # The additional <S> and </S> embeddings added by the embedder should be as expected.
        indexer = ELMoTokenCharactersIndexer()

        elmo_token_embedder = _ElmoCharacterEncoder(self.options_file, self.weight_file)

        for correct_index, token in [[0, u'<S>'], [2, u'</S>']]:
            indices = indexer.tokens_to_indices([Token(token)], Vocabulary(), u"correct")
            indices = torch.from_numpy(numpy.array(indices[u"correct"])).view(1, 1, -1)
            embeddings = elmo_token_embedder(indices)[u'token_embedding']
            assert numpy.allclose(embeddings[0, correct_index, :].data.numpy(), embeddings[0, 1, :].data.numpy()) 
開發者ID:plasticityai,項目名稱:magnitude,代碼行數:13,代碼來源:elmo_test.py


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