--- title: modeling.question_answering keywords: fastai sidebar: home_sidebar summary: "This module contains custom models, loss functions, custom splitters, etc... for question answering tasks" description: "This module contains custom models, loss functions, custom splitters, etc... for question answering tasks" nb_path: "nbs/02b_modeling-question-answering.ipynb" ---
{% raw %}
{% endraw %} {% raw %}
{% endraw %} {% raw %}
torch.cuda.set_device(1)
print(f'Using GPU #{torch.cuda.current_device()}: {torch.cuda.get_device_name()}')
Using GPU #1: GeForce GTX 1080 Ti
{% endraw %}

Question Answer

Given a document (context) and a question, the objective of these models is to predict the start and end token of the correct answer as it exists in the context.

Again, we'll use a subset of pre-processed SQUAD v2 for our purposes below.

{% raw %}
# squad_df = pd.read_csv('./data/task-question-answering/squad_cleaned.csv'); len(squad_df)

# sample
squad_df = pd.read_csv('./squad_sample.csv'); len(squad_df)
1000
{% endraw %} {% raw %}
squad_df.head(2)
id title context question answers ds_type answer_text is_impossible
0 56be85543aeaaa14008c9063 Beyoncé Beyoncé Giselle Knowles-Carter (/biːˈjɒnseɪ/ bee-YON-say) (born September 4, 1981) is an American singer, songwriter, record producer and actress. Born and raised in Houston, Texas, she performed in various singing and dancing competitions as a child, and rose to fame in the late 1990s as lead singer of R&B girl-group Destiny's Child. Managed by her father, Mathew Knowles, the group became one of the world's best-selling girl groups of all time. Their hiatus saw the release of Beyoncé's debut album, Dangerously in Love (2003), which established her as a solo artist worldwide, earned five G... When did Beyonce start becoming popular? {'text': ['in the late 1990s'], 'answer_start': [269]} train in the late 1990s False
1 56be85543aeaaa14008c9065 Beyoncé Beyoncé Giselle Knowles-Carter (/biːˈjɒnseɪ/ bee-YON-say) (born September 4, 1981) is an American singer, songwriter, record producer and actress. Born and raised in Houston, Texas, she performed in various singing and dancing competitions as a child, and rose to fame in the late 1990s as lead singer of R&B girl-group Destiny's Child. Managed by her father, Mathew Knowles, the group became one of the world's best-selling girl groups of all time. Their hiatus saw the release of Beyoncé's debut album, Dangerously in Love (2003), which established her as a solo artist worldwide, earned five G... What areas did Beyonce compete in when she was growing up? {'text': ['singing and dancing'], 'answer_start': [207]} train singing and dancing False
{% endraw %} {% raw %}
pretrained_model_name = 'bert-large-uncased-whole-word-masking-finetuned-squad'
hf_model_cls = BertForQuestionAnswering

hf_arch, hf_config, hf_tokenizer, hf_model = BLURR.get_hf_objects(pretrained_model_name, model_cls=hf_model_cls)

# # here's a pre-trained roberta model for squad you can try too
# pretrained_model_name = "ahotrod/roberta_large_squad2"
# hf_arch, hf_config, hf_tokenizer, hf_model = BLURR.get_hf_objects(pretrained_model_name, 
#                                                                   model_cls=AutoModelForQuestionAnswering)

# # here's a pre-trained xlm model for squad you can try too
# pretrained_model_name = 'xlm-mlm-ende-1024'
# hf_arch, hf_config, hf_tokenizer, hf_model = BLURR.get_hf_objects(pretrained_model_name,
#                                                                   model_cls=AutoModelForQuestionAnswering)

{% endraw %} {% raw %}
squad_df = squad_df.apply(partial(pre_process_squad, hf_arch=hf_arch, hf_tokenizer=hf_tokenizer), axis=1)
{% endraw %} {% raw %}
max_seq_len= 128
{% endraw %} {% raw %}
squad_df = squad_df[(squad_df.tokenized_input_len < max_seq_len) & (squad_df.is_impossible == False)]
{% endraw %} {% raw %}
vocab = list(range(max_seq_len))
# vocab = dict(enumerate(range(max_seq_len)));
{% endraw %} {% raw %}
trunc_strat = 'only_second' if (hf_tokenizer.padding_side == 'right') else 'only_first'

