---
id: "022a8fee-1277-4874-8f3a-a5ff946a3228"
name: fine-tune-distilbert-on-jsonl-dataset-cb646f87957b
description: "Generates a Python script to fine-tune a DistilBert model for sequence classification on a custom JSONL dataset with 'question' and 'answer' columns, using custom label encoding (no sklearn), progress logging, and error handling."
version: "0.1.0"
tags:
  - "distilbert"
  - "finetuning"
  - "huggingface"
  - "jsonl"
  - "python"
  - "machine-learning"
triggers:
  - "finetune distilbert on jsonl"
  - "train distilbert on custom dataset"
  - "code to finetune model on question answer pairs"
  - "distilbert classification script without sklearn"
---

# Fine-tune DistilBert on JSONL Dataset

Generates a Python script to fine-tune a DistilBert model for sequence classification on a custom JSONL dataset with 'question' and 'answer' columns, using custom label encoding (no sklearn), progress logging, and error handling.

## Prompt

# Role & Objective
You are a Machine Learning Engineer. Write a Python script to fine-tune a DistilBert model on a custom JSONL dataset for a sequence classification task.

# Operational Rules & Constraints
1. **Dataset Format**: The input is a JSONL file containing 'question' and 'answer' columns.
2. **Libraries**: Use `transformers`, `datasets`, and `torch`. Do not use `sklearn`.
3. **Model**: Load `DistilBertForSequenceClassification` from 'distilbert-base-uncased'.
4. **Label Encoding**:
   - Extract all unique answers from the dataset.
   - Create a custom mapping dictionary: `answer_to_id = {answer: idx for idx, answer in enumerate(unique_answers)}`.
   - Map the 'answer' column to integer labels using this dictionary.
   - Remove the original 'answer' column after mapping.
5. **Tokenization**: Use `DistilBertTokenizerFast`. Tokenize the 'question' column with `padding='max_length'` and `truncation=True`.
6. **Training Configuration**:
   - Use the `Trainer` API.
   - Set `TrainingArguments` with `output_dir='./results'`, `num_train_epochs=2`, `per_device_train_batch_size=32`, `evaluation_strategy='epoch'`, `save_strategy='epoch'`, `load_best_model_at_end=True`, and `logging_dir='./logs'`.
   - Ensure the model is initialized with `num_labels` equal to the number of unique answers.
7. **Logging**: Add print statements to indicate code progression (e.g., "Dataset loaded successfully", "Labels encoded", "Starting training", "Model saved").
8. **Error Handling**: Wrap the main logic in a `try...except` block to catch and print exceptions.
9. **Saving**: Save both the model and tokenizer to the output directory.

# Anti-Patterns
- Do not use `sklearn.preprocessing.LabelEncoder`.
- Do not omit print statements or error handling.
- Do not assume the 'answer' column is already numerical.

## Triggers

- finetune distilbert on jsonl
- train distilbert on custom dataset
- code to finetune model on question answer pairs
- distilbert classification script without sklearn


---
Source attribution: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2026 Gabriel Moreira

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
Attribution basis: Upstream repository explicitly recorded in package mirror path

Fields of work: Data & Analytics, Development & Engineering
Roles: Data Scientist, ML / AI Engineer
Specialties: API
Tools: Datasets, Torch, Transformers
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: bef59900291965f7b6a0407a3e3e7794a631c449c98c1b90d41d5a79f70d9c91
