CodexBloom - Programming Q&A Platform

Issues with Fine-tuning GPT-3.5 on Custom Dataset - Unexpected Output Format

👀 Views: 45 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-07
openai gpt-3.5 huggingface transformers fine-tuning Python

Could someone explain I recently switched to I'm working on a personal project and I'm trying to fine-tune OpenAI's GPT-3.5 model using the Hugging Face Transformers library (version 4.21.1) on a custom dataset for a text classification task. However, after training, the model is producing outputs with unexpected formatting that doesn't align with what I had anticipated. My dataset is structured in JSON format, where each entry looks like this: ```json { "text": "This is a sample text.", "label": "positive" } ``` I pre-processed the dataset using the following code snippet: ```python from datasets import load_dataset # Load dataset dataset = load_dataset('json', data_files='path_to_your_dataset.json') train_dataset = dataset['train'] ``` I then set up the model for training: ```python from transformers import GPT2Tokenizer, GPT2LMHeadModel, Trainer, TrainingArguments tokenizer = GPT2Tokenizer.from_pretrained('gpt2') model = GPT2LMHeadModel.from_pretrained('gpt2') training_args = TrainingArguments( output_dir='./results', per_device_train_batch_size=4, num_train_epochs=3, logging_dir='./logs', ) trainer = Trainer( model=model, args=training_args, train_dataset=train_dataset, ) trainer.train() ``` After fine-tuning, when I input a sample text to the model, the output looks like this: ``` {'output': 'Here is what I think about the sample text. The classification is: Positive'} ``` I expected a simple output without the additional text. To diagnose the issue, I tried adjusting the `do_sample` and `max_length` parameters in the `generate` method, but it didn't resolve the output format. My generation code looks like this: ```python input_text = "Please classify: This is a sample text." input_ids = tokenizer.encode(input_text, return_tensors='pt') outputs = model.generate(input_ids, max_length=30, do_sample=True) print(tokenizer.decode(outputs[0], skip_special_tokens=True)) ``` What can I do to ensure that the output only returns the classification label as expected? Any insights on model output formatting or best practices for handling outputs in such cases would be greatly appreciated. This is happening in both development and production on macOS. I appreciate any insights! I'd be grateful for any help.