This project fine-tunes Flan-T5-Large to predict the stance of tweets toward the COVID-19 vaccine, classifying each tweet into one of three categories:
- in-favor
- against
- neutral-or-unclear
It supports both full fine-tuning and inference with optional in-context few-shot examples.
stance_prediction/
├── data/ # Raw and processed CSV files
│ └── Q2_20230202_majority.csv # Example data
├── finetuned_models/ # Saved model checkpoints
│ └── best_model/ # Best checkpoint (after training)
├── outputs/ # My output
│ └── predictions_fine_tuning_ICL.csv # The result of fine-tuning model using in-context learning
└── predictions_fine_tuning.csv # The result of fine-tuning model
└── predictions_no_fine_tuning.csv # The result of original model
├── src/ # Source code
│ ├── data_loader.py # DataLoader to read and preprocess CSVs
│ ├── train.py # Training script (fine-tune Flan-T5)
│ ├── infer.py # Inference script (predict on new tweets)
│ └── model.py # Wrapper class for loading & generating
├── requirements.txt # Python dependencies
└── README.md # Project documentation
-
Clone this repository:
git clone https://github.com/yancyou/stance_prediction.git cd stance_prediction -
Create a conda or virtualenv environment and install dependencies:
conda create -n stance_pred python=3.10 conda activate stance_pred pip install -r requirements.txt
Place your CSV file(s) in the data/ folder. The CSV must have two columns:
tweet— the raw tweet textlabel_majority— the ground-truth stance
Use src/train.py to fine-tune Flan-T5:
python src/train.py \
--data_path data/Q2_20230202_majority.csv \--data_path: CSV file for training and validation (80/20 split).- By default it saves checkpoints to
finetuned_modelsand writes the best model underfinetuned_models/best_model.
Key features:
- Automatic tokenization and dataset formatting with 🤗 Datasets.
- Early stopping after 4 epochs without improvement.
- Saves and loads the best checkpoint by macro F1.
After training, run src/infer.py to predict on unseen tweets:
python src/infer.py \
--input your_test_data_path \
--output your_prediction_data_output_path \
--model_path finetuned_models/best_model \--model_path: point to your saved checkpoint.--context_learning(optional): prepend few-shot examples at inference time.
The output CSV will contain a new column label_pred with one of:
in-favor,against,neutral-or-unclear
You can use in-context learning method in inference. Set --context_learning True in inference to activate. However, experiments show that the results of In-Context Learning are not as good as those of normal reasoning.
For my trained model, you can download it directly from the following link and then put it under finetuned_models/ for inference https://drive.google.com/file/d/1pbMnXQ0OHem7AFGJ8aaLz1pvzQyg1-sm/view?usp=sharing
Happy stance predicting! 🚀