mirror of
https://github.com/neuml/txtai.git
synced 2026-10-11 21:39:55 +00:00
12 KiB
12 KiB
In [1]:
%%capture
!pip install git+https://github.com/neuml/txtai#egg=txtai[pipeline-train] datasetsIn [ ]:
from datasets import load_dataset
from transformers import AutoModelForSequenceClassification, AutoTokenizer
from txtai.pipeline import HFTrainer, Labels
def train(teacher, student, distillation, **kwargs):
trainer = HFTrainer()
model = AutoModelForSequenceClassification.from_pretrained(student, trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(student, trust_remote_code=True)
return trainer(
(model, tokenizer),
ds["train"],
columns=("sentence", "label"),
maxlength=maxlength,
teacher=teacher,
distillation=distillation,
**kwargs
)
def test(model):
labels = Labels(model, dynamic=False, trust_remote_code=True)
# Determine accuracy on test set
results = [row["label"] == labels(row["sentence"], max_length=maxlength)[0][0] for row in ds["validation"]]
print(sum(results) / len(ds["validation"]))
# Hugging Face dataset
ds = load_dataset("nyu-mll/glue", "sst2")
maxlength = 128In [3]:
test(train(
"assemblyai/bert-large-uncased-sst2",
"google/bert_uncased_L-4_H-256_A-4",
(1.0, 1.0),
learning_rate=1e-4,
num_train_epochs=5,
per_device_train_batch_size=32,
output_dir="bert-mini-sst2"
))[transformers] [1mBertForSequenceClassification LOAD REPORT[0m from: google/bert_uncased_L-4_H-256_A-4 Key | Status | -------------------------------------------+------------+- cls.predictions.transform.dense.weight | UNEXPECTED | cls.predictions.decoder.bias | UNEXPECTED | cls.predictions.transform.LayerNorm.bias | UNEXPECTED | cls.predictions.decoder.weight | UNEXPECTED | cls.seq_relationship.bias | UNEXPECTED | cls.seq_relationship.weight | UNEXPECTED | cls.predictions.transform.dense.bias | UNEXPECTED | cls.predictions.bias | UNEXPECTED | cls.predictions.transform.LayerNorm.weight | UNEXPECTED | classifier.weight | MISSING | classifier.bias | MISSING | Notes: - UNEXPECTED: can be ignored when loading from different task/architecture; not ok if you expect identical arch. - MISSING: those params were newly initialized because missing from the checkpoint. Consider training on your downstream task.
0.875
In [4]:
test(train(
"bert-mini-sst2",
"google/bert_uncased_L-2_H-128_A-2",
(1.0, 1.0),
learning_rate=1e-4,
num_train_epochs=5,
per_device_train_batch_size=32,
output_dir="bert-tiny-sst2"
))[transformers] [1mBertForSequenceClassification LOAD REPORT[0m from: google/bert_uncased_L-2_H-128_A-2 Key | Status | -------------------------------------------+------------+- cls.predictions.transform.dense.weight | UNEXPECTED | cls.predictions.transform.LayerNorm.bias | UNEXPECTED | cls.seq_relationship.weight | UNEXPECTED | cls.seq_relationship.bias | UNEXPECTED | cls.predictions.transform.dense.bias | UNEXPECTED | cls.predictions.bias | UNEXPECTED | cls.predictions.transform.LayerNorm.weight | UNEXPECTED | classifier.weight | MISSING | classifier.bias | MISSING | Notes: - UNEXPECTED: can be ignored when loading from different task/architecture; not ok if you expect identical arch. - MISSING: those params were newly initialized because missing from the checkpoint. Consider training on your downstream task.
0.8325688073394495
In [9]:
test(train(
"bert-tiny-sst2",
"neuml/bert-hash-femto",
(1.0, 1.0),
learning_rate=3e-4,
num_train_epochs=5,
per_device_train_batch_size=32,
output_dir="bert-femto-sst2"
))[transformers] [1mBertHashForSequenceClassification LOAD REPORT[0m from: neuml/bert-hash-femto Key | Status | -------------------------+---------+- bert.pooler.dense.bias | MISSING | bert.pooler.dense.weight | MISSING | classifier.bias | MISSING | classifier.weight | MISSING | Notes: - MISSING: those params were newly initialized because missing from the checkpoint. Consider training on your downstream task.
0.8084862385321101
In [6]:
test(train(
None,
"neuml/bert-hash-femto",
None,
learning_rate=3e-4,
num_train_epochs=5,
per_device_train_batch_size=32,
output_dir="bert-femto-sst2"
))[transformers] [1mBertHashForSequenceClassification LOAD REPORT[0m from: neuml/bert-hash-femto Key | Status | -------------------------+---------+- bert.pooler.dense.bias | MISSING | bert.pooler.dense.weight | MISSING | classifier.bias | MISSING | classifier.weight | MISSING | Notes: - MISSING: those params were newly initialized because missing from the checkpoint. Consider training on your downstream task.
0.801605504587156