from transformers import AutoModelForSequenceClassification
from fastai.text.all import *
from fastai.callback.wandb import *
from fasthugs.learner import TransLearner
from fasthugs.data import TransformersTextBlock, TextGetter, get_splits, PreprocCategoryBlock
from datasets import load_dataset, concatenate_datasets
Let's define main settings for the run in one place:
ds_name = 'glue'
model_name = "distilroberta-base"
max_len = 512
bs = 32
val_bs = bs*2
lr = 3e-5
GLUE_TASKS = ["cola", "mnli", "mnli-mm", "mrpc", "qnli", "qqp", "rte", "sst2", "stsb", "wnli"]
def validate_task():
assert task in GLUE_TASKS
from fastai.metrics import MatthewsCorrCoef, F1Score, PearsonCorrCoef, SpearmanCorrCoef
glue_metrics = {
'cola':[MatthewsCorrCoef()],
'sst2':[accuracy],
'mrpc':[F1Score(), accuracy],
'stsb':[PearsonCorrCoef(), SpearmanCorrCoef()],
'qqp' :[F1Score(), accuracy],
'mnli':[accuracy],
'qnli':[accuracy],
'rte' :[accuracy],
'wnli':[accuracy],
}
task = 'cola'
validate_task()
ds = load_dataset(ds_name, task)
ds.keys()
len(ds['train']), len(ds['validation'])
train_idx, valid_idx = get_splits(ds)
valid_idx
train_ds = concatenate_datasets([ds['train'], ds['validation']])
train_ds[0]
vocab = train_ds.features['label'].names
dblock = DataBlock(blocks = [TransformersTextBlock(pretrained_model_name=model_name), PreprocCategoryBlock(vocab)],
get_x=ItemGetter('sentence'),
get_y=ItemGetter('label'),
splitter=IndexSplitter(valid_idx))
%%time
dls = dblock.dataloaders(train_ds, bs=bs, val_bs=val_bs)
dls.show_batch(max_n=4)
import wandb
WANDB_NAME = f'{ds_name}-{task}-{model_name}'
GROUP = f'{ds_name}-{task}-{model_name}-{lr:.0e}'
NOTES = f'finetuning {model_name} with RAdam lr={lr:.0e}'
CONFIG = {}
TAGS =[model_name, ds_name, 'radam']
wandb.init(reinit=True, project="fasthugs", entity="fastai_community",
name=WANDB_NAME, group=GROUP, notes=NOTES, tags=TAGS, config=CONFIG);
model = AutoModelForSequenceClassification.from_pretrained(model_name)
metrics = glue_metrics[task]
learn = TransLearner(dls, model, metrics=metrics).to_fp16()
cbs = [WandbCallback(log_preds=False, log_model=False), SaveModelCallback(monitor=metrics[0].name)]
learn.fit_one_cycle(4, lr, cbs=cbs)
learn.show_results()
task = 'sst2'
validate_task()
ds = load_dataset(ds_name, task)
ds.keys()
len(ds['train']), len(ds['validation'])
train_idx, valid_idx = get_splits(ds)
valid_idx
train_ds = concatenate_datasets([ds['train'], ds['validation']])
train_ds[0]
vocab = train_ds.features['label'].names
dblock = DataBlock(blocks = [TransformersTextBlock(pretrained_model_name=model_name), PreprocCategoryBlock(vocab)],
get_x=ItemGetter('sentence'),
get_y=ItemGetter('label'),
splitter=IndexSplitter(valid_idx))
%%time
dls = dblock.dataloaders(train_ds, bs=bs, val_bs=val_bs)
dls.show_batch(max_n=4)
WANDB_NAME = f'{ds_name}-{task}-{model_name}'
GROUP = f'{ds_name}-{task}-{model_name}-{lr:.0e}'
NOTES = f'finetuning {model_name} with RAdam lr={lr:.0e}'
TAGS =[model_name, ds_name, 'radam']
wandb.init(reinit=True, project="fasthugs", entity="fastai_community",
name=WANDB_NAME, group=GROUP, notes=NOTES, tags=TAGS, config=CONFIG);
model = AutoModelForSequenceClassification.from_pretrained(model_name)
metrics = glue_metrics[task]
learn = TransLearner(dls, model, metrics=metrics).to_fp16()
cbs = [WandbCallback(log_preds=False, log_model=False), SaveModelCallback(monitor=metrics[0].__name__)]
learn.fit_one_cycle(4, lr, cbs=cbs)
learn.show_results()
task = 'mrpc'
validate_task()
ds = load_dataset(ds_name, task)
len(ds['train']), len(ds['validation'])
train_idx, valid_idx = get_splits(ds)
valid_idx
train_ds = concatenate_datasets([ds['train'], ds['validation']])
train_ds[0]
vocab = train_ds.features['label'].names
dblock = DataBlock(blocks = [TransformersTextBlock(pretrained_model_name=model_name), PreprocCategoryBlock(vocab)],
get_x=TextGetter('sentence1', 'sentence2'),
get_y=ItemGetter('label'),
splitter=IndexSplitter(valid_idx))
%%time
dls = dblock.dataloaders(train_ds, bs=bs, val_bs=val_bs)
dls.show_batch(max_n=4)
WANDB_NAME = f'{ds_name}-{task}-{model_name}'
GROUP = f'{ds_name}-{task}-{model_name}-{lr:.0e}'
NOTES = f'finetuning {model_name} with RAdam lr={lr:.0e}'
TAGS =[model_name, ds_name, 'radam']
wandb.init(reinit=True, project="fasthugs", entity="fastai_community",
name=WANDB_NAME, group=GROUP, notes=NOTES, tags=TAGS, config=CONFIG);
model = AutoModelForSequenceClassification.from_pretrained(model_name)
metrics = glue_metrics[task]
learn = TransLearner(dls, model, metrics=metrics).to_fp16()
cbs = [WandbCallback(log_preds=False, log_model=False), SaveModelCallback(monitor=metrics[0].name)]
learn.fit_one_cycle(4, lr, cbs=cbs)
learn.show_results()
task = 'stsb'
validate_task()
ds = load_dataset(ds_name, task)
len(ds['train']), len(ds['validation'])
train_idx, valid_idx = get_splits(ds)
valid_idx
train_ds = concatenate_datasets([ds['train'], ds['validation']])
train_ds[0]
dblock = DataBlock(blocks = [TransformersTextBlock(pretrained_model_name=model_name), RegressionBlock(1)],
get_x=TextGetter('sentence1', 'sentence2'),
get_y=ItemGetter('label'),
splitter=IndexSplitter(valid_idx))
%%time
dls = dblock.dataloaders(train_ds, bs=bs, val_bs=val_bs)
dls.show_batch(max_n=4)
WANDB_NAME = f'{ds_name}-{task}-{model_name}'
GROUP = f'{ds_name}-{task}-{model_name}-{lr:.0e}'
NOTES = f'finetuning {model_name} with RAdam lr={lr:.0e}'
TAGS =[model_name, ds_name, 'radam']
wandb.init(reinit=True, project="fasthugs", entity="fastai_community",
name=WANDB_NAME, group=GROUP, notes=NOTES, tags=TAGS, config=CONFIG);
model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=1)
metrics = glue_metrics[task]
learn = TransLearner(dls, model, metrics=metrics).to_fp16()
cbs = []
learn.fit_one_cycle(4, lr, cbs=cbs)
learn.show_results()
task = 'qqp'
validate_task()
ds = load_dataset(ds_name, task)
len(ds['train']), len(ds['validation'])
train_idx, valid_idx = get_splits(ds)
valid_idx
train_ds = concatenate_datasets([ds['train'], ds['validation']])
train_ds[0]
dblock = DataBlock(blocks = [TransformersTextBlock(pretrained_model_name=model_name), PreprocCategoryBlock(vocab)],
get_x=TextGetter('question1', 'question2'),
get_y=ItemGetter('label'),
splitter=IndexSplitter(valid_idx))
%%time
dls = dblock.dataloaders(train_ds, bs=bs, val_bs=val_bs)
dls.show_batch(max_n=4)
WANDB_NAME = f'{ds_name}-{task}-{model_name}'
GROUP = f'{ds_name}-{task}-{model_name}-{lr:.0e}'
NOTES = f'finetuning {model_name} with RAdam lr={lr:.0e}'
TAGS =[model_name, ds_name, 'radam']
wandb.init(reinit=True, project="fasthugs", entity="fastai_community",
name=WANDB_NAME, group=GROUP, notes=NOTES, tags=TAGS, config=CONFIG);
model = AutoModelForSequenceClassification.from_pretrained(model_name)
metrics = glue_metrics[task]
learn = TransLearner(dls, model, metrics=metrics).to_fp16()
cbs = []
learn.fit_one_cycle(4, lr, cbs=cbs)
learn.show_results()
task = 'mnli'
validate_task()
ds = load_dataset(ds_name, task)
ds.keys()
train_idx, valid_idx = get_splits(ds, valid='validation_matched')
valid_idx
train_ds = concatenate_datasets([ds['train'], ds['validation_matched']])
train_ds[0]
lens = train_ds.map(lambda s: {'len': len(s['premise'])+len(s['hypothesis'])}, remove_columns=train_ds.column_names, num_proc=4, keep_in_memory=True)
train_lens = lens.select(train_idx)['len']
valid_lens = lens.select(valid_idx)['len']
dblock = DataBlock(blocks = [TransformersTextBlock(pretrained_model_name=model_name),
CategoryBlock(vocab={0:'entailment', 1:'neutral', 2:'contradiction'})],
get_x=TextGetter('premise', 'hypothesis'),
get_y=ItemGetter('label'),
splitter=IndexSplitter(valid_idx))
%%time
dl_kwargs=[{'res':train_lens}, {'val_res':valid_lens}]
dls = dblock.dataloaders(train_ds, bs=bs, val_bs=val_bs, dl_kwargs=dl_kwargs, num_workers=4)
dls.show_batch(max_n=4)
WANDB_NAME = f'{ds_name}-{task}-{model_name}'
GROUP = f'{ds_name}-{task}-{model_name}-{lr:.0e}'
NOTES = f'finetuning {model_name} with RAdam lr={lr:.0e}'
TAGS =[model_name, ds_name, 'radam']
wandb.init(reinit=True, project="fasthugs", entity="fastai_community",
name=WANDB_NAME, group=GROUP, notes=NOTES, tags=TAGS, config=CONFIG);
model = AutoModelForSequenceClassification.from_pretrained(model_name, num_labels=3)
metrics = glue_metrics[task]
learn = TransLearner(dls, model, metrics=metrics).to_fp16()
cbs = []
learn.fit_one_cycle(4, lr, cbs=cbs)
learn.show_results()
valid_mm_dl = dls.test_dl(ds['validation_mismatched'], with_labels=True)
learn.validate(dl=valid_mm_dl)
task = 'qnli'
validate_task()
ds = load_dataset(ds_name, task)
len(ds['train']), len(ds['validation'])
train_idx, valid_idx = get_splits(ds)
valid_idx
train_ds = concatenate_datasets([ds['train'], ds['validation']])
train_ds[0]
dblock = DataBlock(blocks = [TransformersTextBlock(pretrained_model_name=model_name), PreprocCategoryBlock(vocab)],
get_x=TextGetter('question', 'sentence'),
get_y=ItemGetter('label'),
splitter=IndexSplitter(valid_idx))
%%time
dls = dblock.dataloaders(train_ds, bs=bs, val_bs=val_bs)
dls.show_batch(max_n=4)
WANDB_NAME = f'{ds_name}-{task}-{model_name}'
GROUP = f'{ds_name}-{task}-{model_name}-{lr:.0e}'
NOTES = f'finetuning {model_name} with RAdam lr={lr:.0e}'
TAGS =[model_name, ds_name, 'radam']
wandb.init(reinit=True, project="fasthugs", entity="fastai_community",
name=WANDB_NAME, group=GROUP, notes=NOTES, tags=TAGS, config=CONFIG);
model = AutoModelForSequenceClassification.from_pretrained(model_name)
metrics = glue_metrics[task]
learn = TransLearner(dls, model, metrics=metrics).to_fp16()
cbs = []
learn.fit_one_cycle(4, lr, cbs=cbs)
learn.show_results()
task = 'rte'
validate_task()
ds = load_dataset(ds_name, task)
len(ds['train']), len(ds['validation'])
train_idx, valid_idx = get_splits(ds)
valid_idx
train_ds = concatenate_datasets([ds['train'], ds['validation']])
train_ds[0]
dblock = DataBlock(blocks = [TransformersTextBlock(pretrained_model_name=model_name), PreprocCategoryBlock(vocab)],
get_x=TextGetter('sentence1', 'sentence2'),
get_y=ItemGetter('label'),
splitter=IndexSplitter(valid_idx))
%%time
dls = dblock.dataloaders(train_ds, bs=bs, val_bs=val_bs)
dls.show_batch(max_n=4)
WANDB_NAME = f'{ds_name}-{task}-{model_name}'
GROUP = f'{ds_name}-{task}-{model_name}-{lr:.0e}'
NOTES = f'finetuning {model_name} with RAdam lr={lr:.0e}'
TAGS =[model_name, ds_name, 'radam']
wandb.init(reinit=True, project="fasthugs", entity="fastai_community",
name=WANDB_NAME, group=GROUP, notes=NOTES, tags=TAGS, config=CONFIG);
model = AutoModelForSequenceClassification.from_pretrained(model_name)
metrics = glue_metrics[task]
learn = TransLearner(dls, model, metrics=metrics).to_fp16()
cbs = []
learn.fit_one_cycle(4, lr, cbs=cbs)
learn.show_results()
task = 'wnli'
validate_task()
ds = load_dataset(ds_name, task)
len(ds['train']), len(ds['validation'])
train_idx, valid_idx = get_splits(ds)
valid_idx
train_ds = concatenate_datasets([ds['train'], ds['validation']])
train_ds[0]
dblock = DataBlock(blocks = [TransformersTextBlock(pretrained_model_name=model_name), PreprocCategoryBlock(vocab)],
get_x=TextGetter('sentence1', 'sentence2'),
get_y=ItemGetter('label'),
splitter=IndexSplitter(valid_idx))
%%time
dls = dblock.dataloaders(train_ds, bs=bs, val_bs=val_bs)
dls.show_batch(max_n=4)
WANDB_NAME = f'{ds_name}-{task}-{model_name}'
GROUP = f'{ds_name}-{task}-{model_name}-{lr:.0e}'
NOTES = f'finetuning {model_name} with RAdam lr={lr:.0e}'
TAGS =[model_name, ds_name, 'radam']
wandb.init(reinit=True, project="fasthugs", entity="fastai_community",
name=WANDB_NAME, group=GROUP, notes=NOTES, tags=TAGS, config=CONFIG);
model = AutoModelForSequenceClassification.from_pretrained(model_name)
metrics = glue_metrics[task]
learn = TransLearner(dls, model, metrics=metrics).to_fp16()
cbs = []
learn.fit_one_cycle(4, lr, cbs=cbs)
learn.show_results()