before_batch_tfm = HF_QABeforeBatchTransform(hf_arch, hf_config, hf_tokenizer, hf_model,
                                             max_length=max_seq_len, 
                                             truncation=trunc_strat, 
                                             tok_kwargs={ 'return_special_tokens_mask': True })

blocks = (
    HF_TextBlock(before_batch_tfm=before_batch_tfm, input_return_type=HF_QuestionAnswerInput), 
    CategoryBlock(vocab=vocab),
    CategoryBlock(vocab=vocab)
)

def get_x(x):
    return (x.question, x.context) if (hf_tokenizer.padding_side == 'right') else (x.context, x.question)

dblock = DataBlock(blocks=blocks, 
                   get_x=get_x,
                   get_y=[ColReader('tok_answer_start'), ColReader('tok_answer_end')],
                   splitter=RandomSplitter(),
                   n_inp=1)
{% endraw %} {% raw %}
dls = dblock.dataloaders(squad_df, bs=4)
{% endraw %} {% raw %}
len(dls.vocab), dls.vocab[0], dls.vocab[1]
(2,
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127],
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127])
{% endraw %} {% raw %}
dls.show_batch(dataloaders=dls, max_n=2)
text start/end answer
0 what language did frederic's father teach after they had moved to warsaw? in october 1810, six months after fryderyk's birth, the family moved to warsaw, where his father acquired a post teaching french at the warsaw lyceum, then housed in the saxon palace. fryderyk lived with his family in the palace grounds. the father played the flute and violin ; the mother played the piano and gave lessons to boys in the boarding house that the chopins kept. chopin was of slight build, and even in early childhood was prone to illnesses. (44, 45) french
1 how old was chopin when his family moved to warsaw? in october 1810, six months after fryderyk's birth, the family moved to warsaw, where his father acquired a post teaching french at the warsaw lyceum, then housed in the saxon palace. fryderyk lived with his family in the palace grounds. the father played the flute and violin ; the mother played the piano and gave lessons to boys in the boarding house that the chopins kept. chopin was of slight build, and even in early childhood was prone to illnesses. (17, 19) six months
{% endraw %}

Training

Here we create a question/answer specific subclass of HF_BaseModelCallback in order to get all the start and end prediction. We also add here a new loss function that can handle multiple targets

{% raw %}

class HF_QstAndAnsModelCallback[source]

HF_QstAndAnsModelCallback(after_create=None, before_fit=None, before_epoch=None, before_train=None, before_batch=None, after_pred=None, after_loss=None, before_backward=None, before_step=None, after_cancel_step=None, after_step=None, after_cancel_batch=None, after_batch=None, after_cancel_train=None, after_train=None, before_validate=None, after_cancel_validate=None, after_validate=None, after_cancel_epoch=None, after_epoch=None, after_cancel_fit=None, after_fit=None) :: HF_BaseModelCallback

The prediction is a combination start/end logits

{% endraw %} {% raw %}
{% endraw %}

And here we provide a custom loss function our question answer task, expanding on some techniques learned from here and here.

In fact, this new loss function can be used in many other multi-modal architectures, with any mix of loss functions. For example, this can be ammended to include the is_impossible task, as well as the start/end token tasks in the SQUAD v2 dataset.

{% raw %}

class MultiTargetLoss[source]

MultiTargetLoss(loss_classes=[<class 'fastai.losses.CrossEntropyLossFlat'>, <class 'fastai.losses.CrossEntropyLossFlat'>], loss_classes_kwargs=[{}, {}], weights=[1, 1], reduction='mean') :: Module

Provides the ability to apply different loss functions to multi-modal targets/predictions

{% endraw %} {% raw %}
{% endraw %} {% raw %}
model = HF_BaseModelWrapper(hf_model)

learn = Learner(dls, 
                model,
                opt_func=partial(Adam, decouple_wd=True),
                cbs=[HF_QstAndAnsModelCallback],
                splitter=hf_splitter)

learn.loss_func=MultiTargetLoss()
learn.create_opt()                # -> will create your layer groups based on your "splitter" function
learn.freeze()
{% endraw %}

Notice above how I had to define the loss function after creating the Learner object. I'm not sure why, but the MultiTargetLoss above prohibits the learner from being exported if I do.

