LMLearner raise AttributeError: 'str' object has no attribute 'to'

Hi,
I tried an NLP transfer learning with fast ai version 2.2.5. But I got an AttributeError: ‘str’ object has no attribute ‘to’ error with LMLearner.

    from fastai.data.external import URLs
    from fastai.data.external import untar_data
    from fastai.text.data import TextDataLoaders
    from fastai.text.models.awdlstm import AWD_LSTM
    from fastai.text.learner import language_model_learner
    path = untar_data(URLs.IMDB)
    data_loader = TextDataLoaders.from_folder(path, train='train', valid='test')
    learn_lm = language_model_learner(data_loader, AWD_LSTM)
    learn_lm.predict('Hello')

with error

    AttributeError                            Traceback (most recent call last)
    <ipython-input-16-c86f96a89a3b> in <module>
    ----> 1 learn_lm.predict('Hello')

    /workspace/project/fastai/fastai/text/learner.py in predict(self, text, n_words, no_unk, temperature, min_p, no_bar, decoder, only_last_word)
        156         "Return `text` and the `n_words` that come after"
        157         self.model.reset()
    --> 158         idxs = idxs_all = self.dls.test_dl([text]).items[0].to(self.dls.device)
        159         if no_unk: unk_idx = self.dls.vocab.index(UNK)
        160         for _ in (range(n_words) if no_bar else progress_bar(range(n_words), leave=False)):

    AttributeError: 'str' object has no attribute 'to'

It seems self.dls.test_dl([text]).items[0] returns str format rather than pytorch tensor.
is that line should be data_loader.test_dl('hello').do_item(0)[0].to(data_loader.device)
or I miss something?

Many thanks

You need to pass in a flag is_lm=True to your TextDataLoaders. This tells fastai to load the data ready for the language_model_learner.

1 Like

Thanks for help. I changed the source code in predict function from item to do_item to bypass this issue.
I did not use fast ai before, and the arguments are not explained in docs. It’s a little difficult to find what happened.

The is_lm=True argument is used to the fastai that is should load the text ready for a language modelling task. For example:

path = untar_data(URLs.IMDB)
data_loader = TextDataLoaders.from_folder(path, train='train', valid='test', is_lm=True) #is_lm flag added

Will load the IMDB data in a format that can be used by fastai’s language_model_learner

learn_lm = language_model_learner(data_loader, AWD_LSTM)
learn_lm.predict('Hello',n_words=5)

In this case there is no need to modify the source code for predict unless you also want to modify something else.

Here is a colab notebook with these code examples so you can easily run them.

The Text tutorial is probably the quickest way of getting familiar with the high-level approach to working with text in fastai.