# Buggy code
def predict(
    self, sentences: Union[List[Sentence], Sentence], mini_batch_size=32
) -> List[Sentence]:
    if type(sentences) is Sentence:
        sentences = [sentences]

    # make mini-batches
    batches = [
        sentences[x : x + mini_batch_size]
        for x in range(0, len(sentences), mini_batch_size)
    ]

    for batch in batches:
        score, tag_seq = self._predict_scores_batch(batch)
        predicted_id = tag_seq
        all_tokens = []
        for sentence in batch:
            all_tokens.extend(sentence.tokens)

        for token, pred_id in zip(all_tokens, predicted_id):
            token: Token = token
            # get the predicted tag
            predicted_tag = self.tag_dictionary.get_item_for_index(pred_id)
            token.add_tag(self.tag_type, predicted_tag)

    return sentences


# Traceback (OSError)
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-4-4d86fef1ef40> in <module>()
----> 1 tagger = SequenceTagger.load('ner-ontonotes')

~/.virtualenvs/py36/lib/python3.6/site-packages/flair/tagging_model.py in load(model)
459
460         if model_file is not None:
--> 461             tagger: SequenceTagger = SequenceTagger.load_from_file(model_file)
462             return tagger
463

~/.virtualenvs/py36/lib/python3.6/site-packages/flair/tagging_model.py in load_from_file(cls, model_file)
123         # serialization of torch objects
124         warnings.filterwarnings("ignore")
--> 125         state = torch.load(model_file, map_location={'cuda:0': 'cpu'})
126         warnings.filterwarnings("default")
127

~/.virtualenvs/py36/lib/python3.6/site-packages/torch/serialization.py in load(f, map_location, pickle_module)
301         f = open(f, 'rb')
302     try:
--> 303         return _load(f, map_location, pickle_module)
304     finally:
305         if new_fd:

~/.virtualenvs/py36/lib/python3.6/site-packages/torch/serialization.py in _load(f, map_location, pickle_module)
467     unpickler = pickle_module.Unpickler(f)
468     unpickler.persistent_load = persistent_load
--> 469     result = unpickler.load()
470
471     deserialized_storage_keys = pickle_module.load(f)

OSError: [Errno 22] Invalid argument

# Fixed code
def predict(
    self, sentences: Union[List[Sentence], Sentence], mini_batch_size=32
) -> List[Sentence]:
    if type(sentences) is Sentence:
        sentences = [sentences]

    # remove previous embeddings
    clear_embeddings(sentences)

    # make mini-batches
    batches = [
        sentences[x : x + mini_batch_size]
        for x in range(0, len(sentences), mini_batch_size)
    ]

    for batch in batches:
        score, tag_seq = self._predict_scores_batch(batch)
        predicted_id = tag_seq
        all_tokens = []
        for sentence in batch:
            all_tokens.extend(sentence.tokens)

        for token, pred_id in zip(all_tokens, predicted_id):
            token: Token = token
            # get the predicted tag
            predicted_tag = self.tag_dictionary.get_item_for_index(pred_id)
            token.add_tag(self.tag_type, predicted_tag)

    return sentences