{% raw %}
 
{% endraw %} {% raw %}
print(len(learn.opt.param_groups))
3
{% endraw %} {% raw %}
x, y_start, y_end = dls.one_batch()
preds = learn.model(x)
len(preds),preds[0].shape
(2, torch.Size([4, 116]))
{% endraw %} {% raw %}
learn.lr_find(suggestions=True)
SuggestedLRs(lr_min=0.004786301031708717, lr_steep=6.309573450380412e-07)
{% endraw %} {% raw %}
learn.fit_one_cycle(3, lr_max=1e-3)
epoch train_loss valid_loss time
0 4.097818 2.119996 00:04
1 2.357785 1.464321 00:04
2 1.586194 1.366232 00:04
{% endraw %}

Showing results

Below we'll add in additional functionality to more intuitively show the results of our model.

{% raw %}
{% endraw %} {% raw %}
learn.show_results(learner=learn, skip_special_tokens=True, max_n=2, trunc_at=500)
text start/end answer pred start/end pred answer
0 who's death caused this protest? following the death of freddie gray, beyonce and jay - z, among other notable figures, met with his family. after the imprisonment of protesters of gray's death, beyonce and jay - z donated thousands of dollars to bail them out. (14, 16) freddie gray (14, 16) freddie gray
1 beyonce would take a break from music in which year? beyonce announced a hiatus from her music career in january 2010, heeding her mother's advice, " to live life, to be inspired by things again ". during the break she and her father parted ways as business partners. beyonce's musical break lasted nine months and saw her visit multiple european cities, the great wall of china, the egyptian pyramids, australia, english music festivals and various museums and ballet performances. (23, 24) 2010 (23, 24) 2010
{% endraw %}

... and lets see how Learner.blurr_predict works with question/answering tasks

{% raw %}
inf_df = pd.DataFrame.from_dict([{
    'question': 'What did George Lucas make?',
    'context': 'George Lucas created Star Wars in 1977. He directed and produced it.'   
}], 
    orient='columns')

learn.blurr_predict(inf_df.iloc[0])
[(('11', '13'),
  (#2) [tensor(11),tensor(13)],
  (#2) [tensor([4.6049e-08, 2.1754e-08, 1.2570e-09, 3.3928e-09, 2.2524e-09, 1.5572e-09,
        1.9868e-10, 4.6048e-08, 2.1077e-04, 9.5349e-06, 2.9826e-04, 9.9941e-01,
        6.2525e-05, 1.0289e-07, 3.7425e-06, 1.3626e-07, 2.0185e-06, 2.3964e-06,
        6.6572e-09, 7.2884e-07, 2.7460e-07, 3.5354e-08, 4.6314e-08]),tensor([1.9239e-03, 8.2251e-05, 9.0331e-06, 3.3155e-06, 1.3142e-05, 7.1214e-06,
        2.8237e-05, 1.9240e-03, 4.8337e-05, 1.8588e-04, 1.6767e-04, 3.5178e-05,
        6.4013e-02, 4.5331e-01, 3.1811e-02, 4.1730e-01, 3.1109e-04, 8.1755e-05,
        1.9823e-04, 2.1560e-04, 6.6738e-03, 1.9755e-02, 1.9071e-03])])]
{% endraw %} {% raw %}
inf_df = pd.DataFrame.from_dict([
    {
        'question': 'What did George Lucas make?',
        'context': 'George Lucas created Star Wars in 1977. He directed and produced it.'   
    }, {
        'question': 'What year did Star Wars come out?',
        'context': 'George Lucas created Star Wars in 1977. He directed and produced it.' 
    }, {
        'question': 'What did George Lucas do?',
        'context': 'George Lucas created Star Wars in 1977. He directed and produced it.' 
    }], 
    orient='columns')

