Esempio n. 1
0
    def from_params(cls,
                    vocab: Vocabulary,
                    params: Params,
                    constructor_to_call=None,
                    constructor_to_inspect=None) -> 'BertModel':
        #initialize the class using JSON params
        embedder_params = params.pop("text_field_embedder")
        token_params = embedder_params.pop("tokens")
        embedding = PretrainedTransformerEmbedder.from_params(
            vocab=vocab, params=token_params)
        text_field_embedder = BasicTextFieldEmbedder(
            token_embedders={'tokens': embedding})
        #         text_field_embedder = TextFieldEmbedder.from_params(vocab, embedder_params)

        seq2vec_encoder_params = params.pop("seq2vec_encoder")
        seq2vec_encoder = Seq2VecEncoder.from_params(seq2vec_encoder_params)

        initializer = InitializerApplicator(
        )  #.from_params(params.pop("initializer", []))

        params.assert_empty(cls.__name__)
        #         print(cls)
        return cls(vocab=vocab,
                   text_field_embedder=text_field_embedder,
                   seq2vec_encoder=seq2vec_encoder,
                   initializer=initializer)
 def test_forward_runs_when_initialized_from_params(self):
     # This code just passes things off to pytorch-transformers, so we only have a very simple
     # test.
     params = Params({'model_name': 'bert-base-uncased'})
     embedder = PretrainedTransformerEmbedder.from_params(params)
     tensor = torch.randint(0, 100, (1, 4))
     output = embedder(tensor)
     assert tuple(output.size()) == (1, 4, 768)
 def test_forward_runs_when_initialized_from_params(self):
     # This code just passes things off to `transformers`, so we only have a very simple
     # test.
     params = Params({"model_name": "bert-base-uncased"})
     embedder = PretrainedTransformerEmbedder.from_params(params)
     token_ids = torch.randint(0, 100, (1, 4))
     mask = torch.randint(0, 2, (1, 4))
     output = embedder(token_ids=token_ids, mask=mask)
     assert tuple(output.size()) == (1, 4, 768)