text_plain_output_1.png
from datasets import load_dataset

from sklearn.metrics import f1_score, accuracy_score

from transformers import DataCollatorWithPadding, BertForSequenceClassification, AutoModelForSequenceClassification, AutoTokenizer, Trainer, TrainingArguments

import numpy as np

import torch

train_dataset = load_dataset('csv', data_files='train_data_clean.csv')
val_dataset = load_dataset('csv', data_files='val_data_clean.csv')
test_dataset = load_dataset('csv', data_files='test_data_clean.csv')

train_dataset = train_dataset.rename_column('text', 'sentence1')
train_dataset = train_dataset.rename_column('target', 'label')
val_dataset = val_dataset.rename_column('text', 'sentence1')
val_dataset = val_dataset.rename_column('target', 'label')
test_dataset = test_dataset.rename_column('text', 'sentence1')

checkpoint = '../input/transformers/roberta-base'

tokenizer = AutoTokenizer.from_pretrained(checkpoint)
data_collator = DataCollatorWithPadding(tokenizer=tokenizer)

def tokenize_function(dset):
    return tokenizer(dset['sentence1'])

tokenized_train_dataset = train_dataset.map(tokenize_function, batched=True)
tokenized_val_dataset = val_dataset.map(tokenize_function, batched=True)
tokenized_test_dataset = test_dataset.map(tokenize_function, batched=True)

device = 'cuda' if torch.cuda.is_available() else 'cpu'

model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2).to(device)

def compute_metrics(p):
    pred, labels = p
    pred = np.argmax(pred, axis=1)
    f1 = f1_score(y_true=labels, y_pred=pred)
    return {'f1_score': f1}

training_args = TrainingArguments(output_dir='trainer_dir', report_to='all', num_train_epochs=3)
trainer = Trainer(model, training_args, train_dataset=tokenized_train_dataset['train'], eval_dataset=tokenized_val_dataset['train'], data_collator=data_collator, tokenizer=tokenizer, compute_metrics=compute_metrics)

trainer.train()