Search code examples
pythonhuggingface-transformershuggingface-tokenizers

This method is deprecated, __call__ should be used instead, how to solve this problem in BERT?


I am trying to use the batch_encode_plus() function from BERT. The problem comes when it says that this function does not exist. I go to the documentation in this page and it says the following: "This method is deprecated, __call__ should be used instead.". I tried to use __call__ as a function but it does not work.

Then I get into this page and it seems that the function batch_encode_plus() was replaced by __call__. But when I use the function is not working. I tried to use encoded_plus() but it does not give the the expected results.

I do not know how to use this __call__ function, any suggestions?


Solution

  • I've been working with this recently. Here is a good look at some documentation for the call function

    call( inputs*args**kwargs ) → A list or a list of list of dict

    Parameters:

    • args (str or List[str]) — One or several texts (or one list of prompts) with masked tokens. targets (str or List[str], optional) — When passed, the model will limit the scores to the passed targets instead of looking up in the whole vocab. If the provided targets are not in the model vocab, they will be tokenized and the first resulting token will be used (with a warning, and that might be slower).
    • top_k (int, optional) — When passed, overrides the number of predictions to return.

    Returns: A list or a list of list of dict

    Here's an example of how I used it. (keep in mind that I'm using the fill-mask mode of BERT which is a one word prediction model and the python language)

        from transformers import pipeline
        predicter = pipeline('fill-mask', model='bert-base-uncased')
        print(predicter.__call__("example string. Hello [MASK]",top_k=1)
    

    The model will predict the word that is tokenized by [MASK] the example output for this is:

        {'score': 0.9415972232818604, 'token': 1037, 'token_str': 'kitty', 'sequence': 'example string. Hello kitty'}