Skip to content

Instantly share code, notes, and snippets.

@francoisstamant
Last active April 18, 2023 02:13
Show Gist options
  • Save francoisstamant/cee577a4a17485babfab6b892d2a3271 to your computer and use it in GitHub Desktop.
Save francoisstamant/cee577a4a17485babfab6b892d2a3271 to your computer and use it in GitHub Desktop.
import torch
from transformers import DistilBertTokenizerFast, DistilBertForSequenceClassification, Trainer, TrainingArguments
import pandas as pd
from datasets import load_dataset
from transformers import AutoTokenizer
from transformers import DataCollatorWithPadding
import evaluate
import numpy as np
from transformers import AutoModelForSequenceClassification, TrainingArguments, Trainer
#Prepare and load dataset
dataset = load_dataset('csv', data_files={'train': 'df_train.csv','test': 'df_test.csv'})
#Tonenizer and preprocessing function
tokenizer = AutoTokenizer.from_pretrained('distilbert-base-uncased')
def preprocess_function(examples):
return tokenizer(examples["text"], truncation=True,padding=True)
tokenized_data = dataset.map(preprocess_function, batched=True)
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)
#Prepare accuracy functions
accuracy = evaluate.load("accuracy")
def compute_metrics(eval_pred):
predictions, labels = eval_pred
predictions = np.argmax(predictions, axis=1)
print(predictions)
return accuracy.compute(predictions=predictions, references=labels)
#Map the labels to the values in the column (here its 1 to 1, 0 to 0)
id2label = {0: 0, 1: 1}
label2id = {0: 0, 1: 1}
#Load model
model = AutoModelForSequenceClassification.from_pretrained(
"distilbert-base-uncased", num_labels=2, id2label=id2label, label2id=label2id)
#Define the training arguments
training_args = TrainingArguments(
output_dir="classifier",
learning_rate=2e-5,
per_device_train_batch_size=8,
per_device_eval_batch_size=8,
num_train_epochs=8,
weight_decay=0.01,
evaluation_strategy="epoch",
save_strategy="epoch",
load_best_model_at_end=True)
#Definer the trainer and train the model
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_data["train"],
eval_dataset=tokenized_data["test"],
tokenizer=tokenizer,
data_collator=data_collator,
compute_metrics=compute_metrics)
trainer.train()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment