Giter VIP home page Giter VIP logo

blurr's Introduction

Getting Started

Named after the fastest transformer (well, at least of the Autobots), BLURR provides both a comprehensive and extensible framework for training and deploying 🤗 huggingface transformer models with fastai >= 2.0.

Utilizing features like fastai’s new @typedispatch and @patch decorators, along with a simple class hiearchy, BLURR provides fastai developers with the ability to train and deploy transformers on a variety of tasks. It includes a high, mid, and low-level API that will allow developers to use much of it out-of-the-box or customize it as needed.

Supported Text/NLP Tasks: - Sequence Classification

  • Token Classification
  • Question Answering
  • Summarization
  • Tranlsation
  • Language Modeling (Causal and Masked)

Supported Vision Tasks: - In progress

Supported Audio Tasks: - In progress

Install

You can now pip install blurr via pip install ohmeow-blurr

Or, even better as this library is under very active development, create an editable install like this:

git clone https://github.com/ohmeow/blurr.git
cd blurr
pip install -e ".[dev]"

How to use

Please check the documentation for more thorough examples of how to use this package.

The following two packages need to be installed for blurr to work:

  1. fastai
  2. Hugging Face transformers

Imports

import os, warnings

import torch
from transformers import *
from transformers.utils import logging as hf_logging
from fastai.text.all import *

from blurr.text.data.all import *
from blurr.text.modeling.all import *
warnings.simplefilter("ignore")
hf_logging.set_verbosity_error()

os.environ["TOKENIZERS_PARALLELISM"] = "false"

Get your data

path = untar_data(URLs.IMDB_SAMPLE)

model_path = Path("models")
imdb_df = pd.read_csv(path / "texts.csv")

Get n_labels from data for config later

n_labels = len(imdb_df["label"].unique())

Get your 🤗 objects

model_cls = AutoModelForSequenceClassification

pretrained_model_name = "bert-base-uncased"

config = AutoConfig.from_pretrained(pretrained_model_name)
config.num_labels = n_labels

hf_arch, hf_config, hf_tokenizer, hf_model = get_hf_objects(
    pretrained_model_name,
    model_cls=model_cls, 
    config=config
)

Build your Data 🧱 and your DataLoaders

# single input
blocks = (
    TextBlock(hf_arch, hf_config, hf_tokenizer, hf_model), 
    CategoryBlock
)
dblock = DataBlock(
    blocks=blocks, 
    get_x=ColReader("text"), 
    get_y=ColReader("label"), 
    splitter=ColSplitter()
)

dls = dblock.dataloaders(imdb_df, bs=4)
dls.show_batch(dataloaders=dls, max_n=2, trunc_at=250)
text target
0 raising victor vargas : a review < br / > < br / > you know, raising victor vargas is like sticking your hands into a big, steaming bowl of oatmeal. it's warm and gooey, but you're not sure if it feels right. try as i might, no matter how warm and go negative
1 the shop around the corner is one of the sweetest and most feel - good romantic comedies ever made. there's just no getting around that, and it's hard to actually put one's feeling for this film into words. it's not one of those films that tries too positive

… and 🚂

model = BaseModelWrapper(hf_model)

learn = Learner(
    dls,
    hf_model,
    opt_func=partial(Adam, decouple_wd=True),
    loss_func=CrossEntropyLossFlat(),
    metrics=[accuracy],
    cbs=[BaseModelCallback],
    splitter=blurr_splitter,
)

learn.freeze()