learn.blurr_predict(inf_df)
[(('11', '13'),
  (#2) [tensor(11),tensor(13)],
  (#2) [tensor([4.6049e-08, 2.1754e-08, 1.2570e-09, 3.3928e-09, 2.2524e-09, 1.5572e-09,
        1.9868e-10, 4.6048e-08, 2.1077e-04, 9.5348e-06, 2.9826e-04, 9.9941e-01,
        6.2525e-05, 1.0289e-07, 3.7425e-06, 1.3626e-07, 2.0184e-06, 2.3964e-06,
        6.6572e-09, 7.2883e-07, 2.7460e-07, 3.5354e-08, 4.6314e-08, 1.2056e-10,
        1.5191e-10]),tensor([1.9239e-03, 8.2251e-05, 9.0331e-06, 3.3154e-06, 1.3142e-05, 7.1214e-06,
        2.8237e-05, 1.9240e-03, 4.8337e-05, 1.8588e-04, 1.6767e-04, 3.5178e-05,
        6.4013e-02, 4.5330e-01, 3.1811e-02, 4.1730e-01, 3.1109e-04, 8.1755e-05,
        1.9823e-04, 2.1560e-04, 6.6738e-03, 1.9755e-02, 1.9071e-03, 2.2829e-06,
        1.3567e-06])]),
 (('16', '17'),
  (#2) [tensor(16),tensor(17)],
  (#2) [tensor([3.3287e-07, 2.0221e-06, 2.9138e-08, 1.4128e-08, 1.5207e-08, 1.0268e-08,
        1.8328e-08, 2.2009e-08, 8.9670e-09, 3.3289e-07, 5.7192e-07, 6.9401e-07,
        1.2989e-06, 2.8417e-06, 9.4006e-07, 2.6559e-05, 9.9996e-01, 7.3549e-07,
        1.1192e-07, 1.0062e-07, 1.9109e-08, 1.1104e-07, 3.0953e-07, 4.5891e-07,
        3.3309e-07]),tensor([3.3988e-03, 9.7722e-04, 7.4854e-04, 2.5155e-04, 8.4656e-05, 1.5866e-04,
        9.7510e-05, 1.7856e-04, 5.4339e-04, 3.3987e-03, 6.7723e-04, 1.0115e-03,
        6.9202e-04, 3.0996e-04, 1.1580e-03, 1.6172e-03, 1.1258e-02, 9.5660e-01,
        6.8292e-03, 7.2445e-04, 7.8386e-04, 6.4028e-04, 1.7818e-03, 2.6807e-03,
        3.4020e-03])]),
 (('17', '21'),
  (#2) [tensor(17),tensor(21)],
  (#2) [tensor([2.3450e-06, 1.2078e-07, 2.4842e-08, 7.4360e-08, 2.8128e-08, 2.5845e-08,
        7.5241e-09, 2.3453e-06, 4.8795e-03, 3.2681e-04, 1.5062e-01, 3.1250e-04,
        1.6315e-05, 1.3606e-06, 2.7458e-05, 6.5260e-06, 7.4430e-02, 7.5916e-01,
        7.5640e-06, 1.0184e-02, 1.5776e-05, 4.9533e-06, 2.3616e-06, 3.4540e-09,
        5.1196e-09]),tensor([3.8758e-03, 2.5490e-05, 1.7944e-05, 6.8957e-06, 2.9865e-05, 1.4622e-05,
        3.9466e-05, 3.8760e-03, 5.7416e-05, 3.5180e-04, 5.6071e-04, 4.6226e-04,
        2.0373e-02, 2.1421e-02, 1.0842e-02, 8.9019e-02, 6.3678e-05, 1.2223e-04,
        1.4877e-03, 6.3028e-03, 2.7986e-01, 5.5733e-01, 3.8530e-03, 7.6229e-06,
        2.8311e-06])])]
{% endraw %} {% raw %}
inp_ids = hf_tokenizer.encode('What did George Lucas make?',
                              'George Lucas created Star Wars in 1977. He directed and produced it.')

hf_tokenizer.convert_ids_to_tokens(inp_ids, skip_special_tokens=False)[11:13]
['star', 'wars']
{% endraw %}

