# Buggy code
def _add_embeddings_internal(self, sentences: List[Sentence]) -> List[Sentence]:
    for i, sentence in enumerate(sentences):
        language_code = sentence.get_language_code()
        print(language_code)
        supported = [
            "en",
            "de",
            "bg",
            "ca",
            "hr",
            "cs",
            "da",
            "nl",
            "et",
            "fi",
            "fr",
            "el",
            "he",
            "hu",
            "id",
            "it",
            "mk",
            "no",
            "pl",
            "pt",
            "ro",
            "ru",
            "sk",
        ]
        if language_code not in supported:
            language_code = "en"

        if language_code not in self.language_embeddings:
            log.info(f"Loading up MUSE embeddings for '{language_code}'!")
            # download if necessary
            webpath = "https://alan-nlp.s3.eu-central-1.amazonaws.com/resources/embeddings-muse"
            cache_dir = Path("embeddings") / "MUSE"
            cached_path(
                f"{webpath}/muse.{language_code}.vec.gensim.vectors.npy",
                cache_dir=cache_dir,
            )
            embeddings_file = cached_path(
                f"{webpath}/muse.{language_code}.vec.gensim", cache_dir=cache_dir
            )

            # load the model
            self.language_embeddings[language_code] = gensim.models.KeyedVectors.load(
                str(embeddings_file)
            )

        for token, token_idx in zip(sentence.tokens, range(len(sentence.tokens))):
            if "field" not in self.__dict__ or self.field is None:
                word = token.text
            else:
                word = token.get_tag(self.field).value

            word_embedding = self.get_cached_vec(language_code=language_code, word=word)

            token.set_embedding(self.name, word_embedding)

    return sentences


# Traceback (AttributeError)
2019-09-17 11:47:41,099 read text file with 4 lines
2019-09-17 11:47:41,107 read text file with 4 lines
2019-09-17 11:47:41,205 read text file with 78280 lines
2019-09-17 11:47:41,265 shuffled
2019-09-17 11:48:08,470 Sequence length is 100
2019-09-17 11:48:08,502 Split 1  - (11:48:08)
2019-09-17 11:48:13,678 | split   1 /  1 |   100/  359 batches | ms/batch 51.60 | loss  1.50 | ppl     4.48
2019-09-17 11:48:18,817 | split   1 /  1 |   200/  359 batches | ms/batch 51.39 | loss  1.30 | ppl     3.67
2019-09-17 11:48:23,978 | split   1 /  1 |   300/  359 batches | ms/batch 51.60 | loss  1.24 | ppl     3.44
2019-09-17 11:48:27,022 18 seconds for train split 1
Traceback (most recent call last):
File "main.py", line 192, in <module>
main()
File "main.py", line 188, in main
fine_tune()
File "main.py", line 180, in fine_tune
flair_stuff.fine_tune(corpus_dir, stored_dir, model_name=model_name)
File "/home/nolan/code/lang_model_builder/flair_stuff.py", line 314, in fine_tune
checkpoint=True)
File "/home/nolan/anaconda2/envs/py37/lib/python3.7/site-packages/flair/trainers/language_model_trainer.py", line 428, in train
val_loss = self.evaluate(val_data, mini_batch_size, sequence_length)
File "/home/nolan/anaconda2/envs/py37/lib/python3.7/site-packages/flair/trainers/language_model_trainer.py", line 511, in evaluate
return total_loss.item() / len(data_source)
AttributeError: 'int' object has no attribute 'item'

# Fixed code
def _add_embeddings_internal(self, sentences: List[Sentence]) -> List[Sentence]:
    for i, sentence in enumerate(sentences):
        language_code = sentence.get_language_code()
        supported = [
            "en",
            "de",
            "bg",
            "ca",
            "hr",
            "cs",
            "da",
            "nl",
            "et",
            "fi",
            "fr",
            "el",
            "he",
            "hu",
            "id",
            "it",
            "mk",
            "no",
            "pl",
            "pt",
            "ro",
            "ru",
            "sk",
        ]
        if language_code not in supported:
            language_code = "en"

        if language_code not in self.language_embeddings:
            log.info(f"Loading up MUSE embeddings for '{language_code}'!")
            # download if necessary
            webpath = "https://alan-nlp.s3.eu-central-1.amazonaws.com/resources/embeddings-muse"
            cache_dir = Path("embeddings") / "MUSE"
            cached_path(
                f"{webpath}/muse.{language_code}.vec.gensim.vectors.npy",
                cache_dir=cache_dir,
            )
            embeddings_file = cached_path(
                f"{webpath}/muse.{language_code}.vec.gensim", cache_dir=cache_dir
            )

            # load the model
            self.language_embeddings[language_code] = gensim.models.KeyedVectors.load(
                str(embeddings_file)
            )

        for token, token_idx in zip(sentence.tokens, range(len(sentence.tokens))):
            if "field" not in self.__dict__ or self.field is None:
                word = token.text
            else:
                word = token.get_tag(self.field).value

            word_embedding = self.get_cached_vec(language_code=language_code, word=word)

            token.set_embedding(self.name, word_embedding)

    return sentences