mirror of
https://github.com/neuml/txtai.git
synced 2026-10-11 21:39:55 +00:00
11 KiB
11 KiB
In [ ]:
%%capture
!pip install git+https://github.com/neuml/txtai#egg=txtai[pipeline-train] datasets pandasIn [ ]:
from datasets import load_dataset
from txtai.pipeline import HFTrainer
ds = load_dataset("squad_v2")
trainer = HFTrainer()
trainer("google/bert_uncased_L-2_H-128_A-2", ds["train"].select(range(3000)), task="question-answering", output_dir="bert-tiny-squadv2")
print("Training complete")Reusing dataset squad_v2 (/root/.cache/huggingface/datasets/squad_v2/squad_v2/2.0.0/09187c73c1b837c95d9a249cd97c2c3f1cebada06efe667b4427714b27639b1d) Loading cached processed dataset at /root/.cache/huggingface/datasets/squad_v2/squad_v2/2.0.0/09187c73c1b837c95d9a249cd97c2c3f1cebada06efe667b4427714b27639b1d/cache-73bbe029cf3366fc.arrow Some weights of the model checkpoint at google/bert_uncased_L-2_H-128_A-2 were not used when initializing BertForQuestionAnswering: ['cls.predictions.decoder.bias', 'cls.predictions.transform.dense.bias', 'cls.predictions.transform.LayerNorm.bias', 'cls.predictions.transform.LayerNorm.weight', 'cls.predictions.decoder.weight', 'cls.predictions.bias', 'cls.seq_relationship.bias', 'cls.predictions.transform.dense.weight', 'cls.seq_relationship.weight'] - This IS expected if you are initializing BertForQuestionAnswering from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model). - This IS NOT expected if you are initializing BertForQuestionAnswering from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model). Some weights of BertForQuestionAnswering were not initialized from the model checkpoint at google/bert_uncased_L-2_H-128_A-2 and are newly initialized: ['qa_outputs.bias', 'qa_outputs.weight'] You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
[1131/1131 00:50, Epoch 3/3]
| Step | Training Loss |
|---|---|
| 500 | 4.501800 |
| 1000 | 3.875900 |
Training complete
In [ ]:
# Training data
data = [
{"question": "What ingredient?", "context": "Pour 1 can whole tomatoes", "answers": "tomatoes"},
{"question": "What ingredient?", "context": "Dice 1 yellow onion", "answers": "onion"},
{"question": "What ingredient?", "context": "Cut 1 red pepper", "answers": "pepper"},
{"question": "What ingredient?", "context": "Peel and dice 1 clove garlic", "answers": "garlic"},
{"question": "What ingredient?", "context": "Put 1/2 lb beef", "answers": "beef"},
]
model, tokenizer = trainer("bert-tiny-squadv2", data, task="question-answering", num_train_epochs=10)
[10/10 00:00, Epoch 10/10]
| Step | Training Loss |
|---|
In [ ]:
from transformers import pipeline
questions = pipeline("question-answering", model="bert-tiny-squadv2")
questions("What ingredient?", "Peel and dice 1 shallot"){'answer': 'dice 1 shallot',
'end': 23,
'score': 0.05128436163067818,
'start': 9}In [ ]:
from transformers import pipeline
questions = pipeline("question-answering", model=model.to("cpu"), tokenizer=tokenizer)
questions("What ingredient?", "Peel and dice 1 shallot"){'answer': 'shallot', 'end': 23, 'score': 0.13187439739704132, 'start': 16}