Note that there is a bug currently in fastai v2 (or with how I'm assembling everything) that currently prevents us from seeing the decoded predictions and probabilities for the "end" token.

{% raw %}
inf_df = pd.DataFrame.from_dict([{
    'question': 'When was Star Wars made?',
    'context': 'George Lucas created Star Wars in 1977. He directed and produced it.'
}], 
    orient='columns')

test_dl = dls.test_dl(inf_df)
inp = test_dl.one_batch()[0]['input_ids']
probs, _, preds = learn.get_preds(dl=test_dl, with_input=False, with_decoded=True)
{% endraw %} {% raw %}
hf_tokenizer.convert_ids_to_tokens(inp.tolist()[0], 
                                   skip_special_tokens=False)[torch.argmax(probs[0]):torch.argmax(probs[1])]
['1977']
{% endraw %}

We can unfreeze and continue training like normal

{% raw %}
learn.unfreeze()
{% endraw %} {% raw %}
learn.fit_one_cycle(3, lr_max=slice(1e-7, 1e-4))
epoch train_loss valid_loss time
0 0.851606 1.239299 00:08
1 0.712135 1.093468 00:08
2 0.542332 1.064135 00:08
{% endraw %} {% raw %}
learn.recorder.plot_loss()
{% endraw %} {% raw %}
learn.show_results(learner=learn, max_n=2, trunc_at=100)
text start/end answer pred start/end pred answer
0 who's death caused this protest? following the death of freddie gray, beyonce and jay - z, among oth (14, 16) freddie gray (14, 16) freddie gray
1 what two instruments did chopin's father play? in october 1810, six months after fryderyk's birth, t (70, 73) flute and violin (70, 73) flute and violin
{% endraw %} {% raw %}
learn.blurr_predict(inf_df.iloc[0])
[(('14', '15'),
  (#2) [tensor(14),tensor(15)],
  (#2) [tensor([4.4008e-08, 2.5095e-08, 3.9215e-09, 2.9052e-09, 2.5276e-09, 8.8124e-09,
        1.2282e-09, 4.4009e-08, 8.0718e-07, 2.7321e-07, 2.6573e-06, 1.8319e-06,
        3.0652e-07, 3.3745e-04, 9.9966e-01, 3.0494e-07, 3.7056e-08, 1.9579e-08,
        1.9611e-09, 1.3684e-08, 3.9831e-08, 3.2099e-08, 4.3841e-08]),tensor([3.2812e-04, 1.7397e-05, 7.0053e-06, 2.7075e-06, 4.4468e-06, 3.3043e-06,
        1.1552e-05, 3.2811e-04, 2.8971e-05, 3.3261e-05, 5.7404e-05, 1.0447e-05,
        4.2404e-05, 2.0792e-04, 1.1852e-03, 9.9524e-01, 1.9488e-03, 2.4889e-05,
        1.9213e-05, 1.7275e-05, 6.5059e-05, 8.5398e-05, 3.2749e-04])])]
{% endraw %} {% raw %}
preds, pred_classes, probs = zip(*learn.blurr_predict(inf_df.iloc[0]))
preds
(('14', '15'),)
{% endraw %} {% raw %}
inp_ids = hf_tokenizer.encode('When was Star Wars made?',
                              'George Lucas created Star Wars in 1977. He directed and produced it.')

hf_tokenizer.convert_ids_to_tokens(inp_ids, skip_special_tokens=False)[int(preds[0][0]):int(preds[0][1])]
['1977']
{% endraw %}

Inference

Note that I had to replace the loss function because of the above-mentioned issue to exporting the model with the MultiTargetLoss loss function. After getting our inference learner, we put it back and we're good to go!

{% raw %}
export_name = 'q_and_a_learn_export'
{% endraw %} {% raw %}
learn.loss_func = CrossEntropyLossFlat()
learn.export(fname=f'{export_name}.pkl')
{% endraw %} {% raw %}
inf_learn = load_learner(fname=f'{export_name}.pkl')
inf_learn.loss_func = MultiTargetLoss()

inf_df = pd.DataFrame.from_dict([
    {
        'question': 'What did George Lucas make?',
        'context': 'George Lucas created Star Wars in 1977. He directed and produced it.'   
    }, {
        'question': 'What year did Star Wars come out?',
        'context': 'George Lucas created Star Wars in 1977. He directed and produced it.' 
    }, {
        'question': 'What did George Lucas do?',
        'context': 'George Lucas created Star Wars in 1977. He directed and produced it.' 
    }], 
    orient='columns')

