Views
No views yet
TeenyTinyLlama-460m-HateBR) fine-tuned on the HateBR dataset.torch.optim.AdamW (learning_rate = 4e-5, epsilon = 1e-8)transformers.pipeline:1from transformers import pipeline
2
3text = "Pega a sua opinião e vai a merda com ela!"
4
5classifier = pipeline("text-classification", model="nicholasKluge/TeenyTinyLlama-460m-HateBR")
6classifier(text)
7
8# >>> [{'label': 'TOXIC', 'score': 0.9998729228973389}]1
2# Hatebr
3! pip install transformers datasets evaluate accelerate -q
4
5import evaluate
6import numpy as np
7from huggingface_hub import login
8from datasets import load_dataset, Dataset, DatasetDict
9from transformers import AutoTokenizer, DataCollatorWithPadding
10from transformers import AutoModelForSequenceClassification, TrainingArguments, Trainer
11
12# Load the task
13dataset = load_dataset("ruanchaves/hatebr")
14
15# Format the dataset
16train = dataset['train'].to_pandas()
17train = train[['instagram_comments', 'offensive_language']]
18train.columns = ['text', 'labels']
19train.labels = train.labels.astype(int)
20train = Dataset.from_pandas(train)
21
22test = dataset['test'].to_pandas()
23test = test[['instagram_comments', 'offensive_language']]
24test.columns = ['text', 'labels']
25test.labels = test.labels.astype(int)
26test = Dataset.from_pandas(test)
27
28dataset = DatasetDict({
29 "train": train,
30 "test": test
31})
32
33# Create a `ModelForSequenceClassification`
34model = AutoModelForSequenceClassification.from_pretrained(
35 "nicholasKluge/TeenyTinyLlama-460m",
36 num_labels=2,
37 id2label={0: "NONTOXIC", 1: "TOXIC"},
38 label2id={"NONTOXIC": 0, "TOXIC": 1}
39)
40
41tokenizer = AutoTokenizer.from_pretrained("nicholasKluge/TeenyTinyLlama-460m")
42
43# Preprocess the dataset
44def preprocess_function(examples):
45 return tokenizer(examples["text"], truncation=True)
46
47dataset_tokenized = dataset.map(preprocess_function, batched=True)
48
49# Create a simple data collactor
50data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
51
52# Use accuracy as evaluation metric
53accuracy = evaluate.load("accuracy")
54
55# Function to compute accuracy
56def compute_metrics(eval_pred):
57 predictions, labels = eval_pred
58 predictions = np.argmax(predictions, axis=1)
59 return accuracy.compute(predictions=predictions, references=labels)
60
61# Define training arguments
62training_args = TrainingArguments(
63 output_dir="checkpoints",
64 learning_rate=4e-5,
65 per_device_train_batch_size=16,
66 per_device_eval_batch_size=16,
67 num_train_epochs=3,
68 weight_decay=0.01,
69 evaluation_strategy="epoch",
70 save_strategy="epoch",
71 load_best_model_at_end=True,
72 push_to_hub=True,
73 hub_token="your_token_here",
74 hub_model_id="username/model-ID",
75)
76
77# Define the Trainer
78trainer = Trainer(
79 model=model,
80 args=training_args,
81 train_dataset=dataset_tokenized["train"],
82 eval_dataset=dataset_tokenized["test"],
83 tokenizer=tokenizer,
84 data_collator=data_collator,
85 compute_metrics=compute_metrics,
86)
87
88# Train!
89trainer.train()
90| 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}