learn.fit_one_cycle(3, lr_max=1e-3)
<style> /* Turns off some styling */ progress { /* gets rid of default border in Firefox and Opera. */ border: none; /* Needs to be in here for Safari polyfill so background images work as expected. */ background-size: auto; } progress:not([value]), progress:not([value])::-webkit-progress-bar { background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px); } .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar { background: #F44336; } </style>
epoch train_loss valid_loss accuracy time
0 0.628744 0.453862 0.780000 00:21
1 0.367063 0.294906 0.895000 00:22
2 0.238181 0.279067 0.900000 00:22
learn.show_results(learner=learn, max_n=2, trunc_at=250)
<style> /* Turns off some styling */ progress { /* gets rid of default border in Firefox and Opera. */ border: none; /* Needs to be in here for Safari polyfill so background images work as expected. */ background-size: auto; } progress:not([value]), progress:not([value])::-webkit-progress-bar { background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px); } .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar { background: #F44336; } </style>
text target prediction
0 the trouble with the book, " memoirs of a geisha " is that it had japanese surfaces but underneath the surfaces it was all an american man's way of thinking. reading the book is like watching a magnificent ballet with great music, sets, and costumes negative negative
1 < br / > < br / > i'm sure things didn't exactly go the same way in the real life of homer hickam as they did in the film adaptation of his book, rocket boys, but the movie " october sky " ( an anagram of the book's title ) is good enough to stand al positive positive

Using the high-level Blurr API

Using the high-level API we can reduce DataBlock, DataLoaders, and Learner creation into a single line of code.

Included in the high-level API is a general BLearner class (pronouned “Blurrner”) that you can use with hand crafted DataLoaders, as well as, task specific BLearners like BLearnerForSequenceClassification that will handle everything given your raw data sourced from a pandas DataFrame, CSV file, or list of dictionaries (for example a huggingface datasets dataset)

learn = BlearnerForSequenceClassification.from_data(
    imdb_df, 
    pretrained_model_name, 
    dl_kwargs={"bs": 4}
)
learn.fit_one_cycle(1, lr_max=1e-3)
<style> /* Turns off some styling */ progress { /* gets rid of default border in Firefox and Opera. */ border: none; /* Needs to be in here for Safari polyfill so background images work as expected. */ background-size: auto; } progress:not([value]), progress:not([value])::-webkit-progress-bar { background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px); } .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar { background: #F44336; } </style>
epoch train_loss valid_loss f1_score accuracy time
0 0.530218 0.484683 0.789189 0.805000 00:22
learn.show_results(learner=learn, max_n=2, trunc_at=250)
<style> /* Turns off some styling */ progress { /* gets rid of default border in Firefox and Opera. */ border: none; /* Needs to be in here for Safari polyfill so background images work as expected. */ background-size: auto; } progress:not([value]), progress:not([value])::-webkit-progress-bar { background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px); } .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar { background: #F44336; } </style>
text target prediction
0 the trouble with the book, " memoirs of a geisha " is that it had japanese surfaces but underneath the surfaces it was all an american man's way of thinking. reading the book is like watching a magnificent ballet with great music, sets, and costumes negative negative
1 < br / > < br / > i'm sure things didn't exactly go the same way in the real life of homer hickam as they did in the film adaptation of his book, rocket boys, but the movie " october sky " ( an anagram of the book's title ) is good enough to stand al positive positive

⭐ Props

A word of gratitude to the following individuals, repos, and articles upon which much of this work is inspired from:

blurr's People

Contributors

bogdansalyp avatar danteoz avatar dependabot[bot] avatar henrydashwood avatar manisnesan avatar ohmeow avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

blurr's Issues

Using fastai interpretation

Hey there,

first: Thanks for your awesome tool, that makes everything work so much smoother.
I successfully trained a model and want to evaluate its performance.
I tried using interp = Interpretation.from_learner(learn), but this fails with an error:

/usr/local/lib/python3.7/dist-packages/fastai/torch_core.py in nested_reorder(t, idxs)
    704     elif is_listy(t): return type(t)(nested_reorder(t_, idxs) for t_ in t)
    705     if t is None: return t
--> 706     raise TypeError(f"Expected tensor, tuple, list or L but got {type(t)}")
    707 
    708 # Cell

TypeError: Expected tensor, tuple, list or L but got <class 'dict'>

Am I missing something or is this an error on my side?
Thanks in Advance for your help.

Causal Language Modelling from files

Hi @ohmeow , this is probably a basic question, but I'm having some issues to do causal language modelling from a set of wikitext-100 style files. My data folder is split as follows:

data/
       - train/
           - file1.txt
           - file2.txt
           - file3.txt
           - ..... 
       - valid/
           - file1.txt
           - file2.txt
           - file3.txt
           - .....

So far I have been following your gpt2 tutorial for LM:

pretrained_model_name = "gpt2"
hf_arch, hf_config, hf_tokenizer, hf_model = BLURR.get_hf_objects(pretrained_model_name, model_cls=AutoModelForCausalLM)
if (hf_tokenizer.pad_token is None): hf_tokenizer.pad_token = '[PAD]'
before_batch_tfm = HF_LMBeforeBatchTransform(hf_arch, hf_config, hf_tokenizer, hf_model, lm_strategy_cls=CausalLMStrategy)
blocks = [HF_TextBlock(before_batch_tfm=before_batch_tfm, input_return_type=HF_CausalLMInput), noop]
path = config["data_path"]
get_items = partial(get_text_files, folders=[train, valid])
dblock = DataBlock(blocks=blocks, get_items=get_items, get_y=None, splitter=config["splitter"])
dls = TextDataLoaders.from_dblock(dblock, path, path=path, seq_len=config["max_seq_len"])

Config is a dict containing various params and a splitter class that's able to get the validation / train sets from a the data folder passed in.

This is however, throwing the following exception:

~/code/tgalery/fastai/fastai/data/core.py in from_dblock(cls, dblock, source, path, bs, val_bs, shuffle, device, **kwargs)
    190     @classmethod
    191     def from_dblock(cls, dblock, source, path='.',  bs=64, val_bs=None, shuffle=True, device=None, **kwargs):
--> 192         return dblock.dataloaders(source, path=path, bs=bs, val_bs=val_bs, shuffle=shuffle, device=device, **kwargs)
    193 
    194     _docs=dict(__getitem__="Retrieve `DataLoader` at `i` (`0` is training, `1` is validation)",

~/code/tgalery/fastai/fastai/data/block.py in dataloaders(self, source, path, verbose, **kwargs)
    113         dsets = self.datasets(source, verbose=verbose)
    114         kwargs = {**self.dls_kwargs, **kwargs, 'verbose': verbose}
--> 115         return dsets.dataloaders(path=path, after_item=self.item_tfms, after_batch=self.batch_tfms, **kwargs)
    116 
    117     _docs = dict(new="Create a new `DataBlock` with other `item_tfms` and `batch_tfms`",

~/code/tgalery/fastai/fastai/data/core.py in dataloaders(self, bs, shuffle_train, shuffle, val_shuffle, n, path, dl_type, dl_kwargs, device, drop_last, val_bs, **kwargs)
    229         val_kwargs={k[4:]:v for k,v in kwargs.items() if k.startswith('val_')}
    230         def_kwargs = {'bs':bs,'shuffle':shuffle,'drop_last':drop_last,'n':n,'device':device}
--> 231         dl = dl_type(self.subset(0), **merge(kwargs,def_kwargs, dl_kwargs[0]))
    232         def_kwargs = {'bs':bs if val_bs is None else val_bs,'shuffle':val_shuffle,'n':None,'drop_last':False}
    233         dls = [dl] + [dl.new(self.subset(i), **merge(kwargs,def_kwargs,val_kwargs,dl_kwargs[i]))

~/code/tgalery/fastai/fastai/text/data.py in __init__(self, dataset, sort_func, res, **kwargs)
    187         self.sort_func = _default_sort if sort_func is None else sort_func
    188         if res is None and self.sort_func == _default_sort: res = _get_lengths(dataset)
--> 189         self.res = [self.sort_func(self.do_item(i)) for i in range_of(self.dataset)] if res is None else res
    190         if len(self.res) > 0: self.idx_max = np.argmax(self.res)
    191 

~/code/tgalery/fastai/fastai/text/data.py in <listcomp>(.0)
    187         self.sort_func = _default_sort if sort_func is None else sort_func
    188         if res is None and self.sort_func == _default_sort: res = _get_lengths(dataset)
--> 189         self.res = [self.sort_func(self.do_item(i)) for i in range_of(self.dataset)] if res is None else res
    190         if len(self.res) > 0: self.idx_max = np.argmax(self.res)
    191 

~/code/tgalery/fastai/fastai/text/data.py in _default_sort(x)
    178 
    179 # Cell
--> 180 def _default_sort(x): return len(x[0])
    181 
    182 @delegates(TfmdDL)

TypeError: object of type 'PosixPath' has no len()

Any pointers on what I might be doing wrong ?

How could I set the number of classes for the model?

Hello, thank you for this excellent work!
I'm a beginner of fastai and huggingface.

And I'm now using your library for sequence classfication problem with more than 2 classes.
I think it's necessary to set the number of class for huggingface. (Cause imdb is running correctly while mine cannot.)
But I don't know how according to your docs.
Could you give me some suggestions?

Fix QA task preprocessing

Hi,
very cool lib. Just wanted to say that pre_process_squad function is not working correctly when following docs. There are two problems when nlp package is used like that nlp.load_dataset('squad_v2').

  • Column names differ, to be exact "anwsers" and "anwser_text".
  • Answers are given in dict(list(str)) format and tokenization that sets end and start token targets works as if it was dict(str). This ends up setting all targets as (0,0).

I had to fix that for my usecase so if you want I can make a PR with fixes. Let me know if there are things that I should do before like running tests

Notebook error: "NameError: name 'HF_MODELS' is not defined

I'm running the blurr notebook and hit an error that I'd love some help with:

pretrained_model_name = "bart-large-cnn"

hf_arch, hf_tokenizer, hf_config, hf_model = \
    BLURR_MODEL_HELPER.get_hf_objects(pretrained_model_name, BartTokenizer, HF_MODELS.BartForConditionalGeneration)

hf_arch, type(hf_tokenizer), type(hf_config), type(hf_model)

Error

NameError                                 Traceback (most recent call last)
<ipython-input-8-39f983a4d82d> in <module>()
      1 pretrained_model_name = "bart-large-cnn"
      2 
----> 3 hf_arch, hf_tokenizer, hf_config, hf_model =     BLURR_MODEL_HELPER.get_hf_objects(pretrained_model_name, BartTokenizer, HF_MODELS.BartForConditionalGeneration)
      4 
      5 hf_arch, type(hf_tokenizer), type(hf_config), type(hf_model)

NameError: name 'HF_MODELS' is not defined

I spent some time trying to understand the code and find reference to this but I came up short. I'm presuming the notebook is out of date and there's a new way to call get_hf_objects?

Failure installing blurr, when dealing with the tranformers library

I am having some trouble just installing both the transformers library from huggingface and blurr library. If I install first

!pip install transformers==3.5
!pip install fastai

The option to set transformers == 3.5, is because this seems to be related to the problem I find later, but it does not solver the problem. Then, after checking that everything is ok, for example using

import fastai
import transformers
print('fastai version :', fastai.__version__)
print('transformers version :', transformers.__version__)

I try to

!pip install git+git://github.com/ohmeow/blurr.git@master

or even simpler

pip install ohmeow-blurr

The result from this is that I get

...
  Attempting uninstall: transformers
    Found existing installation: transformers 3.5.0
    Uninstalling transformers-3.5.0:
      Successfully uninstalled transformers-3.5.0
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
allennlp 1.2.2 requires transformers<3.6,>=3.4, but you have transformers 4.1.1 which is incompatible.
Successfully installed nlp-0.4.0 ohmeow-blurr-0.0.20 onnxruntime-1.6.0 rouge-score-0.0.4 seqeval-1.2.2 tokenizers-0.9.4 transformers-4.1.1 xxhash-2.0.0

I am running everything on a Kaggle notebook. Is there anything I am doing wrong, or is this even supposed to be a problem?
Thanks a lot!

How to fine tune masked language model task?

Hi there,

I was reading through your example language modelling notebook and not sure how to adapt the same notebook (CLM) to MLM. Basically I was trying to do some this:

model_cls = AutoModelForMaskedLM
pretrained_model_name = 'roberta-base' # cause gpt2 cannot be used for MLM
hf_arch, hf_config, hf_tokenizer, hf_model = BLURR.get_hf_objects(pretrained_model_name, model_cls=model_cls)

However, I'm stuck at how to modify HF_CausalLMBeforeBatchTransform and there's no such equivalent transform for MaskedLM in the library

blocks = (
    HF_Seq2SeqBlock(before_batch_tfm=HF_CausalLMBeforeBatchTransform(hf_arch, hf_config, hf_tokenizer, hf_model)), 
    noop
)

Could you have a look at this? Thank you in advance!

Unexpected behavior of freeze()?

Hi, thanks for providing the library. It works like a charm.

There's one potential issue I noticed regarding the freeze() method. What I observed it that it freezes until the second last encoder layer. I'm using seq2seq distilbart model. It's a bit blur what fast.ai docstring "Freeze up to last parameter group" means when we have nested layers. But I was expecting it should freeze up to the second last decoder layer or up to before the decoder if it as a whole is counted as a parameter group.

Is the current behavior by design?

To reproduce the finding

pretrained_model_name = "sshleifer/distilbart-cnn-6-6"
hf_arch, hf_config, hf_tokenizer, hf_model = BLURR_MODEL_HELPER.get_hf_objects(pretrained_model_name, model_cls=BartForConditionalGeneration)
text_gen_kwargs = { **hf_config.task_specific_params['summarization'], **{'max_length': 32, 'min_length': 8} }
model = HF_BaseModelWrapper(hf_model)
model_cb = HF_SummarizationModelCallback(text_gen_kwargs=text_gen_kwargs)

learn = Learner(dls, 
                model,
                opt_func=ranger,
                loss_func=CrossEntropyLossFlat(),
                cbs=[model_cb],
                splitter=partial(summarization_splitter, arch=hf_arch)).to_fp16()

learn.create_opt() 
learn.freeze()

then

count = 0
for parameter in learn.model.hf_model.model.decoder.parameters():  # change decoder to encoder, shared
    if parameter.requires_grad:
        count += 1
print(count)

It shows that there're 158 trainable decoder parameters, 26 encoder parameters and 0 shared (embedding) parameters.

Knowledge Distillation

Is there a way to achieve Knowledge Distillation. That is distill knowledge from a HuggingFace trained model to ULMFit (Fast.ai LM)?

Would it be possible to use this library to train a Hugging Face Wav2Vec2 model using FastAI?

I see that this is primarily intended to be used for transformers that deal with textual data.

I was wondering whether it can be still used to train a Wav2Vec2 hugging-face transformer model using FastAI.

I was able to modify the get_hf_objects function enabling it to retrieve the objects related to Wav2Vec2 transformer.

How much more modification do you think would be necessary to get it to basically work on audio data?

use Model Parallelism for GPT2?

hf-transformers GPT2 (and T5) has a parallelize method to use model parallelism to load into multi-GPU so that the combined GPU-mem is enough for the GPT2 size. I've tried to 'hack' around to see if I can make it work within blurr for fastai, but unfortunately have not been successful. Pointers on how to get it working, and how best to add this into blurr codebase as a PR, will be much appreciated.

Snippet of what I did:

model_cls = AutoModelForSequenceClassification
pretrained_model_name = "gpt2-medium"
hf_arch, hf_config, hf_tokenizer, hf_model = BLURR.get_hf_objects(pretrained_model_name, model_cls=model_cls)

hf_model.transformer.parallelize() # the hf method

hf_model.transformer.model_parallel
# True

hf_model.transformer.device_map # I have two GPUs
# {0: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
#  1: [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]}

if (hf_tokenizer.pad_token is None): hf_tokenizer.pad_token = '[PAD]'
hf_tokenizer.pad_token, hf_tokenizer.pad_token_id
hf_model.config.pad_token_id = hf_tokenizer.pad_token_id

blocks = (HF_TextBlock(hf_arch, hf_config, hf_tokenizer, hf_model), CategoryBlock)
dblock = DataBlock(blocks=blocks,  get_x=ColReader('x'), get_y=ColReader('y'), splitter=RandomSplitter())
bs = 1
dls = dblock.dataloaders(df, bs=bs)

model = HF_BaseModelWrapper(hf_model)

learn = Learner(dls, 
                model,
                opt_func=partial(Adam, decouple_wd=True),
                loss_func=CrossEntropyLossFlat(),
                metrics=[accuracy],
                cbs=[HF_BaseModelCallback],
                splitter=hf_splitter).to_fp16()

learn.freeze()
learn.fit_one_cycle(1,1e-3)

The error I got was:

RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:1 and cuda:0! (when checking arugment for argument weight in method wrapper_native_layer_norm)

A few links that I looked at, for the parallelize method:
huggingface/transformers#8696
https://huggingface.co/transformers/_modules/transformers/models/gpt2/modeling_gpt2.html

Thanks.

Changing the default location of the downloaded pretrained models

Hi, I'm trying out the library here the piece of code I'm using:

pretrained_model_name = "bert-base-uncased"
task = HF_TASKS_AUTO.ForSequenceClassification
config = AutoConfig.from_pretrained(pretrained_model_name)

hf_arch, hf_tokenizer, hf_config, hf_model = BLURR_MODEL_HELPER.get_auto_hf_objects(pretrained_model_name, task=task, config=config)

Currently everything is stored in /root/.cache/torch/transformers. How to change the default path of storage?

Windows issue

Hi. Is there a way to fix this?

Collecting git+https://github.com/ohmeow/blurr.git
  Cloning https://github.com/ohmeow/blurr.git to c:\users\runner~1\appdata\local\temp\pip-req-build-gaye0t66
    ERROR: Command errored out with exit status 1:
     command: 'C:\Users\RUNNER~1\AppData\Local\R-MINI~1\envs\R-RETI~1\python.exe' -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\pip-req-build-gaye0t66\\setup.py'"'"'; __file__='"'"'C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\pip-req-build-gaye0t66\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base 'C:\Users\RUNNER~1\AppData\Local\Temp\pip-pip-egg-info-mpwpr2p4'
         cwd: C:\Users\RUNNER~1\AppData\Local\Temp\pip-req-build-gaye0t66\
    Complete output (7 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "C:\Users\RUNNER~1\AppData\Local\Temp\pip-req-build-gaye0t66\setup.py", line 42, in <module>
        long_description = open('README.md').read(),
      File "C:\Users\RUNNER~1\AppData\Local\R-MINI~1\envs\R-RETI~1\lib\encodings\cp1252.py", line 23, in decode
        return codecs.charmap_decode(input,self.errors,decoding_table)[0]
    UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 13891: character maps to <undefined>

Github Actions: https://github.com/henry090/fastai/pull/58/checks?check_run_id=1367643542

Errors when using custom models

Hi, sorry to bother again.
I'm now using a japanese bert model, and I want to use your library. But something went wrong.
The error occurs during the computation of loss, cause I send a batch of 64 data into the training step, but My model could only handle 1.
Is there any code that I can learn from?
Here is my code.

hf_arch = 'distilbert'
tokenizer = AutoTokenizer.from_pretrained("cl-tohoku/bert-base-japanese-whole-word-masking")
distil_model = AutoModel.from_pretrained("bandainamco-mirai/distilbert-base-japanese") 

blocks = (HF_TextBlock(hf_arch=hf_arch,
                       hf_tokenizer=tokenizer, 
                       padding='max_length', 
                       max_length=512), CategoryBlock)

dblock = DataBlock(blocks=blocks,
                   get_x=ColReader('data'), get_y=ColReader('label'),
                   splitter=RandomSplitter())
dls = dblock.dataloaders(df, bs=64)

class DistilBertClassifier(torch.nn.Module):
  def __init__(self):
    super(DistilBertClassifier, self).__init__()
    self.distil_bert = distil_model
    self.linear = nn.Linear(768, 2)
    nn.init.normal_(self.linear.weight, std=0.02)
    nn.init.normal_(self.linear.bias, 0)

  def forward(self, input_ids):
    vec, _ = self.distil_bert(input_ids)
    vec = vec[:,0,:]
    vec = vec.view(-1, 768)
    out = self.linear(vec)
    return F.log_softmax(out)

model = DistilBertClassifier()
learn = Learner(dls,
                HF_BaseModelWrapper(model),
                opt_func=partial(Adam, decouple_wd=True),
                loss_func=CrossEntropyLossFlat(),
                metrics=[accuracy, F1Score(average='macro')],
                cbs=[HF_BaseModelCallback],
                splitter=hf_splitter)

learn.create_opt()
learn.freeze()
# learn.lr_find(suggestions=True)
learn.fit_one_cycle(EPOCH)

Here is the whole error output:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
~/tweets/test.py in <module>
     <a href='file:///home/zchelllo/tweets/test.py?line=112'>113</a> learn.freeze()
     <a href='file:///home/zchelllo/tweets/test.py?line=113'>114</a> # learn.lr_find(suggestions=True)
---> <a href='file:///home/zchelllo/tweets/test.py?line=114'>115</a> learn.fit_one_cycle(EPOCH)

~/anaconda3/lib/python3.8/site-packages/fastcore/logargs.py in _f(*args, **kwargs)
     54         init_args.update(log)
     55         setattr(inst, 'init_args', init_args)
---> 56         return inst if to_return else f(*args, **kwargs)
     57     return _f

~/anaconda3/lib/python3.8/site-packages/fastai/callback/schedule.py in fit_one_cycle(self, n_epoch, lr_max, div, div_final, pct_start, wd, moms, cbs, reset_opt)
    111     scheds = {'lr': combined_cos(pct_start, lr_max/div, lr_max, lr_max/div_final),
    112               'mom': combined_cos(pct_start, *(self.moms if moms is None else moms))}
--> 113     self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd)
    114 
    115 # Cell

~/anaconda3/lib/python3.8/site-packages/fastcore/logargs.py in _f(*args, **kwargs)
     54         init_args.update(log)
     55         setattr(inst, 'init_args', init_args)
---> 56         return inst if to_return else f(*args, **kwargs)
     57     return _f

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
    205             self.opt.set_hypers(lr=self.lr if lr is None else lr)
    206             self.n_epoch,self.loss = n_epoch,tensor(0.)
--> 207             self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)
    208 
    209     def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),(None,),None,None

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    153 
    154     def _with_events(self, f, event_type, ex, final=noop):
--> 155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')
    157         finally:   self(f'after_{event_type}')        ;final()

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in _do_fit(self)
    195         for epoch in range(self.n_epoch):
    196             self.epoch=epoch
--> 197             self._with_events(self._do_epoch, 'epoch', CancelEpochException)
    198 
    199     @log_args(but='cbs')

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    153 
    154     def _with_events(self, f, event_type, ex, final=noop):
--> 155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')
    157         finally:   self(f'after_{event_type}')        ;final()

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in _do_epoch(self)
    189 
    190     def _do_epoch(self):
--> 191         self._do_epoch_train()
    192         self._do_epoch_validate()
    193 

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in _do_epoch_train(self)
    181     def _do_epoch_train(self):
    182         self.dl = self.dls.train
--> 183         self._with_events(self.all_batches, 'train', CancelTrainException)
    184 
    185     def _do_epoch_validate(self, ds_idx=1, dl=None):

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    153 
    154     def _with_events(self, f, event_type, ex, final=noop):
--> 155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')
    157         finally:   self(f'after_{event_type}')        ;final()

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in all_batches(self)
    159     def all_batches(self):
    160         self.n_iter = len(self.dl)
--> 161         for o in enumerate(self.dl): self.one_batch(*o)
    162 
    163     def _do_one_batch(self):

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in one_batch(self, i, b)
    177         self.iter = i
    178         self._split(b)
--> 179         self._with_events(self._do_one_batch, 'batch', CancelBatchException)
    180 
    181     def _do_epoch_train(self):

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    153 
    154     def _with_events(self, f, event_type, ex, final=noop):
--> 155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')
    157         finally:   self(f'after_{event_type}')        ;final()

~/anaconda3/lib/python3.8/site-packages/fastai/learner.py in _do_one_batch(self)
    164         self.pred = self.model(*self.xb)
    165         self('after_pred')
--> 166         if len(self.yb): self.loss = self.loss_func(self.pred, *self.yb)
    167         self('after_loss')
    168         if not self.training or not len(self.yb): return

~/anaconda3/lib/python3.8/site-packages/fastai/layers.py in __call__(self, inp, targ, **kwargs)
    295         if targ.dtype in [torch.int8, torch.int16, torch.int32]: targ = targ.long()
    296         if self.flatten: inp = inp.view(-1,inp.shape[-1]) if self.is_2d else inp.view(-1)
--> 297         return self.func.__call__(inp, targ.view(-1) if self.flatten else targ, **kwargs)
    298 
    299 # Cell

~/anaconda3/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--> 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

~/anaconda3/lib/python3.8/site-packages/torch/nn/modules/loss.py in forward(self, input, target)
    945 
    946     def forward(self, input: Tensor, target: Tensor) -> Tensor:
--> 947         return F.cross_entropy(input, target, weight=self.weight,
    948                                ignore_index=self.ignore_index, reduction=self.reduction)
    949 

~/anaconda3/lib/python3.8/site-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
   2420     if size_average is not None or reduce is not None:
   2421         reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 2422     return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
   2423 
   2424 

~/anaconda3/lib/python3.8/site-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
   2213 
   2214     if input.size(0) != target.size(0):
-> 2215         raise ValueError('Expected input batch_size ({}) to match target batch_size ({}).'
   2216                          .format(input.size(0), target.size(0)))
   2217     if dim == 2:

ValueError: Expected input batch_size (1) to match target batch_size (64).

Best way to use a finetuned/customised LM for downstream task e.g. sequence classification?

Hi,

Starting from pretrained GPT2 hf_model, I finetuned a custom LM using unlabelled dataset, using the model_cls = AutoModelForCausalLM method, and have saved/exported the resulting Learner object (let's call that finetuned_LM_learn).

What is the best way for me to then load that finetuned LM, and use it for downstream task (e.g. sequence classification) on labelled dataset? Should I just go through the same steps here, and then switch out the base_model before starting to train? Something like below?

learn.model.hf_model.base_model = finetuned_LM_learn.model.hf_model.base_model
learn.fit_one_cycle(epoch, lr)

Or is that not the correct / best way? Thanks.

KeyError: 1

I am getting an error when trying to import on Kaggle

`The above exception was the direct cause of the following exception:

KeyError Traceback (most recent call last)
in
3 from fastai.text.all import *
4
----> 5 from blurr.data.all import *
6 from blurr.modeling.all import *

/opt/conda/lib/python3.7/site-packages/blurr/data/all.py in
----> 1 from ..utils import *
2 from .core import *
3 from .question_answering import *
4 from .token_classification import *
5 from .seq2seq.core import *

/opt/conda/lib/python3.7/site-packages/blurr/utils.py in
186
187 # Cell
--> 188 BLURR_MODEL_HELPER = ModelHelper()
189
190 # Cell

/opt/conda/lib/python3.7/site-packages/blurr/utils.py in call(self, *args, **kwargs)
25
26 def call(self, *args, **kwargs):
---> 27 if self._instance == None: self._instance = self._cls(*args, **kwargs)
28 return self._instance
29

/opt/conda/lib/python3.7/site-packages/blurr/utils.py in init(self)
59 model_type_df = self._df[(self._df.functional_area == 'modeling')].class_name.str.split('For', n=1, expand=True)
60
---> 61 model_type_df[1] = np.where(model_type_df[1].notnull(),
62 'For' + model_type_df[1].astype(str),
63 model_type_df[1])

/opt/conda/lib/python3.7/site-packages/pandas/core/frame.py in getitem(self, key)
2904 if self.columns.nlevels > 1:
2905 return self._getitem_multilevel(key)
-> 2906 indexer = self.columns.get_loc(key)
2907 if is_integer(indexer):
2908 indexer = [indexer]

/opt/conda/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2895 return self._engine.get_loc(casted_key)
2896 except KeyError as err:
-> 2897 raise KeyError(key) from err
2898
2899 if tolerance is not None:

KeyError: 1`

How to make predictions on a batch?

Hi,

I'm currently facing some errors when trying to generate predictions on a batch using fastai functions such as dls.test_dl() to create the test dataloaders and then learn.get_preds() to get all the predictions. I searched through the documentation but couldn't find any examples of batch prediction. Could you show me an example of how to do it.?

Issues about summarization part

Hi, sorry to disturb you again. I have been recently fine-tuning mbart and mt5 with the help of blurr docs.
https://ohmeow.github.io/blurr/modeling-seq2seq-summarization/
https://github.com/ohmeow/ohmeow_website/blob/master/_notebooks/2020-05-23-text-generation-with-blurr.ipynb

But something made me confused.

  1. "summarize: " prefix of MT5
    in your test code, you add this prefix only for T5, does MT5 also need it or not?
def add_t5_prefix(inp): return f'summarize: {inp}' if (hf_arch == 't5') else inp

also I get an error for using your code

Traceback (most recent call last):
  File "my_blurr.py", line 40, in <module>
    dblock = DataBlock(blocks=blocks, get_x=Pipeline([ColReader('article'), add_t5_prefix]), get_y=ColReader('summary'), splitter=RandomSplitter())
TypeError: __init__() missing 1 required positional argument: 'tokenizer'
  1. loss function
    In the notebook of fine-tuning bart, you use CrossEntropyLossFlat, but HF_PreCalculatedLoss is used in the test code. Is there any difference between them and which should I use?

  2. got an error when generating using fine-tuned mbart
    I fine-tuned the mbart only with 100 Japanese samples but still got 0.28 of the rouge score. but when I use the generate function, it got an error.

Traceback (most recent call last):
  File "my_blurr.py", line 91, in <module>
    learn.blurr_generate(test_article, early_stopping=True, num_beams=4)
  File "/home/zchelllo/anaconda3/lib/python3.8/site-packages/blurr/modeling/seq2seq/core.py", line 225, in blurr_generate
    input_ids = hf_tokenizer.encode(inp, padding=True, truncation=True, return_tensors='pt', **tok_kwargs)
  File "/home/zchelllo/anaconda3/lib/python3.8/site-packages/transformers/tokenization_utils_base.py", line 2092, in encode
    encoded_inputs = self.encode_plus(
  File "/home/zchelllo/anaconda3/lib/python3.8/site-packages/transformers/tokenization_utils_base.py", line 2408, in encode_plus
    return self._encode_plus(
  File "/home/zchelllo/anaconda3/lib/python3.8/site-packages/transformers/tokenization_utils_fast.py", line 448, in _encode_plus
    batched_output = self._batch_encode_plus(
TypeError: _batch_encode_plus() got an unexpected keyword argument 'src_lang'

Is this related to the following code?

tok_kwargs = {}
if (hf_arch == 'mbart'):
    text_gen_kwargs['decoder_start_token_id'] = hf_tokenizer.get_vocab()["ja_XX"]
    tok_kwargs = {"src_lang": "ja_XX", "tgt_lang": "ja_XX"}

Sorry if they are stupid questions. Thanks in advance!

Attempting to run the simple example from the README results in error: "TypeError: BertForSequenceClassification object argument after ** must be a mapping, not list"

Just trying to run the simplest example from the README page seems to be fine up until:

learn.fit_one_cycle(3, lr_max=1e-3)

Which generates this error. Running this on Databricks, but I don't think that should trigger this error as shown.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<command-4118966838114040> in <module>
----> 1 learn.fit_one_cycle(3, lr_max=1e-3)

/local_disk0/.ephemeral_nfs/envs/pythonEnv-fe74cef5-3200-4e75-a478-1bc42517e0b0/lib/python3.7/site-packages/fastcore/logargs.py in _f(*args, **kwargs)
     54         init_args.update(log)
     55         setattr(inst, 'init_args', init_args)
---> 56         return inst if to_return else f(*args, **kwargs)
     57     return _f

/local_disk0/.ephemeral_nfs/envs/pythonEnv-fe74cef5-3200-4e75-a478-1bc42517e0b0/lib/python3.7/site-packages/fastai/callback/schedule.py in fit_one_cycle(self, n_epoch, lr_max, div, div_final, pct_start, wd, moms, cbs, reset_opt)
    111     scheds = {'lr': combined_cos(pct_start, lr_max/div, lr_max, lr_max/div_final),
    112               'mom': combined_cos(pct_start, *(self.moms if moms is None else moms))}
--> 113     self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd)
    114 
    115 # Cell

/local_disk0/.ephemeral_nfs/envs/pythonEnv-fe74cef5-3200-4e75-a478-1bc42517e0b0/lib/python3.7/site-packages/fastcore/logargs.py in _f(*args, **kwargs)
     54         init_args.update(log)
     55         setattr(inst, 'init_args', init_args)
---> 56         return inst if to_return else f(*args, **kwargs)
     57     return _f

/local_disk0/.ephemeral_nfs/envs/pythonEnv-fe74cef5-3200-4e75-a478-1bc42517e0b0/lib/python3.7/site-packages/fastai/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
    205             self.opt.set_hypers(lr=self.lr if lr is None else lr)
    206             self.n_epoch = n_epoch
--> 207             self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)
    208 
    209     def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),(None,),None,None

/local_disk0/.ephemeral_nfs/envs/pythonEnv-fe74cef5-3200-4e75-a478-1bc42517e0b0/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    153 
    154     def _with_events(self, f, event_type, ex, final=noop):
--> 155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')
    157         finally:   self(f'after_{event_type}')        ;final()

/local_disk0/.ephemeral_nfs/envs/pythonEnv-fe74cef5-3200-4e75-a478-1bc42517e0b0/lib/python3.7/site-packages/fastai/learner.py in _do_fit(self)
    195         for epoch in range(self.n_epoch):
    196             self.epoch=epoch
--> 197             self._with_events(self._do_epoch, 'epoch', CancelEpochException)
    198 
    199     @log_args(but='cbs')

/local_disk0/.ephemeral_nfs/envs/pythonEnv-fe74cef5-3200-4e75-a478-1bc42517e0b0/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    153 
    154     def _with_events(self, f, event_type, ex, final=noop):
--> 155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')
    157         finally:   self(f'after_{event_type}')        ;final()

/local_disk0/.ephemeral_nfs/envs/pythonEnv-fe74cef5-3200-4e75-a478-1bc42517e0b0/lib/python3.7/site-packages/fastai/learner.py in _do_epoch(self)
    189 
    190     def _do_epoch(self):
--> 191         self._do_epoch_train()
    192         self._do_epoch_validate()
    193 

/local_disk0/.ephemeral_nfs/envs/pythonEnv-fe74cef5-3200-4e75-a478-1bc42517e0b0/lib/python3.7/site-packages/fastai/learner.py in _do_epoch_train(self)
    181     def _do_epoch_train(self):
    182         self.dl = self.dls.train
--> 183         self._with_events(self.all_batches, 'train', CancelTrainException)
    184 
    185     def _do_epoch_validate(self, ds_idx=1, dl=None):

/local_disk0/.ephemeral_nfs/envs/pythonEnv-fe74cef5-3200-4e75-a478-1bc42517e0b0/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    153 
    154     def _with_events(self, f, event_type, ex, final=noop):
--> 155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')
    157         finally:   self(f'after_{event_type}')        ;final()

/local_disk0/.ephemeral_nfs/envs/pythonEnv-fe74cef5-3200-4e75-a478-1bc42517e0b0/lib/python3.7/site-packages/fastai/learner.py in all_batches(self)
    159     def all_batches(self):
    160         self.n_iter = len(self.dl)
--> 161         for o in enumerate(self.dl): self.one_batch(*o)
    162 
    163     def _do_one_batch(self):

/local_disk0/.ephemeral_nfs/envs/pythonEnv-fe74cef5-3200-4e75-a478-1bc42517e0b0/lib/python3.7/site-packages/fastai/learner.py in one_batch(self, i, b)
    177         self.iter = i
    178         self._split(b)
--> 179         self._with_events(self._do_one_batch, 'batch', CancelBatchException)
    180 
    181     def _do_epoch_train(self):

/local_disk0/.ephemeral_nfs/envs/pythonEnv-fe74cef5-3200-4e75-a478-1bc42517e0b0/lib/python3.7/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    153 
    154     def _with_events(self, f, event_type, ex, final=noop):
--> 155         try:       self(f'before_{event_type}')       ;f()
    156         except ex: self(f'after_cancel_{event_type}')
    157         finally:   self(f'after_{event_type}')        ;final()

/local_disk0/.ephemeral_nfs/envs/pythonEnv-fe74cef5-3200-4e75-a478-1bc42517e0b0/lib/python3.7/site-packages/fastai/learner.py in _do_one_batch(self)
    162 
    163     def _do_one_batch(self):
--> 164         self.pred = self.model(*self.xb)
    165         self('after_pred')
    166         if len(self.yb): self.loss = self.loss_func(self.pred, *self.yb)

/local_disk0/.ephemeral_nfs/envs/pythonEnv-fe74cef5-3200-4e75-a478-1bc42517e0b0/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    720             result = self._slow_forward(*input, **kwargs)
    721         else:
--> 722             result = self.forward(*input, **kwargs)
    723         for hook in itertools.chain(
    724                 _global_forward_hooks.values(),

/local_disk0/.ephemeral_nfs/envs/pythonEnv-fe74cef5-3200-4e75-a478-1bc42517e0b0/lib/python3.7/site-packages/blurr/modeling/core.py in forward(self, x)
     44                              output_attentions=self.output_hidden_states,
     45                              return_dict=True,
---> 46                              **self.hf_model_kwargs)
     47 
     48 # Cell

TypeError: BertForSequenceClassification object argument after ** must be a mapping, not list

How to do transfer learning on a pretrained downstream task model?

Hi @ohmeow ,

Is it possible to use blurr to do achieve this? I'm trying to do transfer learning on a pretrained NER task model with 39 labels (instead of a LM model like in your example) to a smaller NER dataset with only 5 labels. Unfortunately, I'm getting stuck on how to do so.

I tried 2 different ways but all lead to errors:

  1. Fill in config.num_labels
task = HF_TASKS_AUTO.TokenClassification
pretrained_model_name = 'cahya/bert-base-indonesian-NER' 
config = AutoConfig.from_pretrained(pretrained_model_name)
config.num_labels = len(labels) # 5

hf_arch, hf_config, hf_tokenizer, hf_model = BLURR_MODEL_HELPER.get_hf_objects(pretrained_model_name, 
                                                                               task=task, 
                                                                               config=config)
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-24-b2c1babcdb91> in <module>
      4 config.num_labels = len(labels) # 5
      5 
----> 6 hf_arch, hf_config, hf_tokenizer, hf_model = BLURR_MODEL_HELPER.get_hf_objects(pretrained_model_name, 
      7                                                                                task=task,
      8                                                                                config=config)

/opt/conda/envs/fastai/lib/python3.8/site-packages/blurr/utils.py in get_hf_objects(self, pretrained_model_name_or_path, task, config, tokenizer_cls, model_cls, config_kwargs, tokenizer_kwargs, model_kwargs, cache_dir)
    175                 model_cls = self.get_models(arch="auto", task=task.name)[0]
    176 
--> 177             model = model_cls.from_pretrained(pretrained_model_name_or_path,
    178                                               config=config,
    179                                               cache_dir=cache_dir,

/opt/conda/envs/fastai/lib/python3.8/site-packages/transformers/models/auto/modeling_auto.py in from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
   1611 
   1612         if type(config) in MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING.keys():
-> 1613             return MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING[type(config)].from_pretrained(
   1614                 pretrained_model_name_or_path, *model_args, config=config, **kwargs
   1615             )

/opt/conda/envs/fastai/lib/python3.8/site-packages/transformers/modeling_utils.py in from_pretrained(cls, pretrained_model_name_or_path, *model_args, **kwargs)
   1155                 )
   1156             if len(error_msgs) > 0:
-> 1157                 raise RuntimeError(
   1158                     "Error(s) in loading state_dict for {}:\n\t{}".format(
   1159                         model.__class__.__name__, "\n\t".join(error_msgs)

RuntimeError: Error(s) in loading state_dict for BertForTokenClassification:
	size mismatch for classifier.weight: copying a param with shape torch.Size([39, 768]) from checkpoint, the shape in current model is torch.Size([5, 768]).
	size mismatch for classifier.bias: copying a param with shape torch.Size([39]) from checkpoint, the shape in current model is torch.Size([5]).
  1. Add in an extra Linear(39, 5) layer at the end:
    model = HF_BaseModelWrapper(nn.Sequential(hf_model, nn.Linear(39, 5)))
    But when I call learn.fit(), it results in this:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-8587f3539821> in <module>
----> 1 learn.fit(1)

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
    209             self.opt.set_hypers(lr=self.lr if lr is None else lr)
    210             self.n_epoch = n_epoch
--> 211             self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)
    212 
    213     def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),(None,),None,None

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    158 
    159     def _with_events(self, f, event_type, ex, final=noop):
--> 160         try: self(f'before_{event_type}');  f()
    161         except ex: self(f'after_cancel_{event_type}')
    162         self(f'after_{event_type}');  final()

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/learner.py in _do_fit(self)
    200         for epoch in range(self.n_epoch):
    201             self.epoch=epoch
--> 202             self._with_events(self._do_epoch, 'epoch', CancelEpochException)
    203 
    204     def fit(self, n_epoch, lr=None, wd=None, cbs=None, reset_opt=False):

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    158 
    159     def _with_events(self, f, event_type, ex, final=noop):
--> 160         try: self(f'before_{event_type}');  f()
    161         except ex: self(f'after_cancel_{event_type}')
    162         self(f'after_{event_type}');  final()

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/learner.py in _do_epoch(self)
    194 
    195     def _do_epoch(self):
--> 196         self._do_epoch_train()
    197         self._do_epoch_validate()
    198 

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/learner.py in _do_epoch_train(self)
    186     def _do_epoch_train(self):
    187         self.dl = self.dls.train
--> 188         self._with_events(self.all_batches, 'train', CancelTrainException)
    189 
    190     def _do_epoch_validate(self, ds_idx=1, dl=None):

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    158 
    159     def _with_events(self, f, event_type, ex, final=noop):
--> 160         try: self(f'before_{event_type}');  f()
    161         except ex: self(f'after_cancel_{event_type}')
    162         self(f'after_{event_type}');  final()

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/learner.py in all_batches(self)
    164     def all_batches(self):
    165         self.n_iter = len(self.dl)
--> 166         for o in enumerate(self.dl): self.one_batch(*o)
    167 
    168     def _do_one_batch(self):

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/learner.py in one_batch(self, i, b)
    182         self.iter = i
    183         self._split(b)
--> 184         self._with_events(self._do_one_batch, 'batch', CancelBatchException)
    185 
    186     def _do_epoch_train(self):

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    158 
    159     def _with_events(self, f, event_type, ex, final=noop):
--> 160         try: self(f'before_{event_type}');  f()
    161         except ex: self(f'after_cancel_{event_type}')
    162         self(f'after_{event_type}');  final()

/opt/conda/envs/fastai/lib/python3.8/site-packages/fastai/learner.py in _do_one_batch(self)
    167 
    168     def _do_one_batch(self):
--> 169         self.pred = self.model(*self.xb)
    170         self('after_pred')
    171         if len(self.yb):

/opt/conda/envs/fastai/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

/opt/conda/envs/fastai/lib/python3.8/site-packages/blurr/modeling/core.py in forward(self, x)
     41             if k not in self.hf_model_fwd_args: del x[k]
     42 
---> 43         return self.hf_model(**x,
     44                              output_hidden_states=self.output_hidden_states,
     45                              output_attentions=self.output_attentions,

/opt/conda/envs/fastai/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

TypeError: forward() got an unexpected keyword argument 'output_hidden_states'

Regression task with blurr

Hi there,
I am a beginner and try to teach myself to use blurr for a regression task. My goal is to assign a score (1-5) to a sequence instead of assigning it to a category (e.g., positive or negative). As a starting point, I used the doc’s code on the GLUE benchmarks (https://ohmeow.github.io/blurr/examples-high-level-api.html) and tried to adapt it. I made the following changes to the code:

  • I replaced the GLUE data with the (English part of the) amazon_reviews_multi dataset (raw_datasets = load_dataset("amazon_reviews_multi", "en")
  • Changed the metric and loss function (learn_kwargs = { 'metrics': [rmse], 'loss_func': MSELossFlat() }
  • Specified the number of labels (n_labels=1)

Unfortunately, the code does not work. When I try the build the learner, I get the following error:

Could not do one pass in your dataloader, there is something wrong in it

---------------------------------------------------------------------------

RuntimeError                              Traceback (most recent call last)

<ipython-input-14-45bb1d37df2c> in <module>()
6                                                             dblock_splitter=IndexSplitter(valid_idxs),
7                                                             dl_kwargs=dl_kwargs, learner_kwargs=learn_kwargs,
----> 8                                                             n_labels=1)
9 learn = learn.to_fp16()

/usr/local/lib/python3.7/dist-packages/blurr/modeling/core.py in from_dictionaries(cls, ds, pretrained_model_name_or_path, preprocess_func, text_attr, label_attr, n_labels, dblock_splitter, dl_kwargs, learner_kwargs)
418         return cls._create_learner(ds, pretrained_model_name_or_path, preprocess_func,
419                                    text_attr, label_attr, n_labels, dblock_splitter,
--> 420                                    dl_kwargs, learner_kwargs)

/usr/local/lib/python3.7/dist-packages/blurr/modeling/core.py in _create_learner(cls, data, pretrained_model_name_or_path, preprocess_func, text_attr, label_attr, n_labels, dblock_splitter, dl_kwargs, learner_kwargs)
323
324         # return BLearner instance
--> 325         return cls(dls, hf_model, **learner_kwargs.copy())
326
327     @classmethod

/usr/local/lib/python3.7/dist-packages/blurr/modeling/core.py in __init__(self, dls, hf_model, **kwargs)
245         **kwargs
246     ):
--> 247         super().__init__(dls, hf_model, **kwargs)
248
249     @classmethod

/usr/local/lib/python3.7/dist-packages/blurr/modeling/core.py in __init__(self, dls, hf_model, base_model_cb, **kwargs)
226         **kwargs
227     ):
--> 228         model = kwargs.get('model', HF_BaseModelWrapper(hf_model))
229         loss_func = kwargs.pop('loss_func', dls.loss_func if hasattr(dls, 'loss_func') else None)
230         splitter = kwargs.pop('splitter', hf_splitter)

/usr/local/lib/python3.7/dist-packages/fastcore/meta.py in __call__(cls, *args, **kwargs)
37         if type(res)==cls:
38             if hasattr(res,'__pre_init__'): res.__pre_init__(*args,**kwargs)
---> 39             res.__init__(*args,**kwargs)
40             if hasattr(res,'__post_init__'): res.__post_init__(*args,**kwargs)
41         return res

/usr/local/lib/python3.7/dist-packages/blurr/modeling/core.py in __init__(self, hf_model, output_hidden_states, output_attentions, hf_model_kwargs)
58
59         store_attr(self=self, names='output_hidden_states, output_attentions, hf_model_kwargs')
---> 60         self.hf_model = hf_model.cuda() if torch.cuda.is_available() else hf_model
61
62         self.hf_model_fwd_args = list(inspect.signature(self.hf_model.forward).parameters.keys())

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in cuda(self, device)
678             Module: self
679         """
--> 680         return self._apply(lambda t: t.cuda(device))
681
682     def xpu(self: T, device: Optional[Union[int, device]] = None) -> T:

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _apply(self, fn)
568     def _apply(self, fn):
569         for module in self.children():
--> 570             module._apply(fn)
571
572         def compute_should_use_set_data(tensor, tensor_applied):

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _apply(self, fn)
568     def _apply(self, fn):
569         for module in self.children():
--> 570             module._apply(fn)
571
572         def compute_should_use_set_data(tensor, tensor_applied):

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _apply(self, fn)
568     def _apply(self, fn):
569         for module in self.children():
--> 570             module._apply(fn)
571
572         def compute_should_use_set_data(tensor, tensor_applied):

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _apply(self, fn)
591             # `with torch.no_grad():`
592             with torch.no_grad():
--> 593                 param_applied = fn(param)
594             should_use_set_data = compute_should_use_set_data(param, param_applied)
595             if should_use_set_data:

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in <lambda>(t)
678             Module: self
679         """
--> 680         return self._apply(lambda t: t.cuda(device))
681
682     def xpu(self: T, device: Optional[Union[int, device]] = None) -> T:

RuntimeError: CUDA error: device-side assert triggered
CUDA kernel errors might be asynchronously reported at some other API call,so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1.

I would be very thankful if somebody could help me set up blurr for a regression task!

Here is the Colab notebook: https://colab.research.google.com/drive/1qCwv-nE7JxYXsWOR9gsXZfM8k4Gx32tt?usp=sharing

This is the full code:

pip install -Uq fastai fastcore nbdev ohmeow-blurr datasets

import torch
from transformers import *
from fastai.text.all import *
from blurr.data.all import *
from blurr.modeling.all import *
from datasets import *

raw_datasets = load_dataset("amazon_reviews_multi", "en")
print(f'{raw_datasets}\n')
print(f'{raw_datasets["train"][0]}\n')
print(f'{raw_datasets["train"].features}\n')

train_ds = raw_datasets['train']#.select(range(10000))
valid_ds = raw_datasets['validation']#.select(range(2000))

n_train, n_valid = train_ds.num_rows, valid_ds.num_rows
train_idxs, valid_idxs = L(range(n_train)), L(range(n_train, n_train + n_valid))
raw_ds = concatenate_datasets([train_ds, valid_ds])

dl_kwargs = {'bs': 4, 'val_bs': 8}
learn_kwargs = { 'metrics': [rmse], 'loss_func': MSELossFlat() }

learn = BlearnerForSequenceClassification.from_dictionaries(raw_ds, 'distilroberta-base',
text_attr='review_body', label_attr='stars',
dblock_splitter=IndexSplitter(valid_idxs),
dl_kwargs=dl_kwargs, learner_kwargs=learn_kwargs,
n_labels=1)
learn = learn.to_fp16()

learn.dls.show_batch(dataloaders=learn.dls, trunc_at=500, max_n=5)

learn.fit_one_cycle(1, lr_max=2e-3)

learn.show_results(learner=learn, max_n=5)

Fine tune google/byt5-small

Greetings,
I tried fine tuning with google/byt5-small, it seemed to work well, but when trying to generate with learn.blurr_generate just outputs gibberish. As a comparison, fine tuning t5-small with the same data works well both on training and text generation with learn.blurr_generate.
What could I be missing?
Thanks

Installation issues.

When I run pip install ohmeow-blurr, most of the packages are installed but I get this error at the bottom. Is there something wrong with the dependencies or am I doing it wrong?

ERROR: Could not find a version that satisfies the requirement torch>=1.6.0 (from fastai>=2.0.0->ohmeow-blurr) (from versions: 0.1.2, 0.1.2.post1, 0.1.2.post2) ERROR: No matching distribution found for torch>=1.6.0 (from fastai>=2.0.0->ohmeow-blurr)

learner has no attribute hf_tokenizer

Hi, i'm having an issue with this code.
learn.lr_find(suggestions=True), learn.fit_one_cycle(1, lr_max=1e-2)

It throws an error below when following the codes from: https://github.com/ohmeow/ohmeow_website/blob/master/_notebooks/2020-05-23-text-generation-with-blurr.ipynb

fastai2 version: 0.0.30
blurr version: 0.0.7

`---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/learner.py in with_events(self, f, event_type, ex, final)
154 def with_events(self, f, event_type, ex, final=noop):
--> 155 try: self(f'before
{event_type}') ;f()
156 except ex: self(f'after_cancel
{event_type}')

/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/learner.py in all_batches(self)
160 self.n_iter = len(self.dl)
--> 161 for o in enumerate(self.dl): self.one_batch(*o)
162

/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/learner.py in one_batch(self, i, b)
175 self._split(b)
--> 176 self._with_events(self._do_one_batch, 'batch', CancelBatchException)
177

/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/learner.py in with_events(self, f, event_type, ex, final)
156 except ex: self(f'after_cancel
{event_type}')
--> 157 finally: self(f'after_{event_type}') ;final()
158

/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/learner.py in call(self, event_name)
132
--> 133 def call(self, event_name): L(event_name).map(self._call_one)
134

/anaconda3/envs/test/lib/python3.6/site-packages/fastcore/foundation.py in map(self, f, *args, **kwargs)
382 else f.getitem)
--> 383 return self._new(map(g, self))
384

/anaconda3/envs/test/lib/python3.6/site-packages/fastcore/foundation.py in _new(self, items, *args, **kwargs)
332 def _xtra(self): return None
--> 333 def _new(self, items, *args, **kwargs): return type(self)(items, *args, use_list=None, **kwargs)
334 def getitem(self, idx): return self._get(idx) if is_indexer(idx) else L(self._get(idx), use_list=None)

/anaconda3/envs/test/lib/python3.6/site-packages/fastcore/foundation.py in call(cls, x, args, **kwargs)
46
---> 47 res = super().call(
((x,) + args), **kwargs)
48 res._newchk = 0

/anaconda3/envs/test/lib/python3.6/site-packages/fastcore/foundation.py in init(self, items, use_list, match, *rest)
323 if (use_list is not None) or not _is_array(items):
--> 324 items = list(items) if use_list else _listify(items)
325 if match is not None:

/anaconda3/envs/test/lib/python3.6/site-packages/fastcore/foundation.py in _listify(o)
259 if isinstance(o, str) or _is_array(o): return [o]
--> 260 if is_iter(o): return list(o)
261 return [o]

/anaconda3/envs/test/lib/python3.6/site-packages/fastcore/foundation.py in call(self, *args, **kwargs)
225 fargs = [args[x.i] if isinstance(x, _Arg) else x for x in self.pargs] + args[self.maxi+1:]
--> 226 return self.fn(*fargs, **kwargs)
227

/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/learner.py in _call_one(self, event_name)
136 assert hasattr(event, event_name), event_name
--> 137 [cb(event_name) for cb in sort_by_run(self.cbs)]
138

/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/learner.py in (.0)
136 assert hasattr(event, event_name), event_name
--> 137 [cb(event_name) for cb in sort_by_run(self.cbs)]
138

/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/callback/core.py in call(self, event_name)
43 res = None
---> 44 if self.run and _run: res = getattr(self, event_name, noop)()
45 if event_name=='after_fit': self.run=True #Reset self.run to True at each end of fit

/anaconda3/envs/test/lib/python3.6/site-packages/blurr/modeling/summarization.py in after_batch(self)
80
---> 81 self.generated_ids += gen_ids.tolist()
82 self.refernce_ids += self.yb[0].tolist()

/anaconda3/envs/test/lib/python3.6/site-packages/fastcore/foundation.py in getattr(self, k)
239 attr = getattr(self,self._default,None)
--> 240 if attr is not None: return getattr(attr,k)
241 raise AttributeError(k)

AttributeError: 'Learner' object has no attribute 'generated_ids'

During handling of the above exception, another exception occurred:

AttributeError Traceback (most recent call last)
in
----> 1 learn.fit_one_cycle(1, lr_max=1e-2)

/anaconda3/envs/test/lib/python3.6/site-packages/fastcore/utils.py in _f(*args, **kwargs)
450 init_args.update(log)
451 setattr(inst, 'init_args', init_args)
--> 452 return inst if to_return else f(*args, **kwargs)
453 return _f
454

/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/callback/schedule.py in fit_one_cycle(self, n_epoch, lr_max, div, div_final, pct_start, wd, moms, cbs, reset_opt)
111 scheds = {'lr': combined_cos(pct_start, lr_max/div, lr_max, lr_max/div_final),
112 'mom': combined_cos(pct_start, *(self.moms if moms is None else moms))}
--> 113 self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd)
114
115 # Cell

/anaconda3/envs/test/lib/python3.6/site-packages/fastcore/utils.py in _f(*args, **kwargs)
450 init_args.update(log)
451 setattr(inst, 'init_args', init_args)
--> 452 return inst if to_return else f(*args, **kwargs)
453 return _f
454

/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
202 self.opt.set_hypers(lr=self.lr if lr is None else lr)
203 self.n_epoch,self.loss = n_epoch,tensor(0.)
--> 204 self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)
205
206 def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),(None,),None,None

/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/learner.py in with_events(self, f, event_type, ex, final)
153
154 def with_events(self, f, event_type, ex, final=noop):
--> 155 try: self(f'before
{event_type}') ;f()
156 except ex: self(f'after_cancel
{event_type}')
157 finally: self(f'after_{event_type}') ;final()

/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/learner.py in _do_fit(self)
192 for epoch in range(self.n_epoch):
193 self.epoch=epoch
--> 194 self._with_events(self._do_epoch, 'epoch', CancelEpochException)
195
196 @log_args(but='cbs')

/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/learner.py in with_events(self, f, event_type, ex, final)
153
154 def with_events(self, f, event_type, ex, final=noop):
--> 155 try: self(f'before
{event_type}') ;f()
156 except ex: self(f'after_cancel
{event_type}')
157 finally: self(f'after_{event_type}') ;final()

/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/learner.py in _do_epoch(self)
187 def _do_epoch(self):
188 self._do_epoch_train()
--> 189 self._do_epoch_validate()
190
191 def _do_fit(self):

/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/learner.py in _do_epoch_validate(self, ds_idx, dl)
183 if dl is None: dl = self.dls[ds_idx]
184 self.dl = dl;
--> 185 with torch.no_grad(): self._with_events(self.all_batches, 'validate', CancelValidException)
186
187 def _do_epoch(self):

/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/learner.py in with_events(self, f, event_type, ex, final)
155 try: self(f'before
{event_type}') ;f()
156 except ex: self(f'after_cancel_{event_type}')
--> 157 finally: self(f'after_{event_type}') ;final()
158
159 def all_batches(self):

/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/learner.py in call(self, event_name)
131 def ordered_cbs(self, event): return [cb for cb in sort_by_run(self.cbs) if hasattr(cb, event)]
132
--> 133 def call(self, event_name): L(event_name).map(self._call_one)
134
135 def _call_one(self, event_name):

/anaconda3/envs/test/lib/python3.6/site-packages/fastcore/foundation.py in map(self, f, *args, **kwargs)
381 else f.format if isinstance(f,str)
382 else f.getitem)
--> 383 return self._new(map(g, self))
384
385 def filter(self, f, negate=False, **kwargs):

/anaconda3/envs/test/lib/python3.6/site-packages/fastcore/foundation.py in _new(self, items, *args, **kwargs)
331 @Property
332 def _xtra(self): return None
--> 333 def _new(self, items, *args, **kwargs): return type(self)(items, *args, use_list=None, **kwargs)
334 def getitem(self, idx): return self._get(idx) if is_indexer(idx) else L(self._get(idx), use_list=None)
335 def copy(self): return self._new(self.items.copy())

/anaconda3/envs/test/lib/python3.6/site-packages/fastcore/foundation.py in call(cls, x, args, **kwargs)
45 return x
46
---> 47 res = super().call(
((x,) + args), **kwargs)
48 res._newchk = 0
49 return res

/anaconda3/envs/test/lib/python3.6/site-packages/fastcore/foundation.py in init(self, items, use_list, match, *rest)
322 if items is None: items = []
323 if (use_list is not None) or not _is_array(items):
--> 324 items = list(items) if use_list else _listify(items)
325 if match is not None:
326 if is_coll(match): match = len(match)

/anaconda3/envs/test/lib/python3.6/site-packages/fastcore/foundation.py in _listify(o)
258 if isinstance(o, list): return o
259 if isinstance(o, str) or _is_array(o): return [o]
--> 260 if is_iter(o): return list(o)
261 return [o]
262

/anaconda3/envs/test/lib/python3.6/site-packages/fastcore/foundation.py in call(self, *args, **kwargs)
224 if isinstance(v,_Arg): kwargs[k] = args.pop(v.i)
225 fargs = [args[x.i] if isinstance(x, _Arg) else x for x in self.pargs] + args[self.maxi+1:]
--> 226 return self.fn(*fargs, **kwargs)
227
228 # Cell

/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/learner.py in _call_one(self, event_name)
135 def _call_one(self, event_name):
136 assert hasattr(event, event_name), event_name
--> 137 [cb(event_name) for cb in sort_by_run(self.cbs)]
138
139 def _bn_bias_state(self, with_bias): return norm_bias_params(self.model, with_bias).map(self.opt.state)

/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/learner.py in (.0)
135 def _call_one(self, event_name):
136 assert hasattr(event, event_name), event_name
--> 137 [cb(event_name) for cb in sort_by_run(self.cbs)]
138
139 def _bn_bias_state(self, with_bias): return norm_bias_params(self.model, with_bias).map(self.opt.state)

/anaconda3/envs/test/lib/python3.6/site-packages/fastai2/callback/core.py in call(self, event_name)
42 (self.run_valid and not getattr(self, 'training', False)))
43 res = None
---> 44 if self.run and _run: res = getattr(self, event_name, noop)()
45 if event_name=='after_fit': self.run=True #Reset self.run to True at each end of fit
46 return res

/anaconda3/envs/test/lib/python3.6/site-packages/blurr/modeling/summarization.py in after_validate(self)
89 # are there rouge metrics to calculate?
90 if (self.rouge_metrics is not None and len(self.rouge_metrics) > 0):
---> 91 gen_texts = self.hf_tokenizer.batch_decode(self.generated_ids,
92 skip_special_tokens=True,
93 clean_up_tokenization_spaces=True)

/anaconda3/envs/test/lib/python3.6/site-packages/fastcore/foundation.py in getattr(self, k)
238 if self._component_attr_filter(k):
239 attr = getattr(self,self._default,None)
--> 240 if attr is not None: return getattr(attr,k)
241 raise AttributeError(k)
242 def dir(self): return custom_dir(self,self._dir())

AttributeError: 'Learner' object has no attribute 'hf_tokenizer'`

A suggestion about HF_Splitter

Hi,

First let me express my gratitude to your great work! Without it, I can't run my experiments easily.

I suppose you know that already, since HF 3.3.0, the pooling layer is removed from classification models, so for parameter groups, it becomes 3 instead of 4, which can make discriminative fine-tuning and gradual unfreezing less effective. The situation makes me wonder, perhaps HF_Splitter can have an option (or by default) that further split encoder block of Transformer layer by layer?

Thank you!

Fine tune language model on labeled data for downstream classification

In previous fastai sentence classification problems I've done the following approach, using a standard labeled dataset as df (like IMDB):

dblocks = DataBlock(blocks=(TextBlock.from_df('text', tok=tok, is_lm=True)),
                    get_x=ColReader('text'), 
                    splitter=ColSplitter())
dls = dblocks.dataloaders(df, bs=64)

# finetune language model
learn = language_model_from_pretrained(dls, url=url, drop_mult=1).to_fp16()
learn.lr_find()
lr = 3e-2
learn.fit_one_cycle(1, lr, moms=(0.8,0.7,0.8))
path = learn.save_lm('tmp/test_lm')
vocab = learn.dls.vocab

# train classifier
dblocks = DataBlock(blocks=(TextBlock.from_df('text', tok=tok, vocab=vocab), CategoryBlock),
                    get_x=ColReader('text'),
                    get_y=ColReader('label'), 
                    splitter=ColSplitter())
dls = dblocks.dataloaders(df, bs=128)
learn = text_classifier_from_lm(dls, path=path, metrics=[accuracy]).to_fp16()
learn.lr_find()
learn.fine_tune(5, 1e-2, moms=(0.8,0.7,0.8), wd=0.1)=

My understanding is that the first finetuning of the language model increases performance for downstream classification.

I don't see a clear implementation of this finetuning language modeling process on a pretrained model in your library. Is that because you tried it and it wasn't as useful with transformers as with AWD-LSTM? Are you aware of how one could accomplish this using DistillBERT, or any transformer compatible with your library?

Thanks!

osmeow-blurr seems broken after recent fastai update to v2.3.0

Been using blurr for awhile now but recently started encountering a TypeError: Tensor does not support deleting items error in core.py.

I went back to the docs to recreate the imdb SequenceClassification errors in Colab and I get the same forward function errors using the lr_find, and fine_tune methods for Learner. Has anyone else starting seeing this issue? Does anyone have a working imdb example that can be ran in Colab today?

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-82cb2a399f5a> in <module>()
      1 # learn.fit_one_cycle(1, lr_max=3e-5, cbs=fit_cbs)
----> 2 learn.fine_tune(1)

14 frames
/usr/local/lib/python3.7/dist-packages/fastai/callback/schedule.py in fine_tune(self, epochs, base_lr, freeze_epochs, lr_mult, pct_start, div, **kwargs)
    155     "Fine tune with `freeze` for `freeze_epochs` then with `unfreeze` from `epochs` using discriminative LR"
    156     self.freeze()
--> 157     self.fit_one_cycle(freeze_epochs, slice(base_lr), pct_start=0.99, **kwargs)
    158     base_lr /= 2
    159     self.unfreeze()

/usr/local/lib/python3.7/dist-packages/fastai/callback/schedule.py in fit_one_cycle(self, n_epoch, lr_max, div, div_final, pct_start, wd, moms, cbs, reset_opt)
    110     scheds = {'lr': combined_cos(pct_start, lr_max/div, lr_max, lr_max/div_final),
    111               'mom': combined_cos(pct_start, *(self.moms if moms is None else moms))}
--> 112     self.fit(n_epoch, cbs=ParamScheduler(scheds)+L(cbs), reset_opt=reset_opt, wd=wd)
    113 
    114 # Cell

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in fit(self, n_epoch, lr, wd, cbs, reset_opt)
    210             self.opt.set_hypers(lr=self.lr if lr is None else lr)
    211             self.n_epoch = n_epoch
--> 212             self._with_events(self._do_fit, 'fit', CancelFitException, self._end_cleanup)
    213 
    214     def _end_cleanup(self): self.dl,self.xb,self.yb,self.pred,self.loss = None,(None,),(None,),None,None

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    158 
    159     def _with_events(self, f, event_type, ex, final=noop):
--> 160         try: self(f'before_{event_type}');  f()
    161         except ex: self(f'after_cancel_{event_type}')
    162         self(f'after_{event_type}');  final()

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _do_fit(self)
    201         for epoch in range(self.n_epoch):
    202             self.epoch=epoch
--> 203             self._with_events(self._do_epoch, 'epoch', CancelEpochException)
    204 
    205     def fit(self, n_epoch, lr=None, wd=None, cbs=None, reset_opt=False):

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    158 
    159     def _with_events(self, f, event_type, ex, final=noop):
--> 160         try: self(f'before_{event_type}');  f()
    161         except ex: self(f'after_cancel_{event_type}')
    162         self(f'after_{event_type}');  final()

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _do_epoch(self)
    195 
    196     def _do_epoch(self):
--> 197         self._do_epoch_train()
    198         self._do_epoch_validate()
    199 

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _do_epoch_train(self)
    187     def _do_epoch_train(self):
    188         self.dl = self.dls.train
--> 189         self._with_events(self.all_batches, 'train', CancelTrainException)
    190 
    191     def _do_epoch_validate(self, ds_idx=1, dl=None):

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    158 
    159     def _with_events(self, f, event_type, ex, final=noop):
--> 160         try: self(f'before_{event_type}');  f()
    161         except ex: self(f'after_cancel_{event_type}')
    162         self(f'after_{event_type}');  final()

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in all_batches(self)
    164     def all_batches(self):
    165         self.n_iter = len(self.dl)
--> 166         for o in enumerate(self.dl): self.one_batch(*o)
    167 
    168     def _do_one_batch(self):

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in one_batch(self, i, b)
    183         b_on_device = tuple( e.to(device=self.dls.device) for e in b if hasattr(e, "to")) if self.dls.device is not None else b
    184         self._split(b_on_device)
--> 185         self._with_events(self._do_one_batch, 'batch', CancelBatchException)
    186 
    187     def _do_epoch_train(self):

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _with_events(self, f, event_type, ex, final)
    158 
    159     def _with_events(self, f, event_type, ex, final=noop):
--> 160         try: self(f'before_{event_type}');  f()
    161         except ex: self(f'after_cancel_{event_type}')
    162         self(f'after_{event_type}');  final()

/usr/local/lib/python3.7/dist-packages/fastai/learner.py in _do_one_batch(self)
    167 
    168     def _do_one_batch(self):
--> 169         self.pred = self.model(*self.xb)
    170         self('after_pred')
    171         if len(self.yb):

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
    725             result = self._slow_forward(*input, **kwargs)
    726         else:
--> 727             result = self.forward(*input, **kwargs)
    728         for hook in itertools.chain(
    729                 _global_forward_hooks.values(),

/usr/local/lib/python3.7/dist-packages/blurr/modeling/core.py in forward(self, x)
     39     def forward(self, x):
     40         for k in list(x):
---> 41             if k not in self.hf_model_fwd_args: del x[k]
     42 
     43         return self.hf_model(**x,

TypeError: Tensor does not support deleting items

*I have found an unrelated blurr example notebook that exhibits the same behavior and no longer runs properly, as well. It errors after trying to run the lr_find cell and also the cell containing fit_one_cycle().

https://colab.research.google.com/github/ohmeow/ohmeow_website/blob/master/_notebooks/2020-05-23-text-generation-with-blurr.ipynb#scrollTo=9_xhrbcgAtS0

Cannot suppress nltk warnings

Normally, when downloading wordnet with nltk, you could suppress the warnings like so:

nltk.download('wordnet', quiet=True)

This doesn't work when importing modules, e.g. from blurr.data.all import *. In fact, none of the traditional output suppression techniques seems to work (like running with -W).

This isn't a big deal in a jupyter notebook, but it can be a pain when running inference as part of an automated job.

'NoneType' object has no attribute 'clone'

Using the latest ! pip install -q ohmeow-blurr and getting the above error. Is there an issue with Fastai?

----> 1 learn.fit_one_cycle(1, lr_max=3e-3) 
...
/usr/local/lib/python3.6/dist-packages/fastai/learner.py in _do_one_batch(self)` 
  171         if len(self.yb):
    172             self.loss_grad = self.loss_func(self.pred, *self.yb)
--> 173             self.loss = self.loss_grad.clone()
    174         self('after_loss')
    175         if not self.training or not len(self.yb): return

AttributeError: 'NoneType' object has no attribute 'clone'

how to finetune Bart to summarization task of my own?

Hi Blurr, Thank you for your work first.

My question is about [blurr/nbs/02zc_modeling-seq2seq-summarization.ipynb]

Question one:
I have read through this notebook and could I use the same codes to finetune Bart on my own academic publication dataset?
I mean, after converting my data into the format of cnndm_sample.csv, Is it right that I only need to change {the csv file location} and {params of the function learn.fit_one_cycle()} ?

Question two:
If I only need to change the params of learn.fit_one_cycle(), the first param is epoch, and I could change epoch to finetune different models and compare their performance. How about the lr_max param and could I use 4e-5 also?
About the epoch, do you have suggestions?(I have about 1,000,000 articles of full texts and their abstraction)

Installing Blurr Breaks Kaggle Notebook Saving

Installing blurr in a Kaggle notebook breaks saving the notebook due incompatible nbconvert requirements. Kaggle requires nbconvert-6.1.0, but blurr installs nbconvert-5.6.1.

This appears to be due to blurr requiring nbverbose as a install requirement. A possible fix on blurr's end could be moving nbverbose to dev_requirements until nbdev has it's dependencies updated.

The dependency tree, from pipdeptree:

- ohmeow-blurr==0.1.0 [requires: nbverbose>=0.0.1] 
   - nbverbose==0.0.9 [requires: nbdev<2.0.0]
      - nbdev==1.1.23 [requires: nbconvert<6]

tpu

can i use tpu to train model on this

QA task preprocessing

Hi,
I use the QA example with load_dataset('squad_v2', split='train'). The dataset contains column 'Answer', pre_process_squad expects column 'answer_text' which produces a KeyError: 'answer_text'.

That topic was already described here and should be fixed.
What's wrong here?

BTW - great library!

Typo in _create_learner?

Found hf_confg in classmethod _create_learner from BlearnerForQuestionAnswering

    if (max_seq_len is None):
        max_seq_len = hf_confg.get('max_position_embeddings', 128)

Cannot train model on google colab

I switched to using google colab but i run into the following error
`IndexError Traceback (most recent call last)

in ()
----> 1 learn.fit_one_cycle(4, lr_max=1e-3)

20 frames

/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
2262 .format(input.size(0), target.size(0)))
2263 if dim == 2:
-> 2264 ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
2265 elif dim == 4:
2266 ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)

IndexError: Target 2 is out of bounds.

`

Latest fastcore/fastai breaks blurr

Hi Wayde!

I came across this error today when running this line of code.

hf_batch_tfm = HF_SummarizationBatchTransform(hf_arch, hf_tokenizer)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
 in 
----> 1 hf_batch_tfm = HF_SummarizationBatchTransform(hf_arch, hf_tokenizer)

~/projects/fastcore/fastcore/transform.py in __call__(cls, *args, **kwargs)
     36             getattr(cls,n).add(f)
     37             return f
---> 38         return super().__call__(*args, **kwargs)
     39 
     40     @classmethod

~/projects/blurr/blurr/data/summarization.py in __init__(self, hf_arch, hf_tokenizer, **kwargs)
     20 class HF_SummarizationBatchTransform(HF_BatchTransform):
     21     def __init__(self, hf_arch, hf_tokenizer, **kwargs):
---> 22         super().__init__(hf_arch, hf_tokenizer, HF_SummarizationInput, **kwargs)
     23 
     24     def encodes(self, samples):

~/projects/blurr/blurr/data/core.py in __init__(self, hf_arch, hf_tokenizer, hf_input_return_type, **kwargs)
     57     """
     58     def __init__(self, hf_arch, hf_tokenizer, hf_input_return_type=HF_BaseInput, **kwargs):
---> 59         store_attr(self, 'hf_arch, hf_tokenizer, hf_input_return_type, kwargs')
     60 
     61     def encodes(self, samples): return samples

~/projects/fastcore/fastcore/utils.py in store_attr(names, self, but, **attrs)
     95     args,varargs,keyw,locs = inspect.getargvalues(fr)
     96     if self is None: self = locs[args[0]]
---> 97     if not hasattr(self, '__stored_args__'): self.__stored_args__ = {}
     98     if attrs: return _store_attr(self, **attrs)
     99 

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

This code works with fastai 2.0.6 and fastcore 1.0.0. I still get quite lost reading all Jeremy's fastcore code (planning to go through Hamel Husain's blog about it soon!) but I came across this diff from yesterday which might be where the problem was introduced.

fastai/fastcore@ea1c33f#diff-2d5ca7a202ff50e2df29f58cfa4a4c12

'HF_AfterBatchTransform' object has no attribute 'is_split_into_words'

Hello,
I'm trying to build the REST Api with FastApi and I am getting this error:

File "/Users/brl.314/Downloads/blurr/blurr/modeling/core.py", line 142, in blurr_predict
is_split_str = batch_tfm.is_split_into_words and isinstance(items[0], str)
AttributeError: 'HF_AfterBatchTransform' object has no attribute 'is_split_into_words'

And the core.py has is_split_into_words as an attribute.

error of summarization issue while fine-tuning

Hello,

I have an error in this line.

learn.fit_one_cycle(1, lr_max=3e-5, cbs=fit_cbs)

the error i got is :

/usr/local/lib/python3.7/dist-packages/blurr/modeling/seq2seq/core.py in after_validate(self) 138 for score_key, score in res.items(): 139 if (f'{metric_name}{score_key}' not in self.custom_metric_vals): continue --> 140 self.custom_metric_vals[f'{metric_name}{score_key}'] = score.mean().item() 141 elif (is_listy(return_val)):

AttributeError: 'list' object has no attribute 'mean'.

Can you help me please?

99a_examples-muiltilabel.nbs seems not to use comment in blurr_predict

I've git pulled your project and notebooks and had no problems with the 01_data_token_classifications.nbs. When I tried the 099a_multilabel_classification example, the output seemed reasonable at first. I decided to try substituting other sentences to learn.predict_blurr and got exactly the same results, not matter what I provided. My setup is similar to your test setup.

Using pytorch 1.7.1
Using fastai 2.3.1
Using transformers 4.3.2
Using GPU #0: NVIDIA GeForce GTX 1080

but because of cuda memory constraints, I set my bs=4

results

note that the result vector is always the same. I tried comments of different lengths too with the same resutls.

No module named 'nlp'

I got this when trying to import after installing from master pip install -e ".[dev]" I try to use it on a ipynb and got this:

import torch
from transformers import *
from fastai2.text.all import *

from blurr.data.all import *
from blurr.modeling.all import *

I get

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-906f6e0a84bd> in <module>
      3 from fastai2.text.all import *
      4 
----> 5 from blurr.data.all import *
      6 from blurr.modeling.all import *

~/Documentos/github/blurr/blurr/data/all.py in <module>
      1 from ..utils import *
----> 2 from .core import *
      3 from .language_modeling import *
      4 from .question_answering import *
      5 from .token_classification import *

~/Documentos/github/blurr/blurr/data/core.py in <module>
      6 from functools import reduce
      7 
----> 8 import torch, nlp
      9 from transformers import *
     10 from fastai2.text.all import *

ModuleNotFoundError: No module named 'nlp'

How to use BatchLossFilter callback with blurr

I've been trying to experiment with using tsai's BatchLossFilter callback. If I try to run the training with this callback

model = HF_BaseModelWrapper(hf_model)

learn = Learner(dls, 
                model,
                loss_func=LabelSmoothingCrossEntropyFlat(),
                metrics=[accuracy],
                cbs=[HF_BaseModelCallback],
                splitter=hf_splitter).to_fp16()

learn.unfreeze()

cbs = [BatchLossFilter(loss_perc=0.4)]

learn.fit_one_cycle(
    3,
    lr_max=3e-5,
    cbs = cbs
)

I get the following error, which is due to the SequenceClassifierOutput object from huggingface

---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-13-ce9f3d20977f> in <module>()
      2     3,
      3     lr_max=3e-5,
----> 4     cbs = cbs
      5 )

18 frames

/usr/local/lib/python3.7/dist-packages/fastai/losses.py in __call__(self, inp, targ, **kwargs)
     32         if self.floatify and targ.dtype!=torch.float16: targ = targ.float()
     33         if targ.dtype in [torch.int8, torch.int16, torch.int32]: targ = targ.long()
---> 34         if self.flatten: inp = inp.view(-1,inp.shape[-1]) if self.is_2d else inp.view(-1)
     35         return self.func.__call__(inp, targ.view(-1) if self.flatten else targ, **kwargs)
     36 

AttributeError: 'SequenceClassifierOutput' object has no attribute 'view'

Is there any way I can adapt BatchLossFilter to be functional with blurr? I haven't had any issues using other callbacks with blurr

'float' object is not subscriptable

Hello,
Following the demo at https://github.com/ohmeow/blurr/blob/master/nbs/02e_modeling-summarization.ipynb, I get the following error at dls = dblock.dataloaders(cnndm_df, bs=2) :

TypeError Traceback (most recent call last)
in ()
----> 1 dls = dblock.dataloaders(cnndm_df, bs=2)

18 frames
/usr/local/lib/python3.6/dist-packages/blurr/data/core.py in encodes(self, inp)
30 inps = [inp, None] if (isinstance(inp, str) or self.is_pretokenized) else inp
31
---> 32 res = self.hf_tokenizer(inps[0], inps[1],
33 max_length=self.max_length,
34 padding=self.padding,

TypeError: 'float' object is not subscriptable

fine-tune bart with custom dataset in other language?

Thanks for this great work!
I found your example on how to fine-tune Bart with CNN/DM dataset. It's very helpful.
Is it possible to fine-tune Bart with the dataset in another language?
If possible, I think it is necessary to train a SentencePiece tokenizer with a Japanese corpus.
Do you have any advice?

TypeError: 'PosixPath' object is not iterable

I'm trying to build a dataloader with data from folders and facing this error:

task = HF_TASKS_AUTO.SequenceClassification
pretrained_model_name = "bert-base-uncased"
hf_arch, hf_config, hf_tokenizer, hf_model = BLURR_MODEL_HELPER.get_hf_objects(pretrained_model_name, task=task)

path = untar_data(URLs.IMDB)

db = DataBlock(blocks=(HF_TextBlock(hf_arch=hf_arch, hf_tokenizer=hf_tokenizer), CategoryBlock), 
               get_items=get_text_files,
               splitter=GrandparentSplitter(train_name='train', valid_name='test'),
               get_y=parent_label)

db.summary(path)
Setting-up type transforms pipelines
Collecting items from /storage/data/imdb
Found 100002 items
2 datasets of sizes 25000,25000
Setting up Pipeline: HF_TokenizerTransform
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-34-d2bf6908b967> in <module>
----> 1 db.summary(path)

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastai2/data/block.py in summary(self, source, bs, show_batch, **kwargs)
    152     "Steps through the transform pipeline for one batch, and optionally calls `show_batch(**kwargs)` on the transient `Dataloaders`."
    153     print(f"Setting-up type transforms pipelines")
--> 154     dsets = self.datasets(source, verbose=True)
    155     print("\nBuilding one sample")
    156     for tl in dsets.train.tls:

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastai2/data/block.py in datasets(self, source, verbose)
    102         splits = (self.splitter or RandomSplitter())(items)
    103         pv(f"{len(splits)} datasets of sizes {','.join([str(len(s)) for s in splits])}", verbose)
--> 104         return Datasets(items, tfms=self._combine_type_tfms(), splits=splits, dl_type=self.dl_type, n_inp=self.n_inp, verbose=verbose)
    105 
    106     def dataloaders(self, source, path='.', verbose=False, **kwargs):

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastai2/data/core.py in __init__(self, items, tfms, tls, n_inp, dl_type, **kwargs)
    283     def __init__(self, items=None, tfms=None, tls=None, n_inp=None, dl_type=None, **kwargs):
    284         super().__init__(dl_type=dl_type)
--> 285         self.tls = L(tls if tls else [TfmdLists(items, t, **kwargs) for t in L(ifnone(tfms,[None]))])
    286         self.n_inp = ifnone(n_inp, max(1, len(self.tls)-1))
    287 

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastai2/data/core.py in <listcomp>(.0)
    283     def __init__(self, items=None, tfms=None, tls=None, n_inp=None, dl_type=None, **kwargs):
    284         super().__init__(dl_type=dl_type)
--> 285         self.tls = L(tls if tls else [TfmdLists(items, t, **kwargs) for t in L(ifnone(tfms,[None]))])
    286         self.n_inp = ifnone(n_inp, max(1, len(self.tls)-1))
    287 

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastcore/foundation.py in __call__(cls, x, *args, **kwargs)
     45             return x
     46 
---> 47         res = super().__call__(*((x,) + args), **kwargs)
     48         res._newchk = 0
     49         return res

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastai2/data/core.py in __init__(self, items, tfms, use_list, do_setup, split_idx, train_setup, splits, types, verbose, dl_type)
    221         if do_setup:
    222             pv(f"Setting up {self.tfms}", verbose)
--> 223             self.setup(train_setup=train_setup)
    224 
    225     def _new(self, items, split_idx=None, **kwargs):

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastai2/data/core.py in setup(self, train_setup)
    243             for f in self.tfms.fs:
    244                 self.types.append(getattr(f, 'input_types', type(x)))
--> 245                 x = f(x)
    246             self.types.append(type(x))
    247         types = L(t if is_listy(t) else [t] for t in self.types).concat().unique()

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastcore/transform.py in __call__(self, x, **kwargs)
     70     @property
     71     def name(self): return getattr(self, '_name', _get_name(self))
---> 72     def __call__(self, x, **kwargs): return self._call('encodes', x, **kwargs)
     73     def decode  (self, x, **kwargs): return self._call('decodes', x, **kwargs)
     74     def __repr__(self): return f'{self.name}: {self.encodes} {self.decodes}'

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastcore/transform.py in _call(self, fn, x, split_idx, **kwargs)
     80     def _call(self, fn, x, split_idx=None, **kwargs):
     81         if split_idx!=self.split_idx and self.split_idx is not None: return x
---> 82         return self._do_call(getattr(self, fn), x, **kwargs)
     83 
     84     def _do_call(self, f, x, **kwargs):

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastcore/transform.py in _do_call(self, f, x, **kwargs)
     84     def _do_call(self, f, x, **kwargs):
     85         if not _is_tuple(x):
---> 86             return x if f is None else retain_type(f(x, **kwargs), x, f.returns_none(x))
     87         res = tuple(self._do_call(f, x_, **kwargs) for x_ in x)
     88         return retain_type(res, x)

/opt/conda/envs/fastai/lib/python3.7/site-packages/fastcore/dispatch.py in __call__(self, *args, **kwargs)
     96         if not f: return args[0]
     97         if self.inst is not None: f = MethodType(f, self.inst)
---> 98         return f(*args, **kwargs)
     99 
    100     def __get__(self, inst, owner):

/opt/conda/envs/fastai/lib/python3.7/site-packages/blurr/data/core.py in encodes(self, inp)
     30             toks = self.hf_tokenizer.tokenize(inp, add_prefix_space=self.add_prefix_space)
     31         else:
---> 32             toks = [sub_toks for entity in inp
     33                     for sub_toks in self.hf_tokenizer.tokenize(entity, add_prefix_space=self.add_prefix_space)]
     34 

TypeError: 'PosixPath' object is not iterable

Value Error when importing blurr.modeling.all and blurr.data.all

After pip install in Colab, import blurr.modeling.all and blurr.data.all are throwing errors. Did not encounter errors last week, not sure when they began. Has there been a syntax change for setup?


ValueError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/range.py in get_loc(self, key, method, tolerance)
354 try:
--> 355 return self._range.index(new_key)
356 except ValueError as err:

ValueError: 1 is not in range

The above exception was the direct cause of the following exception:

KeyError Traceback (most recent call last)
6 frames
in ()
----> 1 from blurr.modeling.all import *

/usr/local/lib/python3.6/dist-packages/blurr/modeling/all.py in ()
----> 1 from ..utils import *
2 from .core import *
3 from .question_answering import *
4 from .token_classification import *
5 from .text2text.core import *

/usr/local/lib/python3.6/dist-packages/blurr/utils.py in ()
182
183 # Cell
--> 184 BLURR_MODEL_HELPER = ModelHelper()
185
186 # Cell

/usr/local/lib/python3.6/dist-packages/blurr/utils.py in call(self, *args, **kwargs)
25
26 def call(self, *args, **kwargs):
---> 27 if self._instance == None: self._instance = self._cls(*args, **kwargs)
28 return self._instance
29

/usr/local/lib/python3.6/dist-packages/blurr/utils.py in init(self)
59 model_type_df = self._df[(self._df.functional_area == 'modeling')].class_name.str.split('For', n=1, expand=True)
60
---> 61 model_type_df[1] = np.where(model_type_df[1].notnull(),
62 'For' + model_type_df[1].astype(str),
63 model_type_df[1])

/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py in getitem(self, key)
2904 if self.columns.nlevels > 1:
2905 return self._getitem_multilevel(key)
-> 2906 indexer = self.columns.get_loc(key)
2907 if is_integer(indexer):
2908 indexer = [indexer]

/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/range.py in get_loc(self, key, method, tolerance)
355 return self._range.index(new_key)
356 except ValueError as err:
--> 357 raise KeyError(key) from err
358 raise KeyError(key)
359 return super().get_loc(key, method=method, tolerance=tolerance)

KeyError: 1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.