Views
No views yet
TeenyTinyLlama-460m-AgNews) fine-tuned on the AgNews dataset.torch.optim.AdamW (learning_rate = 4e-5, epsilon = 1e-8)transformers.pipeline:1from transformers import pipeline
2
3text = "Quando falamos de Inteligência Artificial, hoje em dia existem já vários sistemas que começam a ganhar popularidade,\
4 embora nenhum seja ainda tão conhecido como o ChatGPT da OpenAI."
5
6classifier = pipeline("text-classification", model="nicholasKluge/TeenyTinyLlama-460m-AgNews")
7classifier(text)
8
9# >>> [{'label': 'TECNOLOGIA', 'score': 0.9997298121452332}] 1
2# AgNews
3!pip install transformers datasets evaluate accelerate -q
4
5import evaluate
6import numpy as np
7from datasets import load_dataset, Dataset, DatasetDict
8from transformers import AutoTokenizer, DataCollatorWithPadding
9from transformers import AutoModelForSequenceClassification, TrainingArguments, Trainer
10
11# Load the task
12dataset = load_dataset("maritaca-ai/ag_news_pt")
13
14# Create a `ModelForSequenceClassification`
15model = AutoModelForSequenceClassification.from_pretrained(
16 "nicholasKluge/TeenyTinyLlama-460m",
17 num_labels=4,
18 id2label={0: "MUNDO", 1: "ESPORTES", 2: "NEGÓCIOS", 3: "TECNOLOGIA"},
19 label2id={"MUNDO": 0, "ESPORTES": 1, "NEGÓCIOS": 2, "TECNOLOGIA": 3}
20)
21
22tokenizer = AutoTokenizer.from_pretrained("nicholasKluge/TeenyTinyLlama-460m")
23
24# Format the dataset
25train = dataset['train'].to_pandas()
26train["text"] = train["title"] + "\n\n" + train["text"]
27train.label = train.label.astype(int)
28train = Dataset.from_pandas(train)
29
30test = dataset['test'].to_pandas()
31test["text"] = test["title"] + "\n\n" + test["text"]
32test.label = test.label.astype(int)
33test = Dataset.from_pandas(test)
34
35dataset = DatasetDict({
36 "train": train,
37 "test": test
38})
39
40# Preprocess the dataset
41def preprocess_function(examples):
42 return tokenizer(examples["text"], truncation=True, max_length=256)
43
44dataset_tokenized = dataset.map(preprocess_function, batched=True)
45
46# Create a simple data collactor
47data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
48
49# Use accuracy as an evaluation metric
50accuracy = evaluate.load("accuracy")
51
52# Function to compute accuracy
53def compute_metrics(eval_pred):
54 predictions, labels = eval_pred
55 predictions = np.argmax(predictions, axis=1)
56 return accuracy.compute(predictions=predictions, references=labels)
57
58# Define training arguments
59training_args = TrainingArguments(
60 output_dir="checkpoints",
61 learning_rate=4e-5,
62 per_device_train_batch_size=16,
63 per_device_eval_batch_size=16,
64 num_train_epochs=3,
65 weight_decay=0.01,
66 evaluation_strategy="epoch",
67 save_strategy="epoch",
68 load_best_model_at_end=True,
69 push_to_hub=True,
70 hub_token="your_token_here",
71 hub_model_id="username/model-ID",
72)
73
74# Define the Trainer
75trainer = Trainer(
76 model=model,
77 args=training_args,
78 train_dataset=dataset_tokenized["train"],
79 eval_dataset=dataset_tokenized["test"],
80 tokenizer=tokenizer,
81 data_collator=data_collator,
82 compute_metrics=compute_metrics,
83)
84
85# Train!
86trainer.train()
87| Models | IMDB | FaQuAD-NLI | HateBr | Assin2 | AgNews | Average |
|---|---|---|---|---|---|---|
| BERTimbau-large | 93.58 | 92.26 | 91.57 | 88.97 | 94.11 | 92.10 |
| BERTimbau-small | 92.22 | 93.07 | 91.28 | 87.45 | 94.19 | 91.64 |
| TTL-460m | 91.64 | 91.18 | 92.28 | 86.43 | 94.42 | 91.19 |
| TTL-160m | 91.14 | 90.00 | 90.71 | 85.78 | 94.05 | 90.34 |
1@misc{correa24ttllama,
2 title = {TeenyTinyLlama: open-source tiny language models trained in Brazilian Portuguese},
3 author = {Corr{\^e}a, Nicholas Kluge and Falk, Sophia and Fatimah, Shiza and Sen, Aniket and De Oliveira, Nythamar},
4 journal={arXiv preprint arXiv:2401.16640},
5 year={2024}
6}
7
8@misc{correa24ttllama,
9 doi = {10.1016/j.mlwa.2024.100558},
10 url = {https://www.sciencedirect.com/science/article/pii/S2666827024000343},
11 title = {TeenyTinyLlama: open-source tiny language models trained in Brazilian Portuguese},
12 author = {Corr{\^e}a, Nicholas Kluge and Falk, Sophia and Fatimah, Shiza and Sen, Aniket and De Oliveira, Nythamar},
13 journal={Machine Learning With Applications},
14 publisher = {Springer},
15 year={2024}
16}