[Python] evaluation_strategy() not supported in transformers library

Discussão em 'Python' iniciado por Stack, Junho 8, 2025.

  1. Stack

    Stack Membro Participativo

    I've recently been attempting to fine-tune the pretrained neural network, "cardiffnlp/twitter-roberta-base-offensive." However, when I attempted to run a Python program I created, "evaluation_strategy" was considered to be an "unexpected argument" regardless that I have version 4.52.6 installed. Could anyone help me with fixing this error? (There also might be another one with the classifiers since I'm attempting to have it transition to a 6-label classifier rather than a binary classifier since the Jigsaw Dataset uses 6 labels, so if that seems incorrect in my code, please fix that as well).

    Here's the source code:


    from datasets import load_dataset
    from transformers import *
    import torch
    import numpy as np
    from sklearn.metrics import f1_score, accuracy_score
    from sklearn.preprocessing import MultiLabelBinarizer

    # Load dataset (adjust path if needed)
    dataset = load_dataset('csv', data_files={
    "train": "data/train_split.csv",
    "validation": "data/validation_split.csv"
    })

    # Define label columns used in Jigsaw multi-label setup
    LABEL_COLUMNS = ["toxic", "severe_toxic", "obscene", "threat", "insult", "identity_hate"]

    # Tokenizer and model setup
    MODEL_NAME = "cardiffnlp/twitter-roberta-base-offensive"
    tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)

    # Define label count and problem type
    config = AutoConfig.from_pretrained(
    MODEL_NAME,
    num_labels=len(LABEL_COLUMNS),
    problem_type="multi_label_classification"
    )

    model = AutoModelForSequenceClassification.from_pretrained(
    MODEL_NAME,
    config=config,
    ignore_mismatched_sizes=True
    )

    # Preprocessing function
    def preprocess(example):
    encoding = tokenizer(
    example["comment_text"],
    truncation=True,
    padding="max_length",
    max_length=128
    )
    labels = [example[col] for col in LABEL_COLUMNS]
    encoding["labels"] = torch.tensor(labels, dtype=torch.float)
    return encoding

    # Apply preprocessing
    encoded_dataset = dataset.map(preprocess)

    # Training configuration
    training_args = TrainingArguments(
    output_dir="./results",
    save_strategy="epoch",
    evaluation_strategy="epoch", # <-- Add this line
    learning_rate=2e-5,
    per_device_train_batch_size=16,
    per_device_eval_batch_size=16,
    num_train_epochs=3,
    weight_decay=0.01,
    logging_dir='./logs',
    logging_steps=10,
    load_best_model_at_end=True,
    metric_for_best_model="f1"
    )


    # Metric function
    def compute_metrics(pred):
    preds = torch.sigmoid(torch.tensor(pred.predictions)).numpy()
    labels = pred.label_ids
    preds = (preds > 0.5).astype(int)
    f1 = f1_score(labels, preds, average="macro")
    acc = accuracy_score(labels, preds)
    return {"f1": f1, "accuracy": acc}

    # Trainer
    trainer = Trainer(
    model=model,
    args=training_args,
    train_dataset=encoded_dataset["train"],
    eval_dataset=encoded_dataset["validation"],
    tokenizer=tokenizer,
    compute_metrics=compute_metrics
    )

    # Train the model
    trainer.train()


    Could anyone fix it?

    Continue reading...

Compartilhe esta Página