File size: 993 Bytes
690827e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from transformers import Trainer, AutoModelForSequenceClassification, AutoTokenizer
from datasets import load_dataset, load_metric
import json

# Load configuration
with open('../config/config.json') as f:
    config = json.load(f)

# Load model and tokenizer
model = AutoModelForSequenceClassification.from_pretrained('../model')
tokenizer = AutoTokenizer.from_pretrained(config['model_name'])

# Load dataset
dataset = load_dataset('csv', data_files={'test': '../data/test.csv'})
tokenized_datasets = dataset.map(lambda x: tokenizer(x['text'], padding="max_length", truncation=True), batched=True)

# Evaluation
metric = load_metric("accuracy")

def compute_metrics(eval_pred):
    logits, labels = eval_pred
    predictions = logits.argmax(axis=-1)
    return metric.compute(predictions=predictions, references=labels)

trainer = Trainer(
    model=model,
    tokenizer=tokenizer,
    compute_metrics=compute_metrics
)

results = trainer.evaluate(tokenized_datasets['test'])
print(results)