inf_learn.blurr_predict(inf_df)
[(('11', '13'),
  (#2) [tensor(11),tensor(13)],
  (#2) [tensor([2.3385e-08, 1.8765e-08, 6.9642e-10, 1.5719e-09, 1.1217e-09, 1.2074e-09,
        1.1144e-10, 2.3385e-08, 3.7906e-05, 2.5354e-06, 1.1943e-04, 9.9978e-01,
        5.5994e-05, 2.6022e-08, 1.8683e-06, 3.5621e-08, 4.3745e-07, 6.7398e-07,
        2.2025e-09, 2.3992e-07, 7.5966e-08, 1.0224e-08, 2.3401e-08, 7.6357e-11,
        1.0104e-10]),tensor([6.1599e-04, 3.2406e-05, 2.8531e-06, 9.9985e-07, 2.6161e-06, 1.5583e-06,
        6.6910e-06, 6.1601e-04, 1.4603e-05, 3.3881e-05, 4.5351e-05, 1.1776e-05,
        2.3468e-02, 6.8296e-01, 8.3699e-03, 2.7873e-01, 1.0117e-04, 2.7950e-05,
        5.4324e-05, 5.1852e-05, 1.4017e-03, 2.8434e-03, 6.0984e-04, 6.6051e-07,
        4.1278e-07])]),
 (('16', '17'),
  (#2) [tensor(16),tensor(17)],
  (#2) [tensor([1.6810e-07, 9.1037e-07, 1.4105e-08, 6.6887e-09, 7.0839e-09, 5.1611e-09,
        8.1408e-09, 1.0097e-08, 4.5416e-09, 1.6811e-07, 1.5744e-07, 2.0486e-07,
        3.6706e-07, 7.6532e-07, 3.5233e-07, 1.1035e-05, 9.9999e-01, 1.6582e-07,
        3.5065e-08, 3.1265e-08, 8.2434e-09, 3.5238e-08, 1.0789e-07, 1.3661e-07,
        1.6804e-07]),tensor([1.0074e-03, 3.1288e-04, 1.8310e-04, 6.6865e-05, 2.3713e-05, 3.7655e-05,
        2.4929e-05, 3.7836e-05, 9.9261e-05, 1.0074e-03, 1.5139e-04, 2.0920e-04,
        1.7819e-04, 8.9195e-05, 2.4684e-04, 4.2446e-04, 3.5895e-03, 9.8875e-01,
        1.5625e-03, 1.4853e-04, 1.3108e-04, 1.2141e-04, 2.7879e-04, 3.0911e-04,
        1.0090e-03])]),
 (('17', '21'),
  (#2) [tensor(17),tensor(21)],
  (#2) [tensor([1.8887e-06, 1.0929e-07, 2.1427e-08, 5.9553e-08, 2.3174e-08, 2.4211e-08,
        6.4589e-09, 1.8889e-06, 1.9009e-03, 1.7803e-04, 1.5365e-01, 1.9500e-04,
        1.1949e-05, 5.0096e-07, 2.5435e-05, 3.4661e-06, 5.7155e-02, 7.6575e-01,
        4.9602e-06, 2.1112e-02, 9.7899e-06, 2.5444e-06, 1.8963e-06, 2.9934e-09,
        4.6307e-09]),tensor([1.8885e-03, 1.0327e-05, 7.1769e-06, 2.6625e-06, 7.8802e-06, 4.8692e-06,
        1.3196e-05, 1.8886e-03, 2.6837e-05, 1.0920e-04, 2.5462e-04, 2.6529e-04,
        8.6511e-03, 1.5515e-02, 3.3114e-03, 4.5827e-02, 3.5190e-05, 6.6724e-05,
        1.2784e-03, 1.7973e-03, 2.7760e-01, 6.3956e-01, 1.8776e-03, 3.1533e-06,
        1.2262e-06])])]
{% endraw %} {% raw %}
inp_ids = hf_tokenizer.encode('What did George Lucas make?',
                              'George Lucas created Star Wars in 1977. He directed and produced it.')

hf_tokenizer.convert_ids_to_tokens(inp_ids, skip_special_tokens=False)[11:13]
['star', 'wars']
{% endraw %}

... and onnx works here too

{% raw %}
# learn.blurr_to_onnx(export_name)
{% endraw %} {% raw %}
# onnx_inf = blurrONNX(export_name)
{% endraw %} {% raw %}
# onnx_inf.predict(inf_df)
{% endraw %}

